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