1*c819e2cfSJiri Olsa### 2*c819e2cfSJiri Olsa# build: Generic definitions 3*c819e2cfSJiri Olsa# 4*c819e2cfSJiri Olsa# Lots of this code have been borrowed or heavily inspired from parts 5*c819e2cfSJiri Olsa# of kbuild code, which is not credited, but mostly developed by: 6*c819e2cfSJiri Olsa# 7*c819e2cfSJiri Olsa# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015 8*c819e2cfSJiri Olsa# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015 9*c819e2cfSJiri Olsa# 10*c819e2cfSJiri Olsa 11*c819e2cfSJiri Olsa### 12*c819e2cfSJiri Olsa# Convenient variables 13*c819e2cfSJiri Olsacomma := , 14*c819e2cfSJiri Olsasquote := ' 15*c819e2cfSJiri Olsa 16*c819e2cfSJiri Olsa### 17*c819e2cfSJiri Olsa# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 18*c819e2cfSJiri Olsadot-target = $(dir $@).$(notdir $@) 19*c819e2cfSJiri Olsa 20*c819e2cfSJiri Olsa### 21*c819e2cfSJiri Olsa# filename of target with directory and extension stripped 22*c819e2cfSJiri Olsabasetarget = $(basename $(notdir $@)) 23*c819e2cfSJiri Olsa 24*c819e2cfSJiri Olsa### 25*c819e2cfSJiri Olsa# The temporary file to save gcc -MD generated dependencies must not 26*c819e2cfSJiri Olsa# contain a comma 27*c819e2cfSJiri Olsadepfile = $(subst $(comma),_,$(dot-target).d) 28*c819e2cfSJiri Olsa 29*c819e2cfSJiri Olsa### 30*c819e2cfSJiri Olsa# Check if both arguments has same arguments. Result is empty string if equal. 31*c819e2cfSJiri Olsaarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 32*c819e2cfSJiri Olsa $(filter-out $(cmd_$@), $(cmd_$(1))) ) 33*c819e2cfSJiri Olsa 34*c819e2cfSJiri Olsa### 35*c819e2cfSJiri Olsa# Escape single quote for use in echo statements 36*c819e2cfSJiri Olsaescsq = $(subst $(squote),'\$(squote)',$1) 37*c819e2cfSJiri Olsa 38*c819e2cfSJiri Olsa# Echo command 39*c819e2cfSJiri Olsa# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 40*c819e2cfSJiri Olsaecho-cmd = $(if $($(quiet)cmd_$(1)),\ 41*c819e2cfSJiri Olsa echo ' $(call escsq,$($(quiet)cmd_$(1)))';) 42*c819e2cfSJiri Olsa 43*c819e2cfSJiri Olsa### 44*c819e2cfSJiri Olsa# Replace >$< with >$$< to preserve $ when reloading the .cmd file 45*c819e2cfSJiri Olsa# (needed for make) 46*c819e2cfSJiri Olsa# Replace >#< with >\#< to avoid starting a comment in the .cmd file 47*c819e2cfSJiri Olsa# (needed for make) 48*c819e2cfSJiri Olsa# Replace >'< with >'\''< to be able to enclose the whole string in '...' 49*c819e2cfSJiri Olsa# (needed for the shell) 50*c819e2cfSJiri Olsamake-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1))))) 51*c819e2cfSJiri Olsa 52*c819e2cfSJiri Olsa### 53*c819e2cfSJiri Olsa# Find any prerequisites that is newer than target or that does not exist. 54*c819e2cfSJiri Olsa# PHONY targets skipped in both cases. 55*c819e2cfSJiri Olsaany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 56*c819e2cfSJiri Olsa 57*c819e2cfSJiri Olsa### 58*c819e2cfSJiri Olsa# if_changed_dep - execute command if any prerequisite is newer than 59*c819e2cfSJiri Olsa# target, or command line has changed and update 60*c819e2cfSJiri Olsa# dependencies in the cmd file 61*c819e2cfSJiri Olsaif_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \ 62*c819e2cfSJiri Olsa @set -e; \ 63*c819e2cfSJiri Olsa $(echo-cmd) $(cmd_$(1)); \ 64*c819e2cfSJiri Olsa cat $(depfile) > $(dot-target).cmd; \ 65*c819e2cfSJiri Olsa printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd) 66*c819e2cfSJiri Olsa 67*c819e2cfSJiri Olsa# if_changed - execute command if any prerequisite is newer than 68*c819e2cfSJiri Olsa# target, or command line has changed 69*c819e2cfSJiri Olsaif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 70*c819e2cfSJiri Olsa @set -e; \ 71*c819e2cfSJiri Olsa $(echo-cmd) $(cmd_$(1)); \ 72*c819e2cfSJiri Olsa printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 73*c819e2cfSJiri Olsa 74*c819e2cfSJiri Olsa### 75*c819e2cfSJiri Olsa# C flags to be used in rule definitions, includes: 76*c819e2cfSJiri Olsa# - depfile generation 77*c819e2cfSJiri Olsa# - global $(CFLAGS) 78*c819e2cfSJiri Olsa# - per target C flags 79*c819e2cfSJiri Olsa# - per object C flags 80*c819e2cfSJiri Olsa# - BUILD_STR macro to allow '-D"$(variable)"' constructs 81*c819e2cfSJiri Olsac_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj)) 82