Skip to content

Commit

Permalink
kconfig: reference environment variables directly and remove 'option …
Browse files Browse the repository at this point in the history
…env='

To get access to environment variables, Kconfig needs to define a
symbol using "option env=" syntax.  It is tedious to add a symbol entry
for each environment variable given that we need to define much more
such as 'CC', 'AS', 'srctree' etc. to evaluate the compiler capability
in Kconfig.

Adding '$' for symbol references is grammatically inconsistent.
Looking at the code, the symbols prefixed with 'S' are expanded by:
 - conf_expand_value()
   This is used to expand 'arch/$ARCH/defconfig' and 'defconfig_list'
 - sym_expand_string_value()
   This is used to expand strings in 'source' and 'mainmenu'

All of them are fixed values independent of user configuration.  So,
they can be changed into the direct expansion instead of symbols.

This change makes the code much cleaner.  The bounce symbols 'SRCARCH',
'ARCH', 'SUBARCH', 'KERNELVERSION' are gone.

sym_init() hard-coding 'UNAME_RELEASE' is also gone.  'UNAME_RELEASE'
should be replaced with an environment variable.

ARCH_DEFCONFIG is a normal symbol, so it should be simply referenced
without '$' prefix.

The new syntax is addicted by Make.  The variable reference needs
parentheses, like $(FOO), but you can omit them for single-letter
variables, like $F.  Yet, in Makefiles, people tend to use the
parenthetical form for consistency / clarification.

At this moment, only the environment variable is supported, but I will
extend the concept of 'variable' later on.

The variables are expanded in the lexer so we can simplify the token
handling on the parser side.

For example, the following code works.

[Example code]

  config MY_TOOLCHAIN_LIST
          string
          default "My tools: CC=$(CC), AS=$(AS), CPP=$(CPP)"

[Result]

  $ make -s alldefconfig && tail -n 1 .config
  CONFIG_MY_TOOLCHAIN_LIST="My tools: CC=gcc, AS=as, CPP=gcc -E"

Signed-off-by: Masahiro Yamada <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
  • Loading branch information
masahir0y committed May 28, 2018
1 parent f1089c9 commit 104daea
Showing 19 changed files with 343 additions and 154 deletions.
8 changes: 0 additions & 8 deletions Documentation/kbuild/kconfig-language.txt
Original file line number Diff line number Diff line change
@@ -198,14 +198,6 @@ applicable everywhere (see syntax).
enables the third modular state for all config symbols.
At most one symbol may have the "modules" option set.

- "env"=<value>
This imports the environment variable into Kconfig. It behaves like
a default, except that the value comes from the environment, this
also means that the behaviour when mixing it with normal defaults is
undefined at this point. The symbol is currently not exported back
to the build environment (if this is desired, it can be done via
another symbol).

- "allnoconfig_y"
This declares the symbol as one that should have the value y when
using "allnoconfig". Used for symbols that hide other symbols.
8 changes: 2 additions & 6 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -3,10 +3,6 @@
# For a description of the syntax of this configuration file,
# see Documentation/kbuild/kconfig-language.txt.
#
mainmenu "Linux/$ARCH $KERNELVERSION Kernel Configuration"
mainmenu "Linux/$(ARCH) $(KERNELVERSION) Kernel Configuration"

config SRCARCH
string
option env="SRCARCH"

source "arch/$SRCARCH/Kconfig"
source "arch/$(SRCARCH)/Kconfig"
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -284,7 +284,8 @@ include scripts/Kbuild.include
# Read KERNELRELEASE from include/config/kernel.release (if it exists)
KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
UNAME_RELEASE := $(shell uname --release)
export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION UNAME_RELEASE

# SUBARCH tells the usermode build what the underlying arch is. That is set
# first, and if a usermode build is happening, the "ARCH=um" on the command
4 changes: 2 additions & 2 deletions arch/sh/Kconfig
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ config SUPERH
<http://www.linux-sh.org/>.

config SUPERH32
def_bool ARCH = "sh"
def_bool "$(ARCH)" = "sh"
select HAVE_KPROBES
select HAVE_KRETPROBES
select HAVE_IOREMAP_PROT if MMU && !X2TLB
@@ -77,7 +77,7 @@ config SUPERH32
select HAVE_CC_STACKPROTECTOR

config SUPERH64
def_bool ARCH = "sh64"
def_bool "$(ARCH)" = "sh64"
select HAVE_EXIT_THREAD
select KALLSYMS

4 changes: 2 additions & 2 deletions arch/sparc/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
config 64BIT
bool "64-bit kernel" if ARCH = "sparc"
default ARCH = "sparc64"
bool "64-bit kernel" if "$(ARCH)" = "sparc"
default "$(ARCH)" = "sparc64"
help
SPARC is a family of RISC microprocessors designed and marketed by
Sun Microsystems, incorporated. They are very widely found in Sun
4 changes: 0 additions & 4 deletions arch/um/Kconfig.common
Original file line number Diff line number Diff line change
@@ -54,10 +54,6 @@ config HZ
int
default 100

config SUBARCH
string
option env="SUBARCH"

config NR_CPUS
int
range 1 1
4 changes: 2 additions & 2 deletions arch/x86/Kconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
# Select 32 or 64 bit
config 64BIT
bool "64-bit kernel" if ARCH = "x86"
default ARCH != "i386"
bool "64-bit kernel" if "$(ARCH)" = "x86"
default "$(ARCH)" != "i386"
---help---
Say yes to build a 64-bit kernel - formerly known as x86_64
Say no to build a 32-bit kernel - formerly known as i386
6 changes: 3 additions & 3 deletions arch/x86/um/Kconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
mainmenu "User Mode Linux/$SUBARCH $KERNELVERSION Kernel Configuration"
mainmenu "User Mode Linux/$(SUBARCH) $(KERNELVERSION) Kernel Configuration"

source "arch/um/Kconfig.common"

@@ -16,8 +16,8 @@ config UML_X86
select GENERIC_FIND_FIRST_BIT

config 64BIT
bool "64-bit kernel" if SUBARCH = "x86"
default SUBARCH != "i386"
bool "64-bit kernel" if "$(SUBARCH)" = "x86"
default "$(SUBARCH)" != "i386"

config X86_32
def_bool !64BIT
16 changes: 4 additions & 12 deletions init/Kconfig
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
config ARCH
string
option env="ARCH"

config KERNELVERSION
string
option env="KERNELVERSION"

config DEFCONFIG_LIST
string
depends on !UML
option defconfig_list
default "/lib/modules/$UNAME_RELEASE/.config"
default "/lib/modules/$(UNAME_RELEASE)/.config"
default "/etc/kernel-config"
default "/boot/config-$UNAME_RELEASE"
default "$ARCH_DEFCONFIG"
default "arch/$ARCH/defconfig"
default "/boot/config-$(UNAME_RELEASE)"
default ARCH_DEFCONFIG
default "arch/$(ARCH)/defconfig"

config CONSTRUCTORS
bool
33 changes: 4 additions & 29 deletions scripts/kconfig/confdata.c
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ static void conf_message(const char *fmt, ...)
static const char *conf_filename;
static int conf_lineno, conf_warnings;

const char conf_defname[] = "arch/$ARCH/defconfig";
const char conf_defname[] = "arch/$(ARCH)/defconfig";

static void conf_warning(const char *fmt, ...)
{
@@ -81,39 +81,13 @@ const char *conf_get_autoconfig_name(void)
return name ? name : "include/config/auto.conf";
}

static char *conf_expand_value(const char *in)
{
struct symbol *sym;
const char *src;
static char res_value[SYMBOL_MAXLENGTH];
char *dst, name[SYMBOL_MAXLENGTH];

res_value[0] = 0;
dst = name;
while ((src = strchr(in, '$'))) {
strncat(res_value, in, src - in);
src++;
dst = name;
while (isalnum(*src) || *src == '_')
*dst++ = *src++;
*dst = 0;
sym = sym_lookup(name, 0);
sym_calc_value(sym);
strcat(res_value, sym_get_string_value(sym));
in = src;
}
strcat(res_value, in);

return res_value;
}

char *conf_get_default_confname(void)
{
struct stat buf;
static char fullname[PATH_MAX+1];
char *env, *name;

name = conf_expand_value(conf_defname);
name = expand_string(conf_defname);
env = getenv(SRCTREE);
if (env) {
sprintf(fullname, "%s/%s", env, name);
@@ -274,7 +248,8 @@ int conf_read_simple(const char *name, int def)
if (expr_calc_value(prop->visible.expr) == no ||
prop->expr->type != E_SYMBOL)
continue;
name = conf_expand_value(prop->expr->left.sym->name);
sym_calc_value(prop->expr->left.sym);
name = sym_get_string_value(prop->expr->left.sym);
in = zconf_fopen(name);
if (in) {
conf_message("using defaults found in %s",
1 change: 0 additions & 1 deletion scripts/kconfig/kconf_id.c
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ static struct kconf_id kconf_id_array[] = {
{ "on", T_ON, TF_PARAM },
{ "modules", T_OPT_MODULES, TF_OPTION },
{ "defconfig_list", T_OPT_DEFCONFIG_LIST, TF_OPTION },
{ "env", T_OPT_ENV, TF_OPTION },
{ "allnoconfig_y", T_OPT_ALLNOCONFIG_Y, TF_OPTION },
};

5 changes: 1 addition & 4 deletions scripts/kconfig/lkc.h
Original file line number Diff line number Diff line change
@@ -44,7 +44,6 @@ enum conf_def_mode {

#define T_OPT_MODULES 1
#define T_OPT_DEFCONFIG_LIST 2
#define T_OPT_ENV 3
#define T_OPT_ALLNOCONFIG_Y 4

struct kconf_id {
@@ -103,6 +102,7 @@ void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *p, size_t size);
char *xstrdup(const char *s);
char *xstrndup(const char *s, size_t n);

struct gstr {
size_t len;
@@ -120,9 +120,6 @@ void str_printf(struct gstr *gs, const char *fmt, ...);
const char *str_get(struct gstr *gs);

/* symbol.c */
extern struct expr *sym_env_list;

void sym_init(void);
void sym_clear_all_valid(void);
struct symbol *sym_choice_default(struct symbol *sym);
const char *sym_get_string_default(struct symbol *sym);
6 changes: 6 additions & 0 deletions scripts/kconfig/lkc_proto.h
Original file line number Diff line number Diff line change
@@ -49,5 +49,11 @@ const char * sym_get_string_value(struct symbol *sym);

const char * prop_get_type_name(enum prop_type type);

/* preprocess.c */
void env_write_dep(FILE *f, const char *auto_conf_name);
char *expand_string(const char *in);
char *expand_dollar(const char **str);
char *expand_one_token(const char **str);

/* expr.c */
void expr_print(struct expr *e, void (*fn)(void *, struct symbol *, const char *), void *data, int prevtoken);
3 changes: 0 additions & 3 deletions scripts/kconfig/menu.c
Original file line number Diff line number Diff line change
@@ -214,9 +214,6 @@ void menu_add_option(int token, char *arg)
zconf_error("trying to redefine defconfig symbol");
sym_defconfig_list->flags |= SYMBOL_AUTO;
break;
case T_OPT_ENV:
prop_add_env(arg);
break;
case T_OPT_ALLNOCONFIG_Y:
current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y;
break;
Loading

0 comments on commit 104daea

Please sign in to comment.