xref: /OK3568_Linux_fs/buildroot/package/pkg-kconfig.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1################################################################################
2# Kconfig package infrastructure
3#
4# This file implements an infrastructure that eases development of
5# package .mk files for packages that use kconfig for configuration files.
6# It is based on the generic-package infrastructure, and inherits all of its
7# features.
8#
9# See the Buildroot documentation for details on the usage of this
10# infrastructure.
11#
12################################################################################
13
14# Macro to update back the custom (def)config file
15# Must only be called if $(PKG)_KCONFIG_FILE is set and $(PKG)_KCONFIG_DEFCONFIG)
16# is not set.
17# $(1): file to copy from
18define kconfig-package-update-config
19	@$(if $($(PKG)_KCONFIG_FRAGMENT_FILES), \
20		echo "Unable to perform $(@) when fragment files are set"; exit 1)
21	$(Q)if [ -d $($(PKG)_KCONFIG_FILE) ]; then \
22		echo "Unable to perform $(@) when $($(PKG)_KCONFIG_FILE) is a directory"; \
23		exit 1; \
24	fi
25	$(Q)mkdir -p $(dir $($(PKG)_KCONFIG_FILE))
26	cp -f $($(PKG)_DIR)/$(1) $($(PKG)_KCONFIG_FILE)
27	$(Q)touch --reference $($(PKG)_DIR)/$($(PKG)_KCONFIG_STAMP_DOTCONFIG) $($(PKG)_KCONFIG_FILE)
28endef
29
30PKG_KCONFIG_COMMON_OPTS = \
31	HOSTCC="$(HOSTCC_NOCCACHE)"
32
33# Macro to save the defconfig file
34# $(1): the name of the package in upper-case letters
35define kconfig-package-savedefconfig
36	$($(1)_MAKE_ENV) $($(1)_MAKE) -C $($(1)_DIR) \
37		$(PKG_KCONFIG_COMMON_OPTS) $($(1)_KCONFIG_OPTS) savedefconfig
38endef
39
40# The correct way to regenerate a .config file is to use 'make olddefconfig'.
41# For historical reasons, the target name is 'oldnoconfig' between Linux kernel
42# versions 2.6.36 and 3.6, and remains as an alias in later versions.
43# In older versions, and in some other projects that use kconfig, the target is
44# not supported at all, and we use 'yes "" | make oldconfig' as a fallback
45# only, as this can fail in complex cases.
46# $(1): the name of the package in upper-case letters
47define kconfig-package-regen-dot-config
48	$(if $(filter olddefconfig,$($(1)_KCONFIG_RULES)),
49		$(Q)$($(1)_KCONFIG_MAKE) olddefconfig,
50		$(if $(filter oldnoconfig,$($(1)_KCONFIG_RULES)),
51			$(Q)$($(1)_KCONFIG_MAKE) oldnoconfig,
52			$(Q)(yes "" | $($(1)_KCONFIG_MAKE) oldconfig)))
53endef
54
55# Macro to create a .config file where all given fragments are merged into.
56# $(1): the name of the package in upper-case letters
57# $(2): name of the .config file
58# $(3): fragment files to merge
59define kconfig-package-merge-config
60	$(Q)$(if $($(1)_KCONFIG_DEFCONFIG),\
61		$($(1)_KCONFIG_MAKE) $($(1)_KCONFIG_DEFCONFIG),\
62		$(INSTALL) -m 0644 -D $($(1)_KCONFIG_FILE) $(2))
63	$(Q)support/kconfig/merge_config.sh -m -O $(dir $(2)) $(2) $(3)
64	$(call kconfig-package-regen-dot-config,$(1))
65endef
66
67################################################################################
68# inner-kconfig-package -- generates the make targets needed to support a
69# kconfig package
70#
71#  argument 1 is the lowercase package name
72#  argument 2 is the uppercase package name, including a HOST_ prefix
73#             for host packages
74#  argument 3 is the uppercase package name, without the HOST_ prefix
75#             for host packages
76#  argument 4 is the type (target or host)
77################################################################################
78
79define inner-kconfig-package
80
81# Default values
82$(2)_MAKE ?= $$(MAKE)
83$(2)_KCONFIG_EDITORS ?= menuconfig
84$(2)_KCONFIG_OPTS ?=
85$(2)_KCONFIG_FIXUP_CMDS ?=
86$(2)_KCONFIG_FRAGMENT_FILES ?=
87$(2)_KCONFIG_DOTCONFIG ?= .config
88$(2)_KCONFIG_SUPPORTS_DEFCONFIG ?= YES
89
90# Register the kconfig dependencies as regular dependencies, so that
91# they are also accounted for in the generated graphs.
92$(2)_DEPENDENCIES += $$($(2)_KCONFIG_DEPENDENCIES)
93
94# Generate the kconfig-related help: one entry for each editor.
95# Additionally, if the package is *not* using an in-tree defconfig
96# name, an entry for updating the package configuration file.
97ifndef $(2)_HELP_CMDS
98define $(2)_HELP_CMDS
99	$$(foreach editor, $$($(2)_KCONFIG_EDITORS), \
100		@printf '  %-22s - Run %s %s\n' $(1)-$$(editor) $(1) $$(editor)
101	)
102	$$(if $$($(2)_KCONFIG_DEFCONFIG),,\
103		$$(if $$(filter YES,$$($(2)_KCONFIG_SUPPORTS_DEFCONFIG)),\
104			@printf '  %-22s - Save the %s configuration as a defconfig file\n' \
105				$(1)-update-defconfig $(1)
106			@printf '  %-22s     to %s\n' '' $$($(2)_KCONFIG_FILE)
107			@printf '  %-22s     (or override with %s_KCONFIG_FILE)\n' '' $(2)
108		)
109		@printf '  %-22s - Save the %s configuration as a full .config file\n' \
110			$(1)-update-config $(1)
111		@printf '  %-22s     to %s\n' '' $$($(2)_KCONFIG_FILE)
112		@printf '  %-22s     (or override with %s_KCONFIG_FILE)\n' '' $(2)
113	)
114endef
115endif
116
117# Call the generic package infrastructure to generate the necessary
118# make targets.
119# Note: this must be done _before_ attempting to use $$($(2)_DIR) in a
120# dependency expression
121$(call inner-generic-package,$(1),$(2),$(3),$(4))
122
123# Do not use $(2)_KCONFIG_DOTCONFIG as stamp file, because the package
124# buildsystem (e.g. linux >= 4.19) may touch it, thus rendering our
125# timestamps out of date, thus re-trigerring the build of the package.
126# Instead, use a specific file of our own as timestamp.
127$(2)_KCONFIG_STAMP_DOTCONFIG = .stamp_dotconfig
128
129# The config file as well as the fragments could be in-tree, so before
130# depending on them the package should be extracted (and patched) first.
131#
132# Since those files only have a order-only dependency, make would treat
133# any missing one as a "force" target:
134#   https://www.gnu.org/software/make/manual/make.html#Force-Targets
135# and would forcibly any rule that depend on those files, causing a
136# rebuild of the kernel each time make is called.
137#
138# So, we provide a recipe that checks all of those files exist, to
139# overcome that standard make behaviour.
140#
141$$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES): | $(1)-patch
142	for f in $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES); do \
143		if [ ! -f "$$$${f}" ]; then \
144			printf "Kconfig file or fragment '%s' for '%s' does not exist\n" "$$$${f}" "$(1)"; \
145			exit 1; \
146		fi; \
147	done
148
149$(2)_KCONFIG_MAKE = \
150	$$($(2)_MAKE_ENV) $$($(2)_MAKE) -C $$($(2)_DIR) \
151		$$(PKG_KCONFIG_COMMON_OPTS) $$($(2)_KCONFIG_OPTS)
152
153# $(2)_KCONFIG_MAKE may already rely on shell expansion. As the $() syntax
154# of the shell conflicts with Make's own syntax, this means that backticks
155# are used with those shell constructs. Unfortunately, the backtick syntax
156# does not nest, and we need to use Make instead of the shell to handle
157# conditions.
158
159# A recursively expanded variable is necessary, to be sure that the shell
160# command is called when the rule is processed during the build and not
161# when the rule is created when parsing all packages.
162$(2)_KCONFIG_RULES = \
163	$$(shell $$($(2)_KCONFIG_MAKE) -pn config 2>/dev/null | \
164		sed 's/^\([_0-9a-zA-Z]*config\):.*/\1/ p; d')
165
166# The specified source configuration file and any additional configuration file
167# fragments are merged together to .config, after the package has been patched.
168# Since the file could be a defconfig file it needs to be expanded to a
169# full .config first.
170$$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES)
171	$$(call prepare-per-package-directory,$$($(2)_KCONFIG_DEPENDENCIES))
172	$$(call kconfig-package-merge-config,$(2),$$(@D)/$$($(2)_KCONFIG_DOTCONFIG),\
173		$$($(2)_KCONFIG_FRAGMENT_FILES))
174	$$(Q)touch $$(@D)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
175
176# If _KCONFIG_FILE or _KCONFIG_FRAGMENT_FILES exists, this dependency is
177# already implied, but if we only have a _KCONFIG_DEFCONFIG we have to add
178# it explicitly. It doesn't hurt to always have it though.
179$$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): | $(1)-patch
180
181# Some packages may need additional tools to be present by the time their
182# kconfig structure is parsed (e.g. the linux kernel may need to call to
183# the compiler to test its features).
184$$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG): | $$($(2)_KCONFIG_DEPENDENCIES)
185
186# In order to get a usable, consistent configuration, some fixup may be needed.
187# The exact rules are specified by the package .mk file.
188define $(2)_FIXUP_DOT_CONFIG
189	$$($(2)_KCONFIG_FIXUP_CMDS)
190	$$(call kconfig-package-regen-dot-config,$(2))
191	$$(Q)touch $$($(2)_DIR)/.stamp_kconfig_fixup_done
192endef
193
194$$($(2)_DIR)/.stamp_kconfig_fixup_done: PKG=$(2)
195$$($(2)_DIR)/.stamp_kconfig_fixup_done: $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
196	$$($(2)_FIXUP_DOT_CONFIG)
197
198# Before running configure, the configuration file should be present and fixed
199$$($(2)_TARGET_CONFIGURE): $$($(2)_DIR)/.stamp_kconfig_fixup_done
200
201# Force olddefconfig again on -reconfigure
202$(1)-clean-for-reconfigure: $(1)-clean-kconfig-for-reconfigure
203
204$(1)-clean-kconfig-for-reconfigure:
205	rm -f $$($(2)_DIR)/$$($(2)_KCONFIG_STAMP_DOTCONFIG)
206
207# Only enable the foo-*config targets when the package is actually enabled.
208# Note: the variable $(2)_KCONFIG_VAR is not related to the kconfig
209# infrastructure, but defined by pkg-generic.mk. The generic infrastructure is
210# already called above, so we can effectively use this variable.
211ifeq ($$($$($(2)_KCONFIG_VAR)),y)
212
213ifeq ($$(BR_BUILDING),y)
214# Either FOO_KCONFIG_FILE or FOO_KCONFIG_DEFCONFIG is required...
215ifeq ($$(or $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
216$$(error Internal error: no value specified for $(2)_KCONFIG_FILE or $(2)_KCONFIG_DEFCONFIG)
217endif
218# ... but not both:
219ifneq ($$(and $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
220$$(error Internal error: $(2)_KCONFIG_FILE and $(2)_KCONFIG_DEFCONFIG are mutually exclusive but both are defined)
221endif
222endif
223
224# For the configurators, we do want to use the system-provided host
225# tools, not the ones we build. This is particularly true for
226# pkg-config; if we use our pkg-config (from host-pkgconf), then it
227# would not look for the .pc from the host, but we do need them,
228# especially to find ncurses, GTK+, Qt (resp. for menuconfig and
229# nconfig, gconfig, xconfig).
230# So we simply remove our PATH and PKG_CONFIG_* variables.
231$(2)_CONFIGURATOR_MAKE_ENV = \
232	$$(filter-out PATH=% PKG_CONFIG=% PKG_CONFIG_SYSROOT_DIR=% \
233	   PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=% PKG_CONFIG_ALLOW_SYSTEM_LIBS=% \
234	   PKG_CONFIG_LIBDIR=%,$$($(2)_MAKE_ENV)) \
235	PKG_CONFIG_PATH="$(HOST_PKG_CONFIG_PATH)"
236
237# Configuration editors (menuconfig, ...)
238#
239# We need to apply the configuration fixups right after a configuration
240# editor exits, so that it is possible to save the configuration right
241# after exiting an editor, and so the user always sees a .config file
242# that is clean wrt. our requirements.
243#
244# Because commands in $(1)_FIXUP_KCONFIG are probably using $(@D), we
245# need to have a valid @D set. But, because the configurators rules are
246# not real files and do not contain the path to the package build dir,
247# @D would be just '.' in this case. So, we use an intermediate rule
248# with a stamp-like file which path is in the package build dir, so we
249# end up having a valid @D.
250#
251$$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS)): $(1)-%: $$($(2)_DIR)/.kconfig_editor_%
252$$($(2)_DIR)/.kconfig_editor_%: PKG=$(2)
253$$($(2)_DIR)/.kconfig_editor_%: $$($(2)_DIR)/.stamp_kconfig_fixup_done
254	$$($(2)_CONFIGURATOR_MAKE_ENV) $$($(2)_MAKE) -C $$($(2)_DIR) \
255		$$(PKG_KCONFIG_COMMON_OPTS) $$($(2)_KCONFIG_OPTS) $$(*)
256	rm -f $$($(2)_DIR)/.stamp_{kconfig_fixup_done,configured,built}
257	rm -f $$($(2)_DIR)/.stamp_{target,staging,images}_installed
258	$$($(2)_FIXUP_DOT_CONFIG)
259
260# Saving back the configuration
261#
262# Ideally, that should directly depend on $$($(2)_DIR)/.stamp_kconfig_fixup_done,
263# but that breaks the use-case in PR-8156 (from a clean tree):
264#   make menuconfig           <- enable kernel, use an in-tree defconfig, save and exit
265#   make linux-menuconfig     <- enable/disable whatever option, save and exit
266#   make menuconfig           <- change to use a custom defconfig file, set a path, save and exit
267#   make linux-update-config  <- should save to the new custom defconfig file
268#
269# Because of that use-case, saving the configuration can *not* directly
270# depend on the stamp file, because it itself depends on the .config,
271# which in turn depends on the (newly-set an non-existent) custom
272# defconfig file.
273#
274# Instead, we use a PHONY rule that will catch that situation.
275#
276$(1)-check-configuration-done:
277	@if [ ! -f $$($(2)_DIR)/.stamp_kconfig_fixup_done ]; then \
278		echo "$(1) is not yet configured"; \
279		exit 1; \
280	fi
281
282ifeq ($$($(2)_KCONFIG_SUPPORTS_DEFCONFIG),YES)
283.PHONY: $(1)-savedefconfig
284$(1)-savedefconfig: $(1)-check-configuration-done
285	$$(call kconfig-package-savedefconfig,$(2))
286endif
287
288ifeq ($$($(2)_KCONFIG_DEFCONFIG),)
289# Target to copy back the configuration to the source configuration file
290# Even though we could use 'cp --preserve-timestamps' here, the separate
291# cp and 'touch --reference' is used for symmetry with $(1)-update-defconfig.
292.PHONY: $(1)-update-config
293$(1)-update-config: PKG=$(2)
294$(1)-update-config: $(1)-check-configuration-done
295	$$(call kconfig-package-update-config,$$($(2)_KCONFIG_DOTCONFIG))
296
297ifeq ($$($(2)_KCONFIG_SUPPORTS_DEFCONFIG),YES)
298# Note: make sure the timestamp of the stored configuration is not newer than
299# the .config to avoid a useless rebuild. Note that, contrary to
300# $(1)-update-config, the reference for 'touch' is _not_ the file from which
301# we copy.
302.PHONY: $(1)-update-defconfig
303$(1)-update-defconfig: PKG=$(2)
304$(1)-update-defconfig: $(1)-savedefconfig
305	$$(call kconfig-package-update-config,defconfig)
306endif
307
308endif
309
310# Target to output differences between the configuration obtained via the
311# defconfig + fragments (if any) and the current configuration.
312# Note: it preserves the timestamp of the current configuration when moving it
313# around.
314$(1)-diff-config: $(1)-check-configuration-done
315	$$(Q)cp -a $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG) $$($(2)_DIR)/.config.dc.bak
316	$$(call kconfig-package-merge-config,$(2),$$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG),\
317		$$($(2)_KCONFIG_FRAGMENT_FILES))
318	$$(Q)utils/diffconfig $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG) \
319		 $$($(2)_DIR)/.config.dc.bak
320	$$(Q)cp -a $$($(2)_DIR)/.config.dc.bak $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG)
321	$$(Q)rm -f $$($(2)_DIR)/.config.dc.bak
322
323
324endif # package enabled
325
326.PHONY: \
327	$(1)-diff-config \
328	$(1)-check-configuration-done \
329	$$($(2)_DIR)/.kconfig_editor_% \
330	$$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS))
331
332endef # inner-kconfig-package
333
334################################################################################
335# kconfig-package -- the target generator macro for kconfig packages
336################################################################################
337
338kconfig-package = $(call inner-kconfig-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
339