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  * $FreeBSD$
31  */
32 
33 #ifndef	_LINUXKPI_LINUX_KCONFIG_H_
34 #define	_LINUXKPI_LINUX_KCONFIG_H_
35 
36 /*
37  * Checking if an option is defined would be easy if we could do CPP inside CPP.
38  * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either
39  * case the defined value is "1". A more general -Dxxx=<c> case will require
40  * more effort to deal with all possible "true" values. Hope we do not have
41  * to do this as well.
42  * The real problem is the undefined case.  To avoid this problem we do the
43  * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1"
44  * by having a #define for yyy_1 which is "ignore,".
45  * Otherwise we will just get "yyy".
46  * Need to be careful about variable substitutions in macros though.
47  * This way we make a (true, false) problem a (don't care, true, false) or a
48  * (don't care true, false).  Then we can use a variadic macro to only select
49  * the always well known and defined argument #2.  And that seems to be
50  * exactly what we need.  Use 1 for true and 0 for false to also allow
51  * #if IS_*() checks pre-compiler checks which do not like #if true.
52  */
53 #define ___XAB_1		dontcare,
54 #define ___IS_XAB(_ignore, _x, ...)	(_x)
55 #define	__IS_XAB(_x)		___IS_XAB(_x 1, 0)
56 #define	_IS_XAB(_x)		__IS_XAB(__CONCAT(___XAB_, _x))
57 
58 /* This is if CONFIG_ccc=y. */
59 #define	IS_BUILTIN(_x)		_IS_XAB(_x)
60 /* This is if CONFIG_ccc=m. */
61 #define	IS_MODULE(_x)		_IS_XAB(_x ## _MODULE)
62 /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */
63 #define	IS_ENABLED(_x)		(IS_BUILTIN(_x) || IS_MODULE(_x))
64 /*
65  * This is weird case.  If the CONFIG_ccc is builtin (=y) this returns true;
66  * or if the CONFIG_ccc is a module (=m) and the caller is built as a module
67  * (-DMODULE defined) this returns true, but if the callers is not a module
68  * (-DMODULE not defined, which means caller is BUILTIN) then it returns
69  * false.  In other words, a module can reach the kernel, a module can reach
70  * a module, but the kernel cannot reach a module, and code never compiled
71  * cannot be reached either.
72  * XXX -- I'd hope the module-to-module case would be handled by a proper
73  * module dependency definition (MODULE_DEPEND() in FreeBSD).
74  */
75 #define	IS_REACHABLE(_x)	(IS_BUILTIN(_x) || \
76 				    (IS_MODULE(_x) && IS_BUILTIN(MODULE)))
77 
78 #endif /* _LINUXKPI_LINUX_KCONFIG_H_ */
79