blob: d109ed3119e89b67a70ea9c48e0c2b8ebd6233e1 [file] [log] [blame]
Masahiro Yamadaed16f5a2014-07-30 14:08:13 +09001#ifndef __LINUX_KCONFIG_H
2#define __LINUX_KCONFIG_H
3
4#include <generated/autoconf.h>
5
6/*
7 * Helper macros to use CONFIG_ options in C/CPP expressions. Note that
8 * these only work with boolean and tristate options.
9 */
10
11/*
12 * Getting something that works in C and CPP for an arg that may or may
13 * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1"
14 * we match on the placeholder define, insert the "0," for arg1 and generate
15 * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one).
16 * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
17 * the last step cherry picks the 2nd arg, we get a zero.
18 */
19#define __ARG_PLACEHOLDER_1 0,
20#define config_enabled(cfg) _config_enabled(cfg)
21#define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
22#define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0)
23#define ___config_enabled(__ignored, val, ...) val
24
25/*
Rasmus Villemoesc1116152020-07-03 10:37:05 -060026 * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y',
Masahiro Yamadaed16f5a2014-07-30 14:08:13 +090027 * 0 otherwise.
28 *
29 */
30#define IS_ENABLED(option) \
Rasmus Villemoesc1116152020-07-03 10:37:05 -060031 (config_enabled(option))
Masahiro Yamadaed16f5a2014-07-30 14:08:13 +090032
33/*
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090034 * U-Boot add-on: Helper macros to reference to different macros
35 * (CONFIG_ or CONFIG_SPL_ prefixed), depending on the build context.
36 */
Philipp Tomsich60e15782017-06-30 19:02:53 +020037
38#if defined(CONFIG_TPL_BUILD)
Rasmus Villemoesf6de07e2020-07-03 10:37:04 -060039#define _CONFIG_PREFIX TPL_
40#elif defined(CONFIG_SPL_BUILD)
41#define _CONFIG_PREFIX SPL_
Philipp Tomsich60e15782017-06-30 19:02:53 +020042#else
Rasmus Villemoesf6de07e2020-07-03 10:37:04 -060043#define _CONFIG_PREFIX
Philipp Tomsich60e15782017-06-30 19:02:53 +020044#endif
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090045
Rasmus Villemoesf6de07e2020-07-03 10:37:04 -060046#define config_val(cfg) _config_val(_CONFIG_PREFIX, cfg)
47#define _config_val(pfx, cfg) __config_val(pfx, cfg)
48#define __config_val(pfx, cfg) CONFIG_ ## pfx ## cfg
49
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090050/*
51 * CONFIG_VAL(FOO) evaluates to the value of
52 * CONFIG_FOO if CONFIG_SPL_BUILD is undefined,
53 * CONFIG_SPL_FOO if CONFIG_SPL_BUILD is defined.
Lukasz Majewski3d5f27c2019-09-03 15:43:55 +020054 * CONFIG_TPL_FOO if CONFIG_TPL_BUILD is defined.
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090055 */
56#define CONFIG_VAL(option) config_val(option)
57
58/*
Rasmus Villemoesfd5709f2020-07-03 10:37:06 -060059 * Count number of arguments to a variadic macro. Currently only need
60 * it for 1, 2 or 3 arguments.
61 */
62#define __arg6(a1, a2, a3, a4, a5, a6, ...) a6
63#define __count_args(...) __arg6(dummy, ##__VA_ARGS__, 4, 3, 2, 1, 0)
64
65#define __concat(a, b) ___concat(a, b)
66#define ___concat(a, b) a ## b
67
68#define __unwrap(...) __VA_ARGS__
69#define __unwrap1(case1, case0) __unwrap case1
70#define __unwrap0(case1, case0) __unwrap case0
71
72#define __CONFIG_IS_ENABLED_1(option) __CONFIG_IS_ENABLED_3(option, (1), (0))
73#define __CONFIG_IS_ENABLED_2(option, case1) __CONFIG_IS_ENABLED_3(option, case1, ())
74#define __CONFIG_IS_ENABLED_3(option, case1, case0) \
75 __concat(__unwrap, config_enabled(CONFIG_VAL(option))) (case1, case0)
76
77/*
78 * CONFIG_IS_ENABLED(FOO) expands to
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090079 * 1 if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
80 * 1 if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
Rasmus Villemoesc1116152020-07-03 10:37:05 -060081 * 1 if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +090082 * 0 otherwise.
Rasmus Villemoesfd5709f2020-07-03 10:37:06 -060083 *
84 * CONFIG_IS_ENABLED(FOO, (abc)) expands to
85 * abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
86 * abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
87 * abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
88 * nothing otherwise.
89 *
90 * CONFIG_IS_ENABLED(FOO, (abc), (def)) expands to
91 * abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
92 * abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
93 * abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
94 * def otherwise.
95 *
96 * The optional second and third arguments must be parenthesized; that
97 * allows one to include a trailing comma, e.g. for use in
98 *
99 * CONFIG_IS_ENABLED(ACME, ({.compatible = "acme,frobnozzle"},))
100 *
101 * which adds an entry to the array being defined if CONFIG_ACME (or
102 * CONFIG_SPL_ACME/CONFIG_TPL_ACME, depending on build context) is
103 * set, and nothing otherwise.
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900104 */
Rasmus Villemoesfd5709f2020-07-03 10:37:06 -0600105
106#define CONFIG_IS_ENABLED(option, ...) \
107 __concat(__CONFIG_IS_ENABLED_, __count_args(option, ##__VA_ARGS__)) (option, ##__VA_ARGS__)
108
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900109
Masahiro Yamadaed16f5a2014-07-30 14:08:13 +0900110#endif /* __LINUX_KCONFIG_H */