pkg-utils.mk 6.3 KB

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