aliguori | 1775918 | 2009-01-21 18:12:52 +0000 | [diff] [blame] | 1 | |
Juan Quintela | 5ab2886 | 2009-10-06 21:11:14 +0200 | [diff] [blame] | 2 | # Don't use implicit rules or variables |
| 3 | # we have explicit rules for everything |
| 4 | MAKEFLAGS += -rR |
| 5 | |
| 6 | # Files with this suffixes are final, don't try to generate them |
| 7 | # using implicit rules |
| 8 | %.d: |
| 9 | %.h: |
| 10 | %.c: |
Peter Maydell | c3dc9fd | 2014-02-05 17:27:27 +0000 | [diff] [blame] | 11 | %.cc: |
Tomoki Sekiyama | 83f73fc | 2013-08-07 11:39:36 -0400 | [diff] [blame] | 12 | %.cpp: |
Juan Quintela | 5ab2886 | 2009-10-06 21:11:14 +0200 | [diff] [blame] | 13 | %.m: |
| 14 | %.mak: |
| 15 | |
Tomoki Sekiyama | 83f73fc | 2013-08-07 11:39:36 -0400 | [diff] [blame] | 16 | # Flags for C++ compilation |
| 17 | QEMU_CXXFLAGS = -D__STDC_LIMIT_MACROS $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls, $(QEMU_CFLAGS)) |
| 18 | |
Stefan Weil | 7ebf54b | 2009-11-19 20:07:52 +0100 | [diff] [blame] | 19 | # Flags for dependency generation |
Jan Kiszka | a71cd2a | 2010-05-15 13:03:28 +0200 | [diff] [blame] | 20 | QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(*D)/$(*F).d |
malc | 02d5467 | 2009-10-11 17:08:57 +0400 | [diff] [blame] | 21 | |
Paolo Bonzini | 9d9199a | 2012-09-17 10:21:52 +0200 | [diff] [blame] | 22 | # Same as -I$(SRC_PATH) -I., but for the nested source/object directories |
Dunrong Huang | 7e7da8e | 2013-04-29 22:52:12 +0800 | [diff] [blame] | 23 | QEMU_INCLUDES += -I$(<D) -I$(@D) |
Paolo Bonzini | 9d9199a | 2012-09-17 10:21:52 +0200 | [diff] [blame] | 24 | |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 25 | WL_U := -Wl,-u, |
Stefan Weil | 4852ee9 | 2014-09-18 21:55:08 +0200 | [diff] [blame] | 26 | find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2))) |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 27 | defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}') |
| 28 | undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}') |
| 29 | |
| 30 | # All the .mo objects in -m variables are also added into corresponding -y |
| 31 | # variable in unnest-vars, but filtered out here, when LINK is called. |
| 32 | # |
| 33 | # The .mo objects are supposed to be linked as a DSO, for module build. So here |
| 34 | # they are only used as a placeholders to generate those "archive undefined" |
| 35 | # symbol options (-Wl,-u,$symbol_name), which are the archive functions |
| 36 | # referenced by the code in the DSO. |
| 37 | # |
| 38 | # Also the presence in -y variables will also guarantee they are built before |
| 39 | # linking executables that will load them. So we can look up symbol reference |
| 40 | # in LINK. |
| 41 | # |
| 42 | # This is necessary because the exectuable itself may not use the function, in |
| 43 | # which case the function would not be linked in. Then the DSO loading will |
| 44 | # fail because of the missing symbol. |
| 45 | process-archive-undefs = $(filter-out %.a %.mo,$1) \ |
| 46 | $(addprefix $(WL_U), \ |
| 47 | $(filter $(call defined-symbols,$(filter %.a, $1)), \ |
| 48 | $(call undefined-symbols,$(filter %.mo,$1)))) \ |
| 49 | $(filter %.a,$1) |
| 50 | |
Paolo Bonzini | f277015 | 2014-06-16 16:43:25 +0200 | [diff] [blame] | 51 | extract-libs = $(strip $(foreach o,$1,$($o-libs))) |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 52 | expand-objs = $(strip $(sort $(filter %.o,$1)) \ |
| 53 | $(foreach o,$(filter %.mo,$1),$($o-objs)) \ |
| 54 | $(filter-out %.o %.mo,$1)) |
Fam Zheng | 5c0d52b | 2014-02-10 14:48:53 +0800 | [diff] [blame] | 55 | |
Andreas Färber | 0e8c921 | 2010-01-06 20:24:05 +0100 | [diff] [blame] | 56 | %.o: %.c |
Fam Zheng | 5c0d52b | 2014-02-10 14:48:53 +0800 | [diff] [blame] | 57 | $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@") |
Paolo Bonzini | 6821cdc | 2013-04-27 00:25:31 +0200 | [diff] [blame] | 58 | %.o: %.rc |
| 59 | $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@") |
aliguori | 1775918 | 2009-01-21 18:12:52 +0000 | [diff] [blame] | 60 | |
Peter Maydell | 3144f78 | 2014-02-05 17:27:27 +0000 | [diff] [blame] | 61 | # If we have a CXX we might have some C++ objects, in which case we |
| 62 | # must link with the C++ compiler, not the plain C compiler. |
| 63 | LINKPROG = $(or $(CXX),$(CC)) |
| 64 | |
Alon Levy | 44dc0ca | 2011-05-15 11:51:28 +0300 | [diff] [blame] | 65 | ifeq ($(LIBTOOL),) |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 66 | LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \ |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 67 | $(call process-archive-undefs, $1) \ |
| 68 | $(version-obj-y) $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@") |
Alon Levy | 44dc0ca | 2011-05-15 11:51:28 +0300 | [diff] [blame] | 69 | else |
Paolo Bonzini | f141ccf | 2012-12-20 18:32:53 +0100 | [diff] [blame] | 70 | LIBTOOL += $(if $(V),,--quiet) |
Alon Levy | 44dc0ca | 2011-05-15 11:51:28 +0300 | [diff] [blame] | 71 | %.lo: %.c |
Paolo Bonzini | 0db564e | 2014-05-08 14:34:09 +0200 | [diff] [blame] | 72 | $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($*.o-cflags) -c -o $@ $<," lt CC $@") |
Paolo Bonzini | 6821cdc | 2013-04-27 00:25:31 +0200 | [diff] [blame] | 73 | %.lo: %.rc |
| 74 | $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@") |
Paolo Bonzini | 2c13ec5 | 2012-12-21 09:23:18 +0100 | [diff] [blame] | 75 | %.lo: %.dtrace |
| 76 | $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@") |
| 77 | |
Paolo Bonzini | 2165588 | 2012-12-20 18:57:45 +0100 | [diff] [blame] | 78 | LINK = $(call quiet-command,\ |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 79 | $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \ |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 80 | )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \ |
| 81 | $(call process-archive-undefs, $1)\ |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 82 | $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \ |
| 83 | $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \ |
Paolo Bonzini | 0db564e | 2014-05-08 14:34:09 +0200 | [diff] [blame] | 84 | $(call extract-libs,$(1:.lo=.o)) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@") |
Alon Levy | 44dc0ca | 2011-05-15 11:51:28 +0300 | [diff] [blame] | 85 | endif |
| 86 | |
Blue Swirl | 3dd46c7 | 2013-01-05 10:10:27 +0000 | [diff] [blame] | 87 | %.asm: %.S |
| 88 | $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@") |
| 89 | |
| 90 | %.o: %.asm |
| 91 | $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@") |
aliguori | 1775918 | 2009-01-21 18:12:52 +0000 | [diff] [blame] | 92 | |
Peter Maydell | c3dc9fd | 2014-02-05 17:27:27 +0000 | [diff] [blame] | 93 | %.o: %.cc |
Paolo Bonzini | 0db564e | 2014-05-08 14:34:09 +0200 | [diff] [blame] | 94 | $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@") |
Peter Maydell | c3dc9fd | 2014-02-05 17:27:27 +0000 | [diff] [blame] | 95 | |
Tomoki Sekiyama | 83f73fc | 2013-08-07 11:39:36 -0400 | [diff] [blame] | 96 | %.o: %.cpp |
Paolo Bonzini | 0db564e | 2014-05-08 14:34:09 +0200 | [diff] [blame] | 97 | $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@") |
Tomoki Sekiyama | 83f73fc | 2013-08-07 11:39:36 -0400 | [diff] [blame] | 98 | |
aliguori | 1775918 | 2009-01-21 18:12:52 +0000 | [diff] [blame] | 99 | %.o: %.m |
Paolo Bonzini | 0db564e | 2014-05-08 14:34:09 +0200 | [diff] [blame] | 100 | $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@") |
aliguori | 1775918 | 2009-01-21 18:12:52 +0000 | [diff] [blame] | 101 | |
Paolo Bonzini | 2c13ec5 | 2012-12-21 09:23:18 +0100 | [diff] [blame] | 102 | %.o: %.dtrace |
| 103 | $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@") |
| 104 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 105 | %$(DSOSUF): CFLAGS += -fPIC -DBUILD_DSO |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 106 | %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED) |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 107 | %$(DSOSUF): %.mo |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 108 | $(call LINK,$^) |
Fam Zheng | e26110c | 2014-02-10 14:48:57 +0800 | [diff] [blame] | 109 | @# Copy to build root so modules can be loaded when program started without install |
| 110 | $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)")) |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 111 | |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 112 | |
| 113 | LD_REL := $(CC) -nostdlib -Wl,-r |
| 114 | |
| 115 | %.mo: |
| 116 | $(call quiet-command,$(LD_REL) -o $@ $^," LD -r $(TARGET_DIR)$@") |
| 117 | |
Fam Zheng | 1796926 | 2014-02-10 14:48:56 +0800 | [diff] [blame] | 118 | .PHONY: modules |
| 119 | modules: |
| 120 | |
aliguori | 3aa892d | 2009-01-21 18:13:02 +0000 | [diff] [blame] | 121 | %$(EXESUF): %.o |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 122 | $(call LINK,$^) |
aliguori | 4f188f8 | 2009-01-21 18:13:09 +0000 | [diff] [blame] | 123 | |
aliguori | 93a0dba | 2009-01-21 18:13:16 +0000 | [diff] [blame] | 124 | %.a: |
aliguori | 28c699a | 2009-01-26 17:07:46 +0000 | [diff] [blame] | 125 | $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@") |
aliguori | 93a0dba | 2009-01-21 18:13:16 +0000 | [diff] [blame] | 126 | |
aliguori | 28c699a | 2009-01-26 17:07:46 +0000 | [diff] [blame] | 127 | quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1)) |
Juan Quintela | 70071e1 | 2009-07-21 14:11:18 +0200 | [diff] [blame] | 128 | |
| 129 | # cc-option |
Juan Quintela | 8a2e6ab | 2009-08-31 00:48:45 +0200 | [diff] [blame] | 130 | # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0) |
Juan Quintela | 70071e1 | 2009-07-21 14:11:18 +0200 | [diff] [blame] | 131 | |
Thomas Monjalon | fc3baad | 2009-09-11 18:45:40 +0200 | [diff] [blame] | 132 | cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \ |
| 133 | >/dev/null 2>&1 && echo OK), $2, $3) |
Juan Quintela | 1215c6e | 2009-10-07 02:40:58 +0200 | [diff] [blame] | 134 | |
Peter Maydell | c3dc9fd | 2014-02-05 17:27:27 +0000 | [diff] [blame] | 135 | VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc |
Nathan Froyd | 288e7bc | 2010-04-26 14:52:23 -0700 | [diff] [blame] | 136 | set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1))) |
Paolo Bonzini | 076d247 | 2009-12-21 10:06:55 +0100 | [diff] [blame] | 137 | |
Michael Tokarev | 0d65942 | 2014-06-22 10:55:23 +0400 | [diff] [blame] | 138 | # install-prog list, dir |
| 139 | define install-prog |
| 140 | $(INSTALL_DIR) "$2" |
| 141 | $(INSTALL_PROG) $1 "$2" |
| 142 | $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),) |
| 143 | endef |
| 144 | |
Paolo Bonzini | 2b2e59e | 2010-10-21 10:18:40 +0200 | [diff] [blame] | 145 | # find-in-path |
| 146 | # Usage: $(call find-in-path, prog) |
| 147 | # Looks in the PATH if the argument contains no slash, else only considers one |
| 148 | # specific directory. Returns an # empty string if the program doesn't exist |
| 149 | # there. |
| 150 | find-in-path = $(if $(find-string /, $1), \ |
| 151 | $(wildcard $1), \ |
| 152 | $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH))))) |
| 153 | |
Peter Maydell | 837a2e2 | 2013-09-13 18:25:51 +0100 | [diff] [blame] | 154 | # Logical functions (for operating on y/n values like CONFIG_FOO vars) |
| 155 | # Inputs to these must be either "y" (true) or "n" or "" (both false) |
| 156 | # Output is always either "y" or "n". |
| 157 | # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR)) |
| 158 | # Logical NOT |
| 159 | lnot = $(if $(subst n,,$1),n,y) |
| 160 | # Logical AND |
| 161 | land = $(if $(findstring yy,$1$2),y,n) |
| 162 | # Logical OR |
| 163 | lor = $(if $(findstring y,$1$2),y,n) |
| 164 | # Logical XOR (note that this is the inverse of leqv) |
| 165 | lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y) |
| 166 | # Logical equivalence (note that leqv "","n" is true) |
| 167 | leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n) |
| 168 | # Logical if: like make's $(if) but with an leqv-like test |
| 169 | lif = $(if $(subst n,,$1),$2,$3) |
| 170 | |
Peter Maydell | 9ef622e | 2013-09-13 18:25:52 +0100 | [diff] [blame] | 171 | # String testing functions: inputs to these can be any string; |
| 172 | # the output is always either "y" or "n". Leading and trailing whitespace |
| 173 | # is ignored when comparing strings. |
| 174 | # String equality |
| 175 | eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y) |
| 176 | # String inequality |
| 177 | ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n) |
| 178 | # Emptiness/non-emptiness tests: |
| 179 | isempty = $(if $1,n,y) |
| 180 | notempty = $(if $1,y,n) |
| 181 | |
Lluís Vilanova | c042493 | 2012-04-18 20:15:45 +0200 | [diff] [blame] | 182 | # Generate files with tracetool |
| 183 | TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py |
| 184 | |
Juan Quintela | 1215c6e | 2009-10-07 02:40:58 +0200 | [diff] [blame] | 185 | # Generate timestamp files for .h include files |
| 186 | |
Michael S. Tsirkin | 4b25966 | 2013-01-15 13:12:35 +0200 | [diff] [blame] | 187 | config-%.h: config-%.h-timestamp |
| 188 | @cmp $< $@ >/dev/null 2>&1 || cp $< $@ |
Juan Quintela | 1215c6e | 2009-10-07 02:40:58 +0200 | [diff] [blame] | 189 | |
Michael S. Tsirkin | 4b25966 | 2013-01-15 13:12:35 +0200 | [diff] [blame] | 190 | config-%.h-timestamp: config-%.mak |
| 191 | $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h") |
Michael S. Tsirkin | 7dbbbb0 | 2009-12-07 21:04:52 +0200 | [diff] [blame] | 192 | |
Michael S. Tsirkin | 7586317 | 2013-01-15 13:27:54 +0200 | [diff] [blame] | 193 | .PHONY: clean-timestamp |
| 194 | clean-timestamp: |
| 195 | rm -f *.timestamp |
| 196 | clean: clean-timestamp |
| 197 | |
Michael S. Tsirkin | 7dbbbb0 | 2009-12-07 21:04:52 +0200 | [diff] [blame] | 198 | # will delete the target of a rule if commands exit with a nonzero exit status |
| 199 | .DELETE_ON_ERROR: |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 200 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 201 | # save-vars |
| 202 | # Usage: $(call save-vars, vars) |
| 203 | # Save each variable $v in $vars as save-vars-$v, save their object's |
| 204 | # variables, then clear $v. |
| 205 | define save-vars |
| 206 | $(foreach v,$1, |
| 207 | $(eval save-vars-$v := $(value $v)) |
| 208 | $(foreach o,$($v), |
| 209 | $(foreach k,cflags libs objs, |
| 210 | $(if $($o-$k), |
| 211 | $(eval save-vars-$o-$k := $($o-$k)) |
| 212 | $(eval $o-$k := )))) |
| 213 | $(eval $v := )) |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 214 | endef |
| 215 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 216 | # load-vars |
| 217 | # Usage: $(call load-vars, vars, add_var) |
| 218 | # Load the saved value for each variable in @vars, and the per object |
| 219 | # variables. |
| 220 | # Append @add_var's current value to the loaded value. |
| 221 | define load-vars |
| 222 | $(eval $2-new-value := $(value $2)) |
| 223 | $(foreach v,$1, |
| 224 | $(eval $v := $(value save-vars-$v)) |
| 225 | $(foreach o,$($v), |
| 226 | $(foreach k,cflags libs objs, |
| 227 | $(if $(save-vars-$o-$k), |
| 228 | $(eval $o-$k := $(save-vars-$o-$k)) |
| 229 | $(eval save-vars-$o-$k := )))) |
| 230 | $(eval save-vars-$v := )) |
| 231 | $(eval $2 := $(value $2) $($2-new-value)) |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 232 | endef |
| 233 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 234 | # fix-paths |
| 235 | # Usage: $(call fix-paths, obj_path, src_path, vars) |
| 236 | # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all |
| 237 | # directories in @vars. |
| 238 | define fix-paths |
| 239 | $(foreach v,$3, |
| 240 | $(foreach o,$($v), |
| 241 | $(if $($o-libs), |
| 242 | $(eval $1$o-libs := $($o-libs))) |
| 243 | $(if $($o-cflags), |
| 244 | $(eval $1$o-cflags := $($o-cflags))) |
| 245 | $(if $($o-objs), |
| 246 | $(eval $1$o-objs := $(addprefix $1,$($o-objs))))) |
| 247 | $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \ |
| 248 | $(addprefix $2,$(filter %/,$($v))))) |
Fam Zheng | 5c0d52b | 2014-02-10 14:48:53 +0800 | [diff] [blame] | 249 | endef |
| 250 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 251 | # unnest-var-recursive |
| 252 | # Usage: $(call unnest-var-recursive, obj_prefix, vars, var) |
| 253 | # |
| 254 | # Unnest @var by including subdir Makefile.objs, while protect others in @vars |
| 255 | # unchanged. |
| 256 | # |
| 257 | # @obj_prefix is the starting point of object path prefix. |
| 258 | # |
| 259 | define unnest-var-recursive |
| 260 | $(eval dirs := $(sort $(filter %/,$($3)))) |
| 261 | $(eval $3 := $(filter-out %/,$($3))) |
| 262 | $(foreach d,$(dirs:%/=%), |
| 263 | $(call save-vars,$2) |
| 264 | $(eval obj := $(if $1,$1/)$d) |
| 265 | $(eval -include $(SRC_PATH)/$d/Makefile.objs) |
| 266 | $(call fix-paths,$(if $1,$1/)$d/,$d/,$2) |
| 267 | $(call load-vars,$2,$3) |
| 268 | $(call unnest-var-recursive,$1,$2,$3)) |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 269 | endef |
| 270 | |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 271 | # unnest-vars |
| 272 | # Usage: $(call unnest-vars, obj_prefix, vars) |
| 273 | # |
| 274 | # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include |
| 275 | # ending '/'. |
| 276 | # |
| 277 | # @vars: the list of variable names to unnest. |
| 278 | # |
| 279 | # This macro will scan subdirectories's Makefile.objs, include them, to build |
| 280 | # up each variable listed in @vars. |
| 281 | # |
| 282 | # Per object and per module cflags and libs are saved with relative path fixed |
| 283 | # as well, those variables include -libs, -cflags and -objs. Items in -objs are |
| 284 | # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix. |
| 285 | # |
| 286 | # All nested variables postfixed by -m in names are treated as DSO variables, |
| 287 | # and will be built as modules, if enabled. |
| 288 | # |
| 289 | # A simple example of the unnest: |
| 290 | # |
| 291 | # obj_prefix = .. |
| 292 | # vars = hot cold |
| 293 | # hot = fire.o sun.o season/ |
| 294 | # cold = snow.o water/ season/ |
| 295 | # |
| 296 | # Unnest through a faked source directory structure: |
| 297 | # |
| 298 | # SRC_PATH |
| 299 | # ├── water |
| 300 | # │ └── Makefile.objs──────────────────┐ |
| 301 | # │ │ hot += steam.o │ |
| 302 | # │ │ cold += ice.mo │ |
| 303 | # │ │ ice.mo-libs := -licemaker │ |
| 304 | # │ │ ice.mo-objs := ice1.o ice2.o │ |
| 305 | # │ └──────────────────────────────┘ |
| 306 | # │ |
| 307 | # └── season |
| 308 | # └── Makefile.objs──────┐ |
| 309 | # │ hot += summer.o │ |
| 310 | # │ cold += winter.o │ |
| 311 | # └──────────────────┘ |
| 312 | # |
| 313 | # In the end, the result will be: |
| 314 | # |
| 315 | # hot = ../fire.o ../sun.o ../season/summer.o |
| 316 | # cold = ../snow.o ../water/ice.mo ../season/winter.o |
| 317 | # ../water/ice.mo-libs = -licemaker |
| 318 | # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o |
| 319 | # |
| 320 | # Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not |
| 321 | # included. |
| 322 | # |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 323 | define unnest-vars |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 324 | # In the case of target build (i.e. $1 == ..), fix path for top level |
| 325 | # Makefile.objs objects |
| 326 | $(if $1,$(call fix-paths,$1/,,$2)) |
| 327 | |
| 328 | # Descend and include every subdir Makefile.objs |
Fam Zheng | c88f68e | 2015-01-12 12:43:09 +0800 | [diff] [blame] | 329 | $(foreach v, $2, |
| 330 | $(call unnest-var-recursive,$1,$2,$v) |
| 331 | # Pass the .mo-cflags and .mo-libs along to its member objects |
| 332 | $(foreach o, $(filter %.mo,$($v)), |
| 333 | $(foreach p,$($o-objs), |
| 334 | $(if $($o-cflags), $(eval $p-cflags += $($o-cflags))) |
| 335 | $(if $($o-libs), $(eval $p-libs += $($o-libs)))))) |
| 336 | |
| 337 | # For all %.mo objects that are directly added into -y, just expand them |
| 338 | $(foreach v,$(filter %-y,$2), |
| 339 | $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o)))) |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 340 | |
| 341 | $(foreach v,$(filter %-m,$2), |
| 342 | # All .o found in *-m variables are single object modules, create .mo |
| 343 | # for them |
| 344 | $(foreach o,$(filter %.o,$($v)), |
| 345 | $(eval $(o:%.o=%.mo)-objs := $o)) |
| 346 | # Now unify .o in -m variable to .mo |
| 347 | $(eval $v := $($v:%.o=%.mo)) |
| 348 | $(eval modules-m += $($v)) |
| 349 | |
| 350 | # For module build, build shared libraries during "make modules" |
| 351 | # For non-module build, add -m to -y |
| 352 | $(if $(CONFIG_MODULES), |
Fam Zheng | c261d77 | 2014-09-01 18:35:10 +0800 | [diff] [blame] | 353 | $(foreach o,$($v), |
| 354 | $(eval $o: $($o-objs))) |
| 355 | $(eval $(patsubst %-m,%-y,$v) += $($v)) |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 356 | $(eval modules: $($v:%.mo=%$(DSOSUF))), |
| 357 | $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v))))) |
| 358 | |
| 359 | # Post-process all the unnested vars |
| 360 | $(foreach v,$2, |
| 361 | $(foreach o, $(filter %.mo,$($v)), |
| 362 | # Find all the .mo objects in variables and add dependency rules |
| 363 | # according to .mo-objs. Report error if not set |
| 364 | $(if $($o-objs), |
| 365 | $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)), |
Fam Zheng | c88f68e | 2015-01-12 12:43:09 +0800 | [diff] [blame] | 366 | $(error $o added in $v but $o-objs is not set))) |
Fam Zheng | 1c33ac5 | 2014-05-27 15:54:19 +0800 | [diff] [blame] | 367 | $(shell mkdir -p ./ $(sort $(dir $($v)))) |
| 368 | # Include all the .d files |
| 369 | $(eval -include $(addsuffix *.d, $(sort $(dir $($v))))) |
| 370 | $(eval $v := $(filter-out %/,$($v)))) |
Paolo Bonzini | e05804e | 2012-05-22 13:41:27 +0200 | [diff] [blame] | 371 | endef |