pkg-utils.mk 7.5 KB

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