1 /* SPDX-License-Identifier: Apache-2.0 */
2 /*
3  * Copyright (c) 2017 Intel Corporation
4  *
5  */
6 #ifndef __CONFIG_H_
7 #define __CONFIG_H_
8 
9 /**
10  * @brief Check for macro definition in compiler-visible expressions
11  *
12  * This trick was pioneered in Linux as the config_enabled() macro.
13  * The madness has the effect of taking a macro value that may be
14  * defined to "1" (e.g. CONFIG_MYFEATURE), or may not be defined at
15  * all and turning it into a literal expression that can be used at
16  * "runtime".  That is, it works similarly to
17  * "defined(CONFIG_MYFEATURE)" does except that it is an expansion
18  * that can exist in a standard expression and be seen by the compiler
19  * and optimizer.  Thus much ifdef usage can be replaced with cleaner
20  * expressions like:
21  *
22  *     if (IS_ENABLED(CONFIG_MYFEATURE))
23  *             myfeature_enable();
24  *
25  * INTERNAL
26  * First pass just to expand any existing macros, we need the macro
27  * value to be e.g. a literal "1" at expansion time in the next macro,
28  * not "(1)", etc...  Standard recursive expansion does not work.
29  */
30 #define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro)
31 
32 /**
33  * @brief As IS_ENABLED() but to be used with _CFG_* internal switches
34  * to avoid triggering false positives with checkpatch.
35  */
36 #define IS_ENABLED2(config_macro) IS_ENABLED(config_macro)
37 
38 /* Now stick on a "_XXXX" prefix, it will now be "_XXXX1" if config_macro
39  * is "1", or just "_XXXX" if it's undefined.
40  *   ENABLED:   Z_IS_ENABLED2(_XXXX1)
41  *   DISABLED   Z_IS_ENABLED2(_XXXX)
42  */
43 #define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro)
44 
45 /* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
46  * with a trailing comma), so it has the effect of making this a
47  * two-argument tuple to the preprocessor only in the case where the
48  * value is defined to "1"
49  *   ENABLED:    _YYYY,    <--- note comma!
50  *   DISABLED:   _XXXX
51  */
52 #define _XXXX1 _YYYY,
53 
54 /* Then we append an extra argument to fool the gcc preprocessor into
55  * accepting it as a varargs macro.
56  *                         arg1   arg2  arg3
57  *   ENABLED:   Z_IS_ENABLED3(_YYYY,    1,    0)
58  *   DISABLED   Z_IS_ENABLED3(_XXXX 1,  0)
59  */
60 #define Z_IS_ENABLED2(one_or_two_args)		\
61 	Z_IS_ENABLED3(one_or_two_args true, false)
62 
63 /* And our second argument is thus now cooked to be 1 in the case
64  * where the value is defined to 1, and 0 if not:
65  */
66 #define Z_IS_ENABLED3(ignore_this, val, ...) val
67 
68 #endif // __CONFIG_H_
69