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
20 build-dir := $(srctree)/tools/build
23 include $(build-dir)/Build.include
25 # do not force detected configuration
26 -include .config-detected
28 # Init all relevant variables used in build files so
29 # 1) they have correct type
30 # 2) they do not inherit any value from the environment
37 build-file := $(dir)/Build
40 # Create directory unless it exists
41 quiet_cmd_mkdir = MKDIR $(dir $@)
42 cmd_mkdir = mkdir -p $(dir $@)
43 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
46 quiet_cmd_cc_o_c = CC $@
47 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
49 # Link agregate command
50 # If there's nothing to link, create empty $@ object.
51 quiet_cmd_ld_multi = LD $@
52 cmd_ld_multi = $(if $(strip $(obj-y)),\
53 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)
56 $(OUTPUT)%.o: %.c FORCE
58 $(call if_changed_dep,cc_o_c)
60 $(OUTPUT)%.o: %.S FORCE
62 $(call if_changed_dep,cc_o_c)
65 # obj-y - list of build objects
66 # subdir-y - list of directories to nest
67 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
69 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
70 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
71 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
73 # '$(OUTPUT)/dir' prefix to all objects
74 prefix := $(subst ./,,$(OUTPUT)$(dir)/)
75 obj-y := $(addprefix $(prefix),$(obj-y))
76 subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y))
78 # Final '$(obj)-in.o' object
79 in-target := $(prefix)$(obj)-in.o
84 @$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
86 $(sort $(subdir-obj-y)): $(subdir-y) ;
88 $(in-target): $(obj-y) FORCE
90 $(call if_changed,ld_multi)
98 # Include all cmd files to get all the dependency rules
99 # for all objects included
100 targets := $(wildcard $(sort $(obj-y) $(in-target)))
101 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
103 ifneq ($(cmd_files),)