summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-03-19 10:59:06 +0800
committerruki <[email protected]>2023-03-19 10:59:29 +0800
commitb9720e762dd13bae9c9943b3e4a917ef5527a5b6 (patch)
tree338d4973aacbdf29962509b0ae1d2f0fea927524
parent129d4ab78139fd16c12c15b9198fd34e507a2770 (diff)
improve to check cxsnippets #3527
-rw-r--r--tests/apis/check_xxx/xmake.lua7
-rw-r--r--xmake/modules/lib/detect/check_cxsnippets.lua29
2 files changed, 35 insertions, 1 deletions
diff --git a/tests/apis/check_xxx/xmake.lua b/tests/apis/check_xxx/xmake.lua
index d5ed6e2fb..ae13514e5 100644
--- a/tests/apis/check_xxx/xmake.lua
+++ b/tests/apis/check_xxx/xmake.lua
@@ -16,6 +16,13 @@ target("foo")
check_ctypes("HAS_WCHAR", "wchar_t")
check_cincludes("HAS_STRING_H", "string.h")
check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0 : -1;", {tryrun = true})
+ check_csnippets("HAS_INT_4_IN_MAIN", [[
+ int test() {
+ return (sizeof(int) == 4)? 0 : -1;
+ }
+ int main(int argc, char** argv) {
+ return test();
+ }]], {tryrun = true})
check_csnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})
configvar_check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
configvar_check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
diff --git a/xmake/modules/lib/detect/check_cxsnippets.lua b/xmake/modules/lib/detect/check_cxsnippets.lua
index 37ba18ea4..922b7eac6 100644
--- a/xmake/modules/lib/detect/check_cxsnippets.lua
+++ b/xmake/modules/lib/detect/check_cxsnippets.lua
@@ -75,10 +75,20 @@ end
-- make source code
function _sourcecode(snippets, opt)
+ -- has main()?
+ local has_main = false
+ for _, snippet in pairs(snippets) do
+ -- find int main(int argc, char** argv) {}
+ if snippet:find("%s+main%s*%(.-%)") then
+ has_main = true
+ break
+ end
+ end
+
-- add includes
local sourcecode = ""
local includes = table.wrap(opt.includes)
- if opt.tryrun and opt.output then
+ if not has_main and opt.tryrun and opt.output then
table.insert(includes, "stdio.h")
end
for _, include in ipairs(includes) do
@@ -92,6 +102,16 @@ function _sourcecode(snippets, opt)
end
sourcecode = sourcecode .. "\n"
+ -- we just add all snippets if has main function
+ -- @see https://github.com/xmake-io/xmake/issues/3527
+ if has_main then
+ for _, snippet in pairs(snippets) do
+ sourcecode = sourcecode .. "\n" .. snippet
+ end
+ sourcecode = sourcecode .. "\n"
+ return sourcecode
+ end
+
-- add snippets (build only)
if not opt.tryrun then
for _, snippet in pairs(snippets) do
@@ -146,6 +166,13 @@ end
-- local ok, output_or_errors = check_cxsnippets("void test() {}")
-- local ok, output_or_errors = check_cxsnippets({"void test(){}", "#define TEST 1"}, {types = "wchar_t", includes = "stdio.h"})
-- local ok, output_or_errors = check_cxsnippets({snippet_name = "void test(){}", "#define TEST 1"}, {types = "wchar_t", includes = "stdio.h"})
+-- local ok, output_or_errors = check_cxsnippets([[
+-- int test() {
+-- return (sizeof(int) == 4)? 0 : -1;
+-- }
+-- int main(int argc, char** argv) {
+-- return test();
+-- }]], {tryrun = true})
-- @endcode
--
function main(snippets, opt)