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_cc_i_c = CPP $@
72 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
74 quiet_cmd_cc_s_c = AS $@
75 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
77 quiet_cmd_gen = GEN $@
79 # Link agregate command
80 # If there's nothing to link, create empty $@ object.
81 quiet_cmd_ld_multi = LD $@
82 cmd_ld_multi = $(if $(strip $(obj-y)),\
83 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
86 $(OUTPUT)%.o: %.c FORCE
88 $(call if_changed_dep,cc_o_c)
90 $(OUTPUT)%.o: %.S FORCE
92 $(call if_changed_dep,cc_o_c)
94 $(OUTPUT)%.i: %.c FORCE
96 $(call if_changed_dep,cc_i_c)
98 $(OUTPUT)%.i: %.S FORCE
100 $(call if_changed_dep,cc_i_c)
102 $(OUTPUT)%.s: %.c FORCE
104 $(call if_changed_dep,cc_s_c)
107 # obj-y - list of build objects
108 # subdir-y - list of directories to nest
109 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
111 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
112 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
113 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
115 # '$(OUTPUT)/dir' prefix to all objects
116 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
117 obj-y := $(addprefix $(objprefix),$(obj-y))
118 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
120 # Final '$(obj)-in.o' object
121 in-target := $(objprefix)$(obj)-in.o
126 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
128 $(sort $(subdir-obj-y)): $(subdir-y) ;
130 $(in-target): $(obj-y) FORCE
132 $(call if_changed,ld_multi)
134 __build: $(in-target)
140 # Include all cmd files to get all the dependency rules
141 # for all objects included
142 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
143 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
145 ifneq ($(cmd_files),)