Skip to content

Commit df6070b

Browse files
kloenkojeda
authored andcommitted
Use @file to pass the --cfg flags to rustc (fixes #22)
It also helps to avoid spamming the terminal when doing verbose builds. Signed-off-by: Finn Behrens <me@kloenk.de> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 096dd7c commit df6070b

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ $(KCONFIG_CONFIG):
726726
# This exploits the 'multi-target pattern rule' trick.
727727
# The syncconfig should be executed only once to make all the targets.
728728
# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
729-
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG)
729+
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg: $(KCONFIG_CONFIG)
730730
$(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
731731
else # !may-sync-config
732732
# External modules and some install targets need include/generated/autoconf.h

scripts/Makefile.lib

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
229229
RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE)
230230
export RUST_BINDGEN_CFLAGS
231231

232-
rustc_cfg_flags = $(shell sed -nE 's/^(CONFIG_[^=]+)=(y|m)$$/--cfg \1/p' $(srctree)/include/config/auto.conf | xargs)
232+
KCONFIG_RUSTC_CFG ?= $(srctree)/include/generated/rustc_cfg
233233

234-
rustc_flags = $(_rustc_flags) $(modkern_rustcflags) $(rustc_cfg_flags)
234+
rustc_flags = $(_rustc_flags) $(modkern_rustcflags) @$(shell readlink -f $(KCONFIG_RUSTC_CFG))
235235

236236
# Passed by cargo
237237
RUSTFLAGS = $(rustc_flags)

scripts/kconfig/confdata.c

+68-1
Original file line numberDiff line numberDiff line change
@@ -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+
* 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+
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

Comments
 (0)