1 # SPDX-License-Identifier: GPL-2.0
5 # Lots of this code have been borrowed or heavily inspired from parts
6 # of kbuild code, which is not credited, but mostly developed by:
8 # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
9 # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
23 # If the user is running make -s (silent mode), suppress echoing of commands
24 # make-4.0 (and later) keep single letter options in the 1st word of MAKEFLAGS.
25 ifeq ($(filter 3.%,$(MAKE_VERSION)),)
26 short-opts := $(firstword -$(MAKEFLAGS))
28 short-opts := $(filter-out --%,$(MAKEFLAGS))
31 ifneq ($(findstring s,$(short-opts)),)
35 build-dir := $(srctree)/tools/build
37 # Define $(fixdep) for dep-cmd function
39 fixdep := $(build-dir)/fixdep
41 fixdep := $(OUTPUT)/fixdep
45 include $(build-dir)/Build.include
47 # do not force detected configuration
48 -include $(OUTPUT).config-detected
50 # Init all relevant variables used in build files so
51 # 1) they have correct type
52 # 2) they do not inherit any value from the environment
59 build-file := $(dir)/Build
60 -include $(build-file)
62 quiet_cmd_flex = FLEX $@
63 quiet_cmd_bison = BISON $@
64 quiet_cmd_test = TEST $@
66 # Create directory unless it exists
67 quiet_cmd_mkdir = MKDIR $(dir $@)
68 cmd_mkdir = mkdir -p $(dir $@)
69 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
72 quiet_cmd_cc_o_c = CC $@
73 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
75 quiet_cmd_host_cc_o_c = HOSTCC $@
76 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
78 quiet_cmd_cxx_o_c = CXX $@
79 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
81 quiet_cmd_cpp_i_c = CPP $@
82 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
84 quiet_cmd_cc_s_c = AS $@
85 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
87 quiet_cmd_gen = GEN $@
89 # Link agregate command
90 # If there's nothing to link, create empty $@ object.
91 quiet_cmd_ld_multi = LD $@
92 cmd_ld_multi = $(if $(strip $(obj-y)),\
93 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
95 quiet_cmd_host_ld_multi = HOSTLD $@
96 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
97 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
99 ifneq ($(filter $(obj),$(hostprogs)),)
104 $(OUTPUT)%.o: %.c FORCE
106 $(call if_changed_dep,$(host)cc_o_c)
108 $(OUTPUT)%.o: %.cpp FORCE
110 $(call if_changed_dep,cxx_o_c)
112 $(OUTPUT)%.o: %.S FORCE
114 $(call if_changed_dep,$(host)cc_o_c)
116 $(OUTPUT)%.i: %.c FORCE
118 $(call if_changed_dep,cpp_i_c)
120 $(OUTPUT)%.s: %.S FORCE
122 $(call if_changed_dep,cpp_i_c)
124 $(OUTPUT)%.s: %.c FORCE
126 $(call if_changed_dep,cc_s_c)
128 # bison and flex files are generated in the OUTPUT directory
129 # so it needs a separate rule to depend on them properly
130 $(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
132 $(call if_changed_dep,$(host)cc_o_c)
134 $(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
136 $(call if_changed_dep,$(host)cc_o_c)
139 # obj-y - list of build objects
140 # subdir-y - list of directories to nest
141 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
143 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
144 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
145 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
147 # '$(OUTPUT)/dir' prefix to all objects
148 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
149 obj-y := $(addprefix $(objprefix),$(obj-y))
150 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
152 # Final '$(obj)-in.o' object
153 in-target := $(objprefix)$(obj)-in.o
158 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
160 $(sort $(subdir-obj-y)): $(subdir-y) ;
162 $(in-target): $(obj-y) FORCE
164 $(call if_changed,$(host)ld_multi)
166 __build: $(in-target)
172 # Include all cmd files to get all the dependency rules
173 # for all objects included
174 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
175 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
177 ifneq ($(cmd_files),)