1c819e2cfSJiri Olsa### 2c819e2cfSJiri Olsa# build: Generic definitions 3c819e2cfSJiri Olsa# 4c819e2cfSJiri Olsa# Lots of this code have been borrowed or heavily inspired from parts 5c819e2cfSJiri Olsa# of kbuild code, which is not credited, but mostly developed by: 6c819e2cfSJiri Olsa# 7c819e2cfSJiri Olsa# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015 8c819e2cfSJiri Olsa# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015 9c819e2cfSJiri Olsa# 10c819e2cfSJiri Olsa 11c819e2cfSJiri Olsa### 12c819e2cfSJiri Olsa# Convenient variables 13c819e2cfSJiri Olsacomma := , 14c819e2cfSJiri Olsasquote := ' 15c819e2cfSJiri Olsa 16c819e2cfSJiri Olsa### 17c819e2cfSJiri Olsa# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 18c819e2cfSJiri Olsadot-target = $(dir $@).$(notdir $@) 19c819e2cfSJiri Olsa 20c819e2cfSJiri Olsa### 21c819e2cfSJiri Olsa# filename of target with directory and extension stripped 22c819e2cfSJiri Olsabasetarget = $(basename $(notdir $@)) 23c819e2cfSJiri Olsa 24c819e2cfSJiri Olsa### 25c819e2cfSJiri Olsa# The temporary file to save gcc -MD generated dependencies must not 26c819e2cfSJiri Olsa# contain a comma 27c819e2cfSJiri Olsadepfile = $(subst $(comma),_,$(dot-target).d) 28c819e2cfSJiri Olsa 29c819e2cfSJiri Olsa### 30c819e2cfSJiri Olsa# Check if both arguments has same arguments. Result is empty string if equal. 31c819e2cfSJiri Olsaarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ 32c819e2cfSJiri Olsa $(filter-out $(cmd_$@), $(cmd_$(1))) ) 33c819e2cfSJiri Olsa 34c819e2cfSJiri Olsa### 35c819e2cfSJiri Olsa# Escape single quote for use in echo statements 36c819e2cfSJiri Olsaescsq = $(subst $(squote),'\$(squote)',$1) 37c819e2cfSJiri Olsa 38c819e2cfSJiri Olsa# Echo command 39c819e2cfSJiri Olsa# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 40c819e2cfSJiri Olsaecho-cmd = $(if $($(quiet)cmd_$(1)),\ 41c819e2cfSJiri Olsa echo ' $(call escsq,$($(quiet)cmd_$(1)))';) 42c819e2cfSJiri Olsa 43c819e2cfSJiri Olsa### 44c819e2cfSJiri Olsa# Replace >$< with >$$< to preserve $ when reloading the .cmd file 45c819e2cfSJiri Olsa# (needed for make) 46c819e2cfSJiri Olsa# Replace >#< with >\#< to avoid starting a comment in the .cmd file 47c819e2cfSJiri Olsa# (needed for make) 48c819e2cfSJiri Olsa# Replace >'< with >'\''< to be able to enclose the whole string in '...' 49c819e2cfSJiri Olsa# (needed for the shell) 50c819e2cfSJiri Olsamake-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1))))) 51c819e2cfSJiri Olsa 52c819e2cfSJiri Olsa### 53c819e2cfSJiri Olsa# Find any prerequisites that is newer than target or that does not exist. 54c819e2cfSJiri Olsa# PHONY targets skipped in both cases. 55c819e2cfSJiri Olsaany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) 56c819e2cfSJiri Olsa 57c819e2cfSJiri Olsa### 58275e2d95SJiri Olsa# Copy dependency data into .cmd file 59275e2d95SJiri Olsa# - gcc -M dependency info 60275e2d95SJiri Olsa# - command line to create object 'cmd_object :=' 619fb81323SJiri Olsadep-cmd = $(if $(wildcard $(fixdep)), \ 629fb81323SJiri Olsa $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \ 639fb81323SJiri Olsa rm -f $(depfile); \ 649fb81323SJiri Olsa mv -f $(dot-target).tmp $(dot-target).cmd, \ 659fb81323SJiri Olsa printf '\# cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \ 669fb81323SJiri Olsa printf '\# using basic dep data\n\n' >> $(dot-target).cmd; \ 679fb81323SJiri Olsa cat $(depfile) >> $(dot-target).cmd; \ 689fb81323SJiri Olsa printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd) 69275e2d95SJiri Olsa 70275e2d95SJiri Olsa### 71c819e2cfSJiri Olsa# if_changed_dep - execute command if any prerequisite is newer than 72c819e2cfSJiri Olsa# target, or command line has changed and update 73c819e2cfSJiri Olsa# dependencies in the cmd file 74c819e2cfSJiri Olsaif_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \ 75c819e2cfSJiri Olsa @set -e; \ 76275e2d95SJiri Olsa $(echo-cmd) $(cmd_$(1)) && $(dep-cmd)) 77c819e2cfSJiri Olsa 78c819e2cfSJiri Olsa# if_changed - execute command if any prerequisite is newer than 79c819e2cfSJiri Olsa# target, or command line has changed 80c819e2cfSJiri Olsaif_changed = $(if $(strip $(any-prereq) $(arg-check)), \ 81c819e2cfSJiri Olsa @set -e; \ 82c819e2cfSJiri Olsa $(echo-cmd) $(cmd_$(1)); \ 83c819e2cfSJiri Olsa printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) 84c819e2cfSJiri Olsa 85c819e2cfSJiri Olsa### 86c819e2cfSJiri Olsa# C flags to be used in rule definitions, includes: 87c819e2cfSJiri Olsa# - depfile generation 88c819e2cfSJiri Olsa# - global $(CFLAGS) 89c819e2cfSJiri Olsa# - per target C flags 90c819e2cfSJiri Olsa# - per object C flags 91c819e2cfSJiri Olsa# - BUILD_STR macro to allow '-D"$(variable)"' constructs 922ec8107dSJiri Olsac_flags_1 = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj)) 932ec8107dSJiri Olsac_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1)) 942ec8107dSJiri Olsac_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2)) 95f61bdc33SWang Nancxx_flags = -Wp,-MD,$(depfile),-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj)) 960c3b7e42SJiri Olsa 970c3b7e42SJiri Olsa### 980c3b7e42SJiri Olsa## HOSTCC C flags 990c3b7e42SJiri Olsa 1000c3b7e42SJiri Olsahost_c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CHOSTFLAGS) -D"BUILD_STR(s)=\#s" $(CHOSTFLAGS_$(basetarget).o) $(CHOSTFLAGS_$(obj)) 101