summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-30 00:25:24 +0800
committerruki <[email protected]>2021-09-30 00:25:24 +0800
commitb489ff22c57c843e6894b5280fa8930231f86249 (patch)
tree7623d688b75319a029c60d6f99d429fb5f4f4204
parent5d51a5ea5a32bdc17a6830605fa8904279102629 (diff)
add check_macros
-rw-r--r--tests/apis/check_xxx/config.h.in3
-rw-r--r--tests/apis/check_xxx/xmake.lua4
-rw-r--r--xmake/includes/check_macros.lua128
3 files changed, 135 insertions, 0 deletions
diff --git a/tests/apis/check_xxx/config.h.in b/tests/apis/check_xxx/config.h.in
index e5c1bc6a1..9dedbaf96 100644
--- a/tests/apis/check_xxx/config.h.in
+++ b/tests/apis/check_xxx/config.h.in
@@ -17,6 +17,9 @@ ${define HAS_C_STD_89}
${define HAS_C_STD_99}
${define HAS_C_STD_11}
${define HAS_C_STD_17}
+${define HAS_GCC}
+${define HAS_CXX20}
+${define NO_GCC}
${define PTR_SIZE}
${define HAVE_VISIBILITY}
${define CUSTOM_ASSERT}
diff --git a/tests/apis/check_xxx/xmake.lua b/tests/apis/check_xxx/xmake.lua
index e67fdd27f..810a67612 100644
--- a/tests/apis/check_xxx/xmake.lua
+++ b/tests/apis/check_xxx/xmake.lua
@@ -2,6 +2,7 @@ includes("check_links.lua")
includes("check_ctypes.lua")
includes("check_cflags.lua")
includes("check_cfuncs.lua")
+includes("check_macros.lua")
includes("check_features.lua")
includes("check_csnippets.lua")
includes("check_cincludes.lua")
@@ -37,3 +38,6 @@ target("test")
configvar_check_csnippets("PTR_SIZE", 'printf("%d", sizeof(void*)); return 0;', {output = true, number = true})
configvar_check_csnippets("HAVE_VISIBILITY", 'extern __attribute__((__visibility__("hidden"))) int hiddenvar;', {default = 0})
configvar_check_csnippets("CUSTOM_ASSERT=assert", 'assert(1);', {default = "", quote = false})
+ configvar_check_macros("HAS_GCC", "__GNUC__")
+ configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})
+ configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
diff --git a/xmake/includes/check_macros.lua b/xmake/includes/check_macros.lua
new file mode 100644
index 000000000..ec4beddce
--- /dev/null
+++ b/xmake/includes/check_macros.lua
@@ -0,0 +1,128 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file check_macros.lua
+--
+
+-- check macros and add macro definition
+--
+-- e.g.
+--
+-- check_macros("HAS_GCC", "__GNUC__")
+-- check_macros("NO_GCC", "__GNUC__", {defined = false})
+-- check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
+--
+function check_macros(definition, macros, opt)
+ opt = opt or {}
+ local optname = "__" .. (opt.name or definition)
+ local snippets = {}
+ option(optname)
+ for _, macro in ipairs(macros) do
+ if macro:find(' ', 1, true) then
+ table.insert(snippets, ([[
+ #if %s
+ #else
+ # #error %s is not satisfied!
+ #endif
+ ]]):format(macro, macro))
+ else
+ table.insert(snippets, ([[
+ #if%s %s
+ #else
+ # #error %s is not defined!
+ #endif
+ ]]):format(opt.defined ~= false and "def" or "ndef", macro, macro))
+ end
+ end
+ if opt.languages and opt.languages:startswith("c++") then
+ add_cxxsnippets(definition, table.concat(snippets, "\n"))
+ else
+ add_csnippets(definition, table.concat(snippets, "\n"))
+ end
+ add_defines(definition)
+ if opt.languages then
+ set_languages(opt.languages)
+ end
+ if opt.cflags then
+ add_cflags(opt.cflags)
+ end
+ if opt.cxflags then
+ add_cxflags(opt.cxflags)
+ end
+ if opt.cxxflags then
+ add_cxxflags(opt.cxxflags)
+ end
+ option_end()
+ add_options(optname)
+end
+
+-- check macros and add macro definition to the configuration files
+--
+-- e.g.
+-- configvar_check_macros("HAS_GCC", "__GNUC__")
+-- configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})
+-- configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
+function configvar_check_macros(definition, macros, opt)
+ opt = opt or {}
+ local optname = "__" .. (opt.name or definition)
+ local defname, defval = unpack(definition:split('='))
+ local snippets = {}
+ option(optname)
+ for _, macro in ipairs(macros) do
+ if macro:find(' ', 1, true) then
+ table.insert(snippets, ([[
+ #if %s
+ #else
+ # #error %s is not satisfied!
+ #endif
+ ]]):format(macro, macro))
+ else
+ table.insert(snippets, ([[
+ #if%s %s
+ #else
+ # #error %s is not defined!
+ #endif
+ ]]):format(opt.defined ~= false and "def" or "ndef", macro, macro))
+ end
+ end
+ if opt.languages and opt.languages:startswith("c++") then
+ add_cxxsnippets(definition, table.concat(snippets, "\n"))
+ else
+ add_csnippets(definition, table.concat(snippets, "\n"))
+ end
+ if opt.default == nil then
+ set_configvar(defname, defval or 1, {quote = opt.quote})
+ end
+ if opt.languages then
+ set_languages(opt.languages)
+ end
+ if opt.cflags then
+ add_cflags(opt.cflags)
+ end
+ if opt.cxflags then
+ add_cxflags(opt.cxflags)
+ end
+ if opt.cxxflags then
+ add_cxxflags(opt.cxxflags)
+ end
+ option_end()
+ if opt.default == nil then
+ add_options(optname)
+ else
+ set_configvar(defname, has_config(optname) and (defval or 1) or opt.default, {quote = opt.quote})
+ end
+end