From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4AD8AC636CC for ; Mon, 20 Feb 2023 17:15:14 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 542F985924; Mon, 20 Feb 2023 18:15:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 7B27D8591F; Mon, 20 Feb 2023 18:15:08 +0100 (CET) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id DF7F885905 for ; Mon, 20 Feb 2023 18:15:04 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=andre.przywara@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 019FDFEC; Mon, 20 Feb 2023 09:15:47 -0800 (PST) Received: from donnerap.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A661B3F703; Mon, 20 Feb 2023 09:15:02 -0800 (PST) Date: Mon, 20 Feb 2023 17:14:58 +0000 From: Andre Przywara To: Heinrich Schuchardt Cc: Tom Rini , Simon Glass , Andrew Davis , Stefan Roese , Alper Nebi Yasak , =?UTF-8?B?SsOpcsO0bWU=?= Carretero , Harald Seiler , Philippe Reynes , Yanhong Wang , u-boot@lists.denx.de Subject: Re: [PATCH v3 2/3] kconfig: new macro IF_ENABLED() Message-ID: <20230220171458.68656ef8@donnerap.cambridge.arm.com> In-Reply-To: <20230219113629.39142-3-heinrich.schuchardt@canonical.com> References: <20230219113629.39142-1-heinrich.schuchardt@canonical.com> <20230219113629.39142-3-heinrich.schuchardt@canonical.com> Organization: ARM X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On Sun, 19 Feb 2023 12:36:28 +0100 Heinrich Schuchardt 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 Reviewed-by: Andre Przywara 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