Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 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, |
Simon Glass | 69227d8 | 2022-01-22 05:07:25 -0700 | [diff] [blame] | 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) |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 23 | #define ___config_enabled(__ignored, val, ...) val |
| 24 | |
| 25 | /* |
Rasmus Villemoes | c111615 | 2020-07-03 10:37:05 -0600 | [diff] [blame] | 26 | * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 27 | * 0 otherwise. |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 28 | */ |
Simon Glass | 69227d8 | 2022-01-22 05:07:25 -0700 | [diff] [blame] | 29 | #define IS_ENABLED(option) config_enabled(option, 0) |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 30 | |
| 31 | /* |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 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. |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 35 | */ |
Philipp Tomsich | 60e1578 | 2017-06-30 19:02:53 +0200 | [diff] [blame] | 36 | |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 37 | #ifdef USE_HOSTCC |
| 38 | #define _CONFIG_PREFIX TOOLS_ |
| 39 | #elif defined(CONFIG_TPL_BUILD) |
Rasmus Villemoes | f6de07e | 2020-07-03 10:37:04 -0600 | [diff] [blame] | 40 | #define _CONFIG_PREFIX TPL_ |
| 41 | #elif defined(CONFIG_SPL_BUILD) |
| 42 | #define _CONFIG_PREFIX SPL_ |
Philipp Tomsich | 60e1578 | 2017-06-30 19:02:53 +0200 | [diff] [blame] | 43 | #else |
Rasmus Villemoes | f6de07e | 2020-07-03 10:37:04 -0600 | [diff] [blame] | 44 | #define _CONFIG_PREFIX |
Philipp Tomsich | 60e1578 | 2017-06-30 19:02:53 +0200 | [diff] [blame] | 45 | #endif |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 46 | |
Rasmus Villemoes | f6de07e | 2020-07-03 10:37:04 -0600 | [diff] [blame] | 47 | #define config_val(cfg) _config_val(_CONFIG_PREFIX, cfg) |
| 48 | #define _config_val(pfx, cfg) __config_val(pfx, cfg) |
| 49 | #define __config_val(pfx, cfg) CONFIG_ ## pfx ## cfg |
| 50 | |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 51 | /* |
| 52 | * CONFIG_VAL(FOO) evaluates to the value of |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 53 | * CONFIG_TOOLS_FOO if USE_HOSTCC is defined, |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 54 | * CONFIG_FOO if CONFIG_SPL_BUILD is undefined, |
| 55 | * CONFIG_SPL_FOO if CONFIG_SPL_BUILD is defined. |
Lukasz Majewski | 3d5f27c | 2019-09-03 15:43:55 +0200 | [diff] [blame] | 56 | * CONFIG_TPL_FOO if CONFIG_TPL_BUILD is defined. |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 57 | */ |
| 58 | #define CONFIG_VAL(option) config_val(option) |
| 59 | |
| 60 | /* |
Simon Glass | 4cf5d61 | 2022-01-22 05:07:26 -0700 | [diff] [blame] | 61 | * This uses a similar mechanism to config_enabled() above. If cfg is enabled, |
| 62 | * it resolves to the value of opt_cfg, otherwise it resolves to def_val |
| 63 | */ |
| 64 | #define config_opt_enabled(cfg, opt_cfg, def_val) _config_opt_enabled(cfg, opt_cfg, def_val) |
| 65 | #define _config_opt_enabled(cfg_val, opt_value, def_val) \ |
| 66 | __config_opt_enabled(__ARG_PLACEHOLDER_##cfg_val, opt_value, def_val) |
| 67 | #define __config_opt_enabled(arg1_or_junk, arg2, def_val) \ |
| 68 | ___config_opt_enabled(arg1_or_junk arg2, def_val) |
| 69 | #define ___config_opt_enabled(__ignored, val, ...) val |
| 70 | |
| 71 | #ifndef __ASSEMBLY__ |
| 72 | /* |
| 73 | * Detect usage of a the value when the conditional is not enabled. When used |
| 74 | * in assembly context, this likely produces a assembly error, or hopefully at |
| 75 | * least something recognisable. |
| 76 | */ |
| 77 | long invalid_use_of_IF_ENABLED_INT(void); |
| 78 | #endif |
| 79 | |
| 80 | /* Evaluates to int_option if option is defined, otherwise a build error */ |
| 81 | #define IF_ENABLED_INT(option, int_option) \ |
| 82 | config_opt_enabled(option, int_option, invalid_use_of_IF_ENABLED_INT()) |
| 83 | |
| 84 | /* |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 85 | * Count number of arguments to a variadic macro. Currently only need |
| 86 | * it for 1, 2 or 3 arguments. |
| 87 | */ |
| 88 | #define __arg6(a1, a2, a3, a4, a5, a6, ...) a6 |
| 89 | #define __count_args(...) __arg6(dummy, ##__VA_ARGS__, 4, 3, 2, 1, 0) |
| 90 | |
| 91 | #define __concat(a, b) ___concat(a, b) |
| 92 | #define ___concat(a, b) a ## b |
| 93 | |
| 94 | #define __unwrap(...) __VA_ARGS__ |
| 95 | #define __unwrap1(case1, case0) __unwrap case1 |
| 96 | #define __unwrap0(case1, case0) __unwrap case0 |
| 97 | |
| 98 | #define __CONFIG_IS_ENABLED_1(option) __CONFIG_IS_ENABLED_3(option, (1), (0)) |
| 99 | #define __CONFIG_IS_ENABLED_2(option, case1) __CONFIG_IS_ENABLED_3(option, case1, ()) |
| 100 | #define __CONFIG_IS_ENABLED_3(option, case1, case0) \ |
Simon Glass | 69227d8 | 2022-01-22 05:07:25 -0700 | [diff] [blame] | 101 | __concat(__unwrap, config_enabled(CONFIG_VAL(option), 0)) (case1, case0) |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * CONFIG_IS_ENABLED(FOO) expands to |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 105 | * 1 if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y', |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 106 | * 1 if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y', |
| 107 | * 1 if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y', |
Rasmus Villemoes | c111615 | 2020-07-03 10:37:05 -0600 | [diff] [blame] | 108 | * 1 if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y', |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 109 | * 0 otherwise. |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 110 | * |
| 111 | * CONFIG_IS_ENABLED(FOO, (abc)) expands to |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 112 | * abc if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y', |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 113 | * abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y', |
| 114 | * abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y', |
| 115 | * abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y', |
| 116 | * nothing otherwise. |
| 117 | * |
| 118 | * CONFIG_IS_ENABLED(FOO, (abc), (def)) expands to |
Simon Glass | c3236c9 | 2021-09-25 19:43:15 -0600 | [diff] [blame] | 119 | * abc if USE_HOSTCC is defined and CONFIG_TOOLS_FOO is set to 'y', |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 120 | * abc if CONFIG_SPL_BUILD is undefined and CONFIG_FOO is set to 'y', |
| 121 | * abc if CONFIG_SPL_BUILD is defined and CONFIG_SPL_FOO is set to 'y', |
| 122 | * abc if CONFIG_TPL_BUILD is defined and CONFIG_TPL_FOO is set to 'y', |
| 123 | * def otherwise. |
| 124 | * |
| 125 | * The optional second and third arguments must be parenthesized; that |
| 126 | * allows one to include a trailing comma, e.g. for use in |
| 127 | * |
| 128 | * CONFIG_IS_ENABLED(ACME, ({.compatible = "acme,frobnozzle"},)) |
| 129 | * |
| 130 | * which adds an entry to the array being defined if CONFIG_ACME (or |
| 131 | * CONFIG_SPL_ACME/CONFIG_TPL_ACME, depending on build context) is |
| 132 | * set, and nothing otherwise. |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 133 | */ |
Rasmus Villemoes | fd5709f | 2020-07-03 10:37:06 -0600 | [diff] [blame] | 134 | |
| 135 | #define CONFIG_IS_ENABLED(option, ...) \ |
| 136 | __concat(__CONFIG_IS_ENABLED_, __count_args(option, ##__VA_ARGS__)) (option, ##__VA_ARGS__) |
| 137 | |
Simon Glass | 4cf5d61 | 2022-01-22 05:07:26 -0700 | [diff] [blame] | 138 | #ifndef __ASSEMBLY__ |
| 139 | /* |
| 140 | * Detect usage of a the value when the conditional is not enabled. When used |
| 141 | * in assembly context, this likely produces a assembly error, or hopefully at |
| 142 | * least something recognisable. |
| 143 | */ |
| 144 | long invalid_use_of_CONFIG_IF_ENABLED_INT(void); |
| 145 | #endif |
| 146 | |
| 147 | /* |
| 148 | * Evaluates to SPL_/TPL_int_option if SPL_/TPL_/option is not defined, |
| 149 | * otherwise build error |
| 150 | */ |
| 151 | #define CONFIG_IF_ENABLED_INT(option, int_option) \ |
| 152 | CONFIG_IS_ENABLED(option, (CONFIG_VAL(int_option)), \ |
| 153 | (invalid_use_of_CONFIG_IF_ENABLED_INT())) |
Masahiro Yamada | 88c1ccf | 2015-08-12 07:31:43 +0900 | [diff] [blame] | 154 | |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 155 | #endif /* __LINUX_KCONFIG_H */ |