1### 2# Main build makefile. 3# 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: 6# 7# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015 8# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015 9# 10 11PHONY := __build 12__build: 13 14ifeq ($(V),1) 15 quiet = 16else 17 quiet=quiet_ 18endif 19 20build-dir := $(srctree)/tools/build 21 22# Generic definitions 23include $(build-dir)/Build.include 24 25# Init all relevant variables used in build files so 26# 1) they have correct type 27# 2) they do not inherit any value from the environment 28subdir-y := 29obj-y := 30subdir-y := 31subdir-obj-y := 32 33# Build definitions 34build-file := $(dir)/Build 35include $(build-file) 36 37# Compile command 38quiet_cmd_cc_o_c = CC $@ 39 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< 40 41# Link agregate command 42# If there's nothing to link, create empty $@ object. 43quiet_cmd_ld_multi = LD $@ 44 cmd_ld_multi = $(if $(strip $(obj-y)),\ 45 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@) 46 47# Build rules 48$(OUTPUT)%.o: %.c FORCE 49 $(call if_changed_dep,cc_o_c) 50 51$(OUTPUT)%.o: %.S FORCE 52 $(call if_changed_dep,cc_o_c) 53 54# Gather build data: 55# obj-y - list of build objects 56# subdir-y - list of directories to nest 57# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o' 58obj-y := $($(obj)-y) 59subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 60obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y)) 61subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y)) 62 63# '$(OUTPUT)/dir' prefix to all objects 64prefix := $(subst ./,,$(OUTPUT)$(dir)/) 65obj-y := $(addprefix $(prefix),$(obj-y)) 66subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y)) 67 68# Final '$(obj)-in.o' object 69in-target := $(prefix)$(obj)-in.o 70 71PHONY += $(subdir-y) 72 73$(subdir-y): 74 @$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj) 75 76$(sort $(subdir-obj-y)): $(subdir-y) ; 77 78$(in-target): $(obj-y) FORCE 79 $(call rule_mkdir) 80 $(call if_changed,ld_multi) 81 82__build: $(in-target) 83 @: 84 85PHONY += FORCE 86FORCE: 87 88# Include all cmd files to get all the dependency rules 89# for all objects included 90targets := $(wildcard $(sort $(obj-y) $(in-target))) 91cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) 92 93ifneq ($(cmd_files),) 94 include $(cmd_files) 95endif 96 97.PHONY: $(PHONY) 98