summaryrefslogtreecommitdiff
path: root/xmake/modules/lib/detect/check_cxsnippets.lua
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 /xmake/modules/lib/detect/check_cxsnippets.lua
parent129d4ab78139fd16c12c15b9198fd34e507a2770 (diff)
improve to check cxsnippets #3527
Diffstat (limited to 'xmake/modules/lib/detect/check_cxsnippets.lua')
-rw-r--r--xmake/modules/lib/detect/check_cxsnippets.lua29
1 files changed, 28 insertions, 1 deletions
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)