14b0552d5SJean-Sébastien Pédron /*-
24b0552d5SJean-Sébastien Pédron  * SPDX-License-Identifier: BSD-2-Clause
34b0552d5SJean-Sébastien Pédron  *
44b0552d5SJean-Sébastien Pédron  * Copyright (c) 2020 The FreeBSD Foundation
54b0552d5SJean-Sébastien Pédron  *
64b0552d5SJean-Sébastien Pédron  * This software was developed by Björn Zeeb under sponsorship from
74b0552d5SJean-Sébastien Pédron  * the FreeBSD Foundation.
84b0552d5SJean-Sébastien Pédron  *
94b0552d5SJean-Sébastien Pédron  * Redistribution and use in source and binary forms, with or without
104b0552d5SJean-Sébastien Pédron  * modification, are permitted provided that the following conditions
114b0552d5SJean-Sébastien Pédron  * are met:
124b0552d5SJean-Sébastien Pédron  * 1. Redistributions of source code must retain the above copyright
134b0552d5SJean-Sébastien Pédron  *    notice, this list of conditions and the following disclaimer.
144b0552d5SJean-Sébastien Pédron  * 2. Redistributions in binary form must reproduce the above copyright
154b0552d5SJean-Sébastien Pédron  *    notice, this list of conditions and the following disclaimer in the
164b0552d5SJean-Sébastien Pédron  *    documentation and/or other materials provided with the distribution.
174b0552d5SJean-Sébastien Pédron  *
184b0552d5SJean-Sébastien Pédron  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
194b0552d5SJean-Sébastien Pédron  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204b0552d5SJean-Sébastien Pédron  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214b0552d5SJean-Sébastien Pédron  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
224b0552d5SJean-Sébastien Pédron  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
234b0552d5SJean-Sébastien Pédron  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
244b0552d5SJean-Sébastien Pédron  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
254b0552d5SJean-Sébastien Pédron  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
264b0552d5SJean-Sébastien Pédron  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
274b0552d5SJean-Sébastien Pédron  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
284b0552d5SJean-Sébastien Pédron  * SUCH DAMAGE.
294b0552d5SJean-Sébastien Pédron  */
304b0552d5SJean-Sébastien Pédron 
314b0552d5SJean-Sébastien Pédron #ifndef	_LINUXKPI_LINUX_KCONFIG_H_
324b0552d5SJean-Sébastien Pédron #define	_LINUXKPI_LINUX_KCONFIG_H_
334b0552d5SJean-Sébastien Pédron 
344b0552d5SJean-Sébastien Pédron /*
354b0552d5SJean-Sébastien Pédron  * Checking if an option is defined would be easy if we could do CPP inside CPP.
364b0552d5SJean-Sébastien Pédron  * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either
374b0552d5SJean-Sébastien Pédron  * case the defined value is "1". A more general -Dxxx=<c> case will require
384b0552d5SJean-Sébastien Pédron  * more effort to deal with all possible "true" values. Hope we do not have
394b0552d5SJean-Sébastien Pédron  * to do this as well.
404b0552d5SJean-Sébastien Pédron  * The real problem is the undefined case.  To avoid this problem we do the
414b0552d5SJean-Sébastien Pédron  * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1"
424b0552d5SJean-Sébastien Pédron  * by having a #define for yyy_1 which is "ignore,".
434b0552d5SJean-Sébastien Pédron  * Otherwise we will just get "yyy".
444b0552d5SJean-Sébastien Pédron  * Need to be careful about variable substitutions in macros though.
454b0552d5SJean-Sébastien Pédron  * This way we make a (true, false) problem a (don't care, true, false) or a
464b0552d5SJean-Sébastien Pédron  * (don't care true, false).  Then we can use a variadic macro to only select
474b0552d5SJean-Sébastien Pédron  * the always well known and defined argument #2.  And that seems to be
484b0552d5SJean-Sébastien Pédron  * exactly what we need.  Use 1 for true and 0 for false to also allow
494b0552d5SJean-Sébastien Pédron  * #if IS_*() checks pre-compiler checks which do not like #if true.
504b0552d5SJean-Sébastien Pédron  */
514b0552d5SJean-Sébastien Pédron #define ___XAB_1		dontcare,
524b0552d5SJean-Sébastien Pédron #define ___IS_XAB(_ignore, _x, ...)	(_x)
534b0552d5SJean-Sébastien Pédron #define	__IS_XAB(_x)		___IS_XAB(_x 1, 0)
544b0552d5SJean-Sébastien Pédron #define	_IS_XAB(_x)		__IS_XAB(__CONCAT(___XAB_, _x))
554b0552d5SJean-Sébastien Pédron 
564b0552d5SJean-Sébastien Pédron /* This is if CONFIG_ccc=y. */
574b0552d5SJean-Sébastien Pédron #define	IS_BUILTIN(_x)		_IS_XAB(_x)
584b0552d5SJean-Sébastien Pédron /* This is if CONFIG_ccc=m. */
594b0552d5SJean-Sébastien Pédron #define	IS_MODULE(_x)		_IS_XAB(_x ## _MODULE)
604b0552d5SJean-Sébastien Pédron /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */
614b0552d5SJean-Sébastien Pédron #define	IS_ENABLED(_x)		(IS_BUILTIN(_x) || IS_MODULE(_x))
624b0552d5SJean-Sébastien Pédron /*
634b0552d5SJean-Sébastien Pédron  * This is weird case.  If the CONFIG_ccc is builtin (=y) this returns true;
644b0552d5SJean-Sébastien Pédron  * or if the CONFIG_ccc is a module (=m) and the caller is built as a module
654b0552d5SJean-Sébastien Pédron  * (-DMODULE defined) this returns true, but if the callers is not a module
664b0552d5SJean-Sébastien Pédron  * (-DMODULE not defined, which means caller is BUILTIN) then it returns
674b0552d5SJean-Sébastien Pédron  * false.  In other words, a module can reach the kernel, a module can reach
684b0552d5SJean-Sébastien Pédron  * a module, but the kernel cannot reach a module, and code never compiled
694b0552d5SJean-Sébastien Pédron  * cannot be reached either.
704b0552d5SJean-Sébastien Pédron  * XXX -- I'd hope the module-to-module case would be handled by a proper
714b0552d5SJean-Sébastien Pédron  * module dependency definition (MODULE_DEPEND() in FreeBSD).
724b0552d5SJean-Sébastien Pédron  */
734b0552d5SJean-Sébastien Pédron #define	IS_REACHABLE(_x)	(IS_BUILTIN(_x) || \
744b0552d5SJean-Sébastien Pédron 				    (IS_MODULE(_x) && IS_BUILTIN(MODULE)))
754b0552d5SJean-Sébastien Pédron 
764b0552d5SJean-Sébastien Pédron #endif /* _LINUXKPI_LINUX_KCONFIG_H_ */
77