]> Git Repo - buildroot-mgba.git/blame - package/pkg-kconfig.mk
infra/pkg-kconfig: commonalise update-(def)config code
[buildroot-mgba.git] / package / pkg-kconfig.mk
CommitLineData
abb8a8da
TDS
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
f542085a
YM
14# Macro to update back the custom (def)config file
15# $(1): file to copy from
16define kconfig-package-update-config
17 @$(if $($(PKG)_KCONFIG_FRAGMENT_FILES), \
18 echo "Unable to perform $(@) when fragment files are set"; exit 1)
19 @$(if $($(PKG)_KCONFIG_DEFCONFIG), \
20 echo "Unable to perform $(@) when using a defconfig rule"; exit 1)
21 cp -f $($(PKG)_DIR)/$(1) $($(PKG)_KCONFIG_FILE)
22 touch --reference $($(PKG)_DIR)/$($(PKG)_KCONFIG_DOTCONFIG) $($(PKG)_KCONFIG_FILE)
23endef
24
abb8a8da
TDS
25################################################################################
26# inner-kconfig-package -- generates the make targets needed to support a
27# kconfig package
28#
29# argument 1 is the lowercase package name
30# argument 2 is the uppercase package name, including a HOST_ prefix
31# for host packages
32# argument 3 is the uppercase package name, without the HOST_ prefix
33# for host packages
34# argument 4 is the type (target or host)
35################################################################################
36
37define inner-kconfig-package
38
39# Call the generic package infrastructure to generate the necessary
40# make targets.
41# Note: this must be done _before_ attempting to use $$($(2)_DIR) in a
42# dependency expression
43$(call inner-generic-package,$(1),$(2),$(3),$(4))
44
45# Default values
abb8a8da 46$(2)_KCONFIG_EDITORS ?= menuconfig
146f7dc4 47$(2)_KCONFIG_OPTS ?=
abb8a8da 48$(2)_KCONFIG_FIXUP_CMDS ?=
0cfb5549 49$(2)_KCONFIG_FRAGMENT_FILES ?=
bbbc8b5d 50$(2)_KCONFIG_DOTCONFIG ?= .config
abb8a8da 51
f3938f39 52# The config file as well as the fragments could be in-tree, so before
71483616
YM
53# depending on them the package should be extracted (and patched) first.
54#
55# Since those files only have a order-only dependency, make would treat
56# any missing one as a "force" target:
57# https://www.gnu.org/software/make/manual/make.html#Force-Targets
58# and would forcibly any rule that depend on those files, causing a
59# rebuild of the kernel each time make is called.
60#
61# So, we provide a recipe that checks all of those files exist, to
62# overcome that standard make behaviour.
63#
f3938f39 64$$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES): | $(1)-patch
71483616
YM
65 for f in $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES); do \
66 if [ ! -f "$$$${f}" ]; then \
67 printf "Kconfig fragment '%s' for '%s' does not exist\n" "$$$${f}" "$(1)"; \
68 exit 1; \
69 fi; \
70 done
23fd6e0e 71
ce83c3d5
RI
72$(2)_KCONFIG_MAKE = \
73 $$($(2)_MAKE_ENV) $$(MAKE) -C $$($(2)_DIR) $$($(2)_KCONFIG_OPTS)
74
0370cc97
RI
75# $(2)_KCONFIG_MAKE may already rely on shell expansion. As the $() syntax
76# of the shell conflicts with Make's own syntax, this means that backticks
77# are used with those shell constructs. Unfortunately, the backtick syntax
78# does not nest, and we need to use Make instead of the shell to handle
79# conditions.
80
81# A recursively expanded variable is necessary, to be sure that the shell
82# command is called when the rule is processed during the build and not
83# when the rule is created when parsing all packages.
84$(2)_KCONFIG_RULES = \
85 $$(shell $$($(2)_KCONFIG_MAKE) -pn config 2>/dev/null | \
86 sed 's/^\([_0-9a-zA-Z]*config\):.*/\1/ p; d')
87
88# The correct way to regenerate a .config file is to use 'make olddefconfig'.
89# For historical reasons, the target name is 'oldnoconfig' between Linux kernel
90# versions 2.6.36 and 3.6, and remains as an alias in later versions.
91# In older versions, and in some other projects that use kconfig, the target is
92# not supported at all, and we use 'yes "" | make oldconfig' as a fallback
93# only, as this can fail in complex cases.
94define $(2)_REGEN_DOT_CONFIG
95 $$(if $$(filter olddefconfig,$$($(2)_KCONFIG_RULES)),
96 $$(Q)$$($(2)_KCONFIG_MAKE) olddefconfig,
97 $$(if $$(filter oldnoconfig,$$($(2)_KCONFIG_RULES)),
98 $$(Q)$$($(2)_KCONFIG_MAKE) oldnoconfig,
99 $$(Q)(yes "" | $$($(2)_KCONFIG_MAKE) oldconfig)))
100endef
101
0cfb5549
FB
102# The specified source configuration file and any additional configuration file
103# fragments are merged together to .config, after the package has been patched.
39867f3c 104# Since the file could be a defconfig file it needs to be expanded to a
0370cc97 105# full .config first.
bbbc8b5d 106$$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG): $$($(2)_KCONFIG_FILE) $$($(2)_KCONFIG_FRAGMENT_FILES)
15a4210c 107 $$(Q)$$(if $$($(2)_KCONFIG_DEFCONFIG), \
ce83c3d5 108 $$($(2)_KCONFIG_MAKE) $$($(2)_KCONFIG_DEFCONFIG), \
bbbc8b5d 109 $$(INSTALL) -m 0644 -D $$($(2)_KCONFIG_FILE) $$(@))
15a4210c 110 $$(Q)support/kconfig/merge_config.sh -m -O $$(@D) \
8ef62b99 111 $$(@) $$($(2)_KCONFIG_FRAGMENT_FILES)
0370cc97 112 $$($(2)_REGEN_DOT_CONFIG)
abb8a8da 113
c65612ff
AV
114# If _KCONFIG_FILE or _KCONFIG_FRAGMENT_FILES exists, this dependency is
115# already implied, but if we only have a _KCONFIG_DEFCONFIG we have to add
116# it explicitly. It doesn't hurt to always have it though.
bbbc8b5d 117$$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG): | $(1)-patch
c65612ff 118
abb8a8da
TDS
119# In order to get a usable, consistent configuration, some fixup may be needed.
120# The exact rules are specified by the package .mk file.
0e090364 121define $(2)_FIXUP_DOT_CONFIG
abb8a8da 122 $$($(2)_KCONFIG_FIXUP_CMDS)
0370cc97 123 $$($(2)_REGEN_DOT_CONFIG)
0e090364
YM
124 $$(Q)touch $$($(2)_DIR)/.stamp_kconfig_fixup_done
125endef
126
bbbc8b5d 127$$($(2)_DIR)/.stamp_kconfig_fixup_done: $$($(2)_DIR)/$$($(2)_KCONFIG_DOTCONFIG)
e645a3b0 128 $$($(2)_FIXUP_DOT_CONFIG)
abb8a8da
TDS
129
130# Before running configure, the configuration file should be present and fixed
abb8a8da
TDS
131$$($(2)_TARGET_CONFIGURE): $$($(2)_DIR)/.stamp_kconfig_fixup_done
132
4b81badb
YM
133# Force olddefconfig again on -reconfigure
134$(1)-clean-for-reconfigure: $(1)-clean-kconfig-for-reconfigure
135
136$(1)-clean-kconfig-for-reconfigure:
137 rm -f $$($(2)_DIR)/.stamp_kconfig_fixup_done
138
d1f94bf4
YM
139# Only enable the foo-*config targets when the package is actually enabled.
140# Note: the variable $(2)_KCONFIG_VAR is not related to the kconfig
141# infrastructure, but defined by pkg-generic.mk. The generic infrastructure is
142# already called above, so we can effectively use this variable.
143ifeq ($$($$($(2)_KCONFIG_VAR)),y)
144
61ca162f 145ifeq ($$(BR_BUILDING),y)
8ef62b99
S
146# Either FOO_KCONFIG_FILE or FOO_KCONFIG_DEFCONFIG is required...
147ifeq ($$(or $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
148$$(error Internal error: no value specified for $(2)_KCONFIG_FILE or $(2)_KCONFIG_DEFCONFIG)
149endif
150# ... but not both:
151ifneq ($$(and $$($(2)_KCONFIG_FILE),$$($(2)_KCONFIG_DEFCONFIG)),)
152$$(error Internal error: $(2)_KCONFIG_FILE and $(2)_KCONFIG_DEFCONFIG are mutually exclusive but both are defined)
b85bdae0 153endif
61ca162f 154endif
b85bdae0 155
1a91c82f
YM
156# For the configurators, we do want to use the system-provided host
157# tools, not the ones we build. This is particularly true for
158# pkg-config; if we use our pkg-config (from host-pkgconf), then it
159# would not look for the .pc from the host, but we do need them,
160# especially to find ncurses, GTK+, Qt (resp. for menuconfig and
161# nconfig, gconfig, xconfig).
162# So we simply remove our PATH and PKG_CONFIG_* variables.
163$(2)_CONFIGURATOR_MAKE_ENV = \
02302d21
BF
164 $$(filter-out PATH=% PKG_CONFIG=% PKG_CONFIG_SYSROOT_DIR=% PKG_CONFIG_LIBDIR=%,$$($(2)_MAKE_ENV)) \
165 PKG_CONFIG_PATH="$(HOST_PKG_CONFIG_PATH)"
1a91c82f 166
abb8a8da 167# Configuration editors (menuconfig, ...)
31b70a48 168#
13780c7b
YM
169# We need to apply the configuration fixups right after a configuration
170# editor exits, so that it is possible to save the configuration right
171# after exiting an editor, and so the user always sees a .config file
172# that is clean wrt. our requirements.
31b70a48
YM
173#
174# Because commands in $(1)_FIXUP_KCONFIG are probably using $(@D), we
273031ca
YM
175# need to have a valid @D set. But, because the configurators rules are
176# not real files and do not contain the path to the package build dir,
177# @D would be just '.' in this case. So, we use an intermediate rule
178# with a stamp-like file which path is in the package build dir, so we
179# end up having a valid @D.
31b70a48 180#
273031ca
YM
181$$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS)): $(1)-%: $$($(2)_DIR)/.kconfig_editor_%
182$$($(2)_DIR)/.kconfig_editor_%: $$($(2)_DIR)/.stamp_kconfig_fixup_done
1a91c82f 183 $$($(2)_CONFIGURATOR_MAKE_ENV) $$(MAKE) -C $$($(2)_DIR) \
273031ca 184 $$($(2)_KCONFIG_OPTS) $$(*)
abb8a8da 185 rm -f $$($(2)_DIR)/.stamp_{kconfig_fixup_done,configured,built}
d9fec0ad 186 rm -f $$($(2)_DIR)/.stamp_{target,staging,images}_installed
e645a3b0 187 $$($(2)_FIXUP_DOT_CONFIG)
abb8a8da 188
13780c7b
YM
189# Saving back the configuration
190#
191# Ideally, that should directly depend on $$($(2)_DIR)/.stamp_kconfig_fixup_done,
192# but that breaks the use-case in PR-8156 (from a clean tree):
193# make menuconfig <- enable kernel, use an in-tree defconfig, save and exit
194# make linux-menuconfig <- enable/disable whatever option, save and exit
195# make menuconfig <- change to use a custom defconfig file, set a path, save and exit
196# make linux-update-config <- should save to the new custom defconfig file
197#
198# Because of that use-case, saving the configuration can *not* directly
199# depend on the stamp file, because it itself depends on the .config,
200# which in turn depends on the (newly-set an non-existent) custom
201# defconfig file.
202#
412a872e 203# Instead, we use a PHONY rule that will catch that situation.
13780c7b
YM
204#
205$(1)-check-configuration-done:
206 @if [ ! -f $$($(2)_DIR)/.stamp_kconfig_fixup_done ]; then \
207 echo "$(1) is not yet configured"; \
208 exit 1; \
209 fi
210
211$(1)-savedefconfig: $(1)-check-configuration-done
39867f3c
TDS
212 $$($(2)_MAKE_ENV) $$(MAKE) -C $$($(2)_DIR) \
213 $$($(2)_KCONFIG_OPTS) savedefconfig
214
abb8a8da 215# Target to copy back the configuration to the source configuration file
1941386c
TDS
216# Even though we could use 'cp --preserve-timestamps' here, the separate
217# cp and 'touch --reference' is used for symmetry with $(1)-update-defconfig.
f542085a 218$(1)-update-config: PKG=$(2)
13780c7b 219$(1)-update-config: $(1)-check-configuration-done
f542085a 220 $$(call kconfig-package-update-config,$$($(2)_KCONFIG_DOTCONFIG))
abb8a8da 221
39867f3c
TDS
222# Note: make sure the timestamp of the stored configuration is not newer than
223# the .config to avoid a useless rebuild. Note that, contrary to
224# $(1)-update-config, the reference for 'touch' is _not_ the file from which
225# we copy.
f542085a 226$(1)-update-defconfig: PKG=$(2)
39867f3c 227$(1)-update-defconfig: $(1)-savedefconfig
f542085a 228 $$(call kconfig-package-update-config,defconfig)
39867f3c 229
d1f94bf4
YM
230endif # package enabled
231
f1e84386
TP
232.PHONY: \
233 $(1)-update-config \
234 $(1)-update-defconfig \
235 $(1)-savedefconfig \
13780c7b 236 $(1)-check-configuration-done \
273031ca 237 $$($(2)_DIR)/.kconfig_editor_% \
f1e84386
TP
238 $$(addprefix $(1)-,$$($(2)_KCONFIG_EDITORS))
239
abb8a8da
TDS
240endef # inner-kconfig-package
241
242################################################################################
243# kconfig-package -- the target generator macro for kconfig packages
244################################################################################
245
246kconfig-package = $(call inner-kconfig-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
This page took 2.603116 seconds and 4 git commands to generate.