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
25 include $(build-dir)/Build.include
27 # do not force detected configuration
28 -include $(OUTPUT).config-detected
30 # Init all relevant variables used in build files so
31 # 1) they have correct type
32 # 2) they do not inherit any value from the environment
39 build-file := $(dir)/Build
40 -include $(build-file)
42 quiet_cmd_flex = FLEX $@
43 quiet_cmd_bison = BISON $@
45 # Create directory unless it exists
46 quiet_cmd_mkdir = MKDIR $(dir $@)
47 cmd_mkdir = mkdir -p $(dir $@)
48 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
51 quiet_cmd_cc_o_c = CC $@
52 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
54 quiet_cmd_cc_i_c = CPP $@
55 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
57 quiet_cmd_cc_s_c = AS $@
58 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
60 quiet_cmd_gen = GEN $@
62 # Link agregate command
63 # If there's nothing to link, create empty $@ object.
64 quiet_cmd_ld_multi = LD $@
65 cmd_ld_multi = $(if $(strip $(obj-y)),\
66 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
69 $(OUTPUT)%.o: %.c FORCE
71 $(call if_changed_dep,cc_o_c)
73 $(OUTPUT)%.o: %.S FORCE
75 $(call if_changed_dep,cc_o_c)
77 $(OUTPUT)%.i: %.c FORCE
79 $(call if_changed_dep,cc_i_c)
81 $(OUTPUT)%.i: %.S FORCE
83 $(call if_changed_dep,cc_i_c)
85 $(OUTPUT)%.s: %.c FORCE
87 $(call if_changed_dep,cc_s_c)
90 # obj-y - list of build objects
91 # subdir-y - list of directories to nest
92 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
94 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
95 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
96 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
98 # '$(OUTPUT)/dir' prefix to all objects
99 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
100 obj-y := $(addprefix $(objprefix),$(obj-y))
101 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
103 # Final '$(obj)-in.o' object
104 in-target := $(objprefix)$(obj)-in.o
109 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
111 $(sort $(subdir-obj-y)): $(subdir-y) ;
113 $(in-target): $(obj-y) FORCE
115 $(call if_changed,ld_multi)
117 __build: $(in-target)
123 # Include all cmd files to get all the dependency rules
124 # for all objects included
125 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
126 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
128 ifneq ($(cmd_files),)