pkg-utils.mk 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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)_CPE_ID_VALID), \
  110. $(comma) "cpe-id": "$($(1)_CPE_ID)" \
  111. )
  112. $(if $($(1)_IGNORE_CVES),
  113. $(comma) "ignore_cves": [
  114. $(call make-comma-list,$(sort $($(1)_IGNORE_CVES)))
  115. ]
  116. )
  117. endef
  118. define _json-info-pkg-details
  119. "version": "$($(1)_DL_VERSION)",
  120. "licenses": "$($(1)_LICENSE)",
  121. "dl_dir": "$($(1)_DL_SUBDIR)",
  122. "downloads": [
  123. $(foreach dl,$(sort $($(1)_ALL_DOWNLOADS)),
  124. {
  125. "source": "$(notdir $(dl))",
  126. "uris": [
  127. $(call make-comma-list,
  128. $(subst \|,|,
  129. $(call DOWNLOAD_URIS,$(dl),$(1))
  130. )
  131. )
  132. ]
  133. },
  134. )
  135. ],
  136. endef
  137. define _json-info-fs
  138. "dependencies": [
  139. $(call make-comma-list,$(sort $($(1)_DEPENDENCIES)))
  140. ]
  141. endef
  142. # clean-json -- cleanup pseudo-json into clean json:
  143. # - remove commas before closing ] and }
  144. # - minify with $(strip)
  145. clean-json = $(strip \
  146. $(subst $(comma)},}, $(subst $(comma)$(space)},$(space)}, \
  147. $(subst $(comma)],], $(subst $(comma)$(space)],$(space)], \
  148. $(strip $(1)) \
  149. )))) \
  150. )
  151. ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
  152. # rsync the contents of per-package directories
  153. # $1: space-separated list of packages to rsync from
  154. # $2: 'host' or 'target'
  155. # $3: destination directory
  156. define per-package-rsync
  157. mkdir -p $(3)
  158. $(foreach pkg,$(1),\
  159. rsync -a --link-dest=$(PER_PACKAGE_DIR)/$(pkg)/$(2)/ \
  160. $(PER_PACKAGE_DIR)/$(pkg)/$(2)/ \
  161. $(3)$(sep))
  162. endef
  163. # prepares the per-package HOST_DIR and TARGET_DIR of the current
  164. # package, by rsync the host and target directories of the
  165. # dependencies of this package. The list of dependencies is passed as
  166. # argument, so that this function can be used to prepare with
  167. # different set of dependencies (download, extract, configure, etc.)
  168. #
  169. # $1: space-separated list of packages to rsync from
  170. define prepare-per-package-directory
  171. $(call per-package-rsync,$(1),host,$(HOST_DIR))
  172. $(call per-package-rsync,$(1),target,$(TARGET_DIR))
  173. endef
  174. endif
  175. #
  176. # legal-info helper functions
  177. #
  178. LEGAL_INFO_SEPARATOR = "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
  179. define legal-warning # text
  180. echo "WARNING: $(1)" >>$(LEGAL_WARNINGS)
  181. endef
  182. define legal-warning-pkg # pkg, text
  183. echo "WARNING: $(1): $(2)" >>$(LEGAL_WARNINGS)
  184. endef
  185. define legal-warning-nosource # pkg, {local|override}
  186. $(call legal-warning-pkg,$(1),sources not saved ($(2) packages not handled))
  187. endef
  188. define legal-manifest # {HOST|TARGET}, pkg, version, license, license-files, source, url, dependencies
  189. echo '"$(2)","$(3)","$(4)","$(5)","$(6)","$(7)","$(8)"' >>$(LEGAL_MANIFEST_CSV_$(1))
  190. endef
  191. define legal-license-file # pkgname, pkgname-pkgver, pkg-hashfile, filename, file-fullpath, {HOST|TARGET}
  192. mkdir -p $(LICENSE_FILES_DIR_$(6))/$(2)/$(dir $(4)) && \
  193. { \
  194. support/download/check-hash $(3) $(5) $(4); \
  195. case $${?} in (0|3) ;; (*) exit 1;; esac; \
  196. } && \
  197. cp $(5) $(LICENSE_FILES_DIR_$(6))/$(2)/$(4)
  198. endef
  199. non-virtual-deps = $(foreach p,$(1),$(if $($(call UPPERCASE,$(p))_IS_VIRTUAL),,$(p)))
  200. # Returns the list of recursive dependencies and their licensing terms
  201. # for the package specified in parameter (in lowercase). If that
  202. # package is a target package, remove host packages from the list.
  203. legal-deps = \
  204. $(foreach p,\
  205. $(filter-out $(if $(1:host-%=),host-%),\
  206. $(call non-virtual-deps,\
  207. $($(call UPPERCASE,$(1))_FINAL_RECURSIVE_DEPENDENCIES))),$(p) [$($(call UPPERCASE,$(p))_LICENSE)])