Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 2 | # ========================================================================== |
| 3 | # Building |
| 4 | # ========================================================================== |
| 5 | |
Masahiro Yamada | 0d68fb1 | 2014-02-04 17:24:28 +0900 | [diff] [blame] | 6 | # Modified for U-Boot |
Simon Glass | 7eec884 | 2022-04-30 00:56:52 -0600 | [diff] [blame] | 7 | prefix := vpl |
| 8 | src := $(patsubst $(prefix)/%,%,$(obj)) |
| 9 | ifeq ($(obj),$(src)) |
Masahiro Yamada | 60bb734 | 2014-07-30 14:08:17 +0900 | [diff] [blame] | 10 | prefix := tpl |
| 11 | src := $(patsubst $(prefix)/%,%,$(obj)) |
| 12 | ifeq ($(obj),$(src)) |
| 13 | prefix := spl |
| 14 | src := $(patsubst $(prefix)/%,%,$(obj)) |
| 15 | ifeq ($(obj),$(src)) |
| 16 | prefix := . |
| 17 | endif |
Masahiro Yamada | 0d68fb1 | 2014-02-04 17:24:28 +0900 | [diff] [blame] | 18 | endif |
Simon Glass | 7eec884 | 2022-04-30 00:56:52 -0600 | [diff] [blame] | 19 | endif |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 20 | |
| 21 | PHONY := __build |
| 22 | __build: |
| 23 | |
| 24 | # Init all relevant variables used in kbuild files so |
| 25 | # 1) they have correct type |
| 26 | # 2) they do not inherit any value from the environment |
| 27 | obj-y := |
| 28 | obj-m := |
| 29 | lib-y := |
| 30 | lib-m := |
| 31 | always := |
| 32 | targets := |
| 33 | subdir-y := |
| 34 | subdir-m := |
| 35 | EXTRA_AFLAGS := |
| 36 | EXTRA_CFLAGS := |
| 37 | EXTRA_CPPFLAGS := |
| 38 | EXTRA_LDFLAGS := |
| 39 | asflags-y := |
| 40 | ccflags-y := |
| 41 | cppflags-y := |
| 42 | ldflags-y := |
| 43 | |
| 44 | subdir-asflags-y := |
| 45 | subdir-ccflags-y := |
| 46 | |
| 47 | # Read auto.conf if it exists, otherwise ignore |
Masahiro Yamada | 60bb734 | 2014-07-30 14:08:17 +0900 | [diff] [blame] | 48 | # Modified for U-Boot |
Masahiro Yamada | 72fb32b | 2015-02-24 22:26:20 +0900 | [diff] [blame] | 49 | -include include/config/auto.conf |
Masahiro Yamada | 60bb734 | 2014-07-30 14:08:17 +0900 | [diff] [blame] | 50 | -include $(prefix)/include/autoconf.mk |
Masahiro Yamada | 5ac303c | 2014-02-04 17:24:32 +0900 | [diff] [blame] | 51 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 52 | include scripts/Kbuild.include |
| 53 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 54 | # The filename Kbuild has precedence over Makefile |
| 55 | kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) |
| 56 | kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile) |
| 57 | include $(kbuild-file) |
| 58 | |
Masahiro Yamada | 958fe8f | 2014-03-05 16:59:40 +0900 | [diff] [blame] | 59 | # Added for U-Boot |
| 60 | asflags-y += $(PLATFORM_CPPFLAGS) |
| 61 | ccflags-y += $(PLATFORM_CPPFLAGS) |
| 62 | cppflags-y += $(PLATFORM_CPPFLAGS) |
| 63 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 64 | include scripts/Makefile.lib |
| 65 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 66 | # Do not include host rules unless needed |
Tom Rini | 5d4ecf2 | 2020-03-27 11:46:27 -0400 | [diff] [blame] | 67 | ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 68 | include scripts/Makefile.host |
| 69 | endif |
| 70 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 71 | ifndef obj |
| 72 | $(warning kbuild: Makefile.build is included improperly) |
| 73 | endif |
| 74 | |
| 75 | # =========================================================================== |
| 76 | |
Masahiro Yamada | ede865b | 2014-10-30 11:06:13 +0900 | [diff] [blame] | 77 | ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 78 | lib-target := $(obj)/lib.a |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 79 | # Modified for U-Boot |
| 80 | # real-obj-y += $(obj)/lib-ksyms.o |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 81 | endif |
| 82 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 83 | ifneq ($(strip $(real-obj-y) $(need-builtin)),) |
| 84 | builtin-target := $(obj)/built-in.a |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 85 | endif |
| 86 | |
Ilias Apalodimas | fb3b8c8 | 2025-05-20 08:21:30 +0300 | [diff] [blame] | 87 | ifdef CONFIG_MODULES |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 88 | modorder-target := $(obj)/modules.order |
Ilias Apalodimas | fb3b8c8 | 2025-05-20 08:21:30 +0300 | [diff] [blame] | 89 | endif |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 90 | |
| 91 | # We keep a list of all modules in $(MODVERDIR) |
| 92 | |
| 93 | __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \ |
| 94 | $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \ |
| 95 | $(subdir-ym) $(always) |
| 96 | @: |
| 97 | |
| 98 | # Linus' kernel sanity checking tool |
Tom Rini | 5080063 | 2021-06-17 18:07:25 -0400 | [diff] [blame] | 99 | ifeq ($(KBUILD_CHECKSRC),1) |
| 100 | quiet_cmd_checksrc = CHECK $< |
Ilias Apalodimas | 7e3e56c | 2025-05-20 08:21:26 +0300 | [diff] [blame] | 101 | cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< |
Tom Rini | 5080063 | 2021-06-17 18:07:25 -0400 | [diff] [blame] | 102 | else ifeq ($(KBUILD_CHECKSRC),2) |
| 103 | quiet_cmd_force_checksrc = CHECK $< |
Ilias Apalodimas | 7e3e56c | 2025-05-20 08:21:26 +0300 | [diff] [blame] | 104 | cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 105 | endif |
| 106 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 107 | ifneq ($(KBUILD_ENABLE_EXTRA_GCC_CHECKS),) |
| 108 | cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $< |
| 109 | endif |
| 110 | |
| 111 | # Do section mismatch analysis for each module/built-in.a |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 112 | ifdef CONFIG_DEBUG_SECTION_MISMATCH |
| 113 | cmd_secanalysis = ; scripts/mod/modpost $@ |
| 114 | endif |
| 115 | |
| 116 | # Compile C sources (.c) |
| 117 | # --------------------------------------------------------------------------- |
| 118 | |
| 119 | # Default is built-in, unless we know otherwise |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 120 | $(foreach x, i ll lst o s symtypes, $(patsubst %.o,%.$(x),$(real-obj-m))): \ |
| 121 | part-of-module := y |
| 122 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 123 | modkern_cflags = \ |
| 124 | $(if $(part-of-module), \ |
| 125 | $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \ |
| 126 | $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL)) |
Ilias Apalodimas | fa37800 | 2025-05-20 08:21:29 +0300 | [diff] [blame] | 127 | quiet_modtag = $(if $(part-of-module),[M], ) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 128 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 129 | quiet_cmd_cc_s_c = CC $(quiet_modtag) $@ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 130 | cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS), $(c_flags)) $(DISABLE_LTO) -fverbose-asm -S -o $@ $< |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 131 | |
| 132 | $(obj)/%.s: $(src)/%.c FORCE |
| 133 | $(call if_changed_dep,cc_s_c) |
| 134 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 135 | quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@ |
| 136 | cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 137 | |
| 138 | $(obj)/%.i: $(src)/%.c FORCE |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 139 | $(call if_changed_dep,cpp_i_c) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 140 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 141 | # These mirror gensymtypes_S and co below, keep them in synch. |
| 142 | cmd_gensymtypes_c = \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 143 | $(CPP) -D__GENKSYMS__ $(c_flags) $< | \ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 144 | scripts/genksyms/genksyms $(if $(1), -T $(2)) \ |
| 145 | $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 146 | $(if $(KBUILD_PRESERVE),-p) \ |
| 147 | -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null)) |
| 148 | |
| 149 | quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@ |
| 150 | cmd_cc_symtypes_c = \ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 151 | $(call cmd_gensymtypes_c,true,$@) >/dev/null; \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 152 | test -s $@ || rm -f $@ |
| 153 | |
| 154 | $(obj)/%.symtypes : $(src)/%.c FORCE |
| 155 | $(call cmd,cc_symtypes_c) |
| 156 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 157 | # LLVM assembly |
| 158 | # Generate .ll files from .c |
| 159 | quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@ |
| 160 | cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $< |
| 161 | |
| 162 | $(obj)/%.ll: $(src)/%.c FORCE |
| 163 | $(call if_changed_dep,cc_ll_c) |
| 164 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 165 | # C (.c) files |
| 166 | # The C file is compiled and updated dependency information is generated. |
| 167 | # (See cmd_cc_o_c + relevant part of rule_cc_o_c) |
| 168 | |
| 169 | quiet_cmd_cc_o_c = CC $(quiet_modtag) $@ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 170 | cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 171 | |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 172 | ifdef CONFIG_MODVERSIONS |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 173 | # When module versioning is enabled the following steps are executed: |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 174 | # o compile a <file>.o from <file>.c |
| 175 | # o if <file>.o doesn't contain a __ksymtab version, i.e. does |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 176 | # not export symbols, it's done. |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 177 | # o otherwise, we calculate symbol versions using the good old |
| 178 | # genksyms on the preprocessed source and postprocess them in a way |
| 179 | # that they are usable as a linker script |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 180 | # o generate .tmp_<file>.o from <file>.o using the linker to |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 181 | # replace the unresolved symbols __crc_exported_symbol with |
| 182 | # the actual value of the checksum generated by genksyms |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 183 | # o remove .tmp_<file>.o to <file>.o |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 184 | |
| 185 | cmd_modversions_c = \ |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 186 | if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 187 | $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 188 | > $(@D)/.tmp_$(@F:.o=.ver); \ |
| 189 | \ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 190 | $(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 191 | -T $(@D)/.tmp_$(@F:.o=.ver); \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 192 | mv -f $(@D)/.tmp_$(@F) $@; \ |
Ilias Apalodimas | 0785df0 | 2025-05-20 08:21:24 +0300 | [diff] [blame] | 193 | rm -f $(@D)/.tmp_$(@F:.o=.ver); \ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 194 | fi |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 195 | endif |
| 196 | |
| 197 | ifdef CONFIG_FTRACE_MCOUNT_RECORD |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 198 | ifndef CC_USING_RECORD_MCOUNT |
| 199 | # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 200 | ifdef BUILD_C_RECORDMCOUNT |
| 201 | ifeq ("$(origin RECORDMCOUNT_WARN)", "command line") |
| 202 | RECORDMCOUNT_FLAGS = -w |
| 203 | endif |
| 204 | # Due to recursion, we must skip empty.o. |
| 205 | # The empty.o file is created in the make process in order to determine |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 206 | # the target endianness and word size. It is made before all other C |
| 207 | # files, including recordmcount. |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 208 | sub_cmd_record_mcount = \ |
| 209 | if [ $(@) != "scripts/mod/empty.o" ]; then \ |
| 210 | $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ |
| 211 | fi; |
| 212 | recordmcount_source := $(srctree)/scripts/recordmcount.c \ |
| 213 | $(srctree)/scripts/recordmcount.h |
| 214 | else |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 215 | sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \ |
Jiaxun Yang | 0803a27 | 2024-07-17 16:07:03 +0800 | [diff] [blame] | 216 | "$(if $(CONFIG_SYS_BIG_ENDIAN),big,little)" \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 217 | "$(if $(CONFIG_64BIT),64,32)" \ |
Tom Rini | 5d4ecf2 | 2020-03-27 11:46:27 -0400 | [diff] [blame] | 218 | "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ |
| 219 | "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 220 | "$(if $(part-of-module),1,0)" "$(@)"; |
| 221 | recordmcount_source := $(srctree)/scripts/recordmcount.pl |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 222 | endif # BUILD_C_RECORDMCOUNT |
| 223 | cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ |
| 224 | $(sub_cmd_record_mcount)) |
| 225 | endif # CC_USING_RECORD_MCOUNT |
| 226 | endif # CONFIG_FTRACE_MCOUNT_RECORD |
| 227 | |
| 228 | ifdef CONFIG_STACK_VALIDATION |
| 229 | ifneq ($(SKIP_STACK_VALIDATION),1) |
| 230 | |
| 231 | __objtool_obj := $(objtree)/tools/objtool/objtool |
| 232 | |
| 233 | objtool_args = $(if $(CONFIG_UNWINDER_ORC),orc generate,check) |
| 234 | |
| 235 | objtool_args += $(if $(part-of-module), --module,) |
| 236 | |
| 237 | ifndef CONFIG_FRAME_POINTER |
| 238 | objtool_args += --no-fp |
| 239 | endif |
| 240 | ifdef CONFIG_GCOV_KERNEL |
| 241 | objtool_args += --no-unreachable |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 242 | endif |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 243 | ifdef CONFIG_RETPOLINE |
| 244 | objtool_args += --retpoline |
| 245 | endif |
| 246 | |
| 247 | # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory |
| 248 | # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file |
| 249 | # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file |
| 250 | cmd_objtool = $(if $(patsubst y%,, \ |
| 251 | $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \ |
| 252 | $(__objtool_obj) $(objtool_args) $@) |
| 253 | objtool_obj = $(if $(patsubst y%,, \ |
| 254 | $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \ |
| 255 | $(__objtool_obj)) |
| 256 | |
| 257 | endif # SKIP_STACK_VALIDATION |
| 258 | endif # CONFIG_STACK_VALIDATION |
| 259 | |
| 260 | # Rebuild all objects when objtool changes, or is enabled/disabled. |
| 261 | objtool_dep = $(objtool_obj) \ |
| 262 | $(wildcard include/config/orc/unwinder.h \ |
| 263 | include/config/stack/validation.h) |
| 264 | |
| 265 | ifdef CONFIG_TRIM_UNUSED_KSYMS |
| 266 | cmd_gen_ksymdeps = \ |
| 267 | $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 268 | endif |
| 269 | |
| 270 | define rule_cc_o_c |
Ilias Apalodimas | 25c46de | 2025-05-20 08:21:25 +0300 | [diff] [blame] | 271 | $(call cmd,checksrc) |
| 272 | $(call cmd_and_fixdep,cc_o_c) |
| 273 | $(call cmd,gen_ksymdeps) |
| 274 | $(call cmd,checkdoc) |
| 275 | $(call cmd,objtool) |
| 276 | $(call cmd,modversions_c) |
| 277 | $(call cmd,record_mcount) |
Ilias Apalodimas | 342cc87 | 2025-05-20 08:21:12 +0300 | [diff] [blame] | 278 | endef |
| 279 | |
| 280 | define rule_as_o_S |
| 281 | $(call cmd_and_fixdep,as_o_S) |
| 282 | $(call cmd,gen_ksymdeps) |
| 283 | $(call cmd,objtool) |
| 284 | $(call cmd,modversions_S) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 285 | endef |
| 286 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 287 | # List module undefined symbols (or empty line if not enabled) |
| 288 | ifdef CONFIG_TRIM_UNUSED_KSYMS |
| 289 | cmd_undef_syms = $(NM) $@ | sed -n 's/^ *U //p' | xargs echo |
| 290 | else |
| 291 | cmd_undef_syms = echo |
| 292 | endif |
| 293 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 294 | # Built-in and composite module parts |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 295 | $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 296 | $(call cmd,force_checksrc) |
| 297 | $(call if_changed_rule,cc_o_c) |
| 298 | |
| 299 | # Single-part modules are special since we need to mark them in $(MODVERDIR) |
| 300 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 301 | $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 302 | $(call cmd,force_checksrc) |
| 303 | $(call if_changed_rule,cc_o_c) |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 304 | @{ echo $(@:.o=.ko); echo $@; \ |
| 305 | $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 306 | |
| 307 | quiet_cmd_cc_lst_c = MKLST $@ |
| 308 | cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \ |
| 309 | $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \ |
| 310 | System.map $(OBJDUMP) > $@ |
| 311 | |
| 312 | $(obj)/%.lst: $(src)/%.c FORCE |
| 313 | $(call if_changed_dep,cc_lst_c) |
| 314 | |
| 315 | # Compile assembler sources (.S) |
| 316 | # --------------------------------------------------------------------------- |
| 317 | |
| 318 | modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL) |
| 319 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 320 | $(real-obj-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE) |
| 321 | $(real-obj-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE) |
| 322 | |
| 323 | # .S file exports must have their C prototypes defined in asm/asm-prototypes.h |
| 324 | # or a file that it includes, in order to get versioned symbols. We build a |
| 325 | # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from |
| 326 | # the .S file (with trailing ';'), and run genksyms on that, to extract vers. |
| 327 | # |
| 328 | # This is convoluted. The .S file must first be preprocessed to run guards and |
| 329 | # expand names, then the resulting exports must be constructed into plain |
| 330 | # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed |
| 331 | # to make the genksyms input. |
| 332 | # |
| 333 | # These mirror gensymtypes_c and co above, keep them in synch. |
| 334 | cmd_gensymtypes_S = \ |
| 335 | { echo "\#include <linux/kernel.h>" ; \ |
| 336 | echo "\#include <asm/asm-prototypes.h>" ; \ |
| 337 | $(CPP) $(a_flags) $< | \ |
| 338 | grep "\<___EXPORT_SYMBOL\>" | \ |
| 339 | sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \ |
| 340 | $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | \ |
| 341 | scripts/genksyms/genksyms $(if $(1), -T $(2)) \ |
| 342 | $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \ |
| 343 | $(if $(KBUILD_PRESERVE),-p) \ |
| 344 | -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null)) |
| 345 | |
| 346 | quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@ |
| 347 | cmd_cc_symtypes_S = \ |
| 348 | $(call cmd_gensymtypes_S,true,$@) >/dev/null; \ |
| 349 | test -s $@ || rm -f $@ |
| 350 | |
| 351 | $(obj)/%.symtypes : $(src)/%.S FORCE |
| 352 | $(call cmd,cc_symtypes_S) |
| 353 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 354 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 355 | quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@ |
| 356 | cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $< |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 357 | |
| 358 | $(obj)/%.s: $(src)/%.S FORCE |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 359 | $(call if_changed_dep,cpp_s_S) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 360 | |
| 361 | quiet_cmd_as_o_S = AS $(quiet_modtag) $@ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 362 | cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< |
| 363 | |
| 364 | ifdef CONFIG_MODVERSIONS |
| 365 | |
| 366 | ASM_PROTOTYPES := $(wildcard $(srctree)/arch/$(SRCARCH)/include/asm/asm-prototypes.h) |
| 367 | |
| 368 | ifneq ($(ASM_PROTOTYPES),) |
| 369 | |
| 370 | # versioning matches the C process described above, with difference that |
| 371 | # we parse asm-prototypes.h C header to get function definitions. |
| 372 | |
| 373 | cmd_modversions_S = \ |
| 374 | if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \ |
| 375 | $(call cmd_gensymtypes_S,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \ |
| 376 | > $(@D)/.tmp_$(@F:.o=.ver); \ |
| 377 | \ |
| 378 | $(LD) $(KBUILD_LDFLAGS) -r -o $(@D)/.tmp_$(@F) $@ \ |
| 379 | -T $(@D)/.tmp_$(@F:.o=.ver); \ |
| 380 | mv -f $(@D)/.tmp_$(@F) $@; \ |
| 381 | rm -f $(@D)/.tmp_$(@F:.o=.ver); \ |
| 382 | fi |
| 383 | endif |
| 384 | endif |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 385 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 386 | $(obj)/%.o: $(src)/%.S $(objtool_dep) FORCE |
| 387 | $(call if_changed_rule,as_o_S) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 388 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 389 | targets += $(filter-out $(subdir-obj-y), $(real-obj-y)) $(real-obj-m) $(lib-y) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 390 | targets += $(extra-y) $(MAKECMDGOALS) $(always) |
Prasad Kummari | cd91adc | 2024-09-06 12:38:07 +0530 | [diff] [blame] | 391 | targets += $(real-dtb-y) $(lib-y) $(always-y) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 392 | |
| 393 | # Linker scripts preprocessor (.lds.S -> .lds) |
| 394 | # --------------------------------------------------------------------------- |
| 395 | quiet_cmd_cpp_lds_S = LDS $@ |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 396 | cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 397 | -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $< |
| 398 | |
| 399 | $(obj)/%.lds: $(src)/%.lds.S FORCE |
| 400 | $(call if_changed_dep,cpp_lds_S) |
| 401 | |
| 402 | # ASN.1 grammar |
| 403 | # --------------------------------------------------------------------------- |
| 404 | quiet_cmd_asn1_compiler = ASN.1 $@ |
AKASHI Takahiro | f702526 | 2019-11-13 09:44:53 +0900 | [diff] [blame] | 405 | cmd_asn1_compiler = $(objtree)/tools/asn1_compiler $< \ |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 406 | $(subst .h,.c,$@) $(subst .c,.h,$@) |
| 407 | |
AKASHI Takahiro | f702526 | 2019-11-13 09:44:53 +0900 | [diff] [blame] | 408 | $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/tools/asn1_compiler |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 409 | $(call cmd,asn1_compiler) |
| 410 | |
| 411 | # Build the compiled-in targets |
| 412 | # --------------------------------------------------------------------------- |
| 413 | |
| 414 | # To build objects in subdirs, we need to descend into the directories |
| 415 | $(sort $(subdir-obj-y)): $(subdir-ym) ; |
| 416 | |
| 417 | # |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 418 | # Rule to compile a set of .o files into one .a file (without symbol table) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 419 | # |
| 420 | ifdef builtin-target |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 421 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 422 | quiet_cmd_ar_builtin = AR $@ |
| 423 | # Modified for U-Boot, we create thin archives directly, instead of using the kernel script |
| 424 | # Check f49821ee32b76 and 6358d6e8b98 in kernel |
| 425 | cmd_ar_builtin = rm -f $@; $(AR) rcTP$(KBUILD_ARFLAGS) $@ $(real-prereqs) |
| 426 | |
| 427 | $(builtin-target): $(real-obj-y) FORCE |
| 428 | $(call if_changed,ar_builtin) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 429 | |
| 430 | targets += $(builtin-target) |
| 431 | endif # builtin-target |
| 432 | |
| 433 | # |
| 434 | # Rule to create modules.order file |
| 435 | # |
| 436 | # Create commands to either record .ko file or cat modules.order from |
| 437 | # a subdirectory |
| 438 | modorder-cmds = \ |
| 439 | $(foreach m, $(modorder), \ |
| 440 | $(if $(filter %/modules.order, $m), \ |
| 441 | cat $m;, echo kernel/$m;)) |
| 442 | |
| 443 | $(modorder-target): $(subdir-ym) FORCE |
| 444 | $(Q)(cat /dev/null; $(modorder-cmds)) > $@ |
| 445 | |
| 446 | # |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 447 | # Rule to compile a set of .o files into one .a file (with symbol table) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 448 | # |
| 449 | ifdef lib-target |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 450 | |
| 451 | $(lib-target): $(lib-y) FORCE |
Ilias Apalodimas | 367cd3c | 2025-05-20 08:21:21 +0300 | [diff] [blame] | 452 | $(call if_changed,ar) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 453 | |
| 454 | targets += $(lib-target) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 455 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 456 | dummy-object = $(obj)/.lib_exports.o |
| 457 | ksyms-lds = $(dot-target).lds |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 458 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 459 | quiet_cmd_export_list = EXPORTS $@ |
| 460 | cmd_export_list = $(OBJDUMP) -h $< | \ |
| 461 | sed -ne '/___ksymtab/s/.*+\([^ ]*\).*/EXTERN(\1)/p' >$(ksyms-lds);\ |
| 462 | rm -f $(dummy-object);\ |
| 463 | echo | $(CC) $(a_flags) -c -o $(dummy-object) -x assembler -;\ |
| 464 | $(LD) $(ld_flags) -r -o $@ -T $(ksyms-lds) $(dummy-object);\ |
| 465 | rm $(dummy-object) $(ksyms-lds) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 466 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 467 | $(obj)/lib-ksyms.o: $(lib-target) FORCE |
| 468 | $(call if_changed,export_list) |
| 469 | |
| 470 | targets += $(obj)/lib-ksyms.o |
| 471 | |
| 472 | endif |
| 473 | |
| 474 | # NOTE: |
| 475 | # Do not replace $(filter %.o,^) with $(real-prereqs). When a single object |
| 476 | # module is turned into a multi object module, $^ will contain header file |
| 477 | # dependencies recorded in the .*.cmd file. |
| 478 | quiet_cmd_link_multi-m = LD [M] $@ |
| 479 | cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^) $(cmd_secanalysis) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 480 | |
Masahiro Yamada | 8c4532f | 2014-10-30 11:06:10 +0900 | [diff] [blame] | 481 | $(multi-used-m): FORCE |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 482 | $(call if_changed,link_multi-m) |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 483 | @{ echo $(@:.o=.ko); echo $(filter %.o,$^); \ |
| 484 | $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod) |
| 485 | $(call multi_depend, $(multi-used-m), .o, -objs -y -m) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 486 | |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 487 | targets += $(multi-used-m) |
| 488 | targets := $(filter-out $(PHONY), $(targets)) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 489 | |
Eugeniu Rosca | d57dd94 | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 490 | # Add intermediate targets: |
| 491 | # When building objects with specific suffix patterns, add intermediate |
| 492 | # targets that the final targets are derived from. |
| 493 | intermediate_targets = $(foreach sfx, $(2), \ |
| 494 | $(patsubst %$(strip $(1)),%$(sfx), \ |
| 495 | $(filter %$(strip $(1)), $(targets)))) |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 496 | # %.asn1.o <- %.asn1.[ch] <- %.asn1 |
| 497 | # %.dtb.o <- %.dtb.S <- %.dtb <- %.dts |
Eugeniu Rosca | d57dd94 | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 498 | # %.lex.o <- %.lex.c <- %.l |
| 499 | # %.tab.o <- %.tab.[ch] <- %.y |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 500 | targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \ |
| 501 | $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \ |
| 502 | $(call intermediate_targets, .lex.o, .lex.c) \ |
Eugeniu Rosca | d57dd94 | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 503 | $(call intermediate_targets, .tab.o, .tab.c .tab.h) |
| 504 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 505 | # Descending |
| 506 | # --------------------------------------------------------------------------- |
| 507 | |
| 508 | PHONY += $(subdir-ym) |
| 509 | $(subdir-ym): |
Ilias Apalodimas | 7860ad3 | 2025-06-27 21:57:16 +0300 | [diff] [blame] | 510 | $(Q)$(MAKE) $(build)=$@ need-builtin=$(if $(findstring $@,$(subdir-obj-y)),1) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 511 | |
| 512 | # Add FORCE to the prequisites of a target to force it to be always rebuilt. |
| 513 | # --------------------------------------------------------------------------- |
| 514 | |
| 515 | PHONY += FORCE |
| 516 | |
| 517 | FORCE: |
| 518 | |
| 519 | # Read all saved command lines and dependencies for the $(targets) we |
| 520 | # may be building above, using $(if_changed{,_dep}). As an |
| 521 | # optimization, we don't need to read them if the target does not |
| 522 | # exist, we will rebuild anyway in that case. |
| 523 | |
Ilias Apalodimas | 15d8201 | 2025-05-20 08:21:22 +0300 | [diff] [blame] | 524 | existing-targets := $(wildcard $(sort $(targets))) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 525 | |
Ilias Apalodimas | 15d8201 | 2025-05-20 08:21:22 +0300 | [diff] [blame] | 526 | -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 527 | |
Masahiro Yamada | 77ddac8 | 2020-04-17 16:21:36 +0900 | [diff] [blame] | 528 | # Create directories for object files if they do not exist |
| 529 | obj-dirs := $(sort $(obj) $(patsubst %/,%, $(dir $(targets)))) |
Ilias Apalodimas | 15d8201 | 2025-05-20 08:21:22 +0300 | [diff] [blame] | 530 | # If targets exist, their directories apparently exist. Skip mkdir. |
| 531 | existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets)))) |
| 532 | obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs))) |
| 533 | ifneq ($(obj-dirs),) |
Masahiro Yamada | 77ddac8 | 2020-04-17 16:21:36 +0900 | [diff] [blame] | 534 | $(shell mkdir -p $(obj-dirs)) |
Ilias Apalodimas | 15d8201 | 2025-05-20 08:21:22 +0300 | [diff] [blame] | 535 | endif |
Masahiro Yamada | 77ddac8 | 2020-04-17 16:21:36 +0900 | [diff] [blame] | 536 | |
Masahiro Yamada | 47b65f7 | 2014-02-04 17:24:27 +0900 | [diff] [blame] | 537 | .PHONY: $(PHONY) |