public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara@arm.com>
To: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Cc: "Tom Rini" <trini@konsulko.com>, "Simon Glass" <sjg@chromium.org>,
	"Andrew Davis" <afd@ti.com>, "Stefan Roese" <sr@denx.de>,
	"Alper Nebi Yasak" <alpernebiyasak@gmail.com>,
	"Jérôme Carretero" <cJ-uboot@zougloub.eu>,
	"Harald Seiler" <hws@denx.de>,
	"Philippe Reynes" <philippe.reynes@softathome.com>,
	"Yanhong Wang" <yanhong.wang@starfivetech.com>,
	u-boot@lists.denx.de
Subject: Re: [PATCH v3 2/3] kconfig: new macro IF_ENABLED()
Date: Mon, 20 Feb 2023 17:14:58 +0000	[thread overview]
Message-ID: <20230220171458.68656ef8@donnerap.cambridge.arm.com> (raw)
In-Reply-To: <20230219113629.39142-3-heinrich.schuchardt@canonical.com>

On Sun, 19 Feb 2023 12:36:28 +0100
Heinrich Schuchardt <heinrich.schuchardt@canonical.com> wrote:

Hi,

> We want to move from using #ifdef to using if in our code. A lot of our
> code using #ifdef is structured like:
> 
>     #ifdef CONFIG_FOO
>         fun(CONFIG_FOO_OPT);
>     #endif
> 
> In Kconfig you will find
> 
>     config FOO
>         bool "enable foo"
> 
>     config FOO_OPT
>         string "value for foo"
>         depends on FOO
> 
> We cannot use CONFIG_FOO_OPT in our code directly if CONFIG_FOO is not
> defined.
> 
>      if (IS_ENABLED(CONFIG_FOO)
>          fun(CONFIG_FOO_OPT);
> 
> This would result in an error due to the undefined symbol CONFIG_FOO_OPT.
> 
> The new macro IF_ENABLED(CONFIG_FOO, opt_cfg, def_val) which evaluates
> to opt_cfg if CONFIG_FOO is set to 'y' and to def_val otherwise comes to
> the rescue:
> 
>      if (IS_ENABLED(CONFIG_FOO)
>          fun(IF_ENABLED(CONFIG_FOO, CONFIG_FOO_OPT, "");

Ah, yeah, I like this one, this is indeed the most common pattern that
prevents many "#if to if" conversions.
I briefly thought about unifying those two lines, but it's probably
limiting, as it appears more flexible this way:

> 
> If CONFIG_FOO is not defined, the compiler will see
> 
>     if (0)
>         fun("");
> 
> and be happy.
> 
> If CONFIG_FOO is defined, the compiler will see
> 
>     if (1)
>         fun(CONFIG_FOO_OPT)
> 
> and be equally happy.
> 
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

Reviewed-by: Andre Przywara <andre.przywara@arm.com>

Cheers,
Andre

> ---
> v3:
> 	new patch
> ---
>  include/linux/kconfig.h | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h
> index 2bc704e110..7fea72da1a 100644
> --- a/include/linux/kconfig.h
> +++ b/include/linux/kconfig.h
> @@ -28,6 +28,13 @@
>   */
>  #define IS_ENABLED(option)	config_enabled(option, 0)
>  
> +/*
> + * IF_ENABLED(CONFIG_FOO, opt_cfg, def_val) evalutes to opt_cfg if
> + * CONFIG_FOO is set to 'y' and to def_val otherwise.
> + */
> +#define IF_ENABLED(option, opt_cfg, def_val) \
> +	config_opt_enabled(IS_ENABLED(option), opt_cfg, def_val)
> +
>  /*
>   * U-Boot add-on: Helper macros to reference to different macros (prefixed by
>   * CONFIG_, CONFIG_SPL_, CONFIG_TPL_ or CONFIG_TOOLS_), depending on the build


  reply	other threads:[~2023-02-20 17:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-19 11:36 [PATCH v3 0/3] spl: allow loading via partition type GUID Heinrich Schuchardt
2023-02-19 11:36 ` [PATCH v3 1/3] disk: accessors for conditional partition fields Heinrich Schuchardt
2023-02-20 16:21   ` Simon Glass
2023-02-19 11:36 ` [PATCH v3 2/3] kconfig: new macro IF_ENABLED() Heinrich Schuchardt
2023-02-20 17:14   ` Andre Przywara [this message]
2023-02-21 19:41     ` Simon Glass
2023-02-19 11:36 ` [PATCH v3 3/3] spl: allow loading via partition type GUID Heinrich Schuchardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230220171458.68656ef8@donnerap.cambridge.arm.com \
    --to=andre.przywara@arm.com \
    --cc=afd@ti.com \
    --cc=alpernebiyasak@gmail.com \
    --cc=cJ-uboot@zougloub.eu \
    --cc=heinrich.schuchardt@canonical.com \
    --cc=hws@denx.de \
    --cc=philippe.reynes@softathome.com \
    --cc=sjg@chromium.org \
    --cc=sr@denx.de \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=yanhong.wang@starfivetech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox