I have a situation where our software needs to work with several different Linux kernel distributions / kernel trees. (including Android forks)
In trying to automate
The kernel has a tool (./scripts/config) to change specific options on .config. Here is an example:
./scripts/config --set-val CONFIG_OPTION y
Although, it doesn't check the validity of the .config file.
merge_config.sh config fragments
$ cd linux
$ git checkout v4.9
$ make x86_64_defconfig
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
# CONFIG_DEBUG_INFO is not set
$ # GDB_SCRIPTS depends on CONFIG_DEBUG_INFO in lib/Kconfig.debug.
$ cat <<EOF >.config-fragment
> CONFIG_DEBUG_INFO=y
> CONFIG_GDB_SCRIPTS=y
> EOF
$ # Order is important here. Must be first base config, then fragment.
$ ./scripts/kconfig/merge_config.sh .config .config-fragment
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y
Process substitution does not work unfortunately:
./scripts/kconfig/merge_config.sh arch/x86/configs/x86_64_defconfig \
<( printf 'CONFIG_DEBUG_INFO=y\nCONFIG_GDB_SCRIPTS=y\n' )
because of: https://unix.stackexchange.com/a/164109/32558
merge_config.sh
is a simple front-end for the make alldefconfig
target.
When cross compiling, ARCH
must be exported when you run merge_config.sh
, e.g.:
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make defconfig
./scripts/kconfig/merge_config.sh .config .config-fragment
The merged output file can be specified explicitly with the KCONFIG_CONFIG
environment variable; otherwise it just overwrites .config
:
KCONFIG_CONFIG=some/path/.config ./scripts/kconfig/merge_config.sh .config .config-fragment
Buildroot automates it with BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES
: How do I configure the Linux kernel within Buildroot?
Related: https://unix.stackexchange.com/questions/19905/how-to-non-interactively-configure-the-linux-kernel-build Migrated 20 days earlier :-)
Yes, some config options change names between releases, sometimes as an indication of subtle semantic changes.
I have written python classes to merge together a set of configuration fragments into base kernel configuration files. That's overkill for you, though; you can do the same thing as a sed script; you aren't limited to one-liners.
sed -ir 's/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/' .config
Or even create a separate script. Say, config.sed that contains the lines:
s/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/;
Then you can run
sed -ire config.sed .config
Hope that helps!