1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
5 space:= $(empty) $(empty)
6 indent:= $(space)$(space)
8 # define a newline char, useful for debugging with $(info)
14 # $(prefix shell commands with $(Q) to silent them, except if V=1
16 ifeq ("$(V)-$(origin V)", "1-command line")
20 # set variable $1 to $2 if the variable has an implicit value or
23 # $2 new variable content
26 $(compare $(origin $(1)),default), \
27 $(compare $(origin $(1)),undefined) \
35 disp_list = $(info $(1)$(newline)\
36 $(addsuffix $(newline),$(addprefix $(space),$(2))))
38 # add a dot in front of the file name
40 # return: full paths with files prefixed by a dot
41 dotfile = $(strip $(foreach f,$(1),\
42 $(join $(dir $f),.$(notdir $f))))
44 # convert source/obj files into dot-dep filename
46 # return: full paths with files prefixed by a dot and suffixed with .d
47 depfile = $(strip $(call dotfile,$(addsuffix .d,$(1))))
49 # convert source/obj files into dot-dep filename
51 # return: full paths with files prefixed by a dot and suffixed with .d.tmp
52 file2tmpdep = $(strip $(call dotfile,$(addsuffix .d.tmp,$(1))))
54 # convert source/obj files into dot-cmd filename
56 # return: full paths with files prefixed by a dot and suffixed with .cmd
57 cmdfile = $(strip $(call dotfile,$(addsuffix .cmd,$(1))))
59 # add a \ before each quote
60 protect_quote = $(subst ','\'',$(1))
61 #'# editor syntax highlight fix
63 # return an non-empty string if $1 is empty, and vice versa
67 # return 1 if parameter is a non-empty string, else 0
68 boolean = $(if $1,1,0)
70 # return an empty string if string are equal
71 compare = $(strip $(subst $(1),,$(2)) $(subst $(2),,$(1)))
73 # return a non-empty string if a file does not exist
75 file_missing = $(call compare,$(wildcard $1),$1)
77 # return a non-empty string if cmdline changed
78 # $1: file to be built
79 # $2: the command to build it
80 cmdline_changed = $(call compare,$(strip $(cmd-$(1))),$(strip $(2)))
82 # return an non-empty string if the .d file does not exist
83 # $1: the dep file (.d)
84 depfile_missing = $(call compare,$(wildcard $(1)),$(1))
86 # return a non-empty string if, according to dep-xyz variable, a file
87 # needed to build $1 does not exist. In this case we need to rebuild
88 # the file and the .d file.
89 # $1: file to be built
90 dep-missing = $(call compare,$(wildcard $(dep-$(1))),$(dep-$(1)))
92 # return an empty string if no prereq is newer than target
93 # $1: list of prerequisites newer than target ($?)
94 dep-newer = $(strip $(filter-out FORCE,$(1)))
96 # display why a file should be re-built
100 # $4: all prerequisites newer than target ($?)
103 echo -n "$1 -> $2 " ; \
104 echo -n "file_missing=$(call boolean,$(call file_missing,$(2))) " ; \
105 echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(2),$(3))) " ; \
106 echo -n "depfile_missing=$(call boolean,$(call depfile_missing,$(call depfile,$(2)))) " ; \
107 echo -n "dep-missing=$(call boolean,$(call dep-missing,$(2))) " ; \
108 echo "dep-newer=$(call boolean,$(call dep-newer,$(4)))"
113 # return an empty string if a file should be rebuilt
116 # $3: all prerequisites newer than target ($?)
118 $(or $(call file_missing,$(1)),\
119 $(call cmdline_changed,$(1),$(2)),\
120 $(call depfile_missing,$(call depfile,$(1))),\
121 $(call dep-missing,$(1)),\
122 $(call dep-newer,$(3)))
124 # create a depfile (.d) with no additional deps
125 # $1: object file (.o)
126 create_empty_depfile = echo "dep-$(1) =" > $(call depfile,$(1))
128 # save a command in a file
129 # $1: command to build the file
130 # $2: name of the file
131 save_cmd = echo "cmd-$(2) = $(call protect_quote,$(1))" > $(call cmdfile,$(2))
133 # remove the FORCE target from the list of all prerequisites $+
134 # no arguments, use $+
135 prereq = $(filter-out FORCE,$(+))