1XEN_ROOT = $(CURDIR)/../../..
2
3all: xen-shim
4
5.PHONY: FORCE
6FORCE:
7
8D=xen-root
9
10# Minimun set of files / directories go get Xen to build
11LINK_DIRS=config xen
12LINK_FILES=Config.mk
13
14DEP_DIRS=$(foreach i, $(LINK_DIRS), $(XEN_ROOT)/$(i))
15DEP_FILES=$(foreach i, $(LINK_FILES), $(XEN_ROOT)/$(i))
16
17# Exclude some intermediate files and final build products
18LINK_EXCLUDES := '*.[isoa]' '.*.d' '.*.d2' '.config'
19LINK_EXCLUDES += '*.map' 'xen' 'xen.gz' 'xen.efi' 'xen-syms'
20LINK_EXCLUDES += '.*.tmp'
21
22# This is all a giant mess and doesn't really work.
23#
24# The correct solution is to fix Xen to be able to do out-of-tree builds.
25#
26# Until that happens, we set up a linkfarm by iterating over the xen/ tree,
27# linking source files.  This is repeated each time we enter this directory,
28# which poses a problem for a two-step "make; make install" build process.
29#
30# Any time the list of files to link changes, we relink all files, then
31# distclean to take out not-easy-to-classify intermediate files.  This is to
32# support easy development of the shim, but has a side effect of clobbering
33# the already-built shim.
34#
35# $(LINK_EXCLUDES) should be set such that a parallel build of shim and xen/
36# doesn't cause a subsequent `make install` to decide to regenerate the
37# linkfarm.  This means that all final build artefacts must be excluded.
38linkfarm.stamp: $(DEP_DIRS) $(DEP_FILES) FORCE
39	mkdir -p $(D)
40	rm -f linkfarm.stamp.tmp
41	set -e; \
42	$(foreach d, $(LINK_DIRS), \
43		 (mkdir -p $(D)/$(d); \
44		  cd $(D)/$(d); \
45		  find $(XEN_ROOT)/$(d)/ -type d |\
46			sed 's,^$(XEN_ROOT)/$(d)/,,g' | xargs mkdir -p .);) \
47	$(foreach d, $(LINK_DIRS), \
48		(cd $(XEN_ROOT); \
49		 find $(d) ! -type l -type f $(addprefix ! -name ,$(LINK_EXCLUDES))) \
50		 >> linkfarm.stamp.tmp ; ) \
51	$(foreach f, $(LINK_FILES), \
52		echo $(f) >> linkfarm.stamp.tmp ;)
53	cmp -s linkfarm.stamp.tmp linkfarm.stamp && \
54		rm linkfarm.stamp.tmp || { \
55		cat linkfarm.stamp.tmp | while read f; \
56		  do rm -f "$(D)/$$f"; ln -s "$(XEN_ROOT)/$$f" "$(D)/$$f"; done; \
57		mv linkfarm.stamp.tmp linkfarm.stamp; \
58		}
59
60# Copy enough of the tree to build the shim hypervisor
61$(D): linkfarm.stamp
62	$(MAKE) -C $(D)/xen distclean
63
64$(D)/xen/.config: $(D)
65	$(MAKE) -C $(@D) KBUILD_DEFCONFIG=pvshim_defconfig XEN_CONFIG_EXPERT=y defconfig
66
67xen-shim: $(D)/xen/.config
68	$(MAKE) -C $(<D) build XEN_CONFIG_EXPERT=y
69	ln -sf $(D)/xen/xen $@
70	ln -sf $(D)/xen/xen-syms $@-syms
71
72.PHONY: distclean clean
73distclean clean:
74	rm -f xen-shim xen-shim-syms *.old
75	rm -rf $(D)
76	rm -f linkfarm.stamp*
77