0001 ###
0002 # build: Generic definitions
0003 #
0004 # Lots of this code have been borrowed or heavily inspired from parts
0005 # of kbuild code, which is not credited, but mostly developed by:
0006 #
0007 # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
0008 # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
0009 #
0010
0011 ###
0012 # Convenient variables
0013 comma := ,
0014 squote := '
0015 pound := \#
0016
0017 ###
0018 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
0019 dot-target = $(dir $@).$(notdir $@)
0020
0021 ###
0022 # filename of target with directory and extension stripped
0023 basetarget = $(basename $(notdir $@))
0024
0025 ###
0026 # The temporary file to save gcc -MD generated dependencies must not
0027 # contain a comma
0028 depfile = $(subst $(comma),_,$(dot-target).d)
0029
0030 ###
0031 # Check if both arguments has same arguments. Result is empty string if equal.
0032 arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
0033 $(filter-out $(cmd_$@), $(cmd_$(1))) )
0034
0035 ###
0036 # Escape single quote for use in echo statements
0037 escsq = $(subst $(squote),'\$(squote)',$1)
0038
0039 # Echo command
0040 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
0041 echo-cmd = $(if $($(quiet)cmd_$(1)),\
0042 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
0043
0044 ###
0045 # Replace >$< with >$$< to preserve $ when reloading the .cmd file
0046 # (needed for make)
0047 # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
0048 # (needed for make)
0049 # Replace >'< with >'\''< to be able to enclose the whole string in '...'
0050 # (needed for the shell)
0051 make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
0052
0053 ###
0054 # Find any prerequisites that is newer than target or that does not exist.
0055 # PHONY targets skipped in both cases.
0056 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
0057
0058 ###
0059 # Copy dependency data into .cmd file
0060 # - gcc -M dependency info
0061 # - command line to create object 'cmd_object :='
0062 dep-cmd = $(if $(wildcard $(fixdep)), \
0063 $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \
0064 rm -f $(depfile); \
0065 mv -f $(dot-target).tmp $(dot-target).cmd, \
0066 printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
0067 printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd; \
0068 cat $(depfile) >> $(dot-target).cmd; \
0069 printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
0070
0071 ###
0072 # if_changed_dep - execute command if any prerequisite is newer than
0073 # target, or command line has changed and update
0074 # dependencies in the cmd file
0075 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
0076 @set -e; \
0077 $(echo-cmd) $(cmd_$(1)); \
0078 $(dep-cmd))
0079
0080 # if_changed - execute command if any prerequisite is newer than
0081 # target, or command line has changed
0082 if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
0083 @set -e; \
0084 $(echo-cmd) $(cmd_$(1)); \
0085 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
0086
0087 ###
0088 # C flags to be used in rule definitions, includes:
0089 # - depfile generation
0090 # - global $(CFLAGS)
0091 # - per target C flags
0092 # - per object C flags
0093 # - BUILD_STR macro to allow '-D"$(variable)"' constructs
0094 c_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
0095 c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
0096 c_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
0097 cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
0098
0099 ###
0100 ## HOSTCC C flags
0101
0102 host_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))
0103
0104 # output directory for tests below
0105 TMPOUT = .tmp_$$$$
0106
0107 # try-run
0108 # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
0109 # Exit code chooses option. "$$TMP" serves as a temporary file and is
0110 # automatically cleaned up.
0111 try-run = $(shell set -e; \
0112 TMP=$(TMPOUT)/tmp; \
0113 mkdir -p $(TMPOUT); \
0114 trap "rm -rf $(TMPOUT)" EXIT; \
0115 if ($(1)) >/dev/null 2>&1; \
0116 then echo "$(2)"; \
0117 else echo "$(3)"; \
0118 fi)
0119
0120 # cc-option
0121 # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
0122 cc-option = $(call try-run, \
0123 $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
0124
0125 # delete partially updated (i.e. corrupted) files on error
0126 .DELETE_ON_ERROR: