@@ -636,6 +636,56 @@ static struct conf_printer kconfig_printer_cb =
636
636
.print_comment = kconfig_print_comment ,
637
637
};
638
638
639
+ /*
640
+ * rustc cfg printer
641
+ *
642
+ * This printer is used when generating the resulting rustc configuration
643
+ * after kconfig invocation and `defconfig` files.
644
+ */
645
+ static void rustc_cfg_print_symbol (FILE * fp , struct symbol * sym , const char * value , void * arg )
646
+ {
647
+ const char * str ;
648
+
649
+ switch (sym -> type ) {
650
+ case S_INT :
651
+ case S_HEX :
652
+ case S_BOOLEAN :
653
+ case S_TRISTATE :
654
+ str = sym_escape_string_value (value );
655
+
656
+ /*
657
+ * We don't care about disabled ones, i.e. no need for
658
+ * what otherwise are "comments" in other printers.
659
+ */
660
+ if (* value == 'n' )
661
+ return ;
662
+
663
+ /*
664
+ * To have similar functionality to the C macro `IS_ENABLED()`,
665
+ * from Rust, we provide an empty `--cfg CONFIG_X` here in
666
+ * both `y` and `m` cases.
667
+ *
668
+ * Then, the common `fprintf()` below will also give us
669
+ * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
670
+ * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
671
+ */
672
+ if (* value == 'y' || * value == 'm' )
673
+ fprintf (fp , "--cfg=%s%s\n" , CONFIG_ , sym -> name );
674
+
675
+ break ;
676
+ default :
677
+ str = value ;
678
+ break ;
679
+ }
680
+
681
+ fprintf (fp , "--cfg=%s%s=%s\n" , CONFIG_ , sym -> name , str );
682
+ }
683
+
684
+ static struct conf_printer rustc_cfg_printer_cb =
685
+ {
686
+ .print_symbol = rustc_cfg_print_symbol ,
687
+ };
688
+
639
689
/*
640
690
* Header printer
641
691
*
@@ -1043,7 +1093,7 @@ int conf_write_autoconf(int overwrite)
1043
1093
struct symbol * sym ;
1044
1094
const char * name ;
1045
1095
const char * autoconf_name = conf_get_autoconfig_name ();
1046
- FILE * out , * out_h ;
1096
+ FILE * out , * out_h , * out_rustc_cfg ;
1047
1097
int i ;
1048
1098
1049
1099
if (!overwrite && is_present (autoconf_name ))
@@ -1064,6 +1114,13 @@ int conf_write_autoconf(int overwrite)
1064
1114
return 1 ;
1065
1115
}
1066
1116
1117
+ out_rustc_cfg = fopen (".tmp_rustc_cfg" , "w" );
1118
+ if (!out_rustc_cfg ) {
1119
+ fclose (out );
1120
+ fclose (out_h );
1121
+ return 1 ;
1122
+ }
1123
+
1067
1124
conf_write_heading (out , & kconfig_printer_cb , NULL );
1068
1125
conf_write_heading (out_h , & header_printer_cb , NULL );
1069
1126
@@ -1075,9 +1132,11 @@ int conf_write_autoconf(int overwrite)
1075
1132
/* write symbols to auto.conf and autoconf.h */
1076
1133
conf_write_symbol (out , sym , & kconfig_printer_cb , (void * )1 );
1077
1134
conf_write_symbol (out_h , sym , & header_printer_cb , NULL );
1135
+ conf_write_symbol (out_rustc_cfg , sym , & rustc_cfg_printer_cb , NULL );
1078
1136
}
1079
1137
fclose (out );
1080
1138
fclose (out_h );
1139
+ fclose (out_rustc_cfg );
1081
1140
1082
1141
name = getenv ("KCONFIG_AUTOHEADER" );
1083
1142
if (!name )
@@ -1096,6 +1155,14 @@ int conf_write_autoconf(int overwrite)
1096
1155
if (rename (".tmpconfig" , autoconf_name ))
1097
1156
return 1 ;
1098
1157
1158
+ name = getenv ("KCONFIG_RUSTC_CFG" );
1159
+ if (!name )
1160
+ name = "include/generated/rustc_cfg" ;
1161
+ if (make_parent_dir (name ))
1162
+ return 1 ;
1163
+ if (rename (".tmp_rustc_cfg" , name ))
1164
+ return 1 ;
1165
+
1099
1166
return 0 ;
1100
1167
}
1101
1168
0 commit comments