^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # build: Generic definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # Lots of this code have been borrowed or heavily inspired from parts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # of kbuild code, which is not credited, but mostly developed by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^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) # Convenient variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) comma := ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) squote := '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) pound := \#
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) dot-target = $(dir $@).$(notdir $@)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # filename of target with directory and extension stripped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) basetarget = $(basename $(notdir $@))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) # The temporary file to save gcc -MD generated dependencies must not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # contain a comma
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) depfile = $(subst $(comma),_,$(dot-target).d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) # Check if both arguments has same arguments. Result is empty string if equal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) $(filter-out $(cmd_$@), $(cmd_$(1))) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # Escape single quote for use in echo statements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) escsq = $(subst $(squote),'\$(squote)',$1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) # Echo command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) echo-cmd = $(if $($(quiet)cmd_$(1)),\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) # Replace >$< with >$$< to preserve $ when reloading the .cmd file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) # (needed for make)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) # (needed for make)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) # Replace >'< with >'\''< to be able to enclose the whole string in '...'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # (needed for the shell)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) # Find any prerequisites that is newer than target or that does not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) # PHONY targets skipped in both cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) # Copy dependency data into .cmd file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) # - gcc -M dependency info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) # - command line to create object 'cmd_object :='
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) dep-cmd = $(if $(wildcard $(fixdep)), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) rm -f $(depfile); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) mv -f $(dot-target).tmp $(dot-target).cmd, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) cat $(depfile) >> $(dot-target).cmd; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) # if_changed_dep - execute command if any prerequisite is newer than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) # target, or command line has changed and update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) # dependencies in the cmd file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) @set -e; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) $(echo-cmd) $(cmd_$(1)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) $(dep-cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # if_changed - execute command if any prerequisite is newer than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) # target, or command line has changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) @set -e; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) $(echo-cmd) $(cmd_$(1)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # C flags to be used in rule definitions, includes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) # - depfile generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # - global $(CFLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # - per target C flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) # - per object C flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) # - BUILD_STR macro to allow '-D"$(variable)"' constructs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) c_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) c_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ###
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ## HOSTCC C flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) host_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(KBUILD_HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))