Prepare v2023.10
[platform/kernel/u-boot.git] / include / linux / kconfig.h
1 #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, def_val) _config_enabled(cfg, def_val)
21 #define _config_enabled(value, def_val) __config_enabled(__ARG_PLACEHOLDER_##value, def_val)
22 #define __config_enabled(arg1_or_junk, def_val) ___config_enabled(arg1_or_junk 1, def_val)
23 #define ___config_enabled(__ignored, val, ...) val
24
25 /*
26  * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y',
27  * 0 otherwise.
28  */
29 #define IS_ENABLED(option)      config_enabled(option, 0)
30
31 /*
32  * U-Boot add-on: Helper macros to reference to different macros (prefixed by
33  * CONFIG_, CONFIG_SPL_, CONFIG_TPL_ or CONFIG_TOOLS_), depending on the build
34  * context.
35  */
36
37 #ifdef USE_HOSTCC
38 #define _CONFIG_PREFIX TOOLS_
39 #elif defined(CONFIG_TPL_BUILD)
40 #define _CONFIG_PREFIX TPL_
41 #elif defined(CONFIG_VPL_BUILD)
42 #define _CONFIG_PREFIX VPL_
43 #elif defined(CONFIG_SPL_BUILD)
44 #define _CONFIG_PREFIX SPL_
45 #else
46 #define _CONFIG_PREFIX
47 #endif
48
49 #define   config_val(cfg)       _config_val(_CONFIG_PREFIX, cfg)
50 #define  _config_val(pfx, cfg) __config_val(pfx, cfg)
51 #define __config_val(pfx, cfg) CONFIG_ ## pfx ## cfg
52
53 /*
54  * CONFIG_VAL(FOO) evaluates to the value of
55  *  CONFIG_TOOLS_FOO if USE_HOSTCC is defined,
56  *  CONFIG_FOO if CONFIG_SPL_BUILD is undefined,
57  *  CONFIG_SPL_FOO if CONFIG_SPL_BUILD is defined.
58  *  CONFIG_TPL_FOO if CONFIG_TPL_BUILD is defined.
59  *  CONFIG_VPL_FOO if CONFIG_VPL_BUILD is defined.
60  */
61 #define CONFIG_VAL(option)  config_val(option)
62
63 /*
64  * This uses a similar mechanism to config_enabled() above. If cfg is enabled,
65  * it resolves to the value of opt_cfg, otherwise it resolves to def_val
66  */
67 #define config_opt_enabled(cfg, opt_cfg, def_val) _config_opt_enabled(cfg, opt_cfg, def_val)
68 #define _config_opt_enabled(cfg_val, opt_value, def_val) \
69         __config_opt_enabled(__ARG_PLACEHOLDER_##cfg_val, opt_value, def_val)
70 #define __config_opt_enabled(arg1_or_junk, arg2, def_val) \
71         ___config_opt_enabled(arg1_or_junk arg2, def_val)
72 #define ___config_opt_enabled(__ignored, val, ...) val
73
74 #ifndef __ASSEMBLY__
75 /*
76  * Detect usage of a the value when the conditional is not enabled. When used
77  * in assembly context, this likely produces a assembly error, or hopefully at
78  * least something recognisable.
79  */
80 long invalid_use_of_IF_ENABLED_INT(void);
81 #endif
82
83 /* Evaluates to int_option if option is defined, otherwise a build error */
84 #define IF_ENABLED_INT(option, int_option) \
85         config_opt_enabled(option, int_option, invalid_use_of_IF_ENABLED_INT())
86
87 /*
88  * Count number of arguments to a variadic macro. Currently only need
89  * it for 1, 2 or 3 arguments.
90  */
91 #define __arg6(a1, a2, a3, a4, a5, a6, ...) a6
92 #define __count_args(...) __arg6(dummy, ##__VA_ARGS__, 4, 3, 2, 1, 0)
93
94 #define __concat(a, b)   ___concat(a, b)
95 #define ___concat(a, b)  a ## b
96
97 #define __unwrap(...) __VA_ARGS__
98 #define __unwrap1(case1, case0) __unwrap case1
99 #define __unwrap0(case1, case0) __unwrap case0
100
101 #define __CONFIG_IS_ENABLED_1(option)        __CONFIG_IS_ENABLED_3(option, (1), (0))
102 #define __CONFIG_IS_ENABLED_2(option, case1) __CONFIG_IS_ENABLED_3(option, case1, ())
103 #define __CONFIG_IS_ENABLED_3(option, case1, case0) \
104         __concat(__unwrap, config_enabled(CONFIG_VAL(option), 0)) (case1, case0)
105
106 /*
107  * CONFIG_IS_ENABLED(FOO) expands to
108  *  1 if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y',
109  *  1 if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
110  *  1 if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
111  *  1 if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
112  *  0 otherwise.
113  *
114  * CONFIG_IS_ENABLED(FOO, (abc)) expands to
115  *  abc if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y',
116  *  abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
117  *  abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
118  *  abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
119  *  nothing otherwise.
120  *
121  * CONFIG_IS_ENABLED(FOO, (abc), (def)) expands to
122  *  abc if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y',
123  *  abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y',
124  *  abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y',
125  *  abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y',
126  *  def otherwise.
127  *
128  * The optional second and third arguments must be parenthesized; that
129  * allows one to include a trailing comma, e.g. for use in
130  *
131  * CONFIG_IS_ENABLED(ACME, ({.compatible = "acme,frobnozzle"},))
132  *
133  * which adds an entry to the array being defined if CONFIG_ACME (or
134  * CONFIG_SPL_ACME/CONFIG_TPL_ACME, depending on build context) is
135  * set, and nothing otherwise.
136  */
137
138 #define CONFIG_IS_ENABLED(option, ...)                                  \
139         __concat(__CONFIG_IS_ENABLED_, __count_args(option, ##__VA_ARGS__)) (option, ##__VA_ARGS__)
140
141 #ifndef __ASSEMBLY__
142 /*
143  * Detect usage of a the value when the conditional is not enabled. When used
144  * in assembly context, this likely produces a assembly error, or hopefully at
145  * least something recognisable.
146  */
147 long invalid_use_of_CONFIG_IF_ENABLED_INT(void);
148 #endif
149
150 /*
151  * Evaluates to SPL_/TPL_int_option if SPL_/TPL_/option is not defined,
152  * otherwise build error
153  */
154 #define CONFIG_IF_ENABLED_INT(option, int_option) \
155         CONFIG_IS_ENABLED(option, (CONFIG_VAL(int_option)), \
156                 (invalid_use_of_CONFIG_IF_ENABLED_INT()))
157
158 #endif /* __LINUX_KCONFIG_H */