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