Skip to content

feat(cargo): Set rustc-check-cfg for all config options #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ DT_AUGMENTS = \"${DT_AUGMENTS}\"
${config_paths}
")

set(RUST_KCONFIG_BOOL_OPTIONS_FILE "${CMAKE_CURRENT_BINARY_DIR}/rust-kconfig-bool-options.json")

add_custom_command(
OUTPUT ${RUST_KCONFIG_BOOL_OPTIONS_FILE}
COMMAND
${CMAKE_COMMAND} -E
env ${COMMON_KCONFIG_ENV_SETTINGS}
${RUST_MODULE_DIR}/scripts/kconfig-bool-options.py ${KCONFIG_ROOT} ${DOTCONFIG}
${RUST_KCONFIG_BOOL_OPTIONS_FILE}
DEPENDS ${DOTCONFIG}
COMMENT "Finding valid kconfig boolean options"
WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR}
)

# The block of environment variables below could theoretically be captured in a variable, but this
# seems "challenging" in CMake (to be polite), as many of these contain spaces, and the quoting
# rules in CMake are inconsistent, at best.
Expand All @@ -161,6 +175,7 @@ ${config_paths}
add_custom_command(
OUTPUT ${DUMMY_FILE}
BYPRODUCTS ${RUST_LIBRARY} ${WRAPPER_FILE}
DEPENDS ${RUST_KCONFIG_BOOL_OPTIONS_FILE}
USES_TERMINAL
COMMAND
${CMAKE_COMMAND} -E
Expand Down
4 changes: 0 additions & 4 deletions samples/async-philosophers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

#![no_std]
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be
// valid. This prevents a warning about each cfg that is used.
#![allow(unexpected_cfgs)]

extern crate alloc;

Expand Down
4 changes: 0 additions & 4 deletions samples/bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

#![no_std]
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be
// valid. This prevents a warning about each cfg that is used.
#![allow(unexpected_cfgs)]

extern crate alloc;

Expand Down
4 changes: 0 additions & 4 deletions samples/philosophers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

#![no_std]
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be
// valid. This prevents a warning about each cfg that is used.
#![allow(unexpected_cfgs)]

extern crate alloc;

Expand Down
50 changes: 50 additions & 0 deletions scripts/kconfig-bool-options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3

# SPDX-License-Identifier: Apache-2.0

import os
import sys
import argparse
import json

from itertools import chain

sys.path.insert(0, str(os.path.join(os.environ["ZEPHYR_BASE"], "scripts", "kconfig")))
from kconfiglib import Kconfig, _T_BOOL, _T_CHOICE


def main():
args = parse_args()

kconf = Kconfig(args.kconfig_file)
kconf.load_config(args.dotconfig)

options = {}

for sym in chain(kconf.unique_defined_syms, kconf.unique_choices):
if not sym.name:
continue

if "-" in sym.name:
# Rust does not allow hyphens in cfg options.
continue

if sym.type in [_T_BOOL, _T_CHOICE]:
options[f"CONFIG_{sym.name}"] = sym.str_value

with open(args.outfile, "w") as f:
json.dump(options, f, indent=2, sort_keys=True)


def parse_args():
parser = argparse.ArgumentParser(allow_abbrev=False)

parser.add_argument("kconfig_file", help="Top-level Kconfig file")
parser.add_argument("dotconfig", help="Path to dotconfig file")
parser.add_argument("outfile", help="Path to output file")

return parser.parse_args()


if __name__ == "__main__":
main()
28 changes: 18 additions & 10 deletions zephyr-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// This builds a program that is run on the compilation host before the code is compiled. It can
// output configuration settings that affect the compilation.

use std::collections::BTreeMap;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
Expand All @@ -27,19 +28,26 @@ mod devicetree;
/// Export boolean Kconfig entries. This must happen in any crate that wishes to access the
/// configuration settings.
pub fn export_bool_kconfig() {
let dotconfig = env::var("DOTCONFIG").expect("DOTCONFIG must be set by wrapper");
let build_dir = env::var("BUILD_DIR").expect("BUILD_DIR must be set by wrapper");

// Ensure the build script is rerun when the dotconfig changes.
println!("cargo:rerun-if-env-changed=DOTCONFIG");
println!("cargo-rerun-if-changed={}", dotconfig);
let bool_options_path = Path::new(&build_dir)
.join("rust-kconfig-bool-options.json")
.to_str()
.unwrap()
.to_string();

let config_y = Regex::new(r"^(CONFIG_.*)=y$").unwrap();
// Ensure the build script is rerun when kconfig bool options change.
println!("cargo:rerun-if-changed={}", bool_options_path);

let file = File::open(&dotconfig).expect("Unable to open dotconfig");
for line in BufReader::new(file).lines() {
let line = line.expect("reading line from dotconfig");
if let Some(caps) = config_y.captures(&line) {
println!("cargo:rustc-cfg={}", &caps[1]);
let bool_options = File::open(&bool_options_path).expect("Unable to open bool options");
let bool_options: BTreeMap<String, String> =
serde_yaml_ng::from_reader(bool_options).expect("Unable to parse bool options");

for (key, value) in bool_options.iter() {
println!("cargo:rustc-check-cfg=cfg({})", key);

if value == "y" {
println!("cargo:rustc-cfg={}", key);
}
}
}
Expand Down