xref: /OK3568_Linux_fs/buildroot/support/misc/utils.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun################################################################################
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# This file contains various utility macros and variables used about
4*4882a593Smuzhiyun# everywhere in make constructs.
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun################################################################################
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun# Strip quotes and then whitespaces
9*4882a593Smuzhiyunqstrip = $(strip $(subst ",,$(1)))
10*4882a593Smuzhiyun#"))
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun# Variables for use in Make constructs
13*4882a593Smuzhiyuncomma := ,
14*4882a593Smuzhiyunempty :=
15*4882a593Smuzhiyunspace := $(empty) $(empty)
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun# make 4.3:
18*4882a593Smuzhiyun# https://lwn.net/Articles/810071/
19*4882a593Smuzhiyun# Number signs (#) appearing inside a macro reference or function invocation
20*4882a593Smuzhiyun#   no longer introduce comments and should not be escaped with backslashes:
21*4882a593Smuzhiyun#   thus a call such as:
22*4882a593Smuzhiyun#     foo := $(shell echo '#')
23*4882a593Smuzhiyun#   is legal.  Previously the number sign needed to be escaped, for example:
24*4882a593Smuzhiyun#     foo := $(shell echo '\#')
25*4882a593Smuzhiyun#   Now this latter will resolve to "\#".  If you want to write makefiles
26*4882a593Smuzhiyun#   portable to both versions, assign the number sign to a variable:
27*4882a593Smuzhiyun#     H := \#
28*4882a593Smuzhiyun#     foo := $(shell echo '$H')
29*4882a593SmuzhiyunSHARP_SIGN := \#
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun# Case conversion macros. This is inspired by the 'up' macro from gmsl
32*4882a593Smuzhiyun# (http://gmsl.sf.net). It is optimised very heavily because these macros
33*4882a593Smuzhiyun# are used a lot. It is about 5 times faster than forking a shell and tr.
34*4882a593Smuzhiyun#
35*4882a593Smuzhiyun# The caseconvert-helper creates a definition of the case conversion macro.
36*4882a593Smuzhiyun# After expansion by the outer $(eval ), the UPPERCASE macro is defined as:
37*4882a593Smuzhiyun# $(strip $(eval __tmp := $(1))  $(eval __tmp := $(subst a,A,$(__tmp))) ... )
38*4882a593Smuzhiyun# In other words, every letter is substituted one by one.
39*4882a593Smuzhiyun#
40*4882a593Smuzhiyun# The caseconvert-helper allows us to create this definition out of the
41*4882a593Smuzhiyun# [FROM] and [TO] lists, so we don't need to write down every substition
42*4882a593Smuzhiyun# manually. The uses of $ and $$ quoting are chosen in order to do as
43*4882a593Smuzhiyun# much expansion as possible up-front.
44*4882a593Smuzhiyun#
45*4882a593Smuzhiyun# Note that it would be possible to conceive a slightly more optimal
46*4882a593Smuzhiyun# implementation that avoids the use of __tmp, but that would be even
47*4882a593Smuzhiyun# more unreadable and is not worth the effort.
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun[FROM] := a b c d e f g h i j k l m n o p q r s t u v w x y z - .
50*4882a593Smuzhiyun[TO]   := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ _
51*4882a593Smuzhiyun
52*4882a593Smuzhiyundefine caseconvert-helper
53*4882a593Smuzhiyun$(1) = $$(strip \
54*4882a593Smuzhiyun	$$(eval __tmp := $$(1))\
55*4882a593Smuzhiyun	$(foreach c, $(2),\
56*4882a593Smuzhiyun		$$(eval __tmp := $$(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$$(__tmp))))\
57*4882a593Smuzhiyun	$$(__tmp))
58*4882a593Smuzhiyunendef
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun$(eval $(call caseconvert-helper,UPPERCASE,$(join $(addsuffix :,$([FROM])),$([TO]))))
61*4882a593Smuzhiyun$(eval $(call caseconvert-helper,LOWERCASE,$(join $(addsuffix :,$([TO])),$([FROM]))))
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun# Reverse the orders of words in a list. Again, inspired by the gmsl
64*4882a593Smuzhiyun# 'reverse' macro.
65*4882a593Smuzhiyunreverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1))) $(firstword $(1)))
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun# Sanitize macro cleans up generic strings so it can be used as a filename
68*4882a593Smuzhiyun# and in rules. Particularly useful for VCS version strings, that can contain
69*4882a593Smuzhiyun# slashes, colons (OK in filenames but not in rules), and spaces.
70*4882a593Smuzhiyunsanitize = $(subst $(space),_,$(subst :,_,$(subst /,_,$(strip $(1)))))
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun# MESSAGE Macro -- display a message in bold type
73*4882a593SmuzhiyunMESSAGE = echo "$(TERM_BOLD)>>> $($(PKG)_NAME) $($(PKG)_VERSION) $(call qstrip,$(1))$(TERM_RESET)"
74*4882a593SmuzhiyunTERM_BOLD := $(shell tput smso 2>/dev/null)
75*4882a593SmuzhiyunTERM_RESET := $(shell tput rmso 2>/dev/null)
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun# Utility functions for 'find'
78*4882a593Smuzhiyun# findfileclauses(filelist) => -name 'X' -o -name 'Y'
79*4882a593Smuzhiyunfindfileclauses = $(call notfirstword,$(patsubst %,-o -name '%',$(1)))
80*4882a593Smuzhiyun# finddirclauses(base, dirlist) => -path 'base/dirX' -o -path 'base/dirY'
81*4882a593Smuzhiyunfinddirclauses = $(call notfirstword,$(patsubst %,-o -path '$(1)/%',$(2)))
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun# Miscellaneous utility functions
84*4882a593Smuzhiyun# notfirstword(wordlist): returns all but the first word in wordlist
85*4882a593Smuzhiyunnotfirstword = $(wordlist 2,$(words $(1)),$(1))
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun# build a comma-separated list of quoted items, from a space-separated
88*4882a593Smuzhiyun# list of unquoted items:   a b c d  -->  "a", "b", "c", "d"
89*4882a593Smuzhiyunmake-comma-list = $(subst $(space),$(comma)$(space),$(patsubst %,"%",$(strip $(1))))
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun# build a comma-separated list of single quoted items, from a space-separated
92*4882a593Smuzhiyun# list of unquoted items:   a b c d  -->  'a', 'b', 'c', 'd'
93*4882a593Smuzhiyunmake-sq-comma-list = $(subst $(space),$(comma)$(space),$(patsubst %,'%',$(strip $(1))))
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun# Needed for the foreach loops to loop over the list of hooks, so that
96*4882a593Smuzhiyun# each hook call is properly separated by a newline.
97*4882a593Smuzhiyundefine sep
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun
100*4882a593Smuzhiyunendef
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunPERCENT = %
103*4882a593SmuzhiyunQUOTE = '
104*4882a593Smuzhiyun# ' # Meh... syntax-highlighting
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun# This macro properly escapes a command string, then prints it with printf:
107*4882a593Smuzhiyun#
108*4882a593Smuzhiyun#   - first, backslash '\' are self-escaped, so that they do not escape
109*4882a593Smuzhiyun#     the following char and so that printf properly outputs a backslash;
110*4882a593Smuzhiyun#
111*4882a593Smuzhiyun#   - next, single quotes are escaped by closing an existing one, adding
112*4882a593Smuzhiyun#     an escaped one, and re-openning a new one (see below for the reason);
113*4882a593Smuzhiyun#
114*4882a593Smuzhiyun#   - then '%' signs are self-escaped so that the printf does not interpret
115*4882a593Smuzhiyun#     them as a format specifier, in case the variable contains an actual
116*4882a593Smuzhiyun#     printf with a format;
117*4882a593Smuzhiyun#
118*4882a593Smuzhiyun#   - finally, $(sep) is replaced with the literal '\n' so that make does
119*4882a593Smuzhiyun#     not break on the so-expanded variable, but so that the printf does
120*4882a593Smuzhiyun#     correctly output an LF.
121*4882a593Smuzhiyun#
122*4882a593Smuzhiyun# Note: this must be escaped in this order to avoid over-escaping the
123*4882a593Smuzhiyun# previously escaped elements.
124*4882a593Smuzhiyun#
125*4882a593Smuzhiyun# Once everything has been escaped, it is passed between single quotes
126*4882a593Smuzhiyun# (that's why the single-quotes are escaped they way they are, above,
127*4882a593Smuzhiyun# and why the dollar sign is not escaped) to printf(1). A trailing
128*4882a593Smuzhiyun# newline is apended, too.
129*4882a593Smuzhiyun#
130*4882a593Smuzhiyun# Note: leading or trailing spaces are *not* stripped.
131*4882a593Smuzhiyun#
132*4882a593Smuzhiyundefine PRINTF
133*4882a593Smuzhiyun	printf '$(subst $(sep),\n,\
134*4882a593Smuzhiyun		$(subst $(PERCENT),$(PERCENT)$(PERCENT),\
135*4882a593Smuzhiyun			$(subst $(QUOTE),$(QUOTE)\$(QUOTE)$(QUOTE),\
136*4882a593Smuzhiyun				$(subst \,\\,$(1)))))\n'
137*4882a593Smuzhiyunendef
138