snmppp: bump version to 3.3.9
[buildroot-gz.git] / support / misc / utils.mk
blobc44319338ee393ff492707ff2611d55b3a16c512
1 ################################################################################
3 # This file contains various utility macros and variables used about
4 # everywhere in make constructs.
6 ################################################################################
8 # Strip quotes and then whitespaces
9 qstrip = $(strip $(subst ",,$(1)))
10 #"))
12 # Variables for use in Make constructs
13 comma := ,
14 empty :=
15 space := $(empty) $(empty)
17 # Case conversion macros. This is inspired by the 'up' macro from gmsl
18 # (http://gmsl.sf.net). It is optimised very heavily because these macros
19 # are used a lot. It is about 5 times faster than forking a shell and tr.
21 # The caseconvert-helper creates a definition of the case conversion macro.
22 # After expansion by the outer $(eval ), the UPPERCASE macro is defined as:
23 # $(strip $(eval __tmp := $(1)) $(eval __tmp := $(subst a,A,$(__tmp))) ... )
24 # In other words, every letter is substituted one by one.
26 # The caseconvert-helper allows us to create this definition out of the
27 # [FROM] and [TO] lists, so we don't need to write down every substition
28 # manually. The uses of $ and $$ quoting are chosen in order to do as
29 # much expansion as possible up-front.
31 # Note that it would be possible to conceive a slightly more optimal
32 # implementation that avoids the use of __tmp, but that would be even
33 # more unreadable and is not worth the effort.
35 [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 - .
36 [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 _ _
38 define caseconvert-helper
39 $(1) = $$(strip \
40 $$(eval __tmp := $$(1))\
41 $(foreach c, $(2),\
42 $$(eval __tmp := $$(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$$(__tmp))))\
43 $$(__tmp))
44 endef
46 $(eval $(call caseconvert-helper,UPPERCASE,$(join $(addsuffix :,$([FROM])),$([TO]))))
47 $(eval $(call caseconvert-helper,LOWERCASE,$(join $(addsuffix :,$([TO])),$([FROM]))))
49 # Reverse the orders of words in a list. Again, inspired by the gmsl
50 # 'reverse' macro.
51 reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1))) $(firstword $(1)))
53 # Sanitize macro cleans up generic strings so it can be used as a filename
54 # and in rules. Particularly useful for VCS version strings, that can contain
55 # slashes, colons (OK in filenames but not in rules), and spaces.
56 sanitize = $(subst $(space),_,$(subst :,_,$(subst /,_,$(strip $(1)))))
58 # MESSAGE Macro -- display a message in bold type
59 MESSAGE = echo "$(TERM_BOLD)>>> $($(PKG)_NAME) $($(PKG)_VERSION) $(call qstrip,$(1))$(TERM_RESET)"
60 TERM_BOLD := $(shell tput smso 2>/dev/null)
61 TERM_RESET := $(shell tput rmso 2>/dev/null)
63 # Utility functions for 'find'
64 # findfileclauses(filelist) => -name 'X' -o -name 'Y'
65 findfileclauses = $(call notfirstword,$(patsubst %,-o -name '%',$(1)))
66 # finddirclauses(base, dirlist) => -path 'base/dirX' -o -path 'base/dirY'
67 finddirclauses = $(call notfirstword,$(patsubst %,-o -path '$(1)/%',$(2)))
69 # Miscellaneous utility functions
70 # notfirstword(wordlist): returns all but the first word in wordlist
71 notfirstword = $(wordlist 2,$(words $(1)),$(1))
73 # Needed for the foreach loops to loop over the list of hooks, so that
74 # each hook call is properly separated by a newline.
75 define sep
78 endef
80 PERCENT = %
81 QUOTE = '
82 # ' # Meh... syntax-highlighting
84 # This macro properly escapes a command string, then prints it with printf:
86 # - first, backslash '\' are self-escaped, so that they do not escape
87 # the following char and so that printf properly outputs a backslash;
89 # - next, single quotes are escaped by closing an existing one, adding
90 # an escaped one, and re-openning a new one (see below for the reason);
92 # - then '%' signs are self-escaped so that the printf does not interpret
93 # them as a format specifier, in case the variable contains an actual
94 # printf with a format;
96 # - finally, $(sep) is replaced with the literal '\n' so that make does
97 # not break on the so-expanded variable, but so that the printf does
98 # correctly output an LF.
100 # Note: this must be escaped in this order to avoid over-escaping the
101 # previously escaped elements.
103 # Once everything has been escaped, it is passed between single quotes
104 # (that's why the single-quotes are escaped they way they are, above,
105 # and why the dollar sign is not escaped) to printf(1). A trailing
106 # newline is apended, too.
108 # Note: leading or trailing spaces are *not* stripped.
110 define PRINTF
111 printf '$(subst $(sep),\n,\
112 $(subst $(PERCENT),$(PERCENT)$(PERCENT),\
113 $(subst $(QUOTE),$(QUOTE)\$(QUOTE)$(QUOTE),\
114 $(subst \,\\,$(1)))))\n'
115 endef