Outpost-Kconfig
provides build system recipes for Meson and Cargo that handles kconfig
and/or .config
file.
The aim is to ease kconfig
usage across different languages and build systems.
Meson Buildsystem recipe collection that handles Kconfig
and .config
files.
This meson project can be used as a sub-project for every meson based project which uses Kconfig
as configuration system. The top level Kconfig
is parsed at configure time (i.e. during meson setup), the config
file is checked and a build specific .config
file is generated using oldconfig
policy for missing Kconfig
entry.
During project configuration, two dependencies file are generated, one which listed all Kconfig
files that are parsed, and the .config
itself. Thus, if any of those file change (Kconfig new options and/or a manually run menuconfig), the project is automatically reconfigure by Meson
. This project provides a meson configuration_data
object containing all .config
entries (field to y
are converted to 1/0
boolean) and a config.h
is generated using meson configure_file
feature.
The provided configuration_data
can be reused for Meson source_set
configuration.
kconfig
: Top levelKconfig
file.config
: input config file.
NOTE: This is up to the user to provide the input config file (e.g. from a
defconfig
, and/or afterconfig fragment
merge operation). This can be done w/scripts/kconfig.py
but this is out of scope of the meson configuration step.
Options got
yield
attribute and thus can be defined at top-level project and forwarded to sub projects seamlessly
Kconfig
can use environment variables substitution in kconfig
file and/or dotconfig
. This recipe export a meson variable named kconfig_env
to be used as env
keyword argument value for meson function (see: https://mesonbuild.com/Reference-manual_returned_env.html).
srctree
: standard kconfig variable for top level source directory (a.k.a. meson global source tree).subprojects
: helper to path to top level subprojects directory. This ease subprojectsKconfig
files source from top level `Kconfig`` file.KCONFIG_CONFIG
: Outputted dotconfig file path.
kconfig_env
is also added to the meson devenv. Meson devenv is a python-virtual-env-like feature that allowed developers to enable the required variables for the project. Using this, one can activate the project devenv and directly use menuconfig
from kconfiglib
w/o the need of any helper or custom command.
e.g.
meson devenv -C builddir
menuconfig
There is a helper target called menuconfig
for users who wants to edit the configuration during development. This target handles the path of top-level Kconfig
and output .config
.
menuconfig
: runs menuconfig inside the givenbuilddir
TODO: Add oldconfig, savedefconfig targets
The following variables can be use by top level project to use Kconfig
features. (see: https://mesonbuild.com/Reference-manual_returned_subproject.html)
- kconfig_env: environment variables set
- kconfig_data: kconfig meson
configure_data
w/ entry set toy
converted as1/0 boolean
- kconfig_h: C header generated at configure time by meson from processed
kconfig_data
- kconfig_rustargs: Rust flags generated at configure time by meson from processed
kconfig_data
This project uses kconfiglib python package as Kconfig frontend. One could install it with the following:
pip3 install --user kconfiglib
For Cargo build system, there is no handling of Kconfig itself nor any graphical frontend.
We assumed that the .config
file is well-formed.
As a pre requisite, we stated that kconfig entries must be build time evaluated in order to fail as soon as possible, and especially, not at runtime. Moreover, no external crates and no dead code are mandatory requirements too.
Thus, 2 crates are needed:
- kconfig_import: import and parse
.config
from a build script, i.e.build.rs
. - kconfig: provide helper getter macro
This crate allows user to import a .config
file, the file path is given through an environment
variable config
in order to stay coherent w/ meson option.
Blank lines and comments are trimmed and then, each kconfig entry is pushed in cargo config as boolean.
This allows users to use conditional compile with #[cfg]
attribute or cfg!
macro using only
the config entry name, i.e. means that the config is defined, whatever is type or value.
Entries w/ a specific value (i.e. int
, hex
and string
) are pushed to the cargo environment.
Due to some const limitation on string view manipulation, hex
are converted to decimal integer at
build time and an extra entry w/ _HEX_STR
suffix is added, holding the original hexadecimal value
(w/ 0x
prefix) as string.
This crate provide macro helper in order to get value (and converted to the right type) from Rust code
at build time. As all environment variables are text, the get!
macro takes a required parameter
that is the config entry name and an optional parameter that is the integer type in which user wants
to convert config entry. If no optional parameter, the raw string is accessed.
At build time, if the config entry does not exist, compiling process will panic. At build time, if the string to integer conversion fails, compiling process will panic.
For boolean value, rust #[cfg]
and cfg!
can be used.
Cargo.toml
[dependencies]
kconfig
[build-dependencies]
kconfig_import
build.rs
fn main() {
kconfig_import::import_dotconfig_from_script();
}
any.rs
use kconfig:
[...]
// conditional function compilation
#[cfg(CONFIG_EXTRA_UBER_FEATURE)]
fn my_extra_uber_function(...) {
[...]
}
// u16 array size from KConfig `.config` file
const SOME_ARRAY_SIZE: u16 = kconfig::get!("CONFIG_SOME_ARRAY_SIZE", u16);
// string from KConfig `.config` file
const MOTD_BANNER: &str = kconfig::get!("CONFIG_MOTD");
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
NOTE: This project bundles a modified version
kconfig.py
from Zephyr project, licensed underISC
.