You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rules.mak 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. # find-in-path
  129. # Usage: $(call find-in-path, prog)
  130. # Looks in the PATH if the argument contains no slash, else only considers one
  131. # specific directory. Returns an # empty string if the program doesn't exist
  132. # there.
  133. find-in-path = $(if $(findstring /, $1), \
  134. $(wildcard $1), \
  135. $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
  136. # Logical functions (for operating on y/n values like CONFIG_FOO vars)
  137. # Inputs to these must be either "y" (true) or "n" or "" (both false)
  138. # Output is always either "y" or "n".
  139. # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
  140. # Logical NOT
  141. lnot = $(if $(subst n,,$1),n,y)
  142. # Logical AND
  143. land = $(if $(findstring yy,$1$2),y,n)
  144. # Logical OR
  145. lor = $(if $(findstring y,$1$2),y,n)
  146. # Logical XOR (note that this is the inverse of leqv)
  147. lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
  148. # Logical equivalence (note that leqv "","n" is true)
  149. leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
  150. # Logical if: like make's $(if) but with an leqv-like test
  151. lif = $(if $(subst n,,$1),$2,$3)
  152. # String testing functions: inputs to these can be any string;
  153. # the output is always either "y" or "n". Leading and trailing whitespace
  154. # is ignored when comparing strings.
  155. # String equality
  156. eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
  157. # String inequality
  158. ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
  159. # Emptiness/non-emptiness tests:
  160. isempty = $(if $1,n,y)
  161. notempty = $(if $1,y,n)
  162. # Generate files with tracetool
  163. TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
  164. # Generate timestamp files for .h include files
  165. config-%.h: config-%.h-timestamp
  166. @cmp $< $@ >/dev/null 2>&1 || cp $< $@
  167. config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
  168. $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
  169. .PHONY: clean-timestamp
  170. clean-timestamp:
  171. rm -f *.timestamp
  172. clean: clean-timestamp
  173. # will delete the target of a rule if commands exit with a nonzero exit status
  174. .DELETE_ON_ERROR:
  175. # save-vars
  176. # Usage: $(call save-vars, vars)
  177. # Save each variable $v in $vars as save-vars-$v, save their object's
  178. # variables, then clear $v. saved-vars-$v contains the variables that
  179. # where saved for the objects, in order to speedup load-vars.
  180. define save-vars
  181. $(foreach v,$1,
  182. $(eval save-vars-$v := $(value $v))
  183. $(eval saved-vars-$v := $(foreach o,$($v), \
  184. $(if $($o-cflags), $o-cflags $(eval save-vars-$o-cflags := $($o-cflags))$(eval $o-cflags := )) \
  185. $(if $($o-libs), $o-libs $(eval save-vars-$o-libs := $($o-libs))$(eval $o-libs := )) \
  186. $(if $($o-objs), $o-objs $(eval save-vars-$o-objs := $($o-objs))$(eval $o-objs := ))))
  187. $(eval $v := ))
  188. endef
  189. # load-vars
  190. # Usage: $(call load-vars, vars, add_var)
  191. # Load the saved value for each variable in @vars, and the per object
  192. # variables.
  193. # Append @add_var's current value to the loaded value.
  194. define load-vars
  195. $(eval $2-new-value := $(value $2))
  196. $(foreach v,$1,
  197. $(eval $v := $(value save-vars-$v))
  198. $(foreach o,$(saved-vars-$v),
  199. $(eval $o := $(save-vars-$o)) $(eval save-vars-$o := ))
  200. $(eval save-vars-$v := )
  201. $(eval saved-vars-$v := ))
  202. $(eval $2 := $(value $2) $($2-new-value))
  203. endef
  204. # fix-paths
  205. # Usage: $(call fix-paths, obj_path, src_path, vars)
  206. # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
  207. # directories in @vars.
  208. define fix-paths
  209. $(foreach v,$3,
  210. $(foreach o,$($v),
  211. $(if $($o-libs),
  212. $(eval $1$o-libs := $($o-libs)))
  213. $(if $($o-cflags),
  214. $(eval $1$o-cflags := $($o-cflags)))
  215. $(if $($o-objs),
  216. $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
  217. $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
  218. $(addprefix $2,$(filter %/,$($v)))))
  219. endef
  220. # unnest-var-recursive
  221. # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
  222. #
  223. # Unnest @var by including subdir Makefile.objs, while protect others in @vars
  224. # unchanged.
  225. #
  226. # @obj_prefix is the starting point of object path prefix.
  227. #
  228. define unnest-var-recursive
  229. $(eval dirs := $(sort $(filter %/,$($3))))
  230. $(eval $3 := $(filter-out %/,$($3)))
  231. $(foreach d,$(dirs:%/=%),
  232. $(call save-vars,$2)
  233. $(eval obj := $(if $1,$1/)$d)
  234. $(eval -include $(SRC_PATH)/$d/Makefile.objs)
  235. $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
  236. $(call load-vars,$2,$3)
  237. $(call unnest-var-recursive,$1,$2,$3))
  238. endef
  239. # unnest-vars
  240. # Usage: $(call unnest-vars, obj_prefix, vars)
  241. #
  242. # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
  243. # ending '/'.
  244. #
  245. # @vars: the list of variable names to unnest.
  246. #
  247. # This macro will scan subdirectories's Makefile.objs, include them, to build
  248. # up each variable listed in @vars.
  249. #
  250. # Per object and per module cflags and libs are saved with relative path fixed
  251. # as well, those variables include -libs, -cflags and -objs. Items in -objs are
  252. # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
  253. #
  254. # All nested variables postfixed by -m in names are treated as DSO variables,
  255. # and will be built as modules, if enabled.
  256. #
  257. # A simple example of the unnest:
  258. #
  259. # obj_prefix = ..
  260. # vars = hot cold
  261. # hot = fire.o sun.o season/
  262. # cold = snow.o water/ season/
  263. #
  264. # Unnest through a faked source directory structure:
  265. #
  266. # SRC_PATH
  267. # ├── water
  268. # │ └── Makefile.objs──────────────────┐
  269. # │ │ hot += steam.o │
  270. # │ │ cold += ice.mo │
  271. # │ │ ice.mo-libs := -licemaker │
  272. # │ │ ice.mo-objs := ice1.o ice2.o │
  273. # │ └──────────────────────────────┘
  274. # │
  275. # └── season
  276. # └── Makefile.objs──────┐
  277. # │ hot += summer.o │
  278. # │ cold += winter.o │
  279. # └──────────────────┘
  280. #
  281. # In the end, the result will be:
  282. #
  283. # hot = ../fire.o ../sun.o ../season/summer.o
  284. # cold = ../snow.o ../water/ice.mo ../season/winter.o
  285. # ../water/ice.mo-libs = -licemaker
  286. # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
  287. #
  288. # Note that 'hot' didn't include 'water/' in the input, so 'steam.o' is not
  289. # included.
  290. #
  291. define unnest-vars
  292. # In the case of target build (i.e. $1 == ..), fix path for top level
  293. # Makefile.objs objects
  294. $(if $1,$(call fix-paths,$1/,,$2))
  295. # Descend and include every subdir Makefile.objs
  296. $(foreach v, $2,
  297. $(call unnest-var-recursive,$1,$2,$v)
  298. # Pass the .mo-cflags and .mo-libs along to its member objects
  299. $(foreach o, $(filter %.mo,$($v)),
  300. $(foreach p,$($o-objs),
  301. $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
  302. $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
  303. # For all %.mo objects that are directly added into -y, just expand them
  304. $(foreach v,$(filter %-y,$2),
  305. $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
  306. $(foreach v,$(filter %-m,$2),
  307. # All .o found in *-m variables are single object modules, create .mo
  308. # for them
  309. $(foreach o,$(filter %.o,$($v)),
  310. $(eval $(o:%.o=%.mo)-objs := $o))
  311. # Now unify .o in -m variable to .mo
  312. $(eval $v := $($v:%.o=%.mo))
  313. $(eval modules-m += $($v))
  314. # For module build, build shared libraries during "make modules"
  315. # For non-module build, add -m to -y
  316. $(if $(CONFIG_MODULES),
  317. $(foreach o,$($v),
  318. $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
  319. $(eval $o: $($o-objs)))
  320. $(eval $(patsubst %-m,%-y,$v) += $($v))
  321. $(eval modules: $($v:%.mo=%$(DSOSUF))),
  322. $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
  323. # Post-process all the unnested vars
  324. $(foreach v,$2,
  325. $(foreach o, $(filter %.mo,$($v)),
  326. # Find all the .mo objects in variables and add dependency rules
  327. # according to .mo-objs. Report error if not set
  328. $(if $($o-objs),
  329. $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
  330. $(error $o added in $v but $o-objs is not set)))
  331. $(shell mkdir -p ./ $(sort $(dir $($v))))
  332. # Include all the .d files
  333. $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
  334. $(eval $v := $(filter-out %/,$($v))))
  335. endef
  336. TEXI2MAN = $(call quiet-command, \
  337. perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $(TEXI2PODFLAGS) $< $@.pod && \
  338. $(POD2MAN) --section=$(subst .,,$(suffix $@)) --center=" " --release=" " $@.pod > $@, \
  339. "GEN","$@")
  340. %.1:
  341. $(call TEXI2MAN)
  342. %.7:
  343. $(call TEXI2MAN)
  344. %.8:
  345. $(call TEXI2MAN)
  346. GEN_SUBST = $(call quiet-command, \
  347. sed -e "s!@libexecdir@!$(libexecdir)!g" < $< > $@, \
  348. "GEN","$@")
  349. %.json: %.json.in
  350. $(call GEN_SUBST)