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