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