1111// This builds a program that is run on the compilation host before the code is compiled. It can
1212// output configuration settings that affect the compilation.
1313
14+ use std:: collections:: BTreeMap ;
1415use std:: env;
1516use std:: fs:: File ;
1617use std:: io:: { BufRead , BufReader , Write } ;
@@ -27,19 +28,26 @@ mod devicetree;
2728/// Export boolean Kconfig entries. This must happen in any crate that wishes to access the
2829/// configuration settings.
2930pub fn export_bool_kconfig ( ) {
30- let dotconfig = env:: var ( "DOTCONFIG " ) . expect ( "DOTCONFIG must be set by wrapper" ) ;
31+ let build_dir = env:: var ( "BUILD_DIR " ) . expect ( "BUILD_DIR must be set by wrapper" ) ;
3132
32- // Ensure the build script is rerun when the dotconfig changes.
33- println ! ( "cargo:rerun-if-env-changed=DOTCONFIG" ) ;
34- println ! ( "cargo-rerun-if-changed={}" , dotconfig) ;
33+ let bool_options_path = Path :: new ( & build_dir)
34+ . join ( "rust-kconfig-bool-options.json" )
35+ . to_str ( )
36+ . unwrap ( )
37+ . to_string ( ) ;
3538
36- let config_y = Regex :: new ( r"^(CONFIG_.*)=y$" ) . unwrap ( ) ;
39+ // Ensure the build script is rerun when kconfig bool options change.
40+ println ! ( "cargo:rerun-if-changed={}" , bool_options_path) ;
3741
38- let file = File :: open ( & dotconfig) . expect ( "Unable to open dotconfig" ) ;
39- for line in BufReader :: new ( file) . lines ( ) {
40- let line = line. expect ( "reading line from dotconfig" ) ;
41- if let Some ( caps) = config_y. captures ( & line) {
42- println ! ( "cargo:rustc-cfg={}" , & caps[ 1 ] ) ;
42+ let bool_options = File :: open ( & bool_options_path) . expect ( "Unable to open bool options" ) ;
43+ let bool_options: BTreeMap < String , String > =
44+ serde_yaml_ng:: from_reader ( bool_options) . expect ( "Unable to parse bool options" ) ;
45+
46+ for ( key, value) in bool_options. iter ( ) {
47+ println ! ( "cargo:rustc-check-cfg=cfg({})" , key) ;
48+
49+ if value == "y" {
50+ println ! ( "cargo:rustc-cfg={}" , key) ;
4351 }
4452 }
4553}
0 commit comments