1 # SPDX-License-Identifier: GPL-2.0 2 ### 3 # Main build makefile. 4 # 5 # Lots of this code have been borrowed or heavily inspired from parts 6 # of kbuild code, which is not credited, but mostly developed by: 7 # 8 # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015 9 # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015 10 # 11 12 PHONY := __build 13 __build: 14 15 ifeq ($(V),1) 16 quiet = 17 Q = 18 else 19 quiet=quiet_ 20 Q=@ 21 endif 22 23 ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),) 24 quiet=silent_ 25 endif 26 27 build-dir := $(srctree)/tools/build 28 29 # Define $(fixdep) for dep-cmd function 30 ifeq ($(OUTPUT),) 31 fixdep := $(build-dir)/fixdep 32 else 33 fixdep := $(OUTPUT)/fixdep 34 endif 35 36 # Generic definitions 37 include $(build-dir)/Build.include 38 39 # do not force detected configuration 40 -include $(OUTPUT).config-detected 41 42 # Init all relevant variables used in build files so 43 # 1) they have correct type 44 # 2) they do not inherit any value from the environment 45 subdir-y := 46 obj-y := 47 subdir-y := 48 subdir-obj-y := 49 50 # Build definitions 51 build-file := $(dir)/Build 52 -include $(build-file) 53 54 quiet_cmd_flex = FLEX $@ 55 quiet_cmd_bison = BISON $@ 56 57 # Create directory unless it exists 58 quiet_cmd_mkdir = MKDIR $(dir $@) 59 cmd_mkdir = mkdir -p $(dir $@) 60 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir)) 61 62 # Compile command 63 quiet_cmd_cc_o_c = CC $@ 64 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< 65 66 quiet_cmd_host_cc_o_c = HOSTCC $@ 67 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $< 68 69 quiet_cmd_cxx_o_c = CXX $@ 70 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $< 71 72 quiet_cmd_cpp_i_c = CPP $@ 73 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $< 74 75 quiet_cmd_cc_s_c = AS $@ 76 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $< 77 78 quiet_cmd_gen = GEN $@ 79 80 # Link agregate command 81 # If there's nothing to link, create empty $@ object. 82 quiet_cmd_ld_multi = LD $@ 83 cmd_ld_multi = $(if $(strip $(obj-y)),\ 84 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@) 85 86 quiet_cmd_host_ld_multi = HOSTLD $@ 87 cmd_host_ld_multi = $(if $(strip $(obj-y)),\ 88 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@) 89 90 ifneq ($(filter $(obj),$(hostprogs)),) 91 host = host_ 92 endif 93 94 # Build rules 95 $(OUTPUT)%.o: %.c FORCE 96 $(call rule_mkdir) 97 $(call if_changed_dep,$(host)cc_o_c) 98 99 $(OUTPUT)%.o: %.cpp FORCE 100 $(call rule_mkdir) 101 $(call if_changed_dep,cxx_o_c) 102 103 $(OUTPUT)%.o: %.S FORCE 104 $(call rule_mkdir) 105 $(call if_changed_dep,$(host)cc_o_c) 106 107 $(OUTPUT)%.i: %.c FORCE 108 $(call rule_mkdir) 109 $(call if_changed_dep,cpp_i_c) 110 111 $(OUTPUT)%.s: %.S FORCE 112 $(call rule_mkdir) 113 $(call if_changed_dep,cpp_i_c) 114 115 $(OUTPUT)%.s: %.c FORCE 116 $(call rule_mkdir) 117 $(call if_changed_dep,cc_s_c) 118 119 # Gather build data: 120 # obj-y - list of build objects 121 # subdir-y - list of directories to nest 122 # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o' 123 obj-y := $($(obj)-y) 124 subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 125 obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y)) 126 subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y)) 127 128 # '$(OUTPUT)/dir' prefix to all objects 129 objprefix := $(subst ./,,$(OUTPUT)$(dir)/) 130 obj-y := $(addprefix $(objprefix),$(obj-y)) 131 subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y)) 132 133 # Final '$(obj)-in.o' object 134 in-target := $(objprefix)$(obj)-in.o 135 136 PHONY += $(subdir-y) 137 138 $(subdir-y): 139 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj) 140 141 $(sort $(subdir-obj-y)): $(subdir-y) ; 142 143 $(in-target): $(obj-y) FORCE 144 $(call rule_mkdir) 145 $(call if_changed,$(host)ld_multi) 146 147 __build: $(in-target) 148 @: 149 150 PHONY += FORCE 151 FORCE: 152 153 # Include all cmd files to get all the dependency rules 154 # for all objects included 155 targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS))) 156 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) 157 158 ifneq ($(cmd_files),) 159 include $(cmd_files) 160 endif 161 162 .PHONY: $(PHONY) 163