+# Install a file. All arguments are optional. The first argument is the file
+# mode (defaults to 0644). The second argument are extra flags to the install
+# command (defaults to -D). The third argument is the source file to install
+# (defaults to $<) and the last one is the destination (defaults to $@).
+install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
+ $(if $3,$3,$<) $(if $4,$4,$@))
+
+# Concatenate variables together. The first argument is a list of variables
+# names to concatenate. The second argument is an optional prefix for the
+# variables and the third is the string to use as separator (" ~" if omitted).
+# For example:
+# X_A := a
+# X_B := b
+# $(call varcat,A B,X_, --)
+# Will produce something like "a -- b --"
+varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
+
+# Replace variables with specified values in a template file. The first
+# argument is a list of make variables names which will be replaced in the
+# target file. The strings @VARNAME@ in the template file will be replaced
+# with the value of the make $(VARNAME) variable and the result will be stored
+# in the target file. The second (optional) argument is a prefix to add to the
+# make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
+# the template file, it will be replaced by the value of the make variable
+# $(PREFIX_VARNAME). The third and fourth arguments are the source file and
+# the destination file (both optional, $< and $@ are used if omitted). The
+# fifth (optional) argument are options to pass to the substitute sed command
+# (for example, use "g" if you want to do multiple substitutions per line).
+replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
+ > $(if $4,$4,$@))
+
+# Create a symbolic link to the project under the $(INCLUDE_DIR). The first
+# argument is the name of symbolic link to create. The link is only created if
+# it doesn't already exist.
+symlink_include_dir = $(shell \
+ test -L $(INCLUDE_DIR)/$1 \
+ || ln -s $C $(INCLUDE_DIR)/$1 )
+
+# Create a file with flags used to trigger rebuilding when they change. The
+# first argument is the name of the file where to store the flags, the second
+# are the flags and the third argument is a text to be displayed if the flags
+# have changed (optional). This should be used as a rule action or something
+# where a shell script is expected.
+gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
+ $(if $3,test -f $1 && echo "$3";) \
+ echo "$2" > $1 ; fi)
+
+# Include sub-directory's Build.mak. The only argument is a list of
+# subdirectories for which Build.mak should be included. The $S directory is
+# set properly before including each sub-directory's Build.mak and restored
+# afterwards.
+define build_subdir_code
+_parent__$d__dir_ := $$S
+S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
+include $$T/$$S/Build.mak
+S := $$(_parent__$d__dir_)
+endef
+include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))