toolchain-external: Commonize /lib/<tuple> symlinks for Linaro toolchains
[buildroot-gz.git] / package / pkg-utils.mk
blobc61b3b67c4b0ad20f605cafda35dc338f78d72ec
1 ################################################################################
3 # This file contains various utility functions used by the package
4 # infrastructure, or by the packages themselves.
6 ################################################################################
8 # Case conversion macros. This is inspired by the 'up' macro from gmsl
9 # (http://gmsl.sf.net). It is optimised very heavily because these macros
10 # are used a lot. It is about 5 times faster than forking a shell and tr.
12 # The caseconvert-helper creates a definition of the case conversion macro.
13 # After expansion by the outer $(eval ), the UPPERCASE macro is defined as:
14 # $(strip $(eval __tmp := $(1)) $(eval __tmp := $(subst a,A,$(__tmp))) ... )
15 # In other words, every letter is substituted one by one.
17 # The caseconvert-helper allows us to create this definition out of the
18 # [FROM] and [TO] lists, so we don't need to write down every substition
19 # manually. The uses of $ and $$ quoting are chosen in order to do as
20 # much expansion as possible up-front.
22 # Note that it would be possible to conceive a slightly more optimal
23 # implementation that avoids the use of __tmp, but that would be even
24 # more unreadable and is not worth the effort.
26 [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 - .
27 [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 _ _
29 define caseconvert-helper
30 $(1) = $$(strip \
31 $$(eval __tmp := $$(1))\
32 $(foreach c, $(2),\
33 $$(eval __tmp := $$(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$$(__tmp))))\
34 $$(__tmp))
35 endef
37 $(eval $(call caseconvert-helper,UPPERCASE,$(join $(addsuffix :,$([FROM])),$([TO]))))
38 $(eval $(call caseconvert-helper,LOWERCASE,$(join $(addsuffix :,$([TO])),$([FROM]))))
40 # Sanitize macro cleans up generic strings so it can be used as a filename
41 # and in rules. Particularly useful for VCS version strings, that can contain
42 # slashes, colons (OK in filenames but not in rules), and spaces.
43 sanitize = $(subst $(space),_,$(subst :,_,$(subst /,_,$(strip $(1)))))
46 # Manipulation of .config files based on the Kconfig
47 # infrastructure. Used by the BusyBox package, the Linux kernel
48 # package, and more.
51 define KCONFIG_ENABLE_OPT # (option, file)
52 $(SED) "/\\<$(1)\\>/d" $(2)
53 echo '$(1)=y' >> $(2)
54 endef
56 define KCONFIG_SET_OPT # (option, value, file)
57 $(SED) "/\\<$(1)\\>/d" $(3)
58 echo '$(1)=$(2)' >> $(3)
59 endef
61 define KCONFIG_DISABLE_OPT # (option, file)
62 $(SED) "/\\<$(1)\\>/d" $(2)
63 echo '# $(1) is not set' >> $(2)
64 endef
66 # Helper functions to determine the name of a package and its
67 # directory from its makefile directory, using the $(MAKEFILE_LIST)
68 # variable provided by make. This is used by the *-package macros to
69 # automagically find where the package is located.
70 pkgdir = $(dir $(lastword $(MAKEFILE_LIST)))
71 pkgname = $(lastword $(subst /, ,$(pkgdir)))
73 # Define extractors for different archive suffixes
74 INFLATE.bz2 = $(BZCAT)
75 INFLATE.gz = $(ZCAT)
76 INFLATE.lzma = $(XZCAT)
77 INFLATE.tbz = $(BZCAT)
78 INFLATE.tbz2 = $(BZCAT)
79 INFLATE.tgz = $(ZCAT)
80 INFLATE.xz = $(XZCAT)
81 INFLATE.tar = cat
82 # suitable-extractor(filename): returns extractor based on suffix
83 suitable-extractor = $(INFLATE$(suffix $(1)))
85 # MESSAGE Macro -- display a message in bold type
86 MESSAGE = echo "$(TERM_BOLD)>>> $($(PKG)_NAME) $($(PKG)_VERSION) $(call qstrip,$(1))$(TERM_RESET)"
87 TERM_BOLD := $(shell tput smso 2>/dev/null)
88 TERM_RESET := $(shell tput rmso 2>/dev/null)
90 # Utility functions for 'find'
91 # findfileclauses(filelist) => -name 'X' -o -name 'Y'
92 findfileclauses = $(call notfirstword,$(patsubst %,-o -name '%',$(1)))
93 # finddirclauses(base, dirlist) => -path 'base/dirX' -o -path 'base/dirY'
94 finddirclauses = $(call notfirstword,$(patsubst %,-o -path '$(1)/%',$(2)))
96 # Miscellaneous utility functions
97 # notfirstword(wordlist): returns all but the first word in wordlist
98 notfirstword = $(wordlist 2,$(words $(1)),$(1))
100 # Needed for the foreach loops to loop over the list of hooks, so that
101 # each hook call is properly separated by a newline.
102 define sep
105 endef
107 PERCENT = %
108 QUOTE = '
109 # ' # Meh... syntax-highlighting
111 # This macro properly escapes a command string, then prints it with printf:
113 # - first, backslash '\' are self-escaped, so that they do not escape
114 # the following char and so that printf properly outputs a backslash;
116 # - next, single quotes are escaped by closing an existing one, adding
117 # an escaped one, and re-openning a new one (see below for the reason);
119 # - then '%' signs are self-escaped so that the printf does not interpret
120 # them as a format specifier, in case the variable contains an actual
121 # printf with a format;
123 # - finally, $(sep) is replaced with the literal '\n' so that make does
124 # not break on the so-expanded variable, but so that the printf does
125 # correctly output an LF.
127 # Note: this must be escaped in this order to avoid over-escaping the
128 # previously escaped elements.
130 # Once everything has been escaped, it is passed between single quotes
131 # (that's why the single-quotes are escaped they way they are, above,
132 # and why the dollar sign is not escaped) to printf(1). A trailing
133 # newline is apended, too.
135 # Note: leading or trailing spaces are *not* stripped.
137 define PRINTF
138 printf '$(subst $(sep),\n,\
139 $(subst $(PERCENT),$(PERCENT)$(PERCENT),\
140 $(subst $(QUOTE),$(QUOTE)\$(QUOTE)$(QUOTE),\
141 $(subst \,\\,$(1)))))\n'
142 endef
144 # check-deprecated-variable -- throw an error on deprecated variables
145 # example:
146 # $(eval $(call check-deprecated-variable,FOO_MAKE_OPT,FOO_MAKE_OPTS))
147 define check-deprecated-variable # (deprecated var, new var)
148 ifneq ($$(origin $(1)),undefined)
149 $$(error Package error: use $(2) instead of $(1). Please fix your .mk file)
150 endif
151 endef
154 # legal-info helper functions
156 LEGAL_INFO_SEPARATOR = "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
158 define legal-warning # text
159 echo "WARNING: $(1)" >>$(LEGAL_WARNINGS)
160 endef
162 define legal-warning-pkg # pkg, text
163 echo "WARNING: $(1): $(2)" >>$(LEGAL_WARNINGS)
164 endef
166 define legal-warning-nosource # pkg, {local|override}
167 $(call legal-warning-pkg,$(1),sources not saved ($(2) packages not handled))
168 endef
170 define legal-manifest # pkg, version, license, license-files, source, url, {HOST|TARGET}
171 echo '"$(1)","$(2)","$(3)","$(4)","$(5)","$(6)"' >>$(LEGAL_MANIFEST_CSV_$(7))
172 endef
174 define legal-license-header # pkg, license-file, {HOST|TARGET}
175 printf "$(LEGAL_INFO_SEPARATOR)\n\t$(1):\
176 $(2)\n$(LEGAL_INFO_SEPARATOR)\n\n\n" >>$(LEGAL_LICENSES_TXT_$(3))
177 endef
179 define legal-license-nofiles # pkg, {HOST|TARGET}
180 $(call legal-license-header,$(1),unknown license file(s),$(2))
181 endef
183 define legal-license-file # pkg, filename, file-fullpath, {HOST|TARGET}
184 $(call legal-license-header,$(1),$(2) file,$(4)) && \
185 cat $(3) >>$(LEGAL_LICENSES_TXT_$(4)) && \
186 echo >>$(LEGAL_LICENSES_TXT_$(4)) && \
187 mkdir -p $(LICENSE_FILES_DIR_$(4))/$(1)/$(dir $(2)) && \
188 cp $(3) $(LICENSE_FILES_DIR_$(4))/$(1)/$(2)
189 endef