* [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header
@ 2024-04-03 7:23 Ahmad Fatoum
2024-04-03 7:23 ` [PATCH 2/2] include: is_defined: implement __if_defined helper Ahmad Fatoum
2024-04-22 12:16 ` [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-04-03 7:23 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
While we run the preprocessor over device trees before compiling them,
we don't define the Kconfig symbols and including <generated/autoconf.h>
would fail.
The file defines __is_defined however, which can be useful for other
macros that are either defined to 1 or undefined like the macros we
define for device tree fragments to indicate which file they are being
appended to. To make it possible to use __is_defined from fragments,
move the kconfig-independent part out of <linux/kconfig.h> into
<linux/is_defined.h>.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/linux/is_defined.h | 37 +++++++++++++++++++++++++++++++++++++
include/linux/kconfig.h | 33 +--------------------------------
2 files changed, 38 insertions(+), 32 deletions(-)
create mode 100644 include/linux/is_defined.h
diff --git a/include/linux/is_defined.h b/include/linux/is_defined.h
new file mode 100644
index 000000000000..d68f8e877f9b
--- /dev/null
+++ b/include/linux/is_defined.h
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_IS_DEFINED_H_
+#define __LINUX_IS_DEFINED_H_
+
+#define __ARG_PLACEHOLDER_1 0,
+#define __take_second_arg(__ignored, val, ...) val
+
+/*
+ * The use of "&&" / "||" is limited in certain expressions.
+ * The following enable to calculate "and" / "or" with macro expansion only.
+ */
+#define __and(x, y) ___and(x, y)
+#define ___and(x, y) ____and(__ARG_PLACEHOLDER_##x, y)
+#define ____and(arg1_or_junk, y) __take_second_arg(arg1_or_junk y, 0)
+
+#define __or(x, y) ___or(x, y)
+#define ___or(x, y) ____or(__ARG_PLACEHOLDER_##x, y)
+#define ____or(arg1_or_junk, y) __take_second_arg(arg1_or_junk 1, y)
+
+/*
+ * Helper macros to use CONFIG_ options in C/CPP expressions. Note that
+ * these only work with boolean and tristate options.
+ */
+
+/*
+ * Getting something that works in C and CPP for an arg that may or may
+ * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1"
+ * we match on the placeholder define, insert the "0," for arg1 and generate
+ * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one).
+ * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
+ * the last step cherry picks the 2nd arg, we get a zero.
+ */
+#define __is_defined(x) ___is_defined(x)
+#define ___is_defined(val) ____is_defined(__ARG_PLACEHOLDER_##val)
+#define ____is_defined(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0)
+
+#endif
diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h
index fec5076eda91..58f68adbbadf 100644
--- a/include/linux/kconfig.h
+++ b/include/linux/kconfig.h
@@ -3,38 +3,7 @@
#define __LINUX_KCONFIG_H
#include <generated/autoconf.h>
-
-#define __ARG_PLACEHOLDER_1 0,
-#define __take_second_arg(__ignored, val, ...) val
-
-/*
- * The use of "&&" / "||" is limited in certain expressions.
- * The following enable to calculate "and" / "or" with macro expansion only.
- */
-#define __and(x, y) ___and(x, y)
-#define ___and(x, y) ____and(__ARG_PLACEHOLDER_##x, y)
-#define ____and(arg1_or_junk, y) __take_second_arg(arg1_or_junk y, 0)
-
-#define __or(x, y) ___or(x, y)
-#define ___or(x, y) ____or(__ARG_PLACEHOLDER_##x, y)
-#define ____or(arg1_or_junk, y) __take_second_arg(arg1_or_junk 1, y)
-
-/*
- * Helper macros to use CONFIG_ options in C/CPP expressions. Note that
- * these only work with boolean and tristate options.
- */
-
-/*
- * Getting something that works in C and CPP for an arg that may or may
- * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1"
- * we match on the placeholder define, insert the "0," for arg1 and generate
- * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one).
- * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
- * the last step cherry picks the 2nd arg, we get a zero.
- */
-#define __is_defined(x) ___is_defined(x)
-#define ___is_defined(val) ____is_defined(__ARG_PLACEHOLDER_##val)
-#define ____is_defined(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0)
+#include <linux/is_defined.h>
/*
* IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] include: is_defined: implement __if_defined helper
2024-04-03 7:23 [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Ahmad Fatoum
@ 2024-04-03 7:23 ` Ahmad Fatoum
2024-04-22 12:16 ` [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-04-03 7:23 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
This is useful when preprocessing non-C code that lacks an if statement
equivalent. For example, external device tree fragments can now specify:
#include <linux/is_defined.h>
__if_defined(bcm2711_rpi_4_dts, &emmc2, &sdhost) {
instead of having to use the more verbose #ifdef:
#ifdef bcm2711_rpi_4_dts
&emmc2 {
#else
&sdhost {
#endif
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/linux/is_defined.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/include/linux/is_defined.h b/include/linux/is_defined.h
index d68f8e877f9b..bfefca033f70 100644
--- a/include/linux/is_defined.h
+++ b/include/linux/is_defined.h
@@ -34,4 +34,10 @@
#define ___is_defined(val) ____is_defined(__ARG_PLACEHOLDER_##val)
#define ____is_defined(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0)
+#define __if_defined(x, a, b) ___if_defined(__is_defined(x), a, b)
+#define ___if_defined(val, a, b) ____if_defined(val, a, b)
+#define ____if_defined(val, a, b) ____if_defined_##val(a, b)
+#define ____if_defined_1(a, b) a
+#define ____if_defined_0(a, b) b
+
#endif
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header
2024-04-03 7:23 [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Ahmad Fatoum
2024-04-03 7:23 ` [PATCH 2/2] include: is_defined: implement __if_defined helper Ahmad Fatoum
@ 2024-04-22 12:16 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-04-22 12:16 UTC (permalink / raw)
To: barebox, Ahmad Fatoum
On Wed, 03 Apr 2024 09:23:31 +0200, Ahmad Fatoum wrote:
> While we run the preprocessor over device trees before compiling them,
> we don't define the Kconfig symbols and including <generated/autoconf.h>
> would fail.
>
> The file defines __is_defined however, which can be useful for other
> macros that are either defined to 1 or undefined like the macros we
> define for device tree fragments to indicate which file they are being
> appended to. To make it possible to use __is_defined from fragments,
> move the kconfig-independent part out of <linux/kconfig.h> into
> <linux/is_defined.h>.
>
> [...]
Applied, thanks!
[1/2] include: kconfig.h: move __is_defined definition into new header
https://git.pengutronix.de/cgit/barebox/commit/?id=71b94b24f462 (link may not be stable)
[2/2] include: is_defined: implement __if_defined helper
https://git.pengutronix.de/cgit/barebox/commit/?id=1674da1f95ce (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-04-22 12:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-03 7:23 [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Ahmad Fatoum
2024-04-03 7:23 ` [PATCH 2/2] include: is_defined: implement __if_defined helper Ahmad Fatoum
2024-04-22 12:16 ` [PATCH 1/2] include: kconfig.h: move __is_defined definition into new header Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox