Makefile.build 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # Building
  4. # ==========================================================================
  5. src := $(if $(VPATH),$(VPATH)/)$(obj)
  6. PHONY := $(obj)/
  7. $(obj)/:
  8. # Init all relevant variables used in kbuild files so
  9. # 1) they have correct type
  10. # 2) they do not inherit any value from the environment
  11. obj-y :=
  12. obj-m :=
  13. lib-y :=
  14. lib-m :=
  15. always-y :=
  16. always-m :=
  17. targets :=
  18. subdir-y :=
  19. subdir-m :=
  20. EXTRA_AFLAGS :=
  21. EXTRA_CFLAGS :=
  22. EXTRA_CPPFLAGS :=
  23. EXTRA_LDFLAGS :=
  24. asflags-y :=
  25. ccflags-y :=
  26. rustflags-y :=
  27. cppflags-y :=
  28. ldflags-y :=
  29. subdir-asflags-y :=
  30. subdir-ccflags-y :=
  31. # Read auto.conf if it exists, otherwise ignore
  32. -include include/config/auto.conf
  33. include $(srctree)/scripts/Kbuild.include
  34. include $(srctree)/scripts/Makefile.compiler
  35. include $(kbuild-file)
  36. include $(srctree)/scripts/Makefile.lib
  37. ifndef obj
  38. $(warning kbuild: Makefile.build is included improperly)
  39. endif
  40. ifeq ($(need-modorder),)
  41. ifneq ($(obj-m),)
  42. $(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
  43. $(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
  44. endif
  45. endif
  46. # ===========================================================================
  47. # subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
  48. subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
  49. subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
  50. targets-for-builtin := $(extra-y)
  51. ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
  52. targets-for-builtin += $(obj)/lib.a
  53. endif
  54. ifdef need-builtin
  55. targets-for-builtin += $(obj)/built-in.a
  56. endif
  57. targets-for-modules := $(foreach x, o mod, \
  58. $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
  59. ifdef need-modorder
  60. targets-for-modules += $(obj)/modules.order
  61. endif
  62. targets += $(targets-for-builtin) $(targets-for-modules)
  63. # Linus' kernel sanity checking tool
  64. ifeq ($(KBUILD_CHECKSRC),1)
  65. quiet_cmd_checksrc = CHECK $<
  66. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
  67. else ifeq ($(KBUILD_CHECKSRC),2)
  68. quiet_cmd_force_checksrc = CHECK $<
  69. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
  70. endif
  71. ifneq ($(KBUILD_EXTRA_WARN),)
  72. cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \
  73. $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \
  74. $<
  75. endif
  76. # Compile C sources (.c)
  77. # ---------------------------------------------------------------------------
  78. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  79. cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
  80. $(obj)/%.s: $(obj)/%.c FORCE
  81. $(call if_changed_dep,cc_s_c)
  82. quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
  83. cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
  84. $(obj)/%.i: $(obj)/%.c FORCE
  85. $(call if_changed_dep,cpp_i_c)
  86. genksyms = scripts/genksyms/genksyms \
  87. $(if $(1), -T $(2)) \
  88. $(if $(KBUILD_PRESERVE), -p) \
  89. -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
  90. # These mirror gensymtypes_S and co below, keep them in synch.
  91. cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
  92. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  93. cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
  94. $(obj)/%.symtypes : $(obj)/%.c FORCE
  95. $(call cmd,cc_symtypes_c)
  96. # LLVM assembly
  97. # Generate .ll files from .c
  98. quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
  99. cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
  100. $(obj)/%.ll: $(obj)/%.c FORCE
  101. $(call if_changed_dep,cc_ll_c)
  102. # C (.c) files
  103. # The C file is compiled and updated dependency information is generated.
  104. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  105. is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
  106. # When a module consists of a single object, there is no reason to keep LLVM IR.
  107. # Make $(LD) covert LLVM IR to ELF here.
  108. ifdef CONFIG_LTO_CLANG
  109. cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
  110. endif
  111. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  112. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
  113. $(cmd_ld_single_m) \
  114. $(cmd_objtool)
  115. ifdef CONFIG_MODVERSIONS
  116. # When module versioning is enabled the following steps are executed:
  117. # o compile a <file>.o from <file>.c
  118. # o if <file>.o doesn't contain a __export_symbol_*, i.e. does
  119. # not export symbols, it's done.
  120. # o otherwise, we calculate symbol versions using the good old
  121. # genksyms on the preprocessed source and dump them into the .cmd file.
  122. # o modpost will extract versions from that file and create *.c files that will
  123. # be compiled and linked to the kernel and/or modules.
  124. gen_symversions = \
  125. if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \
  126. $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  127. >> $(dot-target).cmd; \
  128. fi
  129. cmd_gen_symversions_c = $(call gen_symversions,c)
  130. endif
  131. ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
  132. # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
  133. ifdef BUILD_C_RECORDMCOUNT
  134. ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
  135. RECORDMCOUNT_FLAGS = -w
  136. endif
  137. # Due to recursion, we must skip empty.o.
  138. # The empty.o file is created in the make process in order to determine
  139. # the target endianness and word size. It is made before all other C
  140. # files, including recordmcount.
  141. sub_cmd_record_mcount = \
  142. if [ $(@) != "scripts/mod/empty.o" ]; then \
  143. $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
  144. fi;
  145. recordmcount_source := $(srctree)/scripts/recordmcount.c \
  146. $(srctree)/scripts/recordmcount.h
  147. else
  148. sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  149. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  150. "$(if $(CONFIG_64BIT),64,32)" \
  151. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
  152. "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
  153. "$(if $(part-of-module),1,0)" "$(@)";
  154. recordmcount_source := $(srctree)/scripts/recordmcount.pl
  155. endif # BUILD_C_RECORDMCOUNT
  156. cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
  157. $(sub_cmd_record_mcount))
  158. endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
  159. # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
  160. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
  161. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
  162. is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(target-stem).o)$(OBJECT_FILES_NON_STANDARD)n),$(is-kernel-object))
  163. $(obj)/%.o: private objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
  164. ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
  165. cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
  166. endif
  167. define rule_cc_o_c
  168. $(call cmd_and_fixdep,cc_o_c)
  169. $(call cmd,checksrc)
  170. $(call cmd,checkdoc)
  171. $(call cmd,gen_objtooldep)
  172. $(call cmd,gen_symversions_c)
  173. $(call cmd,record_mcount)
  174. $(call cmd,warn_shared_object)
  175. endef
  176. define rule_as_o_S
  177. $(call cmd_and_fixdep,as_o_S)
  178. $(call cmd,gen_objtooldep)
  179. $(call cmd,gen_symversions_S)
  180. $(call cmd,warn_shared_object)
  181. endef
  182. # Built-in and composite module parts
  183. $(obj)/%.o: $(obj)/%.c $(recordmcount_source) FORCE
  184. $(call if_changed_rule,cc_o_c)
  185. $(call cmd,force_checksrc)
  186. # To make this rule robust against "Argument list too long" error,
  187. # ensure to add $(obj)/ prefix by a shell command.
  188. cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
  189. $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
  190. $(obj)/%.mod: FORCE
  191. $(call if_changed,mod)
  192. quiet_cmd_cc_lst_c = MKLST $@
  193. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  194. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  195. System.map $(OBJDUMP) > $@
  196. $(obj)/%.lst: $(obj)/%.c FORCE
  197. $(call if_changed_dep,cc_lst_c)
  198. # Compile Rust sources (.rs)
  199. # ---------------------------------------------------------------------------
  200. rust_allowed_features := arbitrary_self_types,lint_reasons,used_with_arg
  201. # `--out-dir` is required to avoid temporaries being created by `rustc` in the
  202. # current working directory, which may be not accessible in the out-of-tree
  203. # modules case.
  204. rust_common_cmd = \
  205. RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
  206. -Zallow-features=$(rust_allowed_features) \
  207. -Zcrate-attr=no_std \
  208. -Zcrate-attr='feature($(rust_allowed_features))' \
  209. -Zunstable-options --extern kernel \
  210. --crate-type rlib -L $(objtree)/rust/ \
  211. --crate-name $(basename $(notdir $@)) \
  212. --sysroot=/dev/null \
  213. --out-dir $(dir $@) --emit=dep-info=$(depfile)
  214. # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
  215. # will be used. We explicitly request `-Ccodegen-units=1` in any case, and
  216. # the compiler shows a warning if it is not 1. However, if we ever stop
  217. # requesting it explicitly and we start using some other `--emit` that does not
  218. # imply it (and for which codegen is performed), then we would be out of sync,
  219. # i.e. the outputs we would get for the different single targets (e.g. `.ll`)
  220. # would not match each other.
  221. quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  222. cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< $(cmd_objtool)
  223. define rule_rustc_o_rs
  224. $(call cmd_and_fixdep,rustc_o_rs)
  225. $(call cmd,gen_objtooldep)
  226. endef
  227. $(obj)/%.o: $(obj)/%.rs FORCE
  228. +$(call if_changed_rule,rustc_o_rs)
  229. quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  230. cmd_rustc_rsi_rs = \
  231. $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
  232. command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
  233. $(obj)/%.rsi: $(obj)/%.rs FORCE
  234. +$(call if_changed_dep,rustc_rsi_rs)
  235. quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  236. cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
  237. $(obj)/%.s: $(obj)/%.rs FORCE
  238. +$(call if_changed_dep,rustc_s_rs)
  239. quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  240. cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
  241. $(obj)/%.ll: $(obj)/%.rs FORCE
  242. +$(call if_changed_dep,rustc_ll_rs)
  243. # Compile assembler sources (.S)
  244. # ---------------------------------------------------------------------------
  245. # .S file exports must have their C prototypes defined in asm/asm-prototypes.h
  246. # or a file that it includes, in order to get versioned symbols. We build a
  247. # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
  248. # the .S file (with trailing ';'), and run genksyms on that, to extract vers.
  249. #
  250. # This is convoluted. The .S file must first be preprocessed to run guards and
  251. # expand names, then the resulting exports must be constructed into plain
  252. # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
  253. # to make the genksyms input.
  254. #
  255. # These mirror gensymtypes_c and co above, keep them in synch.
  256. cmd_gensymtypes_S = \
  257. { echo "\#include <linux/kernel.h>" ; \
  258. echo "\#include <asm/asm-prototypes.h>" ; \
  259. $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
  260. $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
  261. quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
  262. cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
  263. $(obj)/%.symtypes : $(obj)/%.S FORCE
  264. $(call cmd,cc_symtypes_S)
  265. quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
  266. cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
  267. $(obj)/%.s: $(obj)/%.S FORCE
  268. $(call if_changed_dep,cpp_s_S)
  269. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  270. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
  271. ifdef CONFIG_ASM_MODVERSIONS
  272. # versioning matches the C process described above, with difference that
  273. # we parse asm-prototypes.h C header to get function definitions.
  274. cmd_gen_symversions_S = $(call gen_symversions,S)
  275. endif
  276. $(obj)/%.o: $(obj)/%.S FORCE
  277. $(call if_changed_rule,as_o_S)
  278. targets += $(filter-out $(subdir-builtin), $(real-obj-y))
  279. targets += $(filter-out $(subdir-modorder), $(real-obj-m))
  280. targets += $(lib-y) $(always-y)
  281. # Linker scripts preprocessor (.lds.S -> .lds)
  282. # ---------------------------------------------------------------------------
  283. quiet_cmd_cpp_lds_S = LDS $@
  284. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  285. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  286. $(obj)/%.lds: $(src)/%.lds.S FORCE
  287. $(call if_changed_dep,cpp_lds_S)
  288. # ASN.1 grammar
  289. # ---------------------------------------------------------------------------
  290. quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
  291. cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
  292. $(basename $@).c $(basename $@).h
  293. $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
  294. $(call cmd,asn1_compiler)
  295. # Build the compiled-in targets
  296. # ---------------------------------------------------------------------------
  297. # To build objects in subdirs, we need to descend into the directories
  298. $(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
  299. $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
  300. #
  301. # Rule to compile a set of .o files into one .a file (without symbol table)
  302. #
  303. # To make this rule robust against "Argument list too long" error,
  304. # remove $(obj)/ prefix, and restore it by a shell command.
  305. quiet_cmd_ar_builtin = AR $@
  306. cmd_ar_builtin = rm -f $@; \
  307. $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
  308. $(AR) cDPrST $@
  309. $(obj)/built-in.a: $(real-obj-y) FORCE
  310. $(call if_changed,ar_builtin)
  311. # This is a list of build artifacts from the current Makefile and its
  312. # sub-directories. The timestamp should be updated when any of the member files.
  313. cmd_gen_order = { $(foreach m, $(real-prereqs), \
  314. $(if $(filter %/$(notdir $@), $m), cat $m, echo $m);) :; } \
  315. > $@
  316. $(obj)/modules.order: $(obj-m) FORCE
  317. $(call if_changed,gen_order)
  318. #
  319. # Rule to compile a set of .o files into one .a file (with symbol table)
  320. #
  321. $(obj)/lib.a: $(lib-y) FORCE
  322. $(call if_changed,ar)
  323. quiet_cmd_ld_multi_m = LD [M] $@
  324. cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool)
  325. define rule_ld_multi_m
  326. $(call cmd_and_savecmd,ld_multi_m)
  327. $(call cmd,gen_objtooldep)
  328. endef
  329. $(multi-obj-m): private objtool-enabled := $(delay-objtool)
  330. $(multi-obj-m): private part-of-module := y
  331. $(multi-obj-m): %.o: %.mod FORCE
  332. $(call if_changed_rule,ld_multi_m)
  333. $(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
  334. # Add intermediate targets:
  335. # When building objects with specific suffix patterns, add intermediate
  336. # targets that the final targets are derived from.
  337. intermediate_targets = $(foreach sfx, $(2), \
  338. $(patsubst %$(strip $(1)),%$(sfx), \
  339. $(filter %$(strip $(1)), $(targets))))
  340. # %.asn1.o <- %.asn1.[ch] <- %.asn1
  341. targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h)
  342. # Include additional build rules when necessary
  343. # ---------------------------------------------------------------------------
  344. # $(sort ...) is used here to remove duplicated words and excessive spaces.
  345. hostprogs := $(sort $(hostprogs))
  346. ifneq ($(hostprogs),)
  347. include $(srctree)/scripts/Makefile.host
  348. endif
  349. # $(sort ...) is used here to remove duplicated words and excessive spaces.
  350. userprogs := $(sort $(userprogs))
  351. ifneq ($(userprogs),)
  352. include $(srctree)/scripts/Makefile.userprogs
  353. endif
  354. ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)),)
  355. include $(srctree)/scripts/Makefile.dtbs
  356. endif
  357. # Build
  358. # ---------------------------------------------------------------------------
  359. $(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
  360. $(if $(KBUILD_MODULES), $(targets-for-modules)) \
  361. $(subdir-ym) $(always-y)
  362. @:
  363. # Single targets
  364. # ---------------------------------------------------------------------------
  365. single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
  366. single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
  367. $(single-subdir-goals): $(single-subdirs)
  368. @:
  369. # Descending
  370. # ---------------------------------------------------------------------------
  371. PHONY += $(subdir-ym)
  372. $(subdir-ym):
  373. $(Q)$(MAKE) $(build)=$@ \
  374. need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
  375. need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
  376. $(filter $@/%, $(single-subdir-goals))
  377. # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
  378. # ---------------------------------------------------------------------------
  379. PHONY += FORCE
  380. FORCE:
  381. targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
  382. targets := $(filter-out $(PHONY), $(targets))
  383. # Read all saved command lines and dependencies for the $(targets) we
  384. # may be building above, using $(if_changed{,_dep}). As an
  385. # optimization, we don't need to read them if the target does not
  386. # exist, we will rebuild anyway in that case.
  387. existing-targets := $(wildcard $(sort $(targets)))
  388. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  389. # Create directories for object files if they do not exist
  390. obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
  391. # If targets exist, their directories apparently exist. Skip mkdir.
  392. existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
  393. obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
  394. ifneq ($(obj-dirs),)
  395. $(shell mkdir -p $(obj-dirs))
  396. endif
  397. .PHONY: $(PHONY)