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 build-dir := $(srctree)/tools/build
24 # Define $(fixdep) for dep-cmd function
26 fixdep := $(build-dir)/fixdep
28 fixdep := $(OUTPUT)/fixdep
32 include $(build-dir)/Build.include
34 # do not force detected configuration
35 -include $(OUTPUT).config-detected
37 # Init all relevant variables used in build files so
38 # 1) they have correct type
39 # 2) they do not inherit any value from the environment
46 build-file := $(dir)/Build
47 -include $(build-file)
49 quiet_cmd_flex = FLEX $@
50 quiet_cmd_bison = BISON $@
52 # Create directory unless it exists
53 quiet_cmd_mkdir = MKDIR $(dir $@)
54 cmd_mkdir = mkdir -p $(dir $@)
55 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
58 quiet_cmd_cc_o_c = CC $@
59 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
61 quiet_cmd_host_cc_o_c = HOSTCC $@
62 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
64 quiet_cmd_cxx_o_c = CXX $@
65 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
67 quiet_cmd_cpp_i_c = CPP $@
68 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
70 quiet_cmd_cc_s_c = AS $@
71 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
73 quiet_cmd_gen = GEN $@
75 # Link agregate command
76 # If there's nothing to link, create empty $@ object.
77 quiet_cmd_ld_multi = LD $@
78 cmd_ld_multi = $(if $(strip $(obj-y)),\
79 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
81 quiet_cmd_host_ld_multi = HOSTLD $@
82 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
83 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
85 ifneq ($(filter $(obj),$(hostprogs)),)
90 $(OUTPUT)%.o: %.c FORCE
92 $(call if_changed_dep,$(host)cc_o_c)
94 $(OUTPUT)%.o: %.cpp FORCE
96 $(call if_changed_dep,cxx_o_c)
98 $(OUTPUT)%.o: %.S FORCE
100 $(call if_changed_dep,$(host)cc_o_c)
102 $(OUTPUT)%.i: %.c FORCE
104 $(call if_changed_dep,cpp_i_c)
106 $(OUTPUT)%.s: %.S FORCE
108 $(call if_changed_dep,cpp_i_c)
110 $(OUTPUT)%.s: %.c FORCE
112 $(call if_changed_dep,cc_s_c)
115 # obj-y - list of build objects
116 # subdir-y - list of directories to nest
117 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
119 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
120 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
121 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
123 # '$(OUTPUT)/dir' prefix to all objects
124 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
125 obj-y := $(addprefix $(objprefix),$(obj-y))
126 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
128 # Final '$(obj)-in.o' object
129 in-target := $(objprefix)$(obj)-in.o
134 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
136 $(sort $(subdir-obj-y)): $(subdir-y) ;
138 $(in-target): $(obj-y) FORCE
140 $(call if_changed,$(host)ld_multi)
142 __build: $(in-target)
148 # Include all cmd files to get all the dependency rules
149 # for all objects included
150 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
151 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
153 ifneq ($(cmd_files),)