182 lines · plain
1# SPDX-License-Identifier: GPL-2.02###3# Main build makefile.4#5# Lots of this code have been borrowed or heavily inspired from parts6# of kbuild code, which is not credited, but mostly developed by:7#8# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 20159# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 201510#11 12PHONY := __build13__build:14 15ifeq ($(V),1)16 quiet =17 Q =18else19 quiet=quiet_20 Q=@21endif22 23# If the user is running make -s (silent mode), suppress echoing of commands24# make-4.0 (and later) keep single letter options in the 1st word of MAKEFLAGS.25ifeq ($(filter 3.%,$(MAKE_VERSION)),)26short-opts := $(firstword -$(MAKEFLAGS))27else28short-opts := $(filter-out --%,$(MAKEFLAGS))29endif30 31ifneq ($(findstring s,$(short-opts)),)32 quiet=silent_33endif34 35build-dir := $(srctree)/tools/build36 37# Define $(fixdep) for dep-cmd function38ifeq ($(OUTPUT),)39 fixdep := $(build-dir)/fixdep40else41 fixdep := $(OUTPUT)/fixdep42endif43 44# Generic definitions45include $(build-dir)/Build.include46 47# do not force detected configuration48-include $(OUTPUT).config-detected49 50# Init all relevant variables used in build files so51# 1) they have correct type52# 2) they do not inherit any value from the environment53subdir-y :=54obj-y :=55subdir-y :=56subdir-obj-y :=57 58# Build definitions59build-file := $(dir)/Build60-include $(build-file)61 62quiet_cmd_flex = FLEX $@63quiet_cmd_bison = BISON $@64quiet_cmd_test = TEST $@65 66# Create directory unless it exists67quiet_cmd_mkdir = MKDIR $(dir $@)68 cmd_mkdir = mkdir -p $(dir $@)69 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))70 71# Compile command72quiet_cmd_cc_o_c = CC $@73 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<74 75quiet_cmd_host_cc_o_c = HOSTCC $@76 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<77 78quiet_cmd_cxx_o_c = CXX $@79 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<80 81quiet_cmd_cpp_i_c = CPP $@82 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<83 84quiet_cmd_cc_s_c = AS $@85 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<86 87quiet_cmd_gen = GEN $@88 89# Link agregate command90# If there's nothing to link, create empty $@ object.91quiet_cmd_ld_multi = LD $@92 cmd_ld_multi = $(if $(strip $(obj-y)),\93 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)94 95quiet_cmd_host_ld_multi = HOSTLD $@96 cmd_host_ld_multi = $(if $(strip $(obj-y)),\97 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)98 99ifneq ($(filter $(obj),$(hostprogs)),)100 host = host_101endif102 103# Build rules104$(OUTPUT)%.o: %.c FORCE105 $(call rule_mkdir)106 $(call if_changed_dep,$(host)cc_o_c)107 108$(OUTPUT)%.o: %.cpp FORCE109 $(call rule_mkdir)110 $(call if_changed_dep,cxx_o_c)111 112$(OUTPUT)%.o: %.S FORCE113 $(call rule_mkdir)114 $(call if_changed_dep,$(host)cc_o_c)115 116$(OUTPUT)%.i: %.c FORCE117 $(call rule_mkdir)118 $(call if_changed_dep,cpp_i_c)119 120$(OUTPUT)%.s: %.S FORCE121 $(call rule_mkdir)122 $(call if_changed_dep,cpp_i_c)123 124$(OUTPUT)%.s: %.c FORCE125 $(call rule_mkdir)126 $(call if_changed_dep,cc_s_c)127 128# bison and flex files are generated in the OUTPUT directory129# so it needs a separate rule to depend on them properly130$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE131 $(call rule_mkdir)132 $(call if_changed_dep,$(host)cc_o_c)133 134$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE135 $(call rule_mkdir)136 $(call if_changed_dep,$(host)cc_o_c)137 138# Gather build data:139# obj-y - list of build objects140# subdir-y - list of directories to nest141# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'142obj-y := $($(obj)-y)143subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))144obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))145subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))146 147# '$(OUTPUT)/dir' prefix to all objects148objprefix := $(subst ./,,$(OUTPUT)$(dir)/)149obj-y := $(addprefix $(objprefix),$(obj-y))150subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))151 152# Final '$(obj)-in.o' object153in-target := $(objprefix)$(obj)-in.o154 155PHONY += $(subdir-y)156 157$(subdir-y):158 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)159 160$(sort $(subdir-obj-y)): $(subdir-y) ;161 162$(in-target): $(obj-y) FORCE163 $(call rule_mkdir)164 $(call if_changed,$(host)ld_multi)165 166__build: $(in-target)167 @:168 169PHONY += FORCE170FORCE:171 172# Include all cmd files to get all the dependency rules173# for all objects included174targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))175cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))176 177ifneq ($(cmd_files),)178 include $(cmd_files)179endif180 181.PHONY: $(PHONY)182