4 # Lots of this code have been borrowed or heavily inspired from parts
5 # of kbuild code, which is not credited, but mostly developed by:
7 # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
8 # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
22 ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4
23 ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
27 ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
32 build-dir := $(srctree)/tools/build
34 # Define $(fixdep) for dep-cmd function
36 fixdep := $(build-dir)/fixdep
38 fixdep := $(OUTPUT)/fixdep
42 include $(build-dir)/Build.include
44 # do not force detected configuration
45 -include $(OUTPUT).config-detected
47 # Init all relevant variables used in build files so
48 # 1) they have correct type
49 # 2) they do not inherit any value from the environment
56 build-file := $(dir)/Build
57 -include $(build-file)
59 quiet_cmd_flex = FLEX $@
60 quiet_cmd_bison = BISON $@
62 # Create directory unless it exists
63 quiet_cmd_mkdir = MKDIR $(dir $@)
64 cmd_mkdir = mkdir -p $(dir $@)
65 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
68 quiet_cmd_cc_o_c = CC $@
69 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
71 quiet_cmd_host_cc_o_c = HOSTCC $@
72 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
74 quiet_cmd_cxx_o_c = CXX $@
75 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
77 quiet_cmd_cpp_i_c = CPP $@
78 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
80 quiet_cmd_cc_s_c = AS $@
81 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
83 quiet_cmd_gen = GEN $@
85 # Link agregate command
86 # If there's nothing to link, create empty $@ object.
87 quiet_cmd_ld_multi = LD $@
88 cmd_ld_multi = $(if $(strip $(obj-y)),\
89 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
91 quiet_cmd_host_ld_multi = HOSTLD $@
92 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
93 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
95 ifneq ($(filter $(obj),$(hostprogs)),)
100 $(OUTPUT)%.o: %.c FORCE
102 $(call if_changed_dep,$(host)cc_o_c)
104 $(OUTPUT)%.o: %.cpp FORCE
106 $(call if_changed_dep,cxx_o_c)
108 $(OUTPUT)%.o: %.S FORCE
110 $(call if_changed_dep,$(host)cc_o_c)
112 $(OUTPUT)%.i: %.c FORCE
114 $(call if_changed_dep,cpp_i_c)
116 $(OUTPUT)%.s: %.S FORCE
118 $(call if_changed_dep,cpp_i_c)
120 $(OUTPUT)%.s: %.c FORCE
122 $(call if_changed_dep,cc_s_c)
125 # obj-y - list of build objects
126 # subdir-y - list of directories to nest
127 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
129 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
130 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
131 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
133 # '$(OUTPUT)/dir' prefix to all objects
134 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
135 obj-y := $(addprefix $(objprefix),$(obj-y))
136 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
138 # Final '$(obj)-in.o' object
139 in-target := $(objprefix)$(obj)-in.o
144 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
146 $(sort $(subdir-obj-y)): $(subdir-y) ;
148 $(in-target): $(obj-y) FORCE
150 $(call if_changed,$(host)ld_multi)
152 __build: $(in-target)
158 # Include all cmd files to get all the dependency rules
159 # for all objects included
160 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
161 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
163 ifneq ($(cmd_files),)