pkg-utils.mk 12 KB

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