1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds# ========================================================================== 31da177e4SLinus Torvalds# Building 41da177e4SLinus Torvalds# ========================================================================== 51da177e4SLinus Torvalds 61da177e4SLinus Torvaldssrc := $(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 RavnborgEXTRA_AFLAGS := 24d72e5edbSSam RavnborgEXTRA_CFLAGS := 25d72e5edbSSam RavnborgEXTRA_CPPFLAGS := 26d72e5edbSSam RavnborgEXTRA_LDFLAGS := 27f77bf014SSam Ravnborgasflags-y := 28f77bf014SSam Ravnborgccflags-y := 292f7ab126SMiguel Ojedarustflags-y := 30f77bf014SSam Ravnborgcppflags-y := 31f77bf014SSam Ravnborgldflags-y := 32d72e5edbSSam Ravnborg 33720097d8SSam Ravnborgsubdir-asflags-y := 34720097d8SSam Ravnborgsubdir-ccflags-y := 35720097d8SSam Ravnborg 363156fd05SRobert P. J. Day# Read auto.conf if it exists, otherwise ignore 37c955ccafSRoman Zippel-include include/config/auto.conf 381da177e4SLinus Torvalds 393204a7fbSMasahiro Yamadainclude $(srctree)/scripts/Kbuild.include 4057fd251cSMasahiro Yamadainclude $(srctree)/scripts/Makefile.compiler 41a2430b25SMasahiro Yamadainclude $(kbuild-file) 423204a7fbSMasahiro Yamadainclude $(srctree)/scripts/Makefile.lib 431da177e4SLinus Torvalds 4485569d19SMasahiro Yamada# Do not include hostprogs rules unless needed. 4585569d19SMasahiro Yamada# $(sort ...) is used here to remove duplicated words and excessive spaces. 4685569d19SMasahiro Yamadahostprogs := $(sort $(hostprogs)) 4742640b13SMasahiro Yamadaifneq ($(hostprogs),) 483204a7fbSMasahiro Yamadainclude $(srctree)/scripts/Makefile.host 491da177e4SLinus Torvaldsendif 501da177e4SLinus Torvalds 517f3a59dbSMasahiro Yamada# Do not include userprogs rules unless needed. 5285569d19SMasahiro Yamada# $(sort ...) is used here to remove duplicated words and excessive spaces. 537f3a59dbSMasahiro Yamadauserprogs := $(sort $(userprogs)) 547f3a59dbSMasahiro Yamadaifneq ($(userprogs),) 553204a7fbSMasahiro Yamadainclude $(srctree)/scripts/Makefile.userprogs 567f3a59dbSMasahiro Yamadaendif 577f3a59dbSMasahiro Yamada 581da177e4SLinus Torvaldsifndef obj 591da177e4SLinus Torvalds$(warning kbuild: Makefile.build is included improperly) 601da177e4SLinus Torvaldsendif 611da177e4SLinus Torvalds 62394053f4SMasahiro Yamadaifeq ($(need-modorder),) 63c07d8d47SMasahiro Yamadaifneq ($(obj-m),) 64c07d8d47SMasahiro Yamada$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.) 65c07d8d47SMasahiro Yamada$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.) 66c07d8d47SMasahiro Yamadaendif 67c07d8d47SMasahiro Yamadaendif 68c07d8d47SMasahiro Yamada 691da177e4SLinus Torvalds# =========================================================================== 701da177e4SLinus Torvalds 71454753d9SMasahiro Yamada# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...) 72aaa385baSMasahiro Yamadasubdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y))) 73b2c88554SMasahiro Yamadasubdir-modorder := $(sort $(filter %/modules.order, $(obj-m))) 746ba3bcb0SMasahiro Yamada 75f3908ab3SMasahiro Yamadatargets-for-builtin := $(extra-y) 76f3908ab3SMasahiro Yamada 77a4954fd7SMasahiro Yamadaifneq ($(strip $(lib-y) $(lib-m) $(lib-)),) 78f3908ab3SMasahiro Yamadatargets-for-builtin += $(obj)/lib.a 791da177e4SLinus Torvaldsendif 801da177e4SLinus Torvalds 8156d58936SMasahiro Yamadaifdef need-builtin 82f3908ab3SMasahiro Yamadatargets-for-builtin += $(obj)/built-in.a 831da177e4SLinus Torvaldsendif 841da177e4SLinus Torvalds 855e9e95ccSMasahiro Yamadatargets-for-modules := $(foreach x, o mod, \ 869413e764SMasahiro Yamada $(patsubst %.o, %.$x, $(filter %.o, $(obj-m)))) 87f3908ab3SMasahiro Yamada 88b480fec9SMasahiro Yamadaifdef need-modorder 89f3908ab3SMasahiro Yamadatargets-for-modules += $(obj)/modules.order 901d8001efSMasahiro Yamadaendif 91551559e1STejun Heo 92f3908ab3SMasahiro Yamadatargets += $(targets-for-builtin) $(targets-for-modules) 939f69a496SMasahiro Yamada 941da177e4SLinus Torvalds# Linus' kernel sanity checking tool 957d0ea252SMasahiro Yamadaifeq ($(KBUILD_CHECKSRC),1) 961da177e4SLinus Torvalds quiet_cmd_checksrc = CHECK $< 97e5d28910SMasahiro Yamada cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< 987d0ea252SMasahiro Yamadaelse ifeq ($(KBUILD_CHECKSRC),2) 997d0ea252SMasahiro Yamada quiet_cmd_force_checksrc = CHECK $< 100e5d28910SMasahiro Yamada cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< 1011da177e4SLinus Torvaldsendif 1021da177e4SLinus Torvalds 103e27128dbSMasahiro Yamadaifneq ($(KBUILD_EXTRA_WARN),) 104dd203fefSJohannes Berg cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \ 105dd203fefSJohannes Berg $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \ 106dd203fefSJohannes Berg $< 1073a025e1dSMatthew Wilcoxendif 1083a025e1dSMatthew Wilcox 1091da177e4SLinus Torvalds# Compile C sources (.c) 1101da177e4SLinus Torvalds# --------------------------------------------------------------------------- 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvaldsquiet_cmd_cc_s_c = CC $(quiet_modtag) $@ 113dc5723b0SSami Tolvanen cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $< 1141da177e4SLinus Torvalds 115767e581dSSam Ravnborg$(obj)/%.s: $(src)/%.c FORCE 1161da177e4SLinus Torvalds $(call if_changed_dep,cc_s_c) 1171da177e4SLinus Torvalds 11823d43848SMasahiro Yamadaquiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@ 11923d43848SMasahiro Yamadacmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< 1201da177e4SLinus Torvalds 121767e581dSSam Ravnborg$(obj)/%.i: $(src)/%.c FORCE 12223d43848SMasahiro Yamada $(call if_changed_dep,cpp_i_c) 1231da177e4SLinus Torvalds 124f43e31d5SMasahiro Yamadagenksyms = scripts/genksyms/genksyms \ 125f43e31d5SMasahiro Yamada $(if $(1), -T $(2)) \ 12637a8d9f6SSam Ravnborg $(if $(KBUILD_PRESERVE), -p) \ 127f43e31d5SMasahiro Yamada -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null) 128f43e31d5SMasahiro Yamada 129f43e31d5SMasahiro Yamada# These mirror gensymtypes_S and co below, keep them in synch. 130f43e31d5SMasahiro Yamadacmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms) 13137a8d9f6SSam Ravnborg 13215fde675SAndreas Gruenbacherquiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@ 133dc6dc3e7SMasahiro Yamada cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null 13415fde675SAndreas Gruenbacher 135767e581dSSam Ravnborg$(obj)/%.symtypes : $(src)/%.c FORCE 13664e6c1e1SAndreas Gruenbacher $(call cmd,cc_symtypes_c) 13715fde675SAndreas Gruenbacher 138433db3e2SVinícius Tinti# LLVM assembly 139433db3e2SVinícius Tinti# Generate .ll files from .c 140433db3e2SVinícius Tintiquiet_cmd_cc_ll_c = CC $(quiet_modtag) $@ 141c67a85beSNick Desaulniers cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $< 142433db3e2SVinícius Tinti 143433db3e2SVinícius Tinti$(obj)/%.ll: $(src)/%.c FORCE 144433db3e2SVinícius Tinti $(call if_changed_dep,cc_ll_c) 145433db3e2SVinícius Tinti 1461da177e4SLinus Torvalds# C (.c) files 1471da177e4SLinus Torvalds# The C file is compiled and updated dependency information is generated. 1481da177e4SLinus Torvalds# (See cmd_cc_o_c + relevant part of rule_cc_o_c) 1491da177e4SLinus Torvalds 150c25e1c55SMasahiro Yamadais-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y) 151c25e1c55SMasahiro Yamada 152c25e1c55SMasahiro Yamada# When a module consists of a single object, there is no reason to keep LLVM IR. 153c25e1c55SMasahiro Yamada# Make $(LD) covert LLVM IR to ELF here. 154c25e1c55SMasahiro Yamadaifdef CONFIG_LTO_CLANG 155c25e1c55SMasahiro Yamadacmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@) 156c25e1c55SMasahiro Yamadaendif 157c25e1c55SMasahiro Yamada 1581da177e4SLinus Torvaldsquiet_cmd_cc_o_c = CC $(quiet_modtag) $@ 159c25e1c55SMasahiro Yamada cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \ 160c25e1c55SMasahiro Yamada $(cmd_ld_single_m) \ 161c25e1c55SMasahiro Yamada $(cmd_objtool) 1621da177e4SLinus Torvalds 163ee3e46b7SMasahiro Yamadaifdef CONFIG_MODVERSIONS 1641da177e4SLinus Torvalds# When module versioning is enabled the following steps are executed: 165ee3e46b7SMasahiro Yamada# o compile a <file>.o from <file>.c 166ddb5cdbaSMasahiro Yamada# o if <file>.o doesn't contain a __export_symbol_*, i.e. does 167ee3e46b7SMasahiro Yamada# not export symbols, it's done. 1681da177e4SLinus Torvalds# o otherwise, we calculate symbol versions using the good old 1697b453719SMasahiro Yamada# genksyms on the preprocessed source and dump them into the .cmd file. 1707b453719SMasahiro Yamada# o modpost will extract versions from that file and create *.c files that will 1717b453719SMasahiro Yamada# be compiled and linked to the kernel and/or modules. 1724efca4edSNicholas Piggin 1738017ce50SMasahiro Yamadagen_symversions = \ 174ddb5cdbaSMasahiro Yamada if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \ 1758017ce50SMasahiro Yamada $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \ 17678e9e56aSMasahiro Yamada >> $(dot-target).cmd; \ 177e5d28910SMasahiro Yamada fi 1788017ce50SMasahiro Yamada 1798017ce50SMasahiro Yamadacmd_gen_symversions_c = $(call gen_symversions,c) 1808017ce50SMasahiro Yamada 18138e89184SSami Tolvanenendif 1821da177e4SLinus Torvalds 1833b15cdc1SSami Tolvanenifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT 18407d04081SVasily Gorbik# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl 18572441cb1SSteven Rostedtifdef BUILD_C_RECORDMCOUNT 18685356f80SSteven Rostedtifeq ("$(origin RECORDMCOUNT_WARN)", "command line") 18785356f80SSteven Rostedt RECORDMCOUNT_FLAGS = -w 18885356f80SSteven Rostedtendif 189d7b4d6deSSteven Rostedt# Due to recursion, we must skip empty.o. 190d7b4d6deSSteven Rostedt# The empty.o file is created in the make process in order to determine 191d7b4d6deSSteven Rostedt# the target endianness and word size. It is made before all other C 192d7b4d6deSSteven Rostedt# files, including recordmcount. 19345677454SWu Zhangjinsub_cmd_record_mcount = \ 19445677454SWu Zhangjin if [ $(@) != "scripts/mod/empty.o" ]; then \ 19585356f80SSteven Rostedt $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ 196d7b4d6deSSteven Rostedt fi; 197d6971822SMichal Marekrecordmcount_source := $(srctree)/scripts/recordmcount.c \ 198d6971822SMichal Marek $(srctree)/scripts/recordmcount.h 19972441cb1SSteven Rostedtelse 2004317ee3bSMasahiro Yamadasub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \ 201e6299d26SWu Zhangjin "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \ 202b3acf29aSSteven Rostedt "$(if $(CONFIG_64BIT),64,32)" \ 2035a4630aaSJoel Stanley "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ 204d503ac53SMasahiro Yamada "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ 20518c167fdSShaohua Li "$(if $(part-of-module),1,0)" "$(@)"; 206d6971822SMichal Marekrecordmcount_source := $(srctree)/scripts/recordmcount.pl 207312a3d09SCao jinendif # BUILD_C_RECORDMCOUNT 2081a49b2fdSJoe Lawrencecmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ 2091a49b2fdSJoe Lawrence $(sub_cmd_record_mcount)) 2103b15cdc1SSami Tolvanenendif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT 2118da3821bSSteven Rostedt 212b9ab5ebbSJosh Poimboeuf# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory 213b9ab5ebbSJosh Poimboeuf# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file 214b9ab5ebbSJosh Poimboeuf# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file 215918a6b7fSMasahiro Yamada 216c25e1c55SMasahiro Yamadais-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y) 217918a6b7fSMasahiro Yamada 218c25e1c55SMasahiro Yamada$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y)) 219b9ab5ebbSJosh Poimboeuf 220598afa05SMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 221598afa05SMasahiro Yamadacmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi))) 222598afa05SMasahiro Yamadaendif 223598afa05SMasahiro Yamada 2241da177e4SLinus Torvaldsdefine rule_cc_o_c 2253a2429e1SMasahiro Yamada $(call cmd_and_fixdep,cc_o_c) 2260c33f125SLuc Van Oostenryck $(call cmd,checksrc) 2273a2429e1SMasahiro Yamada $(call cmd,checkdoc) 228ef62588cSMasahiro Yamada $(call cmd,gen_objtooldep) 2298017ce50SMasahiro Yamada $(call cmd,gen_symversions_c) 2303a2429e1SMasahiro Yamada $(call cmd,record_mcount) 231598afa05SMasahiro Yamada $(call cmd,warn_shared_object) 2321da177e4SLinus Torvaldsendef 2331da177e4SLinus Torvalds 234b9ab5ebbSJosh Poimboeufdefine rule_as_o_S 2353a2429e1SMasahiro Yamada $(call cmd_and_fixdep,as_o_S) 236ef62588cSMasahiro Yamada $(call cmd,gen_objtooldep) 2377cfa2fcbSMasahiro Yamada $(call cmd,gen_symversions_S) 238598afa05SMasahiro Yamada $(call cmd,warn_shared_object) 239b9ab5ebbSJosh Poimboeufendef 240b9ab5ebbSJosh Poimboeuf 2411da177e4SLinus Torvalds# Built-in and composite module parts 242ef62588cSMasahiro Yamada$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE 2431da177e4SLinus Torvalds $(call if_changed_rule,cc_o_c) 2440c33f125SLuc Van Oostenryck $(call cmd,force_checksrc) 2451da177e4SLinus Torvalds 246c6031b1dSMasahiro Yamada# To make this rule robust against "Argument list too long" error, 247c6031b1dSMasahiro Yamada# ensure to add $(obj)/ prefix by a shell command. 2487bf179deSKevin Lockecmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \ 2497bf179deSKevin Locke $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@ 2509f69a496SMasahiro Yamada 251fc93a4cdSMasahiro Yamada$(obj)/%.mod: FORCE 2529f69a496SMasahiro Yamada $(call if_changed,mod) 2539f69a496SMasahiro Yamada 2541da177e4SLinus Torvaldsquiet_cmd_cc_lst_c = MKLST $@ 2551da177e4SLinus Torvalds cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \ 2561da177e4SLinus Torvalds $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \ 2571da177e4SLinus Torvalds System.map $(OBJDUMP) > $@ 2581da177e4SLinus Torvalds 259767e581dSSam Ravnborg$(obj)/%.lst: $(src)/%.c FORCE 2601da177e4SLinus Torvalds $(call if_changed_dep,cc_lst_c) 2611da177e4SLinus Torvalds 2622f7ab126SMiguel Ojeda# Compile Rust sources (.rs) 2632f7ab126SMiguel Ojeda# --------------------------------------------------------------------------- 2642f7ab126SMiguel Ojeda 2653ed03f4dSMiguel Ojedarust_allowed_features := new_uninit 2662f7ab126SMiguel Ojeda 267df01b7cfSMiguel Ojeda# `--out-dir` is required to avoid temporaries being created by `rustc` in the 268df01b7cfSMiguel Ojeda# current working directory, which may be not accessible in the out-of-tree 269df01b7cfSMiguel Ojeda# modules case. 2702f7ab126SMiguel Ojedarust_common_cmd = \ 2712f7ab126SMiguel Ojeda RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \ 2722f7ab126SMiguel Ojeda -Zallow-features=$(rust_allowed_features) \ 2732f7ab126SMiguel Ojeda -Zcrate-attr=no_std \ 2742f7ab126SMiguel Ojeda -Zcrate-attr='feature($(rust_allowed_features))' \ 275*4805d764SMiguel Ojeda -Zunstable-options --extern force:alloc --extern kernel \ 276295d8398SMasahiro Yamada --crate-type rlib -L $(objtree)/rust/ \ 277295d8398SMasahiro Yamada --crate-name $(basename $(notdir $@)) \ 278df01b7cfSMiguel Ojeda --out-dir $(dir $@) --emit=dep-info=$(depfile) 2792f7ab126SMiguel Ojeda 2802f7ab126SMiguel Ojeda# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit 2812f7ab126SMiguel Ojeda# will be used. We explicitly request `-Ccodegen-units=1` in any case, and 2822f7ab126SMiguel Ojeda# the compiler shows a warning if it is not 1. However, if we ever stop 2832f7ab126SMiguel Ojeda# requesting it explicitly and we start using some other `--emit` that does not 2842f7ab126SMiguel Ojeda# imply it (and for which codegen is performed), then we would be out of sync, 2852f7ab126SMiguel Ojeda# i.e. the outputs we would get for the different single targets (e.g. `.ll`) 2862f7ab126SMiguel Ojeda# would not match each other. 2872f7ab126SMiguel Ojeda 2882f7ab126SMiguel Ojedaquiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2892185242fSMasahiro Yamada cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< 2902f7ab126SMiguel Ojeda 2912f7ab126SMiguel Ojeda$(obj)/%.o: $(src)/%.rs FORCE 2922f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_o_rs) 2932f7ab126SMiguel Ojeda 2942f7ab126SMiguel Ojedaquiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 2952f7ab126SMiguel Ojeda cmd_rustc_rsi_rs = \ 296295d8398SMasahiro Yamada $(rust_common_cmd) -Zunpretty=expanded $< >$@; \ 2972185242fSMasahiro Yamada command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@ 2982f7ab126SMiguel Ojeda 2992f7ab126SMiguel Ojeda$(obj)/%.rsi: $(src)/%.rs FORCE 3002f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_rsi_rs) 3012f7ab126SMiguel Ojeda 3022f7ab126SMiguel Ojedaquiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 3032185242fSMasahiro Yamada cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $< 3042f7ab126SMiguel Ojeda 3052f7ab126SMiguel Ojeda$(obj)/%.s: $(src)/%.rs FORCE 3062f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_s_rs) 3072f7ab126SMiguel Ojeda 3082f7ab126SMiguel Ojedaquiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 3092185242fSMasahiro Yamada cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $< 3102f7ab126SMiguel Ojeda 3112f7ab126SMiguel Ojeda$(obj)/%.ll: $(src)/%.rs FORCE 3122f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_ll_rs) 3132f7ab126SMiguel Ojeda 3141da177e4SLinus Torvalds# Compile assembler sources (.S) 3151da177e4SLinus Torvalds# --------------------------------------------------------------------------- 3161da177e4SLinus Torvalds 3174efca4edSNicholas Piggin# .S file exports must have their C prototypes defined in asm/asm-prototypes.h 3184efca4edSNicholas Piggin# or a file that it includes, in order to get versioned symbols. We build a 3194efca4edSNicholas Piggin# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from 3204efca4edSNicholas Piggin# the .S file (with trailing ';'), and run genksyms on that, to extract vers. 3214efca4edSNicholas Piggin# 3224efca4edSNicholas Piggin# This is convoluted. The .S file must first be preprocessed to run guards and 3234efca4edSNicholas Piggin# expand names, then the resulting exports must be constructed into plain 3244efca4edSNicholas Piggin# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed 3254efca4edSNicholas Piggin# to make the genksyms input. 3264efca4edSNicholas Piggin# 3274efca4edSNicholas Piggin# These mirror gensymtypes_c and co above, keep them in synch. 3284efca4edSNicholas Piggincmd_gensymtypes_S = \ 329b79c6aa6SMasahiro Yamada { echo "\#include <linux/kernel.h>" ; \ 3304efca4edSNicholas Piggin echo "\#include <asm/asm-prototypes.h>" ; \ 331ddb5cdbaSMasahiro Yamada $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \ 332f43e31d5SMasahiro Yamada $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) 3334efca4edSNicholas Piggin 3344efca4edSNicholas Pigginquiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@ 335dc6dc3e7SMasahiro Yamada cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null 3364efca4edSNicholas Piggin 3374efca4edSNicholas Piggin$(obj)/%.symtypes : $(src)/%.S FORCE 3384efca4edSNicholas Piggin $(call cmd,cc_symtypes_S) 3394efca4edSNicholas Piggin 3404efca4edSNicholas Piggin 341e0f41e52SMasahiro Yamadaquiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@ 342e0f41e52SMasahiro Yamadacmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $< 3431da177e4SLinus Torvalds 344767e581dSSam Ravnborg$(obj)/%.s: $(src)/%.S FORCE 345e0f41e52SMasahiro Yamada $(call if_changed_dep,cpp_s_S) 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvaldsquiet_cmd_as_o_S = AS $(quiet_modtag) $@ 34892594d56SMasahiro Yamada cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool) 3491da177e4SLinus Torvalds 3502ff2b7ecSMasahiro Yamadaifdef CONFIG_ASM_MODVERSIONS 3514efca4edSNicholas Piggin 3524efca4edSNicholas Piggin# versioning matches the C process described above, with difference that 3534efca4edSNicholas Piggin# we parse asm-prototypes.h C header to get function definitions. 3544efca4edSNicholas Piggin 3557cfa2fcbSMasahiro Yamadacmd_gen_symversions_S = $(call gen_symversions,S) 3567cfa2fcbSMasahiro Yamada 3574efca4edSNicholas Pigginendif 3584efca4edSNicholas Piggin 359ef62588cSMasahiro Yamada$(obj)/%.o: $(src)/%.S FORCE 360b9ab5ebbSJosh Poimboeuf $(call if_changed_rule,as_o_S) 3611da177e4SLinus Torvalds 362b2c88554SMasahiro Yamadatargets += $(filter-out $(subdir-builtin), $(real-obj-y)) 363b2c88554SMasahiro Yamadatargets += $(filter-out $(subdir-modorder), $(real-obj-m)) 364cc306abdSMasahiro Yamadatargets += $(real-dtb-y) $(lib-y) $(always-y) 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds# Linker scripts preprocessor (.lds.S -> .lds) 3671da177e4SLinus Torvalds# --------------------------------------------------------------------------- 3681da177e4SLinus Torvaldsquiet_cmd_cpp_lds_S = LDS $@ 3695cb0512cSLinus Torvalds cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \ 37042f29a25STim Abbott -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $< 3711da177e4SLinus Torvalds 372767e581dSSam Ravnborg$(obj)/%.lds: $(src)/%.lds.S FORCE 3731da177e4SLinus Torvalds $(call if_changed_dep,cpp_lds_S) 3741da177e4SLinus Torvalds 3754520c6a4SDavid Howells# ASN.1 grammar 3764520c6a4SDavid Howells# --------------------------------------------------------------------------- 37749d5089dSMasahiro Yamadaquiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch] 3784520c6a4SDavid Howells cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \ 37949d5089dSMasahiro Yamada $(basename $@).c $(basename $@).h 3804520c6a4SDavid Howells 3814fa8bc94SMasahiro Yamada$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler 3824520c6a4SDavid Howells $(call cmd,asn1_compiler) 3834520c6a4SDavid Howells 3841da177e4SLinus Torvalds# Build the compiled-in targets 3851da177e4SLinus Torvalds# --------------------------------------------------------------------------- 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds# To build objects in subdirs, we need to descend into the directories 388aaa385baSMasahiro Yamada$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ; 389454753d9SMasahiro Yamada$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds# 392dee94953SMasahiro Yamada# Rule to compile a set of .o files into one .a file (without symbol table) 3931da177e4SLinus Torvalds# 394cd968b97SMasahiro Yamada# To make this rule robust against "Argument list too long" error, 395cd968b97SMasahiro Yamada# remove $(obj)/ prefix, and restore it by a shell command. 396a5967db9SStephen Rothwell 3975e18f029SMasahiro Yamadaquiet_cmd_ar_builtin = AR $@ 398cd968b97SMasahiro Yamada cmd_ar_builtin = rm -f $@; \ 3995439d4d4SMasahiro Yamada $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \ 4005439d4d4SMasahiro Yamada $(AR) cDPrST $@ 4011da177e4SLinus Torvalds 402e578edc7SMasahiro Yamada$(obj)/built-in.a: $(real-obj-y) FORCE 4037375cbcfSMasahiro Yamada $(call if_changed,ar_builtin) 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds# 406551559e1STejun Heo# Rule to create modules.order file 407551559e1STejun Heo# 408551559e1STejun Heo# Create commands to either record .ko file or cat modules.order from 409551559e1STejun Heo# a subdirectory 410b2c88554SMasahiro Yamada# Add $(obj-m) as the prerequisite to avoid updating the timestamp of 411b2c88554SMasahiro Yamada# modules.order unless contained modules are updated. 412b2c88554SMasahiro Yamada 413b2c88554SMasahiro Yamadacmd_modules_order = { $(foreach m, $(real-prereqs), \ 414f65a4868SMasahiro Yamada $(if $(filter %/modules.order, $m), cat $m, echo $m);) :; } \ 415d724b578SMasahiro Yamada > $@ 416551559e1STejun Heo 417b2c88554SMasahiro Yamada$(obj)/modules.order: $(obj-m) FORCE 418b2c88554SMasahiro Yamada $(call if_changed,modules_order) 419b2c88554SMasahiro Yamada 420551559e1STejun Heo# 421dee94953SMasahiro Yamada# Rule to compile a set of .o files into one .a file (with symbol table) 4221da177e4SLinus Torvalds# 42338e89184SSami Tolvanen 424e578edc7SMasahiro Yamada$(obj)/lib.a: $(lib-y) FORCE 4257375cbcfSMasahiro Yamada $(call if_changed,ar) 4261da177e4SLinus Torvalds 427f6b66ca4SMasahiro Yamadaquiet_cmd_ld_multi_m = LD [M] $@ 428f6b66ca4SMasahiro Yamada cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$(patsubst %.o,%.mod,$@) $(cmd_objtool) 429f6b66ca4SMasahiro Yamada 430f6b66ca4SMasahiro Yamadadefine rule_ld_multi_m 431f6b66ca4SMasahiro Yamada $(call cmd_and_savecmd,ld_multi_m) 432f6b66ca4SMasahiro Yamada $(call cmd,gen_objtooldep) 433f6b66ca4SMasahiro Yamadaendef 4341da177e4SLinus Torvalds 435c25e1c55SMasahiro Yamada$(multi-obj-m): objtool-enabled := $(delay-objtool) 436c25e1c55SMasahiro Yamada$(multi-obj-m): part-of-module := y 437feb7d79fSMasahiro Yamada$(multi-obj-m): %.o: %.mod FORCE 438f6b66ca4SMasahiro Yamada $(call if_changed_rule,ld_multi_m) 439a6601e01SMasahiro Yamada$(call multi_depend, $(multi-obj-m), .o, -objs -y -m) 4401da177e4SLinus Torvalds 441b23d1a24SMasahiro Yamada# Add intermediate targets: 442b23d1a24SMasahiro Yamada# When building objects with specific suffix patterns, add intermediate 443b23d1a24SMasahiro Yamada# targets that the final targets are derived from. 444b23d1a24SMasahiro Yamadaintermediate_targets = $(foreach sfx, $(2), \ 445b23d1a24SMasahiro Yamada $(patsubst %$(strip $(1)),%$(sfx), \ 446b23d1a24SMasahiro Yamada $(filter %$(strip $(1)), $(targets)))) 44754a702f7SMasahiro Yamada# %.asn1.o <- %.asn1.[ch] <- %.asn1 448a7f92419SMasahiro Yamada# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts 449dcad240cSAndrew Davis# %.dtbo.o <- %.dtbo.S <- %.dtbo <- %.dtso 450b23d1a24SMasahiro Yamada# %.lex.o <- %.lex.c <- %.l 451b23d1a24SMasahiro Yamada# %.tab.o <- %.tab.[ch] <- %.y 45254a702f7SMasahiro Yamadatargets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \ 45354a702f7SMasahiro Yamada $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \ 454dcad240cSAndrew Davis $(call intermediate_targets, .dtbo.o, .dtbo.S .dtbo) \ 455a7f92419SMasahiro Yamada $(call intermediate_targets, .lex.o, .lex.c) \ 456b23d1a24SMasahiro Yamada $(call intermediate_targets, .tab.o, .tab.c .tab.h) 457b23d1a24SMasahiro Yamada 458394053f4SMasahiro Yamada# Build 459394053f4SMasahiro Yamada# --------------------------------------------------------------------------- 460394053f4SMasahiro Yamada 461cc306abdSMasahiro Yamada$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \ 462f3908ab3SMasahiro Yamada $(if $(KBUILD_MODULES), $(targets-for-modules)) \ 4635f2fb52fSMasahiro Yamada $(subdir-ym) $(always-y) 464394053f4SMasahiro Yamada @: 465394053f4SMasahiro Yamada 466cc306abdSMasahiro Yamada# Single targets 467cc306abdSMasahiro Yamada# --------------------------------------------------------------------------- 468cc306abdSMasahiro Yamada 469cc306abdSMasahiro Yamadasingle-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) 470cc306abdSMasahiro Yamadasingle-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) 471cc306abdSMasahiro Yamada 472cc306abdSMasahiro Yamada$(single-subdir-goals): $(single-subdirs) 473cc306abdSMasahiro Yamada @: 474394053f4SMasahiro Yamada 4751da177e4SLinus Torvalds# Descending 4761da177e4SLinus Torvalds# --------------------------------------------------------------------------- 4771da177e4SLinus Torvalds 4784f193362SPaul SmithPHONY += $(subdir-ym) 4791da177e4SLinus Torvalds$(subdir-ym): 480d9f78edfSMasahiro Yamada $(Q)$(MAKE) $(build)=$@ \ 481aaa385baSMasahiro Yamada need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ 482cc306abdSMasahiro Yamada need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ 483cc306abdSMasahiro Yamada $(filter $@/%, $(single-subdir-goals)) 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds# Add FORCE to the prequisites of a target to force it to be always rebuilt. 4861da177e4SLinus Torvalds# --------------------------------------------------------------------------- 4871da177e4SLinus Torvalds 4884f193362SPaul SmithPHONY += FORCE 4891da177e4SLinus Torvalds 4901da177e4SLinus TorvaldsFORCE: 4911da177e4SLinus Torvalds 492cc306abdSMasahiro Yamadatargets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS)) 493cc306abdSMasahiro Yamadatargets := $(filter-out $(PHONY), $(targets)) 494cc306abdSMasahiro Yamada 4951da177e4SLinus Torvalds# Read all saved command lines and dependencies for the $(targets) we 4961da177e4SLinus Torvalds# may be building above, using $(if_changed{,_dep}). As an 4971da177e4SLinus Torvalds# optimization, we don't need to read them if the target does not 4981da177e4SLinus Torvalds# exist, we will rebuild anyway in that case. 4991da177e4SLinus Torvalds 500392885eeSMasahiro Yamadaexisting-targets := $(wildcard $(sort $(targets))) 5011da177e4SLinus Torvalds 502392885eeSMasahiro Yamada-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 5034f193362SPaul Smith 5048a78756eSMasahiro Yamada# Create directories for object files if they do not exist 505cc8a51caSMasahiro Yamadaobj-dirs := $(sort $(patsubst %/,%, $(dir $(targets)))) 506392885eeSMasahiro Yamada# If targets exist, their directories apparently exist. Skip mkdir. 507392885eeSMasahiro Yamadaexisting-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets)))) 508392885eeSMasahiro Yamadaobj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs))) 509c4da7ed0SMasahiro Yamadaifneq ($(obj-dirs),) 5108a78756eSMasahiro Yamada$(shell mkdir -p $(obj-dirs)) 5118a78756eSMasahiro Yamadaendif 5128a78756eSMasahiro Yamada 5134f193362SPaul Smith.PHONY: $(PHONY) 514