1# A collection of handy macros used all over the build system.
2# This can be included anywhere and must not have any side effects.
3
4# Find the local dir of the make file
5GET_LOCAL_DIR    = $(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))))
6
7# makes sure the target dir exists
8MKDIR = if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi
9
10# prepends the BUILD_DIR var to each item in the list
11TOBUILDDIR = $(addprefix $(BUILDDIR)/,$(1))
12
13# converts specified variable to boolean value
14TOBOOL = $(if $(filter-out 0 false,$1),true,false)
15
16COMMA := ,
17E :=
18SPACE := $E $E
19
20# lower case and upper case translation
21LC = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
22UC = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))
23
24# test if two files are different, replacing the first
25# with the second if so
26# args: $1 - temporary file to test
27#       $2 - file to replace
28define TESTANDREPLACEFILE
29	if [ -f "$2" ]; then \
30		if cmp -- "$1" "$2"; then \
31			rm -f -- $1; \
32		else \
33			mv -- $1 $2; \
34		fi \
35	else \
36		mv -- $1 $2; \
37	fi
38endef
39
40# replace all characters or sequences of letters in defines to convert to a proper C style variable
41MAKECVAR=$(subst C++,CPP,$(subst -,_,$(subst /,_,$(subst .,_,$1))))
42
43# generate a header file at $1 with an expanded variable in $2
44# $3 provides an (optional) raw footer to append to the end
45# NOTE: the left side of the variable will be upper cased and some symbols replaced
46# to be valid C names (see MAKECVAR above).
47# The right side of the #define can be any valid C but cannot contain spaces, even
48# inside a string.
49define MAKECONFIGHEADER
50	$(info generating $1) \
51	$(MKDIR); \
52	echo '#pragma once' > $1.tmp; \
53	$(foreach var,$($(2)), \
54		echo \#define \
55		$(firstword $(subst =,$(SPACE),$(call MAKECVAR,$(call UC,$(var))))) \
56		$(if $(findstring =,$(var)),$(subst $(firstword $(subst =,$(SPACE),$(var)))=,,$(var))) \
57		>> $1.tmp;) \
58	echo $3 >> $1.tmp; \
59	$(call TESTANDREPLACEFILE,$1.tmp,$1)
60endef
61