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