Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 1 | # ========================================================================== |
| 2 | # Building |
| 3 | # ========================================================================== |
| 4 | |
| 5 | src := $(obj) |
| 6 | |
| 7 | PHONY := __build |
| 8 | __build: |
| 9 | |
| 10 | # Read .config if it exist, otherwise ignore |
| 11 | -include .config |
| 12 | |
| 13 | include scripts/Kbuild.include |
| 14 | |
| 15 | # The filename Kbuild has precedence over Makefile |
Denys Vlasenko | 1883cb1 | 2010-07-09 01:25:36 +0200 | [diff] [blame] | 16 | # bbox: we also try to include Kbuild file in obj tree first |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 17 | kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) |
Denys Vlasenko | 1883cb1 | 2010-07-09 01:25:36 +0200 | [diff] [blame] | 18 | include $(if $(wildcard $(src)/Kbuild), $(src)/Kbuild, \ |
| 19 | $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, \ |
| 20 | $(kbuild-dir)/Makefile \ |
| 21 | ) \ |
| 22 | ) |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 23 | |
| 24 | include scripts/Makefile.lib |
| 25 | |
| 26 | ifdef host-progs |
| 27 | ifneq ($(hostprogs-y),$(host-progs)) |
| 28 | $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!) |
| 29 | hostprogs-y += $(host-progs) |
| 30 | endif |
| 31 | endif |
| 32 | |
| 33 | # Do not include host rules unles needed |
| 34 | ifneq ($(hostprogs-y)$(hostprogs-m),) |
| 35 | include scripts/Makefile.host |
| 36 | endif |
| 37 | |
| 38 | ifneq ($(KBUILD_SRC),) |
| 39 | # Create output directory if not already present |
| 40 | _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj)) |
| 41 | |
| 42 | # Create directories for object files if directory does not exist |
| 43 | # Needed when obj-y := dir/file.o syntax is used |
| 44 | _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d))) |
| 45 | endif |
| 46 | |
| 47 | |
| 48 | ifdef EXTRA_TARGETS |
| 49 | $(warning kbuild: $(obj)/Makefile - Usage of EXTRA_TARGETS is obsolete in 2.6. Please fix!) |
| 50 | endif |
| 51 | |
| 52 | ifdef build-targets |
| 53 | $(warning kbuild: $(obj)/Makefile - Usage of build-targets is obsolete in 2.6. Please fix!) |
| 54 | endif |
| 55 | |
| 56 | ifdef export-objs |
| 57 | $(warning kbuild: $(obj)/Makefile - Usage of export-objs is obsolete in 2.6. Please fix!) |
| 58 | endif |
| 59 | |
| 60 | ifdef O_TARGET |
| 61 | $(warning kbuild: $(obj)/Makefile - Usage of O_TARGET := $(O_TARGET) is obsolete in 2.6. Please fix!) |
| 62 | endif |
| 63 | |
| 64 | ifdef L_TARGET |
| 65 | $(error kbuild: $(obj)/Makefile - Use of L_TARGET is replaced by lib-y in 2.6. Please fix!) |
| 66 | endif |
| 67 | |
| 68 | ifdef list-multi |
| 69 | $(warning kbuild: $(obj)/Makefile - list-multi := $(list-multi) is obsolete in 2.6. Please fix!) |
| 70 | endif |
| 71 | |
| 72 | ifndef obj |
| 73 | $(warning kbuild: Makefile.build is included improperly) |
| 74 | endif |
| 75 | |
| 76 | # =========================================================================== |
| 77 | |
| 78 | ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),) |
| 79 | lib-target := $(obj)/lib.a |
| 80 | endif |
| 81 | |
| 82 | ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),) |
| 83 | builtin-target := $(obj)/built-in.o |
| 84 | endif |
| 85 | |
| 86 | # We keep a list of all modules in $(MODVERDIR) |
| 87 | |
| 88 | __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \ |
| 89 | $(if $(KBUILD_MODULES),$(obj-m)) \ |
| 90 | $(subdir-ym) $(always) |
| 91 | @: |
| 92 | |
| 93 | # Linus' kernel sanity checking tool |
| 94 | ifneq ($(KBUILD_CHECKSRC),0) |
| 95 | ifeq ($(KBUILD_CHECKSRC),2) |
| 96 | quiet_cmd_force_checksrc = CHECK $< |
| 97 | cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ; |
| 98 | else |
| 99 | quiet_cmd_checksrc = CHECK $< |
| 100 | cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ; |
| 101 | endif |
| 102 | endif |
| 103 | |
| 104 | |
| 105 | # Compile C sources (.c) |
| 106 | # --------------------------------------------------------------------------- |
| 107 | |
| 108 | # Default is built-in, unless we know otherwise |
| 109 | modkern_cflags := $(CFLAGS_KERNEL) |
| 110 | quiet_modtag := $(empty) $(empty) |
| 111 | |
| 112 | $(real-objs-m) : modkern_cflags := $(CFLAGS_MODULE) |
| 113 | $(real-objs-m:.o=.i) : modkern_cflags := $(CFLAGS_MODULE) |
| 114 | $(real-objs-m:.o=.s) : modkern_cflags := $(CFLAGS_MODULE) |
| 115 | $(real-objs-m:.o=.lst): modkern_cflags := $(CFLAGS_MODULE) |
| 116 | |
| 117 | $(real-objs-m) : quiet_modtag := [M] |
| 118 | $(real-objs-m:.o=.i) : quiet_modtag := [M] |
| 119 | $(real-objs-m:.o=.s) : quiet_modtag := [M] |
| 120 | $(real-objs-m:.o=.lst): quiet_modtag := [M] |
| 121 | |
| 122 | $(obj-m) : quiet_modtag := [M] |
| 123 | |
| 124 | # Default for not multi-part modules |
| 125 | modname = $(*F) |
| 126 | |
| 127 | $(multi-objs-m) : modname = $(modname-multi) |
| 128 | $(multi-objs-m:.o=.i) : modname = $(modname-multi) |
| 129 | $(multi-objs-m:.o=.s) : modname = $(modname-multi) |
| 130 | $(multi-objs-m:.o=.lst) : modname = $(modname-multi) |
| 131 | $(multi-objs-y) : modname = $(modname-multi) |
| 132 | $(multi-objs-y:.o=.i) : modname = $(modname-multi) |
| 133 | $(multi-objs-y:.o=.s) : modname = $(modname-multi) |
| 134 | $(multi-objs-y:.o=.lst) : modname = $(modname-multi) |
| 135 | |
| 136 | quiet_cmd_cc_s_c = CC $(quiet_modtag) $@ |
| 137 | cmd_cc_s_c = $(CC) $(c_flags) -fverbose-asm -S -o $@ $< |
| 138 | |
| 139 | %.s: %.c FORCE |
| 140 | $(call if_changed_dep,cc_s_c) |
| 141 | |
| 142 | quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@ |
| 143 | cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $< |
| 144 | |
| 145 | %.i: %.c FORCE |
| 146 | $(call if_changed_dep,cc_i_c) |
| 147 | |
| 148 | # C (.c) files |
| 149 | # The C file is compiled and updated dependency information is generated. |
| 150 | # (See cmd_cc_o_c + relevant part of rule_cc_o_c) |
| 151 | |
| 152 | quiet_cmd_cc_o_c = CC $(quiet_modtag) $@ |
| 153 | |
| 154 | ifndef CONFIG_MODVERSIONS |
| 155 | cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< |
| 156 | |
| 157 | else |
| 158 | # When module versioning is enabled the following steps are executed: |
| 159 | # o compile a .tmp_<file>.o from <file>.c |
| 160 | # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does |
| 161 | # not export symbols, we just rename .tmp_<file>.o to <file>.o and |
| 162 | # are done. |
| 163 | # o otherwise, we calculate symbol versions using the good old |
| 164 | # genksyms on the preprocessed source and postprocess them in a way |
| 165 | # that they are usable as a linker script |
| 166 | # o generate <file>.o from .tmp_<file>.o using the linker to |
| 167 | # replace the unresolved symbols __crc_exported_symbol with |
| 168 | # the actual value of the checksum generated by genksyms |
| 169 | |
| 170 | cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $< |
| 171 | cmd_modversions = \ |
| 172 | if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \ |
| 173 | $(CPP) -D__GENKSYMS__ $(c_flags) $< \ |
| 174 | | $(GENKSYMS) -a $(ARCH) \ |
| 175 | > $(@D)/.tmp_$(@F:.o=.ver); \ |
| 176 | \ |
| 177 | $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \ |
| 178 | -T $(@D)/.tmp_$(@F:.o=.ver); \ |
| 179 | rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \ |
| 180 | else \ |
| 181 | mv -f $(@D)/.tmp_$(@F) $@; \ |
| 182 | fi; |
| 183 | endif |
| 184 | |
| 185 | define rule_cc_o_c |
| 186 | $(call echo-cmd,checksrc) $(cmd_checksrc) \ |
| 187 | $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \ |
| 188 | $(cmd_modversions) \ |
| 189 | scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > $(@D)/.$(@F).tmp; \ |
| 190 | rm -f $(depfile); \ |
| 191 | mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd |
| 192 | endef |
| 193 | |
| 194 | # Built-in and composite module parts |
| 195 | |
| 196 | %.o: %.c FORCE |
| 197 | $(call cmd,force_checksrc) |
| 198 | $(call if_changed_rule,cc_o_c) |
| 199 | |
| 200 | # Single-part modules are special since we need to mark them in $(MODVERDIR) |
| 201 | |
| 202 | $(single-used-m): %.o: %.c FORCE |
| 203 | $(call cmd,force_checksrc) |
| 204 | $(call if_changed_rule,cc_o_c) |
| 205 | @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod) |
| 206 | |
| 207 | quiet_cmd_cc_lst_c = MKLST $@ |
| 208 | cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \ |
| 209 | $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \ |
| 210 | System.map $(OBJDUMP) > $@ |
| 211 | |
| 212 | %.lst: %.c FORCE |
| 213 | $(call if_changed_dep,cc_lst_c) |
| 214 | |
| 215 | # Compile assembler sources (.S) |
| 216 | # --------------------------------------------------------------------------- |
| 217 | |
| 218 | modkern_aflags := $(AFLAGS_KERNEL) |
| 219 | |
| 220 | $(real-objs-m) : modkern_aflags := $(AFLAGS_MODULE) |
| 221 | $(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE) |
| 222 | |
| 223 | quiet_cmd_as_s_S = CPP $(quiet_modtag) $@ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 224 | cmd_as_s_S = $(CPP) $(a_flags) -o $@ $< |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 225 | |
| 226 | %.s: %.S FORCE |
| 227 | $(call if_changed_dep,as_s_S) |
| 228 | |
| 229 | quiet_cmd_as_o_S = AS $(quiet_modtag) $@ |
| 230 | cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< |
| 231 | |
| 232 | %.o: %.S FORCE |
| 233 | $(call if_changed_dep,as_o_S) |
| 234 | |
| 235 | targets += $(real-objs-y) $(real-objs-m) $(lib-y) |
| 236 | targets += $(extra-y) $(MAKECMDGOALS) $(always) |
| 237 | |
| 238 | # Linker scripts preprocessor (.lds.S -> .lds) |
| 239 | # --------------------------------------------------------------------------- |
| 240 | quiet_cmd_cpp_lds_S = LDS $@ |
| 241 | cmd_cpp_lds_S = $(CPP) $(cpp_flags) -D__ASSEMBLY__ -o $@ $< |
| 242 | |
| 243 | %.lds: %.lds.S FORCE |
| 244 | $(call if_changed_dep,cpp_lds_S) |
| 245 | |
| 246 | # Build the compiled-in targets |
| 247 | # --------------------------------------------------------------------------- |
| 248 | |
| 249 | # To build objects in subdirs, we need to descend into the directories |
| 250 | $(sort $(subdir-obj-y)): $(subdir-ym) ; |
| 251 | |
| 252 | # |
| 253 | # Rule to compile a set of .o files into one .o file |
| 254 | # |
| 255 | ifdef builtin-target |
| 256 | quiet_cmd_link_o_target = LD $@ |
| 257 | # If the list of objects to link is empty, just create an empty built-in.o |
| 258 | cmd_link_o_target = $(if $(strip $(obj-y)),\ |
Denis Vlasenko | 1da86d2 | 2008-06-04 11:28:24 +0000 | [diff] [blame] | 259 | $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\ |
| 260 | rm -f $@; $(AR) rcs $@) |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 261 | |
| 262 | $(builtin-target): $(obj-y) FORCE |
| 263 | $(call if_changed,link_o_target) |
| 264 | |
| 265 | targets += $(builtin-target) |
| 266 | endif # builtin-target |
| 267 | |
| 268 | # |
| 269 | # Rule to compile a set of .o files into one .a file |
| 270 | # |
| 271 | ifdef lib-target |
| 272 | quiet_cmd_link_l_target = AR $@ |
| 273 | cmd_link_l_target = rm -f $@; $(AR) $(EXTRA_ARFLAGS) rcs $@ $(lib-y) |
| 274 | |
| 275 | $(lib-target): $(lib-y) FORCE |
| 276 | $(call if_changed,link_l_target) |
| 277 | |
| 278 | targets += $(lib-target) |
| 279 | endif |
| 280 | |
| 281 | # |
| 282 | # Rule to link composite objects |
| 283 | # |
| 284 | # Composite objects are specified in kbuild makefile as follows: |
| 285 | # <composite-object>-objs := <list of .o files> |
| 286 | # or |
| 287 | # <composite-object>-y := <list of .o files> |
| 288 | link_multi_deps = \ |
| 289 | $(filter $(addprefix $(obj)/, \ |
| 290 | $($(subst $(obj)/,,$(@:.o=-objs))) \ |
| 291 | $($(subst $(obj)/,,$(@:.o=-y)))), $^) |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 293 | quiet_cmd_link_multi-y = LD $@ |
| 294 | cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) |
| 295 | |
| 296 | quiet_cmd_link_multi-m = LD [M] $@ |
| 297 | cmd_link_multi-m = $(LD) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps) |
| 298 | |
| 299 | # We would rather have a list of rules like |
| 300 | # foo.o: $(foo-objs) |
| 301 | # but that's not so easy, so we rather make all composite objects depend |
| 302 | # on the set of all their parts |
| 303 | $(multi-used-y) : %.o: $(multi-objs-y) FORCE |
| 304 | $(call if_changed,link_multi-y) |
| 305 | |
| 306 | $(multi-used-m) : %.o: $(multi-objs-m) FORCE |
| 307 | $(call if_changed,link_multi-m) |
| 308 | @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod) |
| 309 | |
| 310 | targets += $(multi-used-y) $(multi-used-m) |
| 311 | |
| 312 | |
| 313 | # Descending |
| 314 | # --------------------------------------------------------------------------- |
| 315 | |
| 316 | PHONY += $(subdir-ym) |
| 317 | $(subdir-ym): |
| 318 | $(Q)$(MAKE) $(build)=$@ |
| 319 | |
| 320 | # Add FORCE to the prequisites of a target to force it to be always rebuilt. |
| 321 | # --------------------------------------------------------------------------- |
| 322 | |
| 323 | PHONY += FORCE |
| 324 | |
| 325 | FORCE: |
| 326 | |
| 327 | # Read all saved command lines and dependencies for the $(targets) we |
| 328 | # may be building above, using $(if_changed{,_dep}). As an |
| 329 | # optimization, we don't need to read them if the target does not |
| 330 | # exist, we will rebuild anyway in that case. |
| 331 | |
| 332 | targets := $(wildcard $(sort $(targets))) |
| 333 | cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) |
| 334 | |
| 335 | ifneq ($(cmd_files),) |
| 336 | include $(cmd_files) |
| 337 | endif |
| 338 | |
| 339 | |
| 340 | # Declare the contents of the .PHONY variable as phony. We keep that |
| 341 | # information in a variable se we can use it in if_changed and friends. |
| 342 | |
| 343 | .PHONY: $(PHONY) |