From 310bfa737f2cdf6bdcab7637b220a061d2807e99 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 22 Jan 2022 05:07:25 -0700 Subject: kconfig: Update IS_ENABLED() internals The config_enabled() macro currently uses 0 as the default value. Update it to allow any value, so we can pass it something else, such as a non-existent function, to produce a build error if it is not defined. Also tidy up the code style for IS_ENABLED() and drop the unnecessary brackets (the value is a simple 0 or 1). Signed-off-by: Simon Glass --- include/linux/kconfig.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'include/linux') diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h index a1d1a298426..52586a7557b 100644 --- a/include/linux/kconfig.h +++ b/include/linux/kconfig.h @@ -17,18 +17,16 @@ * the last step cherry picks the 2nd arg, we get a zero. */ #define __ARG_PLACEHOLDER_1 0, -#define config_enabled(cfg) _config_enabled(cfg) -#define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value) -#define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0) +#define config_enabled(cfg, def_val) _config_enabled(cfg, def_val) +#define _config_enabled(value, def_val) __config_enabled(__ARG_PLACEHOLDER_##value, def_val) +#define __config_enabled(arg1_or_junk, def_val) ___config_enabled(arg1_or_junk 1, def_val) #define ___config_enabled(__ignored, val, ...) val /* * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', * 0 otherwise. - * */ -#define IS_ENABLED(option) \ - (config_enabled(option)) +#define IS_ENABLED(option) config_enabled(option, 0) /* * U-Boot add-on: Helper macros to reference to different macros (prefixed by @@ -76,7 +74,7 @@ #define __CONFIG_IS_ENABLED_1(option) __CONFIG_IS_ENABLED_3(option, (1), (0)) #define __CONFIG_IS_ENABLED_2(option, case1) __CONFIG_IS_ENABLED_3(option, case1, ()) #define __CONFIG_IS_ENABLED_3(option, case1, case0) \ - __concat(__unwrap, config_enabled(CONFIG_VAL(option))) (case1, case0) + __concat(__unwrap, config_enabled(CONFIG_VAL(option), 0)) (case1, case0) /* * CONFIG_IS_ENABLED(FOO) expands to -- cgit v1.3.1 From 097ff01fb98a6f4ad03eed8cf639a5cc90fb6619 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 22 Jan 2022 05:07:26 -0700 Subject: kconfig: Add support for conditional values At present if an optional Kconfig value needs to be used it must be bracketed by #ifdef. For example, with this Kconfig setup: config WIBBLE bool "Support wibbles, the world needs more wibbles" config WIBBLE_ADDR hex "Address of the wibble" depends on WIBBLE then the following code must be used: #ifdef CONFIG_WIBBLE static void handle_wibble(void) { int val = CONFIG_WIBBLE_ADDR; ... } #endif static void init_machine() { ... #ifdef CONFIG_WIBBLE handle_wibble(); #endif } Add a new IF_ENABLED_INT() to help with this. So now it is possible to write, without #ifdefs: static void handle_wibble(void) { int val = IF_ENABLED_INT(CONFIG_WIBBLE, CONFIG_WIBBLE_ADDR); ... } static void init_machine() { ... if (IS_ENABLED(CONFIG_WIBBLE)) handle_wibble(); } The value will be CONFIG_WIBBLE_ADDR if CONFIG_WIBBLE is defined and will produce a build error if not.. This allows us to reduce the use of #ifdef in the code, ensuring that the compiler still checks the code even if it is not ultimately used for a particular build. Add a CONFIG_IF_ENABLED_INT() version as well. If an attempt is made to use a value that does not exist (i.e. when the conditional is not enabled), an error about a non-existing function is generated, e.g.: common/bloblist.c:447: undefined reference to `invalid_use_of_IF_ENABLED_INT' Signed-off-by: Simon Glass --- include/linux/kconfig.h | 40 ++++++++++++++++++++++++++++++++++++++++ scripts/Makefile.autoconf | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'include/linux') diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h index 52586a7557b..d20da615b96 100644 --- a/include/linux/kconfig.h +++ b/include/linux/kconfig.h @@ -57,6 +57,30 @@ */ #define CONFIG_VAL(option) config_val(option) +/* + * This uses a similar mechanism to config_enabled() above. If cfg is enabled, + * it resolves to the value of opt_cfg, otherwise it resolves to def_val + */ +#define config_opt_enabled(cfg, opt_cfg, def_val) _config_opt_enabled(cfg, opt_cfg, def_val) +#define _config_opt_enabled(cfg_val, opt_value, def_val) \ + __config_opt_enabled(__ARG_PLACEHOLDER_##cfg_val, opt_value, def_val) +#define __config_opt_enabled(arg1_or_junk, arg2, def_val) \ + ___config_opt_enabled(arg1_or_junk arg2, def_val) +#define ___config_opt_enabled(__ignored, val, ...) val + +#ifndef __ASSEMBLY__ +/* + * Detect usage of a the value when the conditional is not enabled. When used + * in assembly context, this likely produces a assembly error, or hopefully at + * least something recognisable. + */ +long invalid_use_of_IF_ENABLED_INT(void); +#endif + +/* Evaluates to int_option if option is defined, otherwise a build error */ +#define IF_ENABLED_INT(option, int_option) \ + config_opt_enabled(option, int_option, invalid_use_of_IF_ENABLED_INT()) + /* * Count number of arguments to a variadic macro. Currently only need * it for 1, 2 or 3 arguments. @@ -111,5 +135,21 @@ #define CONFIG_IS_ENABLED(option, ...) \ __concat(__CONFIG_IS_ENABLED_, __count_args(option, ##__VA_ARGS__)) (option, ##__VA_ARGS__) +#ifndef __ASSEMBLY__ +/* + * Detect usage of a the value when the conditional is not enabled. When used + * in assembly context, this likely produces a assembly error, or hopefully at + * least something recognisable. + */ +long invalid_use_of_CONFIG_IF_ENABLED_INT(void); +#endif + +/* + * Evaluates to SPL_/TPL_int_option if SPL_/TPL_/option is not defined, + * otherwise build error + */ +#define CONFIG_IF_ENABLED_INT(option, int_option) \ + CONFIG_IS_ENABLED(option, (CONFIG_VAL(int_option)), \ + (invalid_use_of_CONFIG_IF_ENABLED_INT())) #endif /* __LINUX_KCONFIG_H */ diff --git a/scripts/Makefile.autoconf b/scripts/Makefile.autoconf index 5ed9abc8e14..0b3ffa08bfa 100644 --- a/scripts/Makefile.autoconf +++ b/scripts/Makefile.autoconf @@ -68,7 +68,7 @@ quiet_cmd_u_boot_cfg = CFG $@ cmd_u_boot_cfg = \ $(CPP) $(c_flags) $2 -DDO_DEPS_ONLY -dM $(srctree)/include/common.h > $@.tmp && { \ grep 'define CONFIG_' $@.tmp | \ - sed '/define CONFIG_IS_ENABLED(/d;/define CONFIG_VAL(/d;' > $@; \ + sed '/define CONFIG_IS_ENABLED(/d;/define CONFIG_IF_ENABLED_INT(/d;/define CONFIG_VAL(/d;' > $@; \ rm $@.tmp; \ } || { \ rm $@.tmp; false; \ -- cgit v1.3.1