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
17 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
18 dot-target = $(dir $@).$(notdir $@)
21 # filename of target with directory and extension stripped
22 basetarget = $(basename $(notdir $@))
25 # The temporary file to save gcc -MD generated dependencies must not
27 depfile = $(subst $(comma),_,$(dot-target).d)
30 # Check if both arguments has same arguments. Result is empty string if equal.
31 arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
32 $(filter-out $(cmd_$@), $(cmd_$(1))) )
35 # Escape single quote for use in echo statements
36 escsq = $(subst $(squote),'\$(squote)',$1)
39 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
40 echo-cmd = $(if $($(quiet)cmd_$(1)),\
41 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
44 # Replace >$< with >$$< to preserve $ when reloading the .cmd file
46 # Replace >#< with >\#< to avoid starting a comment in the .cmd file
48 # Replace >'< with >'\''< to be able to enclose the whole string in '...'
49 # (needed for the shell)
50 make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
53 # Find any prerequisites that is newer than target or that does not exist.
54 # PHONY targets skipped in both cases.
55 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
58 # if_changed_dep - execute command if any prerequisite is newer than
59 # target, or command line has changed and update
60 # dependencies in the cmd file
61 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
63 $(echo-cmd) $(cmd_$(1)); \
64 cat $(depfile) > $(dot-target).cmd; \
65 printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
67 # if_changed - execute command if any prerequisite is newer than
68 # target, or command line has changed
69 if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
71 $(echo-cmd) $(cmd_$(1)); \
72 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
75 # C flags to be used in rule definitions, includes:
76 # - depfile generation
78 # - per target C flags
79 # - per object C flags
80 # - BUILD_STR macro to allow '-D"$(variable)"' constructs
81 c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))