1cur_makefile := $(lastword $(MAKEFILE_LIST))
2
3# ==========================================================================
4# Building
5# ==========================================================================
6
7src := $(obj)
8
9PHONY := __build
10__build:
11
12$(cur_makefile): ;
13
14# Init all relevant variables used in kbuild files so
15# 1) they have correct type
16# 2) they do not inherit any value from the environment
17obj-y :=
18lib-y :=
19subdir-y :=
20archive-custom :=
21archive-custom-valid :=
22always :=
23targets :=
24EXTRA_AFLAGS   :=
25EXTRA_CFLAGS   :=
26EXTRA_CPPFLAGS :=
27EXTRA_LDFLAGS  :=
28asflags-y  :=
29ccflags-y  :=
30cppflags-y :=
31ldflags-y  :=
32
33subdir-asflags-y :=
34subdir-ccflags-y :=
35
36include $(srctree)/scripts/include.mk
37
38# For backward compatibility check that these variables do not change
39save-cflags := $(CFLAGS)
40
41kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
42kbuild-file := $(kbuild-dir)/Makefile
43include $(kbuild-file)
44
45$(kbuild-file): ;
46
47# If the save-* variables changed error out
48ifeq ($(KBUILD_NOPEDANTIC),)
49        ifneq ("$(save-cflags)","$(CFLAGS)")
50                $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use ccflags-y)
51        endif
52endif
53
54include $(srctree)/scripts/lib.mk
55
56ifeq ($(WIN_PLAT),y)
57create_dir = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1))
58else
59create_dir = [ -d $(1) ] || mkdir -p $(1)
60endif
61
62ifneq ($(KBUILD_SRC),)
63# Create output directory if not already present
64_dummy := $(shell $(call create_dir,$(obj)))
65
66# Create directories for object files if directory does not exist
67# Needed when obj-y := dir/file.o syntax is used
68_dummy := $(foreach d,$(obj-dirs), $(shell $(call create_dir,$(d))))
69endif
70
71ifndef obj
72$(warning kbuild: build.mk is included improperly)
73endif
74
75# ===========================================================================
76
77ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
78lib-target := $(obj)/lib.a
79endif
80
81ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
82builtin-target := $(obj)/built-in$(built_in_suffix)
83endif
84
85submodgoals =
86SUBMODS := $(filter $(obj)/%,$(SUBMODS))
87
88ifneq ($(SUBMODS),)
89
90lst_target := $(filter %.s %.i %.lst, $(SUBMODS))
91ifeq ($(lib-target),)
92ifneq ($(filter $(obj)/lib.lst,$(lst_target)),)
93$(error Cannot make lib.lst: lib-target not exists)
94endif
95endif
96ifeq ($(builtin-target),)
97ifneq ($(filter $(obj)/built-in.lst,$(lst_target)),)
98$(error Cannot make built-in.lst: builtin-target not exists)
99endif
100endif
101
102lst_target_obj := $(lst_target)
103lst_target_obj := $(filter-out $(obj)/built-in.lst $(obj)/lib.lst,$(lst_target_obj))
104lst_target_obj := $(lst_target_obj:.s=.o)
105lst_target_obj := $(lst_target_obj:.i=.o)
106lst_target_obj := $(lst_target_obj:.lst=.o)
107lst_target_obj := $(sort $(lst_target_obj))
108
109cur_submods := $(filter-out $(lst_target),$(SUBMODS))
110cur_submods += $(lst_target_obj)
111
112include scripts/submods.mk
113
114buildextra-y := $(filter $(cur_submods),$(extra-y))
115
116ifeq ($(filter $(obj)/built-in.lst,$(lst_target)),)
117
118# Just compile some of the object files
119buildsubdir-y := $(call get_subdirs,$(subdir-y),$(cur_submods))
120buildobj-y := $(filter $(cur_submods) $(addsuffix /built-in$(built_in_suffix),$(buildsubdir-y)),$(obj-y))
121buildobj-y += $(foreach m,$(multi-used-y),$(if $(filter $(cur_submods),$(call get_multi_objs,$m))),$m,)
122
123else
124
125# Compile all the object files
126buildsubdir-y := $(subdir-y)
127buildobj-y := $(obj-y)
128
129endif
130
131ifeq ($(filter $(obj)/lib.lst,$(lst_target)),)
132# Just compile some of the lib files
133buildlib-y := $(filter $(cur_submods),$(lib-y))
134else
135# Compile all the lib files
136buildlib-y := $(lib-y)
137endif
138
139# Check for invalid targets
140errobj-y := $(cur_submods)
141errobj-y := $(filter-out $(buildsubdir-y) $(addsuffix /%,$(buildsubdir-y)),$(errobj-y))
142errobj-y := $(filter-out $(buildextra-y) $(buildobj-y) $(buildlib-y),$(errobj-y))
143errobj-y := $(filter-out $(foreach m,$(multi-used-y),$(call get_multi_objs,$m)),$(errobj-y))
144ifneq ($(errobj-y),)
145$(warning Unknown targets in [$(obj)]: $(errobj-y))
146endif
147
148# Update lst targets
149all_buildobj-y := $(buildextra-y) $(buildobj-y) $(filter-out %.a,$(builtin-target))
150all_buildlib-y := $(filter %.a,$(builtin-target)) $(lib-target)
151lst_target := $(filter $(lst_target), \
152		$(all_buildobj-y:.o=.s) \
153		$(all_buildobj-y:.o=.i) \
154		$(all_buildobj-y:.o=.lst) \
155		$(patsubst %.a,%.lst,$(all_buildlib-y)))
156
157# Avoid .o compile error affecting .i generation
158lst_i_target := $(filter %.i, $(lst_target))
159ifneq ($(lst_i_target),)
160$(lst_i_target:.i=.o) : | $(lst_i_target)
161endif
162
163ifeq ($(COMPILE_ONLY),1)
164submodgoals = $(call get_submodgoals,$@,$(SUBMODS))
165endif
166
167else # SUBMODS
168
169buildsubdir-y := $(subdir-y)
170buildextra-y := $(extra-y)
171buildobj-y := $(obj-y)
172buildlib-y := $(lib-y)
173
174endif # SUBMODS
175
176archive-src-target :=
177archive-custom-target :=
178ifeq ($(GEN_LIB),1)
179archive-src-target := $(addprefix $(srctree)/$(src)/lib/,$(notdir $(archive-src-y)))
180archive-src-target += $(archive-custom-valid)
181archive-src-target := $(strip $(archive-src-target))
182archive-custom-target := $(addprefix $(srctree)/$(src)/lib/,$(notdir $(archive-custom-valid)))
183endif
184
185__build: $(builtin-target) $(lib-target) $(buildextra-y) $(buildsubdir-y) $(lst_target) $(archive-src-target) $(archive-custom-target) $(always)
186	@:
187
188# Compile C sources (.c)
189# ---------------------------------------------------------------------------
190
191# Default is built-in, unless we know otherwise
192quiet_modtag := $(empty)   $(empty)
193
194quiet_cmd_cc_dummy = CC $(quiet_modtag)  $@
195cmd_cc_dummy       = $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -c -x c -o $@ $(devnull)
196
197# For dummy object files. Recipe is: $(call if_changed,cc_dummy)
198
199quiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
200cmd_cc_s_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
201
202$(obj)/%.s: $(src)/%.c FORCE
203	$(call if_changed_dep,cc_s_c)
204
205quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
206cmd_cc_i_c       = $(CPP) $(c_flags) -o $@ $<
207
208$(obj)/%.i: $(src)/%.c FORCE
209	$(call if_changed_dep,cc_i_c)
210
211# C (.c) files
212# The C file is compiled and updated dependency information is generated.
213# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
214
215quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
216cmd_cc_o_c       = $(CC) $(c_flags) -c -o $@ $<
217
218define rule_cc_o_c
219	$(call echo-cmd,cc_o_c) $(cmd_cc_o_c)
220endef
221
222# Built-in and composite module parts
223$(obj)/%.o: $(src)/%.c FORCE
224	$(call if_changed_rule,cc_o_c)
225
226# Compile C++ sources (.cpp)
227# ---------------------------------------------------------------------------
228
229quiet_cmd_cc_s_c++ = C++ $(quiet_modtag) $@
230cmd_cc_s_c++       = $(CC) $(c++_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
231
232$(obj)/%.s: $(src)/%.cpp FORCE
233	$(call if_changed_dep,cc_s_c++)
234
235$(obj)/%.s: $(src)/%.cc FORCE
236	$(call if_changed_dep,cc_s_c++)
237
238quiet_cmd_cc_i_c++ = CPP $(quiet_modtag) $@
239cmd_cc_i_c++       = $(CPP) $(c++_flags) -o $@ $<
240
241$(obj)/%.i: $(src)/%.cpp FORCE
242	$(call if_changed_dep,cc_i_c++)
243
244$(obj)/%.i: $(src)/%.cc FORCE
245	$(call if_changed_dep,cc_i_c++)
246
247# C++ (.cpp) files
248# The C++ file is compiled and updated dependency information is generated.
249# (See cmd_cc_o_c++ + relevant part of rule_cc_o_c)
250
251quiet_cmd_cc_o_c++ = C++ $(quiet_modtag) $@
252cmd_cc_o_c++       = $(C++) $(c++_flags) -c -o $@ $<
253
254define rule_cc_o_c++
255	$(call echo-cmd,cc_o_c++) $(cmd_cc_o_c++)
256endef
257
258# Built-in and composite module parts
259$(obj)/%.o: $(src)/%.cpp FORCE
260	$(call if_changed_rule,cc_o_c++)
261
262$(obj)/%.o: $(src)/%.cc FORCE
263	$(call if_changed_rule,cc_o_c++)
264
265# Compile assembler sources (.S)
266# ---------------------------------------------------------------------------
267
268quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
269cmd_as_s_S       = $(CPP) $(a_cpp_flags) -o $@ $<
270
271$(obj)/%.s: $(src)/%.S FORCE
272	$(call if_changed_dep,as_s_S)
273
274quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
275cmd_as_o_S       = $(CC) $(a_flags) -c -o $@ $<
276
277$(obj)/%.o: $(src)/%.S FORCE
278	$(call if_changed_dep,as_o_S)
279
280targets += $(real-objs-y) $(real-objs-m) $(lib-y) $(lst_target)
281targets += $(MAKECMDGOALS) $(always)
282
283# Common list command
284# ---------------------------------------------------------------------------
285
286quiet_cmd_dump_lst_o = MKLST   $@
287ifeq ($(TOOLCHAIN),armclang)
288      cmd_dump_lst_o = $(OBJDUMP) --datasymbols --text -c -d --interleave=source --output=$@ $<
289else
290      cmd_dump_lst_o = $(OBJDUMP) -Sdlxr $< > $@
291endif
292
293lst-cmd = @$(call echo-cmd,dump_lst_o) $(cmd_dump_lst_o)
294
295$(obj)/%.lst: $(obj)/%.o
296	$(call lst-cmd)
297
298# Build the compiled-in targets
299# ---------------------------------------------------------------------------
300
301# To build objects in subdirs, we need to descend into the directories
302$(sort $(subdir-obj-y)): $(buildsubdir-y) ;
303
304# Archive command
305
306ifeq ($(TOOLCHAIN),armclang)
307archive-cmd = $(AR) --create --debug_symbols $@ $(1)
308else
309ifeq ($(WIN_PLAT),y)
310archive-cmd = ( ( echo create $@ && \
311  echo addmod $(subst $(space),$(comma),$(strip $(filter-out %.a,$(1)))) && \
312  $(foreach o,$(filter %.a,$(1)),echo addlib $o && ) \
313  echo save && \
314  echo end ) | $(AR) -M )
315else
316# Command "/bin/echo -e" cannot work on Apple Mac machines, so we use "/usr/bin/printf" instead
317archive-cmd = ( /usr/bin/printf 'create $@\n\
318  addmod $(subst $(space),$(comma),$(strip $(filter-out %.a,$(1))))\n\
319  $(foreach o,$(filter %.a,$(1)),addlib $o\n)save\nend' | $(AR) -M )
320endif
321endif
322
323# Archive check
324
325ifeq ($(CHECK_LIB_SRC),1)
326ifneq ($(archive-bin-y),)
327$(info )
328$(info Library source check OK: $(archive-bin-y))
329$(info )
330endif
331
332ifneq ($(archive-src-y),)
333$(info )
334$(error Error: The source files exist for libraries: $(archive-src-y))
335endif
336endif
337
338# Archive without source files
339
340      cmd_use_lib_file = $(call CMDCPFILE,$<,$@)
341quiet_cmd_use_lib_file = USELIB  $(@)
342
343ifneq ($(archive-bin-y),)
344
345ifeq ($(GEN_LIB),1)
346$(info )
347$(warning WARNING: No source files found for libraries: $(archive-bin-y))
348$(info )
349endif
350
351$(archive-bin-y): $(obj)/%: $(srctree)/$(src)/lib/% FORCE
352ifeq ($(FORCE_TO_USE_LIB),1)
353	$(call cmd,use_lib_file)
354else
355	$(call if_changed,use_lib_file)
356endif
357
358targets += $(archive-bin-y)
359
360endif # archive-bin-y
361
362# Archive generation (with source files)
363
364ifeq ($(GEN_LIB),1)
365ifeq ($(FORCE_TO_USE_LIB),1)
366$(error Conflicted options: GEN_LIB and FORCE_TO_USE_LIB)
367endif
368ifneq ($(archive-src-target),)
369
370      cmd_gen_lib_file = ( $(call create_dir,$(dir $(@))) ) && $(call archive-cmd,$(filter-out $(PHONY),$^))
371quiet_cmd_gen_lib_file = GENLIB  $(@)
372
373define archive-src-target-rule
374$(1): $(addprefix $(obj)/,$(patsubst %/,%/$(notdir $(builtin-target)),$($(notdir $(1:.a=-y)))))
375endef
376
377$(foreach m, $(archive-src-target), $(eval $(call archive-src-target-rule,$(m))))
378
379$(archive-src-target): FORCE
380ifeq ($(CHECK_GEN_LIB_DEP),1)
381	$(call if_changed,gen_lib_file)
382else
383# Always pack lib
384	$(call cmd,gen_lib_file)
385endif
386
387targets += $(archive-src-target)
388
389endif # archive-src-y
390endif # GEN_LIB
391
392#
393# Rule to compile a set of .o files into one .o/.a built-in file
394#
395ifdef builtin-target
396
397ifeq ($(BUILT-IN-OBJ),1)
398
399quiet_cmd_link_o_target = LD      $@
400# If the list of objects to link is empty, just create an empty built-in.o
401ifeq ($(TOOLCHAIN),armclang)
402      cmd_link_o_target = $(if $(strip $(obj-y)),\
403              $(LD) -Wl$(comma)$(subst $(space),$(comma),$(strip $(ld_flags) --partial)) -nostdlib $(LINK_CFLAGS) -o $@ $(obj-y), \
404              $(call CMDRMFILE,$@) && $(AR) --create --debug_symbols $@)
405else
406      cmd_link_o_target = $(if $(strip $(obj-y)),\
407              $(LD) -Wl$(comma)$(subst $(space),$(comma),$(strip $(ld_flags) -r)) -nostdlib -nostartfiles $(LINK_CFLAGS) -o $@ $(obj-y), \
408              $(call CMDRMFILE,$@) && $(AR) rcs$(KBUILD_ARFLAGS) $@)
409endif
410
411$(builtin-target): $(buildobj-y) FORCE
412	$(call if_changed,link_o_target)
413
414else # BUILT-IN-OBJ
415
416quiet_cmd_ar_o_target = AR      $@
417cmd_ar_o_target = $(call archive-cmd,$(obj-y))
418
419$(builtin-target): $(buildobj-y) FORCE
420	$(call if_changed,ar_o_target)
421
422endif # BUILT-IN-OBJ
423
424targets += $(builtin-target)
425
426$(obj)/built-in.lst: $(builtin-target)
427	$(call lst-cmd)
428
429endif # builtin-target
430
431#
432# Rule to compile a set of .o files into one .a file
433#
434ifdef lib-target
435
436quiet_cmd_link_l_target = AR      $@
437ifeq ($(TOOLCHAIN),armclang)
438      cmd_link_l_target = $(call CMDRMFILE,$@) && $(AR) --create --debug_symbols $@ $(lib-y)
439else
440      cmd_link_l_target = $(call CMDRMFILE,$@) && $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
441endif
442
443$(lib-target): $(buildlib-y) FORCE
444	$(call if_changed,link_l_target)
445
446targets += $(lib-target)
447
448$(obj)/lib.lst: $(lib-target)
449	$(call lst-cmd)
450
451endif
452
453#
454# Rule to link composite objects
455#
456#  Composite objects are specified in kbuild makefile as follows:
457#    <composite-object>-objs := <list of .o files>
458#  or
459#    <composite-object>-y    := <list of .o files>
460link_multi_deps =                     \
461$(filter $(addprefix $(obj)/,         \
462$($(subst $(obj)/,,$(@:.o=-objs)))    \
463$($(subst $(obj)/,,$(@:.o=-y)))), $^)
464
465quiet_cmd_link_multi-y = LD      $@
466ifeq ($(TOOLCHAIN),armclang)
467      cmd_link_multi-y = $(LD) -Wl,$(subst $(space),$(comma),$(strip $(ld_flags) --partial)) -nostdlib $(LINK_CFLAGS) -o $@ $(link_multi_deps)
468else
469      cmd_link_multi-y = $(LD) -Wl,$(subst $(space),$(comma),$(strip $(ld_flags) -r --whole-archive)) -nostdlib -nostartfiles $(LINK_CFLAGS) -o $@ $(link_multi_deps)
470endif
471
472$(multi-used-y): FORCE
473	$(call if_changed,link_multi-y)
474$(call multi_depend, $(multi-used-y), .o, -objs -y)
475
476targets += $(multi-used-y)
477
478$(multi-used-y:.o=.lst): %.lst : %.o
479	$(call lst-cmd)
480
481# Descending
482# ---------------------------------------------------------------------------
483
484PHONY += $(subdir-y)
485
486$(subdir-y):
487	$(Q)$(MAKE) $(build)=$@ $(submodgoals)
488
489# Add FORCE to the prequisites of a target to force it to be always rebuilt.
490# ---------------------------------------------------------------------------
491
492PHONY += FORCE
493
494FORCE:
495
496# Read all saved command lines and dependencies for the $(targets) we
497# may be building above, using $(if_changed{,_dep}). As an
498# optimization, we don't need to read them if the target does not
499# exist, we will rebuild anyway in that case.
500
501targets := $(wildcard $(sort $(targets)))
502cmd_files := $(wildcard $(foreach f,$(targets),$(call get_depfile_name,$(f))))
503
504ifneq ($(cmd_files),)
505include $(cmd_files)
506$(cmd_files): ;
507endif
508
509# Declare the contents of the .PHONY variable as phony.  We keep that
510# information in a variable se we can use it in if_changed and friends.
511
512.PHONY: $(PHONY)
513
514# Last-resort default rule for unknown targets
515
516%::
517	@$(call echo-help,*** Error: No rule is defined for target: $@)
518	@exit 1
519
520