pkg-utils.mk 6.8 KB

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