2 # build: Generic definitions
4 # Lots of this code have been borrowed or heavily inspired from parts
5 # of kbuild code, which is not credited, but mostly developed by:
7 # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
8 # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
12 # Convenient variables
18 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
19 dot-target = $(dir $@).$(notdir $@)
22 # filename of target with directory and extension stripped
23 basetarget = $(basename $(notdir $@))
26 # The temporary file to save gcc -MD generated dependencies must not
28 depfile = $(subst $(comma),_,$(dot-target).d)
31 # Check if both arguments has same arguments. Result is empty string if equal.
32 arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
33 $(filter-out $(cmd_$@), $(cmd_$(1))) )
36 # Escape single quote for use in echo statements
37 escsq = $(subst $(squote),'\$(squote)',$1)
40 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
41 echo-cmd = $(if $($(quiet)cmd_$(1)),\
42 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
45 # Replace >$< with >$$< to preserve $ when reloading the .cmd file
47 # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
49 # Replace >'< with >'\''< to be able to enclose the whole string in '...'
50 # (needed for the shell)
51 make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
54 # Find any prerequisites that is newer than target or that does not exist.
55 # PHONY targets skipped in both cases.
56 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
59 # Copy dependency data into .cmd file
60 # - gcc -M dependency info
61 # - command line to create object 'cmd_object :='
62 dep-cmd = $(if $(wildcard $(fixdep)), \
63 $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \
65 mv -f $(dot-target).tmp $(dot-target).cmd, \
66 printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
67 printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd; \
68 cat $(depfile) >> $(dot-target).cmd; \
69 printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
72 # if_changed_dep - execute command if any prerequisite is newer than
73 # target, or command line has changed and update
74 # dependencies in the cmd file
75 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
77 $(echo-cmd) $(cmd_$(1)) && $(dep-cmd))
79 # if_changed - execute command if any prerequisite is newer than
80 # target, or command line has changed
81 if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
83 $(echo-cmd) $(cmd_$(1)); \
84 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
87 # C flags to be used in rule definitions, includes:
88 # - depfile generation
90 # - per target C flags
91 # - per object C flags
92 # - BUILD_STR macro to allow '-D"$(variable)"' constructs
93 c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))