Skip to content

Commit

Permalink
Use @file to pass the --cfg flags to rustc (fixes #22)
Browse files Browse the repository at this point in the history
It also helps to avoid spamming the terminal when doing verbose builds.

Signed-off-by: Finn Behrens <[email protected]>
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
kloenk authored and ojeda committed Nov 28, 2020
1 parent 096dd7c commit 39484fa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ $(KCONFIG_CONFIG):
# This exploits the 'multi-target pattern rule' trick.
# The syncconfig should be executed only once to make all the targets.
# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG)
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg: $(KCONFIG_CONFIG)
$(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
else # !may-sync-config
# External modules and some install targets need include/generated/autoconf.h
Expand Down
4 changes: 2 additions & 2 deletions scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE)
export RUST_BINDGEN_CFLAGS

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

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

# Passed by cargo
RUSTFLAGS = $(rustc_flags)
Expand Down
69 changes: 68 additions & 1 deletion scripts/kconfig/confdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,56 @@ static struct conf_printer kconfig_printer_cb =
.print_comment = kconfig_print_comment,
};

/*
* rustc cfg printer
*
* This printer is used when generating the resulting rustc configuration
* after kconfig invocation and `defconfig` files.
*/
static void rustc_cfg_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
{
const char *str;

switch (sym->type) {
case S_INT:
case S_HEX:
case S_BOOLEAN:
case S_TRISTATE:
str = sym_escape_string_value(value);

/*
* We don't care about disabled ones, i.e. no need for
* what otherwise are "comments" in other printers.
*/
if (*value == 'n')
return;

/*
* To have similar functionality to the C macro `IS_ENABLED()`
* we provide an empty `--cfg CONFIG_X` here in both `y`
* and `m` cases.
*
* Then, the common `fprintf()` below will also give us
* a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
* be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
*/
if (*value == 'y' || *value == 'm')
fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);

break;
default:
str = value;
break;
}

fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, str);
}

static struct conf_printer rustc_cfg_printer_cb =
{
.print_symbol = rustc_cfg_print_symbol,
};

/*
* Header printer
*
Expand Down Expand Up @@ -1043,7 +1093,7 @@ int conf_write_autoconf(int overwrite)
struct symbol *sym;
const char *name;
const char *autoconf_name = conf_get_autoconfig_name();
FILE *out, *out_h;
FILE *out, *out_h, *out_rustc_cfg;
int i;

if (!overwrite && is_present(autoconf_name))
Expand All @@ -1064,6 +1114,13 @@ int conf_write_autoconf(int overwrite)
return 1;
}

out_rustc_cfg = fopen(".tmp_rustc_cfg", "w");
if (!out_rustc_cfg) {
fclose(out);
fclose(out_h);
return 1;
}

conf_write_heading(out, &kconfig_printer_cb, NULL);
conf_write_heading(out_h, &header_printer_cb, NULL);

Expand All @@ -1075,9 +1132,11 @@ int conf_write_autoconf(int overwrite)
/* write symbols to auto.conf and autoconf.h */
conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
conf_write_symbol(out_rustc_cfg, sym, &rustc_cfg_printer_cb, NULL);
}
fclose(out);
fclose(out_h);
fclose(out_rustc_cfg);

name = getenv("KCONFIG_AUTOHEADER");
if (!name)
Expand All @@ -1096,6 +1155,14 @@ int conf_write_autoconf(int overwrite)
if (rename(".tmpconfig", autoconf_name))
return 1;

name = getenv("KCONFIG_RUSTC_CFG");
if (!name)
name = "include/generated/rustc_cfg";
if (make_parent_dir(name))
return 1;
if (rename(".tmp_rustc_cfg", name))
return 1;

return 0;
}

Expand Down

0 comments on commit 39484fa

Please sign in to comment.