2
0

rules.mak 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # These are used when we want to do substitutions without confusing Make
  2. NULL :=
  3. SPACE := $(NULL) #
  4. COMMA := ,
  5. # Don't use implicit rules or variables
  6. # we have explicit rules for everything
  7. MAKEFLAGS += -rR
  8. # Files with this suffixes are final, don't try to generate them
  9. # using implicit rules
  10. %/trace-events:
  11. %.hx:
  12. %.py:
  13. %.objs:
  14. %.d:
  15. %.h:
  16. %.c:
  17. %.cc:
  18. %.cpp:
  19. %.m:
  20. %.mak:
  21. clean-target:
  22. # Flags for dependency generation
  23. QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
  24. # Compiler searches the source file dir first, but in vpath builds
  25. # we need to make it search the build dir too, before any other
  26. # explicit search paths. There are two search locations in the build
  27. # dir, one absolute and the other relative to the compiler working
  28. # directory. These are the same for target-independent files, but
  29. # different for target-dependent ones.
  30. QEMU_LOCAL_INCLUDES = -iquote $(BUILD_DIR)/$(@D) -iquote $(@D)
  31. WL_U := -Wl,-u,
  32. find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
  33. defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
  34. undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
  35. # All the .mo objects in -m variables are also added into corresponding -y
  36. # variable in unnest-vars, but filtered out here, when LINK is called.
  37. #
  38. # The .mo objects are supposed to be linked as a DSO, for module build. So here
  39. # they are only used as a placeholders to generate those "archive undefined"
  40. # symbol options (-Wl,-u,$symbol_name), which are the archive functions
  41. # referenced by the code in the DSO.
  42. #
  43. # Also the presence in -y variables will also guarantee they are built before
  44. # linking executables that will load them. So we can look up symbol reference
  45. # in LINK.
  46. #
  47. # This is necessary because the exectuable itself may not use the function, in
  48. # which case the function would not be linked in. Then the DSO loading will
  49. # fail because of the missing symbol.
  50. process-archive-undefs = $(filter-out %.a %.mo,$1) \
  51. $(addprefix $(WL_U), \
  52. $(filter $(call defined-symbols,$(filter %.a, $1)), \
  53. $(call undefined-symbols,$(filter %.mo,$1)))) \
  54. $(filter %.a,$1)
  55. extract-libs = $(strip $(foreach o,$(filter-out %.mo,$1),$($o-libs)))
  56. expand-objs = $(strip $(sort $(filter %.o,$1)) \
  57. $(foreach o,$(filter %.mo,$1),$($o-objs)) \
  58. $(filter-out %.o %.mo,$1))
  59. %.o: %.c
  60. $(call quiet-command,$(CC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
  61. $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
  62. -c -o $@ $<,"CC","$(TARGET_DIR)$@")
  63. %.o: %.rc
  64. $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
  65. # If we have a CXX we might have some C++ objects, in which case we
  66. # must link with the C++ compiler, not the plain C compiler.
  67. LINKPROG = $(or $(CXX),$(CC))
  68. LINK = $(call quiet-command, $(LINKPROG) $(QEMU_LDFLAGS) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
  69. $(call process-archive-undefs, $1) \
  70. $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
  71. %.o: %.S
  72. $(call quiet-command,$(CCAS) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
  73. $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) \
  74. -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
  75. %.o: %.cc
  76. $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
  77. $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
  78. -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
  79. %.o: %.cpp
  80. $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
  81. $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
  82. -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
  83. %.o: %.m
  84. $(call quiet-command,$(OBJCC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
  85. $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
  86. -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
  87. %.o: %.dtrace
  88. $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
  89. DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
  90. module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
  91. %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
  92. %$(DSOSUF): %.mo
  93. $(call LINK,$^)
  94. @# Copy to build root so modules can be loaded when program started without install
  95. $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
  96. LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
  97. %.mo:
  98. $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
  99. .PHONY: modules
  100. modules:
  101. %$(EXESUF): %.o
  102. $(call LINK,$(filter %.o %.a %.mo, $^))
  103. %.a:
  104. $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
  105. # Usage: $(call quiet-command,command and args,"NAME","args to print")
  106. # This will run "command and args", and either:
  107. # if V=1 just print the whole command and args
  108. # otherwise print the 'quiet' output in the format " NAME args to print"
  109. # NAME should be a short name of the command, 7 letters or fewer.
  110. # If called with only a single argument, will print nothing in quiet mode.
  111. quiet-command-run = $(if $(V),,$(if $2,printf " %-7s %s\n" $2 $3 && ))$1
  112. quiet-@ = $(if $(V),,@)
  113. quiet-command = $(quiet-@)$(call quiet-command-run,$1,$2,$3)
  114. # cc-option
  115. # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
  116. cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
  117. >/dev/null 2>&1 && echo OK), $2, $3)
  118. cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
  119. >/dev/null 2>&1 && echo OK), $2, $3)
  120. VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc Kconfig% %.json.in
  121. set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
  122. # install-prog list, dir
  123. define install-prog
  124. $(INSTALL_DIR) "$2"
  125. $(INSTALL_PROG) $1 "$2"
  126. $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
  127. endef
  128. # install-so list, dir
  129. define install-so
  130. $(INSTALL_DIR) "$2"
  131. $(INSTALL_LIB) $1 "$2"
  132. endef
  133. # find-in-path
  134. # Usage: $(call find-in-path, prog)
  135. # Looks in the PATH if the argument contains no slash, else only considers one
  136. # specific directory. Returns an # empty string if the program doesn't exist
  137. # there.
  138. find-in-path = $(if $(findstring /, $1), \
  139. $(wildcard $1), \
  140. $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
  141. # Logical functions (for operating on y/n values like CONFIG_FOO vars)
  142. # Inputs to these must be either "y" (true) or "n" or "" (both false)
  143. # Output is always either "y" or "n".
  144. # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
  145. # Logical NOT
  146. lnot = $(if $(subst n,,$1),n,y)
  147. # Logical AND
  148. land = $(if $(findstring yy,$1$2),y,n)
  149. # Logical OR
  150. lor = $(if $(findstring y,$1$2),y,n)
  151. # Logical XOR (note that this is the inverse of leqv)
  152. lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
  153. # Logical equivalence (note that leqv "","n" is true)
  154. leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
  155. # Logical if: like make's $(if) but with an leqv-like test
  156. lif = $(if $(subst n,,$1),$2,$3)
  157. # String testing functions: inputs to these can be any string;
  158. # the output is always either "y" or "n". Leading and trailing whitespace
  159. # is ignored when comparing strings.
  160. # String equality
  161. eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
  162. # String inequality
  163. ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
  164. # Emptiness/non-emptiness tests:
  165. isempty = $(if $1,n,y)
  166. notempty = $(if $1,y,n)
  167. # Generate files with tracetool
  168. TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
  169. # Generate timestamp files for .h include files
  170. config-%.h: config-%.h-timestamp
  171. @cmp $< $@ >/dev/null 2>&1 || cp $< $@
  172. config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
  173. $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
  174. .PHONY: clean-timestamp
  175. clean-timestamp:
  176. rm -f *.timestamp
  177. clean: clean-timestamp
  178. # will delete the target of a rule if commands exit with a nonzero exit status
  179. .DELETE_ON_ERROR:
  180. # save-vars
  181. # Usage: $(call save-vars, vars)
  182. # Save each variable $v in $vars as save-vars-$v, save their object's
  183. # variables, then clear $v. saved-vars-$v contains the variables that
  184. # where saved for the objects, in order to speedup load-vars.
  185. define save-vars
  186. $(foreach v,$1,
  187. $(eval save-vars-$v := $(value $v))
  188. $(eval saved-vars-$v := $(foreach o,$($v), \
  189. $(if $($o-cflags), $o-cflags $(eval save-vars-$o-cflags := $($o-cflags))$(eval $o-cflags := )) \
  190. $(if $($o-libs), $o-libs $(eval save-vars-$o-libs := $($o-libs))$(eval $o-libs := )) \
  191. $(if $($o-objs), $o-objs $(eval save-vars-$o-objs := $($o-objs))$(eval $o-objs := ))))
  192. $(eval $v := ))
  193. endef
  194. # load-vars
  195. # Usage: $(call load-vars, vars, add_var)
  196. # Load the saved value for each variable in @vars, and the per object
  197. # variables.
  198. # Append @add_var's current value to the loaded value.
  199. define load-vars
  200. $(eval $2-new-value := $(value $2))
  201. $(foreach v,$1,
  202. $(eval $v := $(value save-vars-$v))
  203. $(foreach o,$(saved-vars-$v),
  204. $(eval $o := $(save-vars-$o)) $(eval save-vars-$o := ))
  205. $(eval save-vars-$v := )
  206. $(eval saved-vars-$v := ))
  207. $(eval $2 := $(value $2) $($2-new-value))
  208. endef
  209. # fix-paths
  210. # Usage: $(call fix-paths, obj_path, src_path, vars)
  211. # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
  212. # directories in @vars.
  213. define fix-paths
  214. $(foreach v,$3,
  215. $(foreach o,$($v),
  216. $(if $($o-libs),
  217. $(eval $1$o-libs := $($o-libs)))
  218. $(if $($o-cflags),
  219. $(eval $1$o-cflags := $($o-cflags)))
  220. $(if $($o-objs),
  221. $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
  222. $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
  223. $(addprefix $2,$(filter %/,$($v)))))
  224. endef
  225. # unnest-var-recursive
  226. # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
  227. #
  228. # Unnest @var by including subdir Makefile.objs, while protect others in @vars
  229. # unchanged.
  230. #
  231. # @obj_prefix is the starting point of object path prefix.
  232. #
  233. define unnest-var-recursive
  234. $(eval dirs := $(sort $(filter %/,$($3))))
  235. $(eval $3 := $(filter-out %/,$($3)))
  236. $(foreach d,$(dirs:%/=%),
  237. $(call save-vars,$2)
  238. $(eval obj := $(if $1,$1/)$d)
  239. $(eval -include $(SRC_PATH)/$d/Makefile.objs)
  240. $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
  241. $(call load-vars,$2,$3)
  242. $(call unnest-var-recursive,$1,$2,$3))
  243. endef
  244. # unnest-vars
  245. # Usage: $(call unnest-vars, obj_prefix, vars)
  246. #
  247. # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
  248. # ending '/'.
  249. #
  250. # @vars: the list of variable names to unnest.
  251. #
  252. # This macro will scan subdirectories's Makefile.objs, include them, to build
  253. # up each variable listed in @vars.
  254. #
  255. # Per object and per module cflags and libs are saved with relative path fixed
  256. # as well, those variables include -libs, -cflags and -objs. Items in -objs are
  257. # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
  258. #
  259. # All nested variables postfixed by -m in names are treated as DSO variables,
  260. # and will be built as modules, if enabled.
  261. #
  262. # A simple example of the unnest:
  263. #
  264. # obj_prefix = ..
  265. # vars = hot cold
  266. # hot = fire.o sun.o season/
  267. # cold = snow.o water/ season/
  268. #
  269. # Unnest through a faked source directory structure:
  270. #
  271. # SRC_PATH
  272. # ├── water
  273. # │ └── Makefile.objs──────────────────┐
  274. # │ │ hot += steam.o │
  275. # │ │ cold += ice.mo │
  276. # │ │ ice.mo-libs := -licemaker │
  277. # │ │ ice.mo-objs := ice1.o ice2.o │
  278. # │ └──────────────────────────────┘
  279. # │
  280. # └── season
  281. # └── Makefile.objs──────┐
  282. # │ hot += summer.o │
  283. # │ cold += winter.o │
  284. # └──────────────────┘
  285. #
  286. # In the end, the result will be:
  287. #
  288. # hot = ../fire.o ../sun.o ../season/summer.o
  289. # cold = ../snow.o ../water/ice.mo ../season/winter.o
  290. # ../water/ice.mo-libs = -licemaker
  291. # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
  292. #
  293. # Note that 'hot' didn't include 'water/' in the input, so 'steam.o' is not
  294. # included.
  295. #
  296. define unnest-vars
  297. # In the case of target build (i.e. $1 == ..), fix path for top level
  298. # Makefile.objs objects
  299. $(if $1,$(call fix-paths,$1/,,$2))
  300. # Descend and include every subdir Makefile.objs
  301. $(foreach v, $2,
  302. $(call unnest-var-recursive,$1,$2,$v)
  303. # Pass the .mo-cflags and .mo-libs along to its member objects
  304. $(foreach o, $(filter %.mo,$($v)),
  305. $(foreach p,$($o-objs),
  306. $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
  307. $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
  308. # For all %.mo objects that are directly added into -y, just expand them
  309. $(foreach v,$(filter %-y,$2),
  310. $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
  311. $(foreach v,$(filter %-m,$2),
  312. # All .o found in *-m variables are single object modules, create .mo
  313. # for them
  314. $(foreach o,$(filter %.o,$($v)),
  315. $(eval $(o:%.o=%.mo)-objs := $o))
  316. # Now unify .o in -m variable to .mo
  317. $(eval $v := $($v:%.o=%.mo))
  318. $(eval modules-m += $($v))
  319. # For module build, build shared libraries during "make modules"
  320. # For non-module build, add -m to -y
  321. $(if $(CONFIG_MODULES),
  322. $(foreach o,$($v),
  323. $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
  324. $(eval $o: $($o-objs)))
  325. $(eval $(patsubst %-m,%-y,$v) += $($v))
  326. $(eval modules: $($v:%.mo=%$(DSOSUF))),
  327. $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
  328. # Post-process all the unnested vars
  329. $(foreach v,$2,
  330. $(foreach o, $(filter %.mo,$($v)),
  331. # Find all the .mo objects in variables and add dependency rules
  332. # according to .mo-objs. Report error if not set
  333. $(if $($o-objs),
  334. $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
  335. $(error $o added in $v but $o-objs is not set)))
  336. $(shell mkdir -p ./ $(sort $(dir $($v))))
  337. # Include all the .d files
  338. $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
  339. $(eval $v := $(filter-out %/,$($v))))
  340. endef
  341. TEXI2MAN = $(call quiet-command, \
  342. perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $(TEXI2PODFLAGS) $< $@.pod && \
  343. $(POD2MAN) --section=$(subst .,,$(suffix $@)) --center=" " --release=" " $@.pod > $@, \
  344. "GEN","$@")
  345. %.1:
  346. $(call TEXI2MAN)
  347. %.7:
  348. $(call TEXI2MAN)
  349. %.8:
  350. $(call TEXI2MAN)
  351. GEN_SUBST = $(call quiet-command, \
  352. sed -e "s!@libexecdir@!$(libexecdir)!g" < $< > $@, \
  353. "GEN","$@")
  354. %.json: %.json.in
  355. $(call GEN_SUBST)