lualogging: add hash file
[buildroot-gz.git] / package / pkg-utils.mk
blob44bd2c93055852adcc7a198ce99f98e77802682b
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)
88 TERM_RESET := $(shell tput rmso)
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 # check-deprecated-variable -- throw an error on deprecated variables
108 # example:
109 # $(eval $(call check-deprecated-variable,FOO_MAKE_OPT,FOO_MAKE_OPTS))
110 define check-deprecated-variable # (deprecated var, new var)
111 ifneq ($$(origin $(1)),undefined)
112 $$(error Package error: use $(2) instead of $(1). Please fix your .mk file)
113 endif
114 endef
117 # legal-info helper functions
119 LEGAL_INFO_SEPARATOR = "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
121 define legal-warning # text
122 echo "WARNING: $(1)" >>$(LEGAL_WARNINGS)
123 endef
125 define legal-warning-pkg # pkg, text
126 echo "WARNING: $(1): $(2)" >>$(LEGAL_WARNINGS)
127 endef
129 define legal-warning-nosource # pkg, {local|override}
130 $(call legal-warning-pkg,$(1),sources not saved ($(2) packages not handled))
131 endef
133 define legal-manifest # pkg, version, license, license-files, source, url, {HOST|TARGET}
134 echo '"$(1)","$(2)","$(3)","$(4)","$(5)","$(6)"' >>$(LEGAL_MANIFEST_CSV_$(7))
135 endef
137 define legal-license-header # pkg, license-file, {HOST|TARGET}
138 printf "$(LEGAL_INFO_SEPARATOR)\n\t$(1):\
139 $(2)\n$(LEGAL_INFO_SEPARATOR)\n\n\n" >>$(LEGAL_LICENSES_TXT_$(3))
140 endef
142 define legal-license-nofiles # pkg, {HOST|TARGET}
143 $(call legal-license-header,$(1),unknown license file(s),$(2))
144 endef
146 define legal-license-file # pkg, filename, file-fullpath, {HOST|TARGET}
147 $(call legal-license-header,$(1),$(2) file,$(4)) && \
148 cat $(3) >>$(LEGAL_LICENSES_TXT_$(4)) && \
149 echo >>$(LEGAL_LICENSES_TXT_$(4)) && \
150 mkdir -p $(LICENSE_FILES_DIR_$(4))/$(1)/$(dir $(2)) && \
151 cp $(3) $(LICENSE_FILES_DIR_$(4))/$(1)/$(2)
152 endef