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