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