[PATCH 02/31] kconfig: Add support for conditional values

Simon Glass sjg at chromium.org
Wed Jan 12 22:28:21 CET 2022


Hi Ilias,

On Mon, 1 Nov 2021 at 01:05, Ilias Apalodimas
<ilias.apalodimas at linaro.org> wrote:
>
> On Mon, 1 Nov 2021 at 03:19, Simon Glass <sjg at chromium.org> wrote:
> >
> > 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
> >
>
> The example here might be a bit off and we might need this for int
> related values. Was this function handle_wibble() supposed to return
> an int or not?  We could shield the linker easier here without adding
> macros. Something along the lines of
> static void handle_wibble(void)
> {
> #ifdef CONFIG_WIBBLE
> int val = CONFIG_WIBBLE_ADDR;
> #endif
> }
>
> In that case you don't an extra ifdef to call handle_wibble().
> Personally I find this easier to read.

But how does that help with the problem here? I am trying to avoid
using preprocessor macros in this case.

Regards,
Simon

>
> >  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 0 if CONFIG_WIBBLE is not defined, and
> > CONFIG_WIBBLE_ADDR if it is. 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.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> >  include/linux/kconfig.h      | 18 ++++++++++++++++++
> >  scripts/config_whitelist.txt |  1 +
> >  2 files changed, 19 insertions(+)
> >
> > diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h
> > index a1d1a298426..119c698a158 100644
> > --- a/include/linux/kconfig.h
> > +++ b/include/linux/kconfig.h
> > @@ -59,6 +59,18 @@
> >   */
> >  #define CONFIG_VAL(option)  config_val(option)
> >
> > +/* This use a similar mechanism to config_enabled() above */
> > +#define config_opt_enabled(cfg, opt_cfg) _config_opt_enabled(cfg, opt_cfg)
> > +#define _config_opt_enabled(cfg_val, opt_value) \
> > +       __config_opt_enabled(__ARG_PLACEHOLDER_##cfg_val, opt_value)
> > +#define __config_opt_enabled(arg1_or_junk, arg2) \
> > +       ___config_opt_enabled(arg1_or_junk arg2, 0)
> > +#define ___config_opt_enabled(__ignored, val, ...) val
> > +
> > +/* Evaluates to 0 if option is not defined, int_option if it is defined */
> > +#define IF_ENABLED_INT(option, int_option) \
> > +       config_opt_enabled(option, int_option)
> > +
> >  /*
> >   * Count number of arguments to a variadic macro. Currently only need
> >   * it for 1, 2 or 3 arguments.
> > @@ -113,5 +125,11 @@
> >  #define CONFIG_IS_ENABLED(option, ...)                                 \
> >         __concat(__CONFIG_IS_ENABLED_, __count_args(option, ##__VA_ARGS__)) (option, ##__VA_ARGS__)
> >
> > +/*
> > + * Evaluates to 0 if SPL_/TPL_/option is not defined, SPL_/TPL_int_option if it
> > + * is defined
> > + */
> > +#define CONFIG_IF_ENABLED_INT(option, int_option) \
> > +       CONFIG_IS_ENABLED(option, (CONFIG_VAL(int_option)), (0))
> >
> >  #endif /* __LINUX_KCONFIG_H */
> > diff --git a/scripts/config_whitelist.txt b/scripts/config_whitelist.txt
> > index 022a27288c9..f9d9f4a9cfe 100644
> > --- a/scripts/config_whitelist.txt
> > +++ b/scripts/config_whitelist.txt
> > @@ -609,6 +609,7 @@ CONFIG_ICS307_REFCLK_HZ
> >  CONFIG_IDE_PREINIT
> >  CONFIG_IDE_RESET
> >  CONFIG_IDE_SWAP_IO
> > +CONFIG_IF_ENABLED_INT
> >  CONFIG_IMA
> >  CONFIG_IMX
> >  CONFIG_IMX6_PWM_PER_CLK
> > --
> > 2.33.1.1089.g2158813163f-goog
> >
>
> Regards
> /Ilias


More information about the U-Boot mailing list