1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 The FreeBSD Foundation
5  *
6  * This software was developed by Björn Zeeb under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef	_LINUXKPI_LINUX_KCONFIG_H_
32 #define	_LINUXKPI_LINUX_KCONFIG_H_
33 
34 /*
35  * Checking if an option is defined would be easy if we could do CPP inside CPP.
36  * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either
37  * case the defined value is "1". A more general -Dxxx=<c> case will require
38  * more effort to deal with all possible "true" values. Hope we do not have
39  * to do this as well.
40  * The real problem is the undefined case.  To avoid this problem we do the
41  * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1"
42  * by having a #define for yyy_1 which is "ignore,".
43  * Otherwise we will just get "yyy".
44  * Need to be careful about variable substitutions in macros though.
45  * This way we make a (true, false) problem a (don't care, true, false) or a
46  * (don't care true, false).  Then we can use a variadic macro to only select
47  * the always well known and defined argument #2.  And that seems to be
48  * exactly what we need.  Use 1 for true and 0 for false to also allow
49  * #if IS_*() checks pre-compiler checks which do not like #if true.
50  */
51 #define ___XAB_1		dontcare,
52 #define ___IS_XAB(_ignore, _x, ...)	(_x)
53 #define	__IS_XAB(_x)		___IS_XAB(_x 1, 0)
54 #define	_IS_XAB(_x)		__IS_XAB(__CONCAT(___XAB_, _x))
55 
56 /* This is if CONFIG_ccc=y. */
57 #define	IS_BUILTIN(_x)		_IS_XAB(_x)
58 /* This is if CONFIG_ccc=m. */
59 #define	IS_MODULE(_x)		_IS_XAB(_x ## _MODULE)
60 /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */
61 #define	IS_ENABLED(_x)		(IS_BUILTIN(_x) || IS_MODULE(_x))
62 /*
63  * This is weird case.  If the CONFIG_ccc is builtin (=y) this returns true;
64  * or if the CONFIG_ccc is a module (=m) and the caller is built as a module
65  * (-DMODULE defined) this returns true, but if the callers is not a module
66  * (-DMODULE not defined, which means caller is BUILTIN) then it returns
67  * false.  In other words, a module can reach the kernel, a module can reach
68  * a module, but the kernel cannot reach a module, and code never compiled
69  * cannot be reached either.
70  * XXX -- I'd hope the module-to-module case would be handled by a proper
71  * module dependency definition (MODULE_DEPEND() in FreeBSD).
72  */
73 #define	IS_REACHABLE(_x)	(IS_BUILTIN(_x) || \
74 				    (IS_MODULE(_x) && IS_BUILTIN(MODULE)))
75 
76 #endif /* _LINUXKPI_LINUX_KCONFIG_H_ */
77