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_cc_i_c = CPP $@
62 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
64 quiet_cmd_cc_s_c = AS $@
65 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
67 quiet_cmd_gen = GEN $@
69 # Link agregate command
70 # If there's nothing to link, create empty $@ object.
71 quiet_cmd_ld_multi = LD $@
72 cmd_ld_multi = $(if $(strip $(obj-y)),\
73 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
76 $(OUTPUT)%.o: %.c FORCE
78 $(call if_changed_dep,cc_o_c)
80 $(OUTPUT)%.o: %.S FORCE
82 $(call if_changed_dep,cc_o_c)
84 $(OUTPUT)%.i: %.c FORCE
86 $(call if_changed_dep,cc_i_c)
88 $(OUTPUT)%.s: %.S FORCE
90 $(call if_changed_dep,cc_i_c)
92 $(OUTPUT)%.s: %.c FORCE
94 $(call if_changed_dep,cc_s_c)
97 # obj-y - list of build objects
98 # subdir-y - list of directories to nest
99 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
101 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
102 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
103 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
105 # '$(OUTPUT)/dir' prefix to all objects
106 objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
107 obj-y := $(addprefix $(objprefix),$(obj-y))
108 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
110 # Final '$(obj)-in.o' object
111 in-target := $(objprefix)$(obj)-in.o
116 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
118 $(sort $(subdir-obj-y)): $(subdir-y) ;
120 $(in-target): $(obj-y) FORCE
122 $(call if_changed,ld_multi)
124 __build: $(in-target)
130 # Include all cmd files to get all the dependency rules
131 # for all objects included
132 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
133 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
135 ifneq ($(cmd_files),)