1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds# ========================================================================== 31da177e4SLinus Torvalds# Building 41da177e4SLinus Torvalds# ========================================================================== 51da177e4SLinus Torvalds 613b25489SMasahiro Yamadasrc := $(srcroot)/$(obj) 71da177e4SLinus Torvalds 8cc306abdSMasahiro YamadaPHONY := $(obj)/ 9cc306abdSMasahiro Yamada$(obj)/: 101da177e4SLinus Torvalds 11d72e5edbSSam Ravnborg# Init all relevant variables used in kbuild files so 12d72e5edbSSam Ravnborg# 1) they have correct type 13d72e5edbSSam Ravnborg# 2) they do not inherit any value from the environment 14d72e5edbSSam Ravnborgobj-y := 15d72e5edbSSam Ravnborgobj-m := 16d72e5edbSSam Ravnborglib-y := 17d72e5edbSSam Ravnborglib-m := 185f2fb52fSMasahiro Yamadaalways-y := 195f2fb52fSMasahiro Yamadaalways-m := 20d72e5edbSSam Ravnborgtargets := 21d72e5edbSSam Ravnborgsubdir-y := 22d72e5edbSSam Ravnborgsubdir-m := 23d72e5edbSSam Ravnborgasflags-y := 24d72e5edbSSam Ravnborgccflags-y := 25d72e5edbSSam Ravnborgrustflags-y := 26d72e5edbSSam Ravnborgcppflags-y := 27f77bf014SSam Ravnborgldflags-y := 28f77bf014SSam Ravnborg 292f7ab126SMiguel Ojedasubdir-asflags-y := 30f77bf014SSam Ravnborgsubdir-ccflags-y := 31f77bf014SSam Ravnborg 32d72e5edbSSam Ravnborg# Read auto.conf if it exists, otherwise ignore 33720097d8SSam Ravnborg-include $(objtree)/include/config/auto.conf 34720097d8SSam Ravnborg 35720097d8SSam Ravnborginclude $(srctree)/scripts/Kbuild.include 363156fd05SRobert P. J. Dayinclude $(srctree)/scripts/Makefile.compiler 37214c0eeaSMasahiro Yamadainclude $(kbuild-file) 381da177e4SLinus Torvaldsinclude $(srctree)/scripts/Makefile.lib 393204a7fbSMasahiro Yamada 4057fd251cSMasahiro Yamadaifndef obj 41a2430b25SMasahiro Yamada$(warning kbuild: Makefile.build is included improperly) 423204a7fbSMasahiro Yamadaendif 431da177e4SLinus Torvalds 441da177e4SLinus Torvaldsifeq ($(need-modorder),) 451da177e4SLinus Torvaldsifneq ($(obj-m),) 461da177e4SLinus Torvalds$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.) 471da177e4SLinus Torvalds$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.) 48394053f4SMasahiro Yamadaendif 49c07d8d47SMasahiro Yamadaendif 50c07d8d47SMasahiro Yamada 51c07d8d47SMasahiro Yamada# =========================================================================== 52c07d8d47SMasahiro Yamada 53c07d8d47SMasahiro Yamada# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...) 54c07d8d47SMasahiro Yamadasubdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y))) 551da177e4SLinus Torvaldssubdir-modorder := $(sort $(filter %/modules.order, $(obj-m))) 561da177e4SLinus Torvalds 57454753d9SMasahiro Yamadatargets-for-builtin := $(extra-y) 58aaa385baSMasahiro Yamada 59b2c88554SMasahiro Yamadaifneq ($(strip $(lib-y) $(lib-m) $(lib-)),) 606ba3bcb0SMasahiro Yamadatargets-for-builtin += $(obj)/lib.a 61f3908ab3SMasahiro Yamadaendif 62f3908ab3SMasahiro Yamada 63a4954fd7SMasahiro Yamadaifdef need-builtin 64f3908ab3SMasahiro Yamadatargets-for-builtin += $(obj)/built-in.a 651da177e4SLinus Torvaldsendif 661da177e4SLinus Torvalds 6756d58936SMasahiro Yamadatargets-for-modules := $(foreach x, o mod, \ 68f3908ab3SMasahiro Yamada $(patsubst %.o, %.$x, $(filter %.o, $(obj-m)))) 691da177e4SLinus Torvalds 701da177e4SLinus Torvaldsifdef need-modorder 715e9e95ccSMasahiro Yamadatargets-for-modules += $(obj)/modules.order 729413e764SMasahiro Yamadaendif 73f3908ab3SMasahiro Yamada 74b480fec9SMasahiro Yamadatargets += $(targets-for-builtin) $(targets-for-modules) 75f3908ab3SMasahiro Yamada 761d8001efSMasahiro Yamada# Linus' kernel sanity checking tool 77551559e1STejun Heoifeq ($(KBUILD_CHECKSRC),1) 78f3908ab3SMasahiro Yamada quiet_cmd_checksrc = CHECK $< 799f69a496SMasahiro Yamada cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< 801da177e4SLinus Torvaldselse ifeq ($(KBUILD_CHECKSRC),2) 817d0ea252SMasahiro Yamada quiet_cmd_force_checksrc = CHECK $< 821da177e4SLinus Torvalds cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< 83e5d28910SMasahiro Yamadaendif 847d0ea252SMasahiro Yamada 857d0ea252SMasahiro Yamadaifneq ($(KBUILD_EXTRA_WARN),) 86e5d28910SMasahiro Yamada cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \ 871da177e4SLinus Torvalds $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \ 881da177e4SLinus Torvalds $< 89e27128dbSMasahiro Yamadaendif 90dd203fefSJohannes Berg 91dd203fefSJohannes Berg# Compile C sources (.c) 92dd203fefSJohannes Berg# --------------------------------------------------------------------------- 933a025e1dSMatthew Wilcox 943a025e1dSMatthew Wilcoxquiet_cmd_cc_s_c = CC $(quiet_modtag) $@ 951da177e4SLinus Torvalds cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $< 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds$(obj)/%.s: $(obj)/%.c FORCE 981da177e4SLinus Torvalds $(call if_changed_dep,cc_s_c) 99dc5723b0SSami Tolvanen 1001da177e4SLinus Torvaldsquiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@ 1019a0ebe50SMasahiro Yamadacmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds$(obj)/%.i: $(obj)/%.c FORCE 10423d43848SMasahiro Yamada $(call if_changed_dep,cpp_i_c) 10523d43848SMasahiro Yamada 1061da177e4SLinus Torvaldsgetexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p' 1079a0ebe50SMasahiro Yamada 10823d43848SMasahiro Yamadagendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms \ 1091da177e4SLinus Torvalds $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes)) \ 1109c3681f9SSami Tolvanen $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) 1119c3681f9SSami Tolvanen 1129c3681f9SSami Tolvanengenksyms = $(objtree)/scripts/genksyms/genksyms \ 1139c3681f9SSami Tolvanen $(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes)) \ 1149c3681f9SSami Tolvanen $(if $(KBUILD_PRESERVE), -p) \ 1159c3681f9SSami Tolvanen $(addprefix -r , $(wildcard $(@:.o=.symref))) 116214c0eeaSMasahiro Yamada 11791ca8be3SMasahiro Yamada# These mirror gensymtypes_S and co below, keep them in synch. 11837a8d9f6SSam Ravnborgifdef CONFIG_GENDWARFKSYMS 11991ca8be3SMasahiro Yamadacmd_gensymtypes_c = $(if $(skip_gendwarfksyms),, \ 120f43e31d5SMasahiro Yamada $(call getexportsymbols,\1) | $(gendwarfksyms) $@) 121f43e31d5SMasahiro Yamadaelse 1229c3681f9SSami Tolvanencmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms) 1239c3681f9SSami Tolvanenendif # CONFIG_GENDWARFKSYMS 1249c3681f9SSami Tolvanen 1259c3681f9SSami Tolvanen# LLVM assembly 126f43e31d5SMasahiro Yamada# Generate .ll files from .c 1279c3681f9SSami Tolvanenquiet_cmd_cc_ll_c = CC $(quiet_modtag) $@ 12837a8d9f6SSam Ravnborg cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $< 129433db3e2SVinícius Tinti 130433db3e2SVinícius Tinti$(obj)/%.ll: $(obj)/%.c FORCE 131433db3e2SVinícius Tinti $(call if_changed_dep,cc_ll_c) 132c67a85beSNick Desaulniers 133433db3e2SVinícius Tinti# C (.c) files 1349a0ebe50SMasahiro Yamada# The C file is compiled and updated dependency information is generated. 135433db3e2SVinícius Tinti# (See cmd_cc_o_c + relevant part of rule_cc_o_c) 136433db3e2SVinícius Tinti 1371da177e4SLinus Torvaldsis-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y) 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvaldsifdef CONFIG_MODVERSIONS 1401da177e4SLinus Torvalds# When module versioning is enabled the following steps are executed: 141c25e1c55SMasahiro Yamada# o compile a <file>.o from <file>.c 142c25e1c55SMasahiro Yamada# o if <file>.o doesn't contain a __export_symbol_*, i.e. does 143ee3e46b7SMasahiro Yamada# not export symbols, it's done. 1441da177e4SLinus Torvalds# o otherwise, we calculate symbol versions using the good old 145ee3e46b7SMasahiro Yamada# genksyms on the preprocessed source and dump them into the .cmd file. 146ddb5cdbaSMasahiro Yamada# o modpost will extract versions from that file and create *.c files that will 147ee3e46b7SMasahiro Yamada# be compiled and linked to the kernel and/or modules. 1481da177e4SLinus Torvalds 1497b453719SMasahiro Yamadagen_symversions = \ 1507b453719SMasahiro Yamada if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \ 1517b453719SMasahiro Yamada $(cmd_gensymtypes_$1) >> $(dot-target).cmd; \ 1524efca4edSNicholas Piggin fi 1538017ce50SMasahiro Yamada 154ddb5cdbaSMasahiro Yamadacmd_gen_symversions_c = $(call gen_symversions,c) 15591ca8be3SMasahiro Yamada 156e5d28910SMasahiro Yamadaendif 1578017ce50SMasahiro Yamada 1588017ce50SMasahiro Yamadaifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT 1598017ce50SMasahiro Yamada# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl 16038e89184SSami Tolvanenifdef BUILD_C_RECORDMCOUNT 1611da177e4SLinus Torvaldsifeq ("$(origin RECORDMCOUNT_WARN)", "command line") 1623b15cdc1SSami Tolvanen RECORDMCOUNT_FLAGS = -w 16307d04081SVasily Gorbikendif 16472441cb1SSteven Rostedt# Due to recursion, we must skip empty.o. 16585356f80SSteven Rostedt# The empty.o file is created in the make process in order to determine 16685356f80SSteven Rostedt# the target endianness and word size. It is made before all other C 16785356f80SSteven Rostedt# files, including recordmcount. 168d7b4d6deSSteven Rostedtsub_cmd_record_mcount = \ 169d7b4d6deSSteven Rostedt if [ $(@) != "scripts/mod/empty.o" ]; then \ 170d7b4d6deSSteven Rostedt $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ 171d7b4d6deSSteven Rostedt fi; 17245677454SWu Zhangjinrecordmcount_source := $(srctree)/scripts/recordmcount.c \ 17345677454SWu Zhangjin $(srctree)/scripts/recordmcount.h 17485356f80SSteven Rostedtelse 175d7b4d6deSSteven Rostedtsub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \ 176d6971822SMichal Marek "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \ 177d6971822SMichal Marek "$(if $(CONFIG_64BIT),64,32)" \ 17872441cb1SSteven Rostedt "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ 1794317ee3bSMasahiro Yamada "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ 180e6299d26SWu Zhangjin "$(if $(part-of-module),1,0)" "$(@)"; 181b3acf29aSSteven Rostedtrecordmcount_source := $(srctree)/scripts/recordmcount.pl 1825a4630aaSJoel Stanleyendif # BUILD_C_RECORDMCOUNT 183d503ac53SMasahiro Yamadacmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ 18418c167fdSShaohua Li $(sub_cmd_record_mcount)) 185d6971822SMichal Marekendif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT 186312a3d09SCao jin 1871a49b2fdSJoe Lawrence# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory 1881a49b2fdSJoe Lawrence# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file 1893b15cdc1SSami Tolvanen# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file 1908da3821bSSteven Rostedt 191b9ab5ebbSJosh Poimboeufis-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(target-stem).o)$(OBJECT_FILES_NON_STANDARD)n),$(is-kernel-object)) 192b9ab5ebbSJosh Poimboeuf 193b9ab5ebbSJosh Poimboeufifdef CONFIG_OBJTOOL 194918a6b7fSMasahiro Yamada$(obj)/%.o: private objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y)) 1959c2d1328SMasahiro Yamadaendif 196918a6b7fSMasahiro Yamada 197695ed93bSMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 198d98dba88SMasahiro Yamadacmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi))) 199695ed93bSMasahiro Yamadaendif 200b9ab5ebbSJosh Poimboeuf 201598afa05SMasahiro Yamada# Built-in and composite module parts 202598afa05SMasahiro Yamada$(obj)/%.o: $(obj)/%.c $(recordmcount_source) FORCE 203598afa05SMasahiro Yamada $(call if_changed_rule,cc_o_c) 204598afa05SMasahiro Yamada $(call cmd,force_checksrc) 2051da177e4SLinus Torvalds 2069a0ebe50SMasahiro Yamada# To make this rule robust against "Argument list too long" error, 2071da177e4SLinus Torvalds# ensure to add $(obj)/ prefix by a shell command. 2080c33f125SLuc Van Oostenryckcmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \ 2091da177e4SLinus Torvalds $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@ 210c6031b1dSMasahiro Yamada 211c6031b1dSMasahiro Yamada$(obj)/%.mod: FORCE 2127bf179deSKevin Locke $(call if_changed,mod) 2137bf179deSKevin Locke 2149f69a496SMasahiro Yamadaquiet_cmd_cc_lst_c = MKLST $@ 215fc93a4cdSMasahiro Yamada cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \ 2169f69a496SMasahiro Yamada $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \ 2179f69a496SMasahiro Yamada System.map $(OBJDUMP) > $@ 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds$(obj)/%.lst: $(obj)/%.c FORCE 2201da177e4SLinus Torvalds $(call if_changed_dep,cc_lst_c) 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds# Compile Rust sources (.rs) 2239a0ebe50SMasahiro Yamada# --------------------------------------------------------------------------- 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvaldsrust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op 2262f7ab126SMiguel Ojeda 2272f7ab126SMiguel Ojeda# `--out-dir` is required to avoid temporaries being created by `rustc` in the 2282f7ab126SMiguel Ojeda# current working directory, which may be not accessible in the out-of-tree 229*e1dfaa33SAntonio Hickey# modules case. 2302f7ab126SMiguel Ojedarust_common_cmd = \ 231df01b7cfSMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 232df01b7cfSMiguel Ojeda RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \ 233df01b7cfSMiguel Ojeda -Zallow-features=$(rust_allowed_features) \ 2342f7ab126SMiguel Ojeda -Zcrate-attr=no_std \ 235169484abSAlice Ryhl -Zcrate-attr='feature($(rust_allowed_features))' \ 2362f7ab126SMiguel Ojeda -Zunstable-options --extern pin_init --extern kernel \ 2372f7ab126SMiguel Ojeda --crate-type rlib -L $(objtree)/rust/ \ 2382f7ab126SMiguel Ojeda --crate-name $(basename $(notdir $@)) \ 2392f7ab126SMiguel Ojeda --sysroot=/dev/null \ 240d7659accSMiguel Ojeda --out-dir $(dir $@) --emit=dep-info=$(depfile) 241295d8398SMasahiro Yamada 242295d8398SMasahiro Yamada# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit 24371479eeeSMatthew Maurer# will be used. We explicitly request `-Ccodegen-units=1` in any case, and 244df01b7cfSMiguel Ojeda# the compiler shows a warning if it is not 1. However, if we ever stop 2452f7ab126SMiguel Ojeda# requesting it explicitly and we start using some other `--emit` that does not 2462f7ab126SMiguel Ojeda# imply it (and for which codegen is performed), then we would be out of sync, 2472f7ab126SMiguel Ojeda# i.e. the outputs we would get for the different single targets (e.g. `.ll`) 2482f7ab126SMiguel Ojeda# would not match each other. 2492f7ab126SMiguel Ojeda 2502f7ab126SMiguel Ojedaquiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2512f7ab126SMiguel Ojeda cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< $(cmd_objtool) 2522f7ab126SMiguel Ojeda 2532f7ab126SMiguel Ojedadefine rule_rustc_o_rs 2542f7ab126SMiguel Ojeda $(call cmd_and_fixdep,rustc_o_rs) 255c4d7f546SMiguel Ojeda $(call cmd,gen_objtooldep) 256c4d7f546SMiguel Ojedaendef 257c4d7f546SMiguel Ojeda 258c4d7f546SMiguel Ojeda$(obj)/%.o: $(obj)/%.rs FORCE 259c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_o_rs) 260c4d7f546SMiguel Ojeda 2612f7ab126SMiguel Ojedaquiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2629a0ebe50SMasahiro Yamada cmd_rustc_rsi_rs = \ 263c4d7f546SMiguel Ojeda $(rust_common_cmd) -Zunpretty=expanded $< >$@; \ 2642f7ab126SMiguel Ojeda command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@ 2652f7ab126SMiguel Ojeda 2662f7ab126SMiguel Ojeda$(obj)/%.rsi: $(obj)/%.rs FORCE 267295d8398SMasahiro Yamada +$(call if_changed_dep,rustc_rsi_rs) 2682185242fSMasahiro Yamada 2692f7ab126SMiguel Ojedaquiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2709a0ebe50SMasahiro Yamada cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $< 271ecab4115SMiguel Ojeda 2722f7ab126SMiguel Ojeda$(obj)/%.s: $(obj)/%.rs FORCE 2732f7ab126SMiguel Ojeda +$(call if_changed_dep,rustc_s_rs) 2742185242fSMasahiro Yamada 2752f7ab126SMiguel Ojedaquiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2769a0ebe50SMasahiro Yamada cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $< 277ecab4115SMiguel Ojeda 2782f7ab126SMiguel Ojeda$(obj)/%.ll: $(obj)/%.rs FORCE 2792f7ab126SMiguel Ojeda +$(call if_changed_dep,rustc_ll_rs) 2802185242fSMasahiro Yamada 2812f7ab126SMiguel Ojedaquiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@ 2829a0ebe50SMasahiro Yamada cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@ 283ecab4115SMiguel Ojeda 2842f7ab126SMiguel Ojeda$(obj)/%.rs: $(obj)/%.rs.S FORCE 285169484abSAlice Ryhl +$(call if_changed_dep,rustc_rs_rs_S) 286169484abSAlice Ryhl 287169484abSAlice Ryhl# Compile assembler sources (.S) 288169484abSAlice Ryhl# --------------------------------------------------------------------------- 289169484abSAlice Ryhl 290169484abSAlice Ryhl# .S file exports must have their C prototypes defined in asm/asm-prototypes.h 2911da177e4SLinus Torvalds# or a file that it includes, in order to get versioned symbols. We build a 2921da177e4SLinus Torvalds# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from 2931da177e4SLinus Torvalds# the .S file (with trailing ';'), and run genksyms on that, to extract vers. 2944efca4edSNicholas Piggin# 2954efca4edSNicholas Piggin# This is convoluted. The .S file must first be preprocessed to run guards and 2964efca4edSNicholas Piggin# expand names, then the resulting exports must be constructed into plain 2974efca4edSNicholas Piggin# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed 2984efca4edSNicholas Piggin# to make the genksyms input or compiled into an object for gendwarfksyms. 2994efca4edSNicholas Piggin# 3004efca4edSNicholas Piggin# These mirror gensymtypes_c and co above, keep them in synch. 3014efca4edSNicholas Piggingetasmexports = \ 3029c3681f9SSami Tolvanen { echo "\#include <linux/kernel.h>" ; \ 3034efca4edSNicholas Piggin echo "\#include <linux/string.h>" ; \ 3044efca4edSNicholas Piggin echo "\#include <asm/asm-prototypes.h>" ; \ 3059c3681f9SSami Tolvanen $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; } 306b79c6aa6SMasahiro Yamada 3079c3681f9SSami Tolvanenifdef CONFIG_GENDWARFKSYMS 3084efca4edSNicholas Piggincmd_gensymtypes_S = \ 3099c3681f9SSami Tolvanen $(getasmexports) | \ 3109c3681f9SSami Tolvanen $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -; \ 3119c3681f9SSami Tolvanen $(call getexportsymbols,\1) | \ 3129c3681f9SSami Tolvanen $(gendwarfksyms) $(@:.o=.gendwarfksyms.o) 3139c3681f9SSami Tolvanenelse 3149c3681f9SSami Tolvanencmd_gensymtypes_S = \ 3159c3681f9SSami Tolvanen $(getasmexports) | \ 3169c3681f9SSami Tolvanen $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) 3179c3681f9SSami Tolvanenendif # CONFIG_GENDWARFKSYMS 3189c3681f9SSami Tolvanen 3199c3681f9SSami Tolvanenquiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@ 320f43e31d5SMasahiro Yamadacmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $< 3219c3681f9SSami Tolvanen 3224efca4edSNicholas Piggin$(obj)/%.s: $(obj)/%.S FORCE 323e0f41e52SMasahiro Yamada $(call if_changed_dep,cpp_s_S) 324e0f41e52SMasahiro Yamada 3251da177e4SLinus Torvaldsifdef CONFIG_ASM_MODVERSIONS 3269a0ebe50SMasahiro Yamada 327e0f41e52SMasahiro Yamada# versioning matches the C process described above, with difference that 3281da177e4SLinus Torvalds# we parse asm-prototypes.h C header to get function definitions. 3292ff2b7ecSMasahiro Yamada 3304efca4edSNicholas Piggincmd_gen_symversions_S = $(call gen_symversions,S) 3314efca4edSNicholas Piggin 3324efca4edSNicholas Pigginendif 3334efca4edSNicholas Piggin 3347cfa2fcbSMasahiro Yamada$(obj)/%.o: $(obj)/%.S FORCE 3357cfa2fcbSMasahiro Yamada $(call if_changed_rule,as_o_S) 3364efca4edSNicholas Piggin 3374efca4edSNicholas Piggintargets += $(filter-out $(subdir-builtin), $(real-obj-y)) 3389a0ebe50SMasahiro Yamadatargets += $(filter-out $(subdir-modorder), $(real-obj-m)) 339b9ab5ebbSJosh Poimboeuftargets += $(lib-y) $(always-y) 3401da177e4SLinus Torvalds 341b2c88554SMasahiro Yamada# Linker scripts preprocessor (.lds.S -> .lds) 342b2c88554SMasahiro Yamada# --------------------------------------------------------------------------- 343e7e29413SMasahiro Yamadaquiet_cmd_cpp_lds_S = LDS $@ 3441da177e4SLinus Torvalds cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \ 3451da177e4SLinus Torvalds -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $< 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds$(obj)/%.lds: $(src)/%.lds.S FORCE 3485cb0512cSLinus Torvalds $(call if_changed_dep,cpp_lds_S) 34942f29a25STim Abbott 3501da177e4SLinus Torvalds# ASN.1 grammar 351767e581dSSam Ravnborg# --------------------------------------------------------------------------- 3521da177e4SLinus Torvaldsquiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch] 3531da177e4SLinus Torvalds cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \ 3544520c6a4SDavid Howells $(basename $@).c $(basename $@).h 3554520c6a4SDavid Howells 35649d5089dSMasahiro Yamada$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler 3574520c6a4SDavid Howells $(call cmd,asn1_compiler) 35849d5089dSMasahiro Yamada 3594520c6a4SDavid Howells# Build the compiled-in targets 3604fa8bc94SMasahiro Yamada# --------------------------------------------------------------------------- 3614520c6a4SDavid Howells 3624520c6a4SDavid Howells# To build objects in subdirs, we need to descend into the directories 3631da177e4SLinus Torvalds$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ; 3641da177e4SLinus Torvalds$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds# 367aaa385baSMasahiro Yamada# Rule to compile a set of .o files into one .a file (without symbol table) 368454753d9SMasahiro Yamada# 3691da177e4SLinus Torvalds# To make this rule robust against "Argument list too long" error, 3701da177e4SLinus Torvalds# remove $(obj)/ prefix, and restore it by a shell command. 371dee94953SMasahiro Yamada 3721da177e4SLinus Torvaldsquiet_cmd_ar_builtin = AR $@ 373cd968b97SMasahiro Yamada cmd_ar_builtin = rm -f $@; \ 374cd968b97SMasahiro Yamada $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \ 375a5967db9SStephen Rothwell $(AR) cDPrST $@ 3765e18f029SMasahiro Yamada 377cd968b97SMasahiro Yamada$(obj)/built-in.a: $(real-obj-y) FORCE 3785439d4d4SMasahiro Yamada $(call if_changed,ar_builtin) 3795439d4d4SMasahiro Yamada 3801da177e4SLinus Torvalds# This is a list of build artifacts from the current Makefile and its 381e578edc7SMasahiro Yamada# sub-directories. The timestamp should be updated when any of the member files. 3827375cbcfSMasahiro Yamada 3831da177e4SLinus Torvaldscmd_gen_order = { $(foreach m, $(real-prereqs), \ 384e7e29413SMasahiro Yamada $(if $(filter %/$(notdir $@), $m), cat $m, echo $m);) :; } \ 385e7e29413SMasahiro Yamada > $@ 386b2c88554SMasahiro Yamada 38724507871SMasahiro Yamada$(obj)/modules.order: $(obj-m) FORCE 38824507871SMasahiro Yamada $(call if_changed,gen_order) 389d724b578SMasahiro Yamada 390551559e1STejun Heo# 391b2c88554SMasahiro Yamada# Rule to compile a set of .o files into one .a file (with symbol table) 39224507871SMasahiro Yamada# 39324507871SMasahiro Yamada 394551559e1STejun Heo$(obj)/lib.a: $(lib-y) FORCE 395dee94953SMasahiro Yamada $(call if_changed,ar) 3961da177e4SLinus Torvalds 39738e89184SSami Tolvanenquiet_cmd_ld_multi_m = LD [M] $@ 398e578edc7SMasahiro Yamada cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool) 3997375cbcfSMasahiro Yamada 4001da177e4SLinus Torvaldsdefine rule_ld_multi_m 401f6b66ca4SMasahiro Yamada $(call cmd_and_savecmd,ld_multi_m) 4021b1595cdSMasahiro Yamada $(call cmd,gen_objtooldep) 403f6b66ca4SMasahiro Yamadaendef 404f6b66ca4SMasahiro Yamada 405f6b66ca4SMasahiro Yamada$(multi-obj-m): private objtool-enabled := $(delay-objtool) 406f6b66ca4SMasahiro Yamada$(multi-obj-m): private part-of-module := y 407f6b66ca4SMasahiro Yamada$(multi-obj-m): %.o: %.mod FORCE 4081da177e4SLinus Torvalds $(call if_changed_rule,ld_multi_m) 409d98dba88SMasahiro Yamada$(call multi_depend, $(multi-obj-m), .o, -objs -y -m) 410d98dba88SMasahiro Yamada 411feb7d79fSMasahiro Yamada# Add intermediate targets: 412f6b66ca4SMasahiro Yamada# When building objects with specific suffix patterns, add intermediate 413a6601e01SMasahiro Yamada# targets that the final targets are derived from. 4141da177e4SLinus Torvaldsintermediate_targets = $(foreach sfx, $(2), \ 415b23d1a24SMasahiro Yamada $(patsubst %$(strip $(1)),%$(sfx), \ 416b23d1a24SMasahiro Yamada $(filter %$(strip $(1)), $(targets)))) 417b23d1a24SMasahiro Yamada# %.asn1.o <- %.asn1.[ch] <- %.asn1 418b23d1a24SMasahiro Yamadatargets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) 419b23d1a24SMasahiro Yamada 420b23d1a24SMasahiro Yamada# Include additional build rules when necessary 42154a702f7SMasahiro Yamada# --------------------------------------------------------------------------- 422e7e29413SMasahiro Yamada 423fc41a0a7SMasahiro Yamada# $(sort ...) is used here to remove duplicated words and excessive spaces. 424fc41a0a7SMasahiro Yamadahostprogs := $(sort $(hostprogs)) 425fc41a0a7SMasahiro Yamadaifneq ($(hostprogs),) 426fc41a0a7SMasahiro Yamadainclude $(srctree)/scripts/Makefile.host 427fc41a0a7SMasahiro Yamadaendif 428fc41a0a7SMasahiro Yamada 429fc41a0a7SMasahiro Yamada# $(sort ...) is used here to remove duplicated words and excessive spaces. 430fc41a0a7SMasahiro Yamadauserprogs := $(sort $(userprogs)) 431fc41a0a7SMasahiro Yamadaifneq ($(userprogs),) 432fc41a0a7SMasahiro Yamadainclude $(srctree)/scripts/Makefile.userprogs 433fc41a0a7SMasahiro Yamadaendif 434fc41a0a7SMasahiro Yamada 435fc41a0a7SMasahiro Yamadaifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)),) 436fc41a0a7SMasahiro Yamadainclude $(srctree)/scripts/Makefile.dtbs 437fc41a0a7SMasahiro Yamadaendif 438b23d1a24SMasahiro Yamada 439e7e29413SMasahiro Yamada# Build 440e7e29413SMasahiro Yamada# --------------------------------------------------------------------------- 441e7e29413SMasahiro Yamada 442e7e29413SMasahiro Yamada$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \ 443394053f4SMasahiro Yamada $(if $(KBUILD_MODULES), $(targets-for-modules)) \ 444394053f4SMasahiro Yamada $(subdir-ym) $(always-y) 445394053f4SMasahiro Yamada @: 446cc306abdSMasahiro Yamada 447f3908ab3SMasahiro Yamada# Single targets 4485f2fb52fSMasahiro Yamada# --------------------------------------------------------------------------- 449394053f4SMasahiro Yamada 450394053f4SMasahiro Yamadasingle-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) 451cc306abdSMasahiro Yamadasingle-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) 452cc306abdSMasahiro Yamada 453cc306abdSMasahiro Yamada$(single-subdir-goals): $(single-subdirs) 454cc306abdSMasahiro Yamada @: 455cc306abdSMasahiro Yamada 456cc306abdSMasahiro Yamada# Descending 457cc306abdSMasahiro Yamada# --------------------------------------------------------------------------- 458cc306abdSMasahiro Yamada 459394053f4SMasahiro YamadaPHONY += $(subdir-ym) 4601da177e4SLinus Torvalds$(subdir-ym): 4611da177e4SLinus Torvalds $(Q)$(MAKE) $(build)=$@ \ 4621da177e4SLinus Torvalds need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ 4634f193362SPaul Smith need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ 4641da177e4SLinus Torvalds $(filter $@/%, $(single-subdir-goals)) 465d9f78edfSMasahiro Yamada 466aaa385baSMasahiro Yamada# Add FORCE to the prerequisites of a target to force it to be always rebuilt. 467cc306abdSMasahiro Yamada# --------------------------------------------------------------------------- 468cc306abdSMasahiro Yamada 4691da177e4SLinus TorvaldsPHONY += FORCE 4708fb4ac1cSMasahiro Yamada 4711da177e4SLinus TorvaldsFORCE: 4721da177e4SLinus Torvalds 4734f193362SPaul Smithtargets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS)) 4741da177e4SLinus Torvaldstargets := $(filter-out $(PHONY), $(targets)) 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds# Read all saved command lines and dependencies for the $(targets) we 477cc306abdSMasahiro Yamada# may be building above, using $(if_changed{,_dep}). As an 478cc306abdSMasahiro Yamada# optimization, we don't need to read them if the target does not 479cc306abdSMasahiro Yamada# exist, we will rebuild anyway in that case. 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvaldsexisting-targets := $(wildcard $(sort $(targets))) 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 4841da177e4SLinus Torvalds 485392885eeSMasahiro Yamada# Create directories for object files if they do not exist 4861da177e4SLinus Torvaldsobj-dirs := $(sort $(patsubst %/,%, $(dir $(targets)))) 487392885eeSMasahiro Yamada# If targets exist, their directories apparently exist. Skip mkdir. 4884f193362SPaul Smithexisting-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets)))) 4898a78756eSMasahiro Yamadaobj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs))) 490cc8a51caSMasahiro Yamadaifneq ($(obj-dirs),) 491392885eeSMasahiro Yamada$(shell mkdir -p $(obj-dirs)) 492392885eeSMasahiro Yamadaendif 493392885eeSMasahiro Yamada 494c4da7ed0SMasahiro Yamada.PHONY: $(PHONY) 4958a78756eSMasahiro Yamada