pkg-utils.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ################################################################################
  2. #
  3. # This file contains various utility functions used by the package
  4. # infrastructure, or by the packages themselves.
  5. #
  6. ################################################################################
  7. #
  8. # Manipulation of .config files based on the Kconfig
  9. # infrastructure. Used by the BusyBox package, the Linux kernel
  10. # package, and more.
  11. #
  12. # KCONFIG_DOT_CONFIG ([file])
  13. # Returns the path to the .config file that should be used, which will
  14. # be $(1) if provided, or the current package .config file otherwise.
  15. KCONFIG_DOT_CONFIG = $(strip \
  16. $(if $(strip $(1)), $(1), \
  17. $($(PKG)_BUILDDIR)/$($(PKG)_KCONFIG_DOTCONFIG) \
  18. ) \
  19. )
  20. # KCONFIG_MUNGE_DOT_CONFIG (option, newline [, file])
  21. define KCONFIG_MUNGE_DOT_CONFIG
  22. $(SED) '/^\(# \)\?$(strip $(1))\>/d' $(call KCONFIG_DOT_CONFIG,$(3)) && \
  23. echo '$(strip $(2))' >> $(call KCONFIG_DOT_CONFIG,$(3))
  24. endef
  25. # KCONFIG_ENABLE_OPT (option [, file])
  26. # If the option is already set to =m or =y, ignore.
  27. define KCONFIG_ENABLE_OPT
  28. $(Q)if ! grep -q '^$(strip $(1))=[my]' $(call KCONFIG_DOT_CONFIG,$(2)); then \
  29. $(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(1)=y, $(2)); \
  30. fi
  31. endef
  32. # KCONFIG_SET_OPT (option, value [, file])
  33. KCONFIG_SET_OPT = $(Q)$(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(1)=$(2), $(3))
  34. # KCONFIG_DISABLE_OPT (option [, file])
  35. KCONFIG_DISABLE_OPT = $(Q)$(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(SHARP_SIGN) $(1) is not set, $(2))
  36. # Helper functions to determine the name of a package and its
  37. # directory from its makefile directory, using the $(MAKEFILE_LIST)
  38. # variable provided by make. This is used by the *-package macros to
  39. # automagically find where the package is located.
  40. pkgdir = $(dir $(lastword $(MAKEFILE_LIST)))
  41. pkgname = $(lastword $(subst /, ,$(pkgdir)))
  42. # Helper to build the extension for a package archive, based on various
  43. # conditions.
  44. # $(1): upper-case package name
  45. pkg_source_ext = $(BR_FMT_VERSION_$($(1)_SITE_METHOD)).tar.gz
  46. # Define extractors for different archive suffixes
  47. INFLATE.bz2 = $(BZCAT)
  48. INFLATE.gz = $(ZCAT)
  49. INFLATE.lz = $(LZCAT)
  50. INFLATE.lzma = $(XZCAT)
  51. INFLATE.tbz = $(BZCAT)
  52. INFLATE.tbz2 = $(BZCAT)
  53. INFLATE.tgz = $(ZCAT)
  54. INFLATE.xz = $(XZCAT)
  55. INFLATE.tar = cat
  56. # suitable-extractor(filename): returns extractor based on suffix
  57. suitable-extractor = $(INFLATE$(suffix $(1)))
  58. EXTRACTOR_PKG_DEPENDENCY.lzma = $(BR2_XZCAT_HOST_DEPENDENCY)
  59. EXTRACTOR_PKG_DEPENDENCY.xz = $(BR2_XZCAT_HOST_DEPENDENCY)
  60. EXTRACTOR_PKG_DEPENDENCY.lz = $(BR2_LZIP_HOST_DEPENDENCY)
  61. # extractor-pkg-dependency(filename): returns a Buildroot package
  62. # dependency needed to extract file based on suffix
  63. extractor-pkg-dependency = $(EXTRACTOR_PKG_DEPENDENCY$(suffix $(1)))
  64. # extractor-system-dependency(filename): returns the name of the tool
  65. # needed to extract 'filename', and is meant to be used with
  66. # DL_TOOLS_DEPENDENCIES, in order to check that the necessary tool is
  67. # provided by the system Buildroot runs on.
  68. #
  69. # $(firstword) is used here because the extractor can have arguments,
  70. # like ZCAT="gzip -d -c", and to check for the dependency we only want
  71. # 'gzip'.
  72. extractor-system-dependency = $(if $(EXTRACTOR_PKG_DEPENDENCY$(suffix $(1))),,\
  73. $(firstword $(INFLATE$(suffix $(1)))))
  74. # check-deprecated-variable -- throw an error on deprecated variables
  75. # example:
  76. # $(eval $(call check-deprecated-variable,FOO_MAKE_OPT,FOO_MAKE_OPTS))
  77. define check-deprecated-variable # (deprecated var, new var)
  78. ifneq ($$(origin $(1)),undefined)
  79. $$(error Package error: use $(2) instead of $(1). Please fix your .mk file)
  80. endif
  81. endef
  82. # $(1): YES or NO
  83. define yesno-to-bool
  84. $(subst NO,false,$(subst YES,true,$(1)))
  85. endef
  86. # json-info -- return package or filesystem metadata formatted as an entry
  87. # of a JSON dictionary
  88. # $(1): upper-case package or filesystem name
  89. define json-info
  90. "$($(1)_NAME)": {
  91. "type": $(call mk-json-str,$($(1)_TYPE)),
  92. $(if $(filter rootfs,$($(1)_TYPE)), \
  93. $(call _json-info-fs,$(1)), \
  94. $(call _json-info-pkg,$(1)), \
  95. )
  96. }
  97. endef
  98. # _json-info-pkg, _json-info-pkg-details, _json-info-fs: private helpers
  99. # for json-info, above
  100. define _json-info-pkg
  101. "name": $(call mk-json-str,$($(1)_RAWNAME)),
  102. $(if $($(1)_IS_VIRTUAL), \
  103. "virtual": true$(comma),
  104. "virtual": false$(comma)
  105. $(call _json-info-pkg-details,$(1)) \
  106. )
  107. "stamp_dir": $(call mk-json-str,$(patsubst $(CONFIG_DIR)/%,%,$($(1)_DIR))),
  108. "source_dir": $(call mk-json-str,$(patsubst $(CONFIG_DIR)/%,%,$($(1)_DIR))),
  109. "build_dir": $(call mk-json-str,$(patsubst $(CONFIG_DIR)/%,%,$($(1)_BUILDDIR))),
  110. $(if $(filter target,$($(1)_TYPE)), \
  111. "install_target": $(call yesno-to-bool,$($(1)_INSTALL_TARGET))$(comma) \
  112. "install_staging": $(call yesno-to-bool,$($(1)_INSTALL_STAGING))$(comma) \
  113. "install_images": $(call yesno-to-bool,$($(1)_INSTALL_IMAGES))$(comma) \
  114. )
  115. "dependencies": [
  116. $(call make-comma-list, \
  117. $(foreach dep,$(sort $($(1)_FINAL_ALL_DEPENDENCIES)), \
  118. $(call mk-json-str,$(dep)) \
  119. ) \
  120. )
  121. ],
  122. "reverse_dependencies": [
  123. $(call make-comma-list, \
  124. $(foreach dep,$(sort $($(1)_RDEPENDENCIES)), \
  125. $(call mk-json-str,$(dep)) \
  126. ) \
  127. )
  128. ]
  129. $(if $($(1)_CPE_ID_VALID), \
  130. $(comma) "cpe-id": $(call mk-json-str,$($(1)_CPE_ID)) \
  131. )
  132. $(if $($(1)_IGNORE_CVES),
  133. $(comma) "ignore_cves": [
  134. $(call make-comma-list, \
  135. $(foreach cve,$(sort $($(1)_IGNORE_CVES)), \
  136. $(call mk-json-str,$(cve)) \
  137. ) \
  138. )
  139. ]
  140. )
  141. endef
  142. define _json-info-pkg-details
  143. "version": $(call mk-json-str,$($(1)_DL_VERSION)),
  144. "licenses": $(call mk-json-str,$($(1)_LICENSE)),
  145. "license_files": [
  146. $(foreach f, $($(1)_LICENSE_FILES),$(call mk-json-str,$(f))$(comma))
  147. ],
  148. "redistributable": $(if $(filter NO,$($(1)_REDISTRIBUTE)),false,true),
  149. "dl_dir": $(call mk-json-str,$($(1)_DL_SUBDIR)),
  150. "downloads": [
  151. $(foreach dl,$(sort $($(1)_ALL_DOWNLOADS)),
  152. {
  153. "source": $(call mk-json-str,$(notdir $(dl))),
  154. "uris": [
  155. $(call make-comma-list, \
  156. $(foreach uri,$(call DOWNLOAD_URIS,$(dl),$(1)), \
  157. $(call mk-json-str,$(subst \|,|,$(uri))) \
  158. ) \
  159. )
  160. ]
  161. },
  162. )
  163. ],
  164. endef
  165. define _json-info-fs
  166. "image_name": $(if $($(1)_FINAL_IMAGE_NAME), \
  167. $(call mk-json-str,$($(1)_FINAL_IMAGE_NAME)), \
  168. null \
  169. ),
  170. "dependencies": [
  171. $(call make-comma-list, \
  172. $(foreach dep,$(sort $($(1)_DEPENDENCIES)), \
  173. $(call mk-json-str,$(dep)) \
  174. ) \
  175. )
  176. ]
  177. endef
  178. # clean-json -- cleanup pseudo-json into clean json:
  179. # - remove commas before closing ] and }
  180. # - minify with $(strip)
  181. clean-json = $(strip \
  182. $(subst $(comma)},}, $(subst $(comma)$(space)},$(space)}, \
  183. $(subst $(comma)],], $(subst $(comma)$(space)],$(space)], \
  184. $(strip $(1)) \
  185. )))) \
  186. )
  187. # mk-json-str -- escape and double-quote a string to make it a valid json string
  188. # - escape \
  189. # - escape "
  190. # - escape \n
  191. # - escape \t
  192. # - escape ESC
  193. # - escape SPACE (so that we can $(strip) a JSON blurb without squashing multiple spaces)
  194. # This unfortunately has to be on a single line...
  195. mk-json-str = "$(subst $(space),\u0020,$(subst $(escape),\u001b,$(subst $(tab),\t,$(subst $(sep),\n,$(subst ",\",$(subst \,\\,$(1)))))))"
  196. # )))))" # Syntax colouring
  197. ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
  198. # rsync the contents of per-package directories
  199. # $1: space-separated list of packages to rsync from
  200. # $2: 'host' or 'target'
  201. # $3: destination directory
  202. # $4: literal "copy" or "hardlink" to copy or hardlink files from src to dest
  203. define per-package-rsync
  204. mkdir -p $(3)
  205. $(foreach pkg,$(1),\
  206. rsync -a \
  207. --hard-links \
  208. $(if $(filter hardlink,$(4)), \
  209. --link-dest=$(PER_PACKAGE_DIR)/$(pkg)/$(2)/, \
  210. $(if $(filter copy,$(4)), \
  211. $(empty), \
  212. $(error per-package-rsync can only "copy" or "hardlink", not "$(4)") \
  213. ) \
  214. ) \
  215. $(PER_PACKAGE_DIR)/$(pkg)/$(2)/ \
  216. $(3)$(sep))
  217. endef
  218. # prepares the per-package HOST_DIR and TARGET_DIR of the current
  219. # package, by rsync the host and target directories of the
  220. # dependencies of this package. The list of dependencies is passed as
  221. # argument, so that this function can be used to prepare with
  222. # different set of dependencies (download, extract, configure, etc.)
  223. #
  224. # $1: space-separated list of packages to rsync from
  225. define prepare-per-package-directory
  226. $(call per-package-rsync,$(1),host,$(HOST_DIR),hardlink)
  227. $(call per-package-rsync,$(1),target,$(TARGET_DIR),hardlink)
  228. endef
  229. # Ensure files like .la, .pc, .pri, .cmake, and so on, point to the
  230. # proper staging and host directories for the current package: find
  231. # all text files that contain the PPD root, and replace it with the
  232. # current package's PPD.
  233. # $1: destination root directory containing host and staging
  234. define ppd-fixup-paths
  235. $(Q)grep --binary-files=without-match -lrZ '$(PER_PACKAGE_DIR)/[^/]\+/' $(HOST_DIR) \
  236. |while read -d '' f; do \
  237. file -b --mime-type "$${f}" | grep -q '^text/' || continue; \
  238. printf '%s\0' "$${f}"; \
  239. done \
  240. |xargs -0 --no-run-if-empty \
  241. $(SED) 's:$(PER_PACKAGE_DIR)/[^/]\+/:$(1)/:g'
  242. endef
  243. endif
  244. #
  245. # legal-info helper functions
  246. #
  247. LEGAL_INFO_SEPARATOR = "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
  248. define legal-warning # text
  249. echo "WARNING: $(1)" >>$(LEGAL_WARNINGS)
  250. endef
  251. define legal-warning-pkg # pkg, text
  252. echo "WARNING: $(1): $(2)" >>$(LEGAL_WARNINGS)
  253. endef
  254. define legal-warning-nosource # pkg, {local|override}
  255. $(call legal-warning-pkg,$(1),sources not saved ($(2) packages not handled))
  256. endef
  257. define legal-manifest # {HOST|TARGET}, pkg, version, license, license-files, source, url, dependencies
  258. echo '"$(2)","$(3)","$(4)","$(5)","$(6)","$(7)","$(8)"' >>$(LEGAL_MANIFEST_CSV_$(1))
  259. endef
  260. define legal-license-file # {HOST|TARGET}, pkgname, pkgname-pkgver, filename, file-fullpath, pkg-hashfiles
  261. mkdir -p $(LICENSE_FILES_DIR_$(1))/$(3)/$(dir $(4)) && \
  262. { \
  263. support/download/check-hash $(5) $(4) $(6); \
  264. case $${?} in (0|3) ;; (*) exit 1;; esac; \
  265. } && \
  266. cp $(5) $(LICENSE_FILES_DIR_$(1))/$(3)/$(4)
  267. endef
  268. non-virtual-deps = $(foreach p,$(1),$(if $($(call UPPERCASE,$(p))_IS_VIRTUAL),,$(p)))
  269. # Returns the list of recursive dependencies and their licensing terms
  270. # for the package specified in parameter (in lowercase). If that
  271. # package is a target package, remove host packages from the list.
  272. legal-deps = \
  273. $(foreach p,\
  274. $(filter-out $(if $(1:host-%=),host-%),\
  275. $(call non-virtual-deps,\
  276. $($(call UPPERCASE,$(1))_FINAL_RECURSIVE_DEPENDENCIES))),$(p) [$($(call UPPERCASE,$(p))_LICENSE)])
  277. # Helper for self-extracting binaries distributed by NXP, and
  278. # formerlly Freescale.
  279. #
  280. # The --force option makes sure it doesn't fail if the source
  281. # directory already exists. The --auto-accept skips the license check,
  282. # as it is not needed in Buildroot because we have legal-info. Since
  283. # there's a EULA in the binary file, we extract it in this macro, and
  284. # it should therefore be added to the LICENSE_FILES variable of
  285. # packages using this macro. Also, remember to set REDISTRIBUTE to
  286. # "NO". Indeed, this is a legal minefield: the EULA specifies that the
  287. # Board Support Package includes software and hardware (sic!) for
  288. # which a separate license is needed...
  289. #
  290. # $(1): full path to the archive file
  291. #
  292. define NXP_EXTRACT_HELPER
  293. awk 'BEGIN { start = 0; } \
  294. /^EOEULA/ { start = 0; } \
  295. { if (start) print; } \
  296. /<<EOEULA/ { start = 1; }' \
  297. $(1) > $(@D)/EULA
  298. cd $(@D) && sh $(1) --force --auto-accept
  299. find $(@D)/$(basename $(notdir $(1))) -mindepth 1 -maxdepth 1 -exec mv {} $(@D) \;
  300. rmdir $(@D)/$(basename $(notdir $(1)))
  301. endef