pkg-utils.mk 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. KCONFIG_ENABLE_OPT = $(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(1)=y, $(2))
  27. # KCONFIG_SET_OPT (option, value [, file])
  28. KCONFIG_SET_OPT = $(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(1)=$(2), $(3))
  29. # KCONFIG_DISABLE_OPT (option [, file])
  30. KCONFIG_DISABLE_OPT = $(call KCONFIG_MUNGE_DOT_CONFIG, $(1), $(SHARP_SIGN) $(1) is not set, $(2))
  31. # Helper functions to determine the name of a package and its
  32. # directory from its makefile directory, using the $(MAKEFILE_LIST)
  33. # variable provided by make. This is used by the *-package macros to
  34. # automagically find where the package is located.
  35. pkgdir = $(dir $(lastword $(MAKEFILE_LIST)))
  36. pkgname = $(lastword $(subst /, ,$(pkgdir)))
  37. # Helper to build the extension for a package archive, based on various
  38. # conditions.
  39. # $(1): upper-case package name
  40. pkg_source_ext = $(BR_FMT_VERSION_$($(1)_SITE_METHOD)).tar.gz
  41. # Define extractors for different archive suffixes
  42. INFLATE.bz2 = $(BZCAT)
  43. INFLATE.gz = $(ZCAT)
  44. INFLATE.lz = $(LZCAT)
  45. INFLATE.lzma = $(XZCAT)
  46. INFLATE.tbz = $(BZCAT)
  47. INFLATE.tbz2 = $(BZCAT)
  48. INFLATE.tgz = $(ZCAT)
  49. INFLATE.xz = $(XZCAT)
  50. INFLATE.tar = cat
  51. # suitable-extractor(filename): returns extractor based on suffix
  52. suitable-extractor = $(INFLATE$(suffix $(1)))
  53. EXTRACTOR_PKG_DEPENDENCY.lzma = $(BR2_XZCAT_HOST_DEPENDENCY)
  54. EXTRACTOR_PKG_DEPENDENCY.xz = $(BR2_XZCAT_HOST_DEPENDENCY)
  55. EXTRACTOR_PKG_DEPENDENCY.lz = $(BR2_LZIP_HOST_DEPENDENCY)
  56. # extractor-pkg-dependency(filename): returns a Buildroot package
  57. # dependency needed to extract file based on suffix
  58. extractor-pkg-dependency = $(EXTRACTOR_PKG_DEPENDENCY$(suffix $(1)))
  59. # extractor-system-dependency(filename): returns the name of the tool
  60. # needed to extract 'filename', and is meant to be used with
  61. # DL_TOOLS_DEPENDENCIES, in order to check that the necesary tool is
  62. # provided by the system Buildroot runs on.
  63. #
  64. # $(firstword) is used here because the extractor can have arguments,
  65. # like ZCAT="gzip -d -c", and to check for the dependency we only want
  66. # 'gzip'.
  67. extractor-system-dependency = $(if $(EXTRACTOR_PKG_DEPENDENCY$(suffix $(1))),,\
  68. $(firstword $(INFLATE$(suffix $(1)))))
  69. # check-deprecated-variable -- throw an error on deprecated variables
  70. # example:
  71. # $(eval $(call check-deprecated-variable,FOO_MAKE_OPT,FOO_MAKE_OPTS))
  72. define check-deprecated-variable # (deprecated var, new var)
  73. ifneq ($$(origin $(1)),undefined)
  74. $$(error Package error: use $(2) instead of $(1). Please fix your .mk file)
  75. endif
  76. endef
  77. # $(1): YES or NO
  78. define yesno-to-bool
  79. $(subst NO,false,$(subst YES,true,$(1)))
  80. endef
  81. # json-info -- return package or filesystem metadata formatted as an entry
  82. # of a JSON dictionnary
  83. # $(1): upper-case package or filesystem name
  84. define json-info
  85. "$($(1)_NAME)": {
  86. "name": "$($(1)_RAWNAME)",
  87. "type": "$($(1)_TYPE)",
  88. $(if $(filter rootfs,$($(1)_TYPE)), \
  89. $(call _json-info-fs,$(1)), \
  90. $(call _json-info-pkg,$(1)), \
  91. )
  92. }
  93. endef
  94. # _json-info-pkg, _json-info-pkg-details, _json-info-fs: private helpers
  95. # for json-info, above
  96. define _json-info-pkg
  97. $(if $($(1)_IS_VIRTUAL), \
  98. "virtual": true$(comma),
  99. "virtual": false$(comma)
  100. $(call _json-info-pkg-details,$(1)) \
  101. )
  102. "build_dir": "$(patsubst $(CONFIG_DIR)/%,%,$($(1)_BUILDDIR))",
  103. $(if $(filter target,$($(1)_TYPE)), \
  104. "install_target": $(call yesno-to-bool,$($(1)_INSTALL_TARGET))$(comma) \
  105. "install_staging": $(call yesno-to-bool,$($(1)_INSTALL_STAGING))$(comma) \
  106. "install_images": $(call yesno-to-bool,$($(1)_INSTALL_IMAGES))$(comma) \
  107. )
  108. "dependencies": [
  109. $(call make-comma-list,$(sort $($(1)_FINAL_ALL_DEPENDENCIES)))
  110. ],
  111. "reverse_dependencies": [
  112. $(call make-comma-list,$(sort $($(1)_RDEPENDENCIES)))
  113. ]
  114. $(if $($(1)_CPE_ID_VALID), \
  115. $(comma) "cpe-id": "$($(1)_CPE_ID)" \
  116. )
  117. $(if $($(1)_IGNORE_CVES),
  118. $(comma) "ignore_cves": [
  119. $(call make-comma-list,$(sort $($(1)_IGNORE_CVES)))
  120. ]
  121. )
  122. endef
  123. define _json-info-pkg-details
  124. "version": "$($(1)_DL_VERSION)",
  125. "licenses": "$($(1)_LICENSE)",
  126. "dl_dir": "$($(1)_DL_SUBDIR)",
  127. "downloads": [
  128. $(foreach dl,$(sort $($(1)_ALL_DOWNLOADS)),
  129. {
  130. "source": "$(notdir $(dl))",
  131. "uris": [
  132. $(call make-comma-list,
  133. $(subst \|,|,
  134. $(call DOWNLOAD_URIS,$(dl),$(1))
  135. )
  136. )
  137. ]
  138. },
  139. )
  140. ],
  141. endef
  142. define _json-info-fs
  143. "image_name": $(if $($(1)_FINAL_IMAGE_NAME), \
  144. "$($(1)_FINAL_IMAGE_NAME)", \
  145. null \
  146. ),
  147. "dependencies": [
  148. $(call make-comma-list,$(sort $($(1)_DEPENDENCIES)))
  149. ]
  150. endef
  151. # clean-json -- cleanup pseudo-json into clean json:
  152. # - remove commas before closing ] and }
  153. # - minify with $(strip)
  154. clean-json = $(strip \
  155. $(subst $(comma)},}, $(subst $(comma)$(space)},$(space)}, \
  156. $(subst $(comma)],], $(subst $(comma)$(space)],$(space)], \
  157. $(subst \,\\, \
  158. $(strip $(1)) \
  159. ))))) \
  160. )
  161. ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
  162. # rsync the contents of per-package directories
  163. # $1: space-separated list of packages to rsync from
  164. # $2: 'host' or 'target'
  165. # $3: destination directory
  166. define per-package-rsync
  167. mkdir -p $(3)
  168. $(foreach pkg,$(1),\
  169. rsync -a --link-dest=$(PER_PACKAGE_DIR)/$(pkg)/$(2)/ \
  170. $(PER_PACKAGE_DIR)/$(pkg)/$(2)/ \
  171. $(3)$(sep))
  172. endef
  173. # prepares the per-package HOST_DIR and TARGET_DIR of the current
  174. # package, by rsync the host and target directories of the
  175. # dependencies of this package. The list of dependencies is passed as
  176. # argument, so that this function can be used to prepare with
  177. # different set of dependencies (download, extract, configure, etc.)
  178. #
  179. # $1: space-separated list of packages to rsync from
  180. define prepare-per-package-directory
  181. $(call per-package-rsync,$(1),host,$(HOST_DIR))
  182. $(call per-package-rsync,$(1),target,$(TARGET_DIR))
  183. endef
  184. endif
  185. #
  186. # legal-info helper functions
  187. #
  188. LEGAL_INFO_SEPARATOR = "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
  189. define legal-warning # text
  190. echo "WARNING: $(1)" >>$(LEGAL_WARNINGS)
  191. endef
  192. define legal-warning-pkg # pkg, text
  193. echo "WARNING: $(1): $(2)" >>$(LEGAL_WARNINGS)
  194. endef
  195. define legal-warning-nosource # pkg, {local|override}
  196. $(call legal-warning-pkg,$(1),sources not saved ($(2) packages not handled))
  197. endef
  198. define legal-manifest # {HOST|TARGET}, pkg, version, license, license-files, source, url, dependencies
  199. echo '"$(2)","$(3)","$(4)","$(5)","$(6)","$(7)","$(8)"' >>$(LEGAL_MANIFEST_CSV_$(1))
  200. endef
  201. define legal-license-file # pkgname, pkgname-pkgver, pkg-hashfile, filename, file-fullpath, {HOST|TARGET}
  202. mkdir -p $(LICENSE_FILES_DIR_$(6))/$(2)/$(dir $(4)) && \
  203. { \
  204. support/download/check-hash $(3) $(5) $(4); \
  205. case $${?} in (0|3) ;; (*) exit 1;; esac; \
  206. } && \
  207. cp $(5) $(LICENSE_FILES_DIR_$(6))/$(2)/$(4)
  208. endef
  209. non-virtual-deps = $(foreach p,$(1),$(if $($(call UPPERCASE,$(p))_IS_VIRTUAL),,$(p)))
  210. # Returns the list of recursive dependencies and their licensing terms
  211. # for the package specified in parameter (in lowercase). If that
  212. # package is a target package, remove host packages from the list.
  213. legal-deps = \
  214. $(foreach p,\
  215. $(filter-out $(if $(1:host-%=),host-%),\
  216. $(call non-virtual-deps,\
  217. $($(call UPPERCASE,$(1))_FINAL_RECURSIVE_DEPENDENCIES))),$(p) [$($(call UPPERCASE,$(p))_LICENSE)])