1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun #ifndef _TOOLS_LINUX_KCONFIG_H 3*4882a593Smuzhiyun #define _TOOLS_LINUX_KCONFIG_H 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun /* CONFIG_CC_VERSION_TEXT (Do not delete this comment. See help in Kconfig) */ 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun #define __ARG_PLACEHOLDER_1 0, 8*4882a593Smuzhiyun #define __take_second_arg(__ignored, val, ...) val 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun /* 11*4882a593Smuzhiyun * The use of "&&" / "||" is limited in certain expressions. 12*4882a593Smuzhiyun * The following enable to calculate "and" / "or" with macro expansion only. 13*4882a593Smuzhiyun */ 14*4882a593Smuzhiyun #define __and(x, y) ___and(x, y) 15*4882a593Smuzhiyun #define ___and(x, y) ____and(__ARG_PLACEHOLDER_##x, y) 16*4882a593Smuzhiyun #define ____and(arg1_or_junk, y) __take_second_arg(arg1_or_junk y, 0) 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun #define __or(x, y) ___or(x, y) 19*4882a593Smuzhiyun #define ___or(x, y) ____or(__ARG_PLACEHOLDER_##x, y) 20*4882a593Smuzhiyun #define ____or(arg1_or_junk, y) __take_second_arg(arg1_or_junk 1, y) 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun /* 23*4882a593Smuzhiyun * Helper macros to use CONFIG_ options in C/CPP expressions. Note that 24*4882a593Smuzhiyun * these only work with boolean and tristate options. 25*4882a593Smuzhiyun */ 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun /* 28*4882a593Smuzhiyun * Getting something that works in C and CPP for an arg that may or may 29*4882a593Smuzhiyun * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1" 30*4882a593Smuzhiyun * we match on the placeholder define, insert the "0," for arg1 and generate 31*4882a593Smuzhiyun * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one). 32*4882a593Smuzhiyun * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when 33*4882a593Smuzhiyun * the last step cherry picks the 2nd arg, we get a zero. 34*4882a593Smuzhiyun */ 35*4882a593Smuzhiyun #define __is_defined(x) ___is_defined(x) 36*4882a593Smuzhiyun #define ___is_defined(val) ____is_defined(__ARG_PLACEHOLDER_##val) 37*4882a593Smuzhiyun #define ____is_defined(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0) 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun /* 40*4882a593Smuzhiyun * IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0 41*4882a593Smuzhiyun * otherwise. For boolean options, this is equivalent to 42*4882a593Smuzhiyun * IS_ENABLED(CONFIG_FOO). 43*4882a593Smuzhiyun */ 44*4882a593Smuzhiyun #define IS_BUILTIN(option) __is_defined(option) 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun /* 47*4882a593Smuzhiyun * IS_MODULE(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'm', 0 48*4882a593Smuzhiyun * otherwise. 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyun #define IS_MODULE(option) __is_defined(option##_MODULE) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun /* 53*4882a593Smuzhiyun * IS_REACHABLE(CONFIG_FOO) evaluates to 1 if the currently compiled 54*4882a593Smuzhiyun * code can call a function defined in code compiled based on CONFIG_FOO. 55*4882a593Smuzhiyun * This is similar to IS_ENABLED(), but returns false when invoked from 56*4882a593Smuzhiyun * built-in code when CONFIG_FOO is set to 'm'. 57*4882a593Smuzhiyun */ 58*4882a593Smuzhiyun #define IS_REACHABLE(option) __or(IS_BUILTIN(option), \ 59*4882a593Smuzhiyun __and(IS_MODULE(option), __is_defined(MODULE))) 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun /* 62*4882a593Smuzhiyun * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm', 63*4882a593Smuzhiyun * 0 otherwise. 64*4882a593Smuzhiyun */ 65*4882a593Smuzhiyun #define IS_ENABLED(option) __or(IS_BUILTIN(option), IS_MODULE(option)) 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun #endif /* _TOOLS_LINUX_KCONFIG_H */ 68