@@ -636,6 +636,56 @@ static struct conf_printer kconfig_printer_cb =
636636 .print_comment = kconfig_print_comment ,
637637};
638638
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+ * we provide an empty `--cfg CONFIG_X` here in both `y`
666+ * 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+
639689/*
640690 * Header printer
641691 *
@@ -1043,7 +1093,7 @@ int conf_write_autoconf(int overwrite)
10431093 struct symbol * sym ;
10441094 const char * name ;
10451095 const char * autoconf_name = conf_get_autoconfig_name ();
1046- FILE * out , * out_h ;
1096+ FILE * out , * out_h , * out_rustc_cfg ;
10471097 int i ;
10481098
10491099 if (!overwrite && is_present (autoconf_name ))
@@ -1064,6 +1114,13 @@ int conf_write_autoconf(int overwrite)
10641114 return 1 ;
10651115 }
10661116
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+
10671124 conf_write_heading (out , & kconfig_printer_cb , NULL );
10681125 conf_write_heading (out_h , & header_printer_cb , NULL );
10691126
@@ -1075,9 +1132,11 @@ int conf_write_autoconf(int overwrite)
10751132 /* write symbols to auto.conf and autoconf.h */
10761133 conf_write_symbol (out , sym , & kconfig_printer_cb , (void * )1 );
10771134 conf_write_symbol (out_h , sym , & header_printer_cb , NULL );
1135+ conf_write_symbol (out_rustc_cfg , sym , & rustc_cfg_printer_cb , NULL );
10781136 }
10791137 fclose (out );
10801138 fclose (out_h );
1139+ fclose (out_rustc_cfg );
10811140
10821141 name = getenv ("KCONFIG_AUTOHEADER" );
10831142 if (!name )
@@ -1096,6 +1155,14 @@ int conf_write_autoconf(int overwrite)
10961155 if (rename (".tmpconfig" , autoconf_name ))
10971156 return 1 ;
10981157
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+
10991166 return 0 ;
11001167}
11011168
0 commit comments