summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-29 11:22:53 +0800
committerGitHub <[email protected]>2025-12-29 11:22:53 +0800
commit7c06c581c3f99a3eda87d88a334e42802175ddfd (patch)
treebb6e90071765465d8fb5e6e87fe2a3d94158f0bf
parent399988ff7a79d7d9ee994c8911bf6e69588dd7c5 (diff)
parent34c820992ce4dd7a11ed644f7ab253a98466c54d (diff)
Merge pull request #7172 from xmake-io/modules
prepare test targets in compile_commands
-rw-r--r--tests/projects/c++/modules/test_base.lua6
-rw-r--r--tests/projects/c++/modules/test_xmake_test_stdmodules.lua42
-rw-r--r--tests/projects/c++/modules/xmake_tests3/src/foo.cppm7
-rw-r--r--tests/projects/c++/modules/xmake_tests3/src/main.cpp8
-rw-r--r--tests/projects/c++/modules/xmake_tests3/test.lua1
-rw-r--r--tests/projects/c++/modules/xmake_tests3/xmake.lua12
-rw-r--r--tests/projects/c++/modules/xmake_tests4/.gitignore4
-rw-r--r--tests/projects/c++/modules/xmake_tests4/src/main.cpp5
-rw-r--r--tests/projects/c++/modules/xmake_tests4/src/work.mpp14
-rw-r--r--tests/projects/c++/modules/xmake_tests4/test.lua1
-rw-r--r--tests/projects/c++/modules/xmake_tests4/test/test.cpp5
-rw-r--r--tests/projects/c++/modules/xmake_tests4/xmake.lua7
-rw-r--r--xmake/actions/test/main.lua2
-rw-r--r--xmake/core/sandbox/modules/import/core/project/project.lua37
-rw-r--r--xmake/modules/private/utils/target.lua39
-rw-r--r--xmake/plugins/project/clang/compile_commands.lua33
-rw-r--r--xmake/plugins/project/utils/target_cmds.lua4
17 files changed, 182 insertions, 45 deletions
diff --git a/tests/projects/c++/modules/test_base.lua b/tests/projects/c++/modules/test_base.lua
index 6f2a42088..6116b36e6 100644
--- a/tests/projects/c++/modules/test_base.lua
+++ b/tests/projects/c++/modules/test_base.lua
@@ -136,12 +136,12 @@ function run_tests(clang_options, gcc_options, msvc_options)
build_tests("clang-cl", clang_cl_options)
build_tests("clang-cl", table.join(clang_options, {two_phases = false}))
end
- if not clang_options.stdmodule then
+ if clang_options.stdmodule then
+ wprint("std modules tests skipped for Windows clang libc++ as it's not currently supported officially")
+ else
build_tests("llvm", clang_libcpp_options)
build_tests("clang", clang_libcpp_options)
build_tests("clang", table.join(clang_libcpp_options, {two_phases = false}))
- else
- wprint("std modules tests skipped for Windows clang libc++ as it's not currently supported officially")
end
end
if msvc_options then
diff --git a/tests/projects/c++/modules/test_xmake_test_stdmodules.lua b/tests/projects/c++/modules/test_xmake_test_stdmodules.lua
new file mode 100644
index 000000000..0e95de5bd
--- /dev/null
+++ b/tests/projects/c++/modules/test_xmake_test_stdmodules.lua
@@ -0,0 +1,42 @@
+inherit("test_base")
+import("utils.ci.is_running", {alias = "ci_is_running"})
+
+CLANG_MIN_VER = "19"
+GCC_MIN_VER = "15"
+MSVC_MIN_VER = "14.35"
+
+function run_xmake_test(...)
+ local flags = ""
+ if ci_is_running() then
+ flags = "-vD"
+ end
+ local outdata, errdata = os.iorun("xmake test " .. flags)
+ assert(outdata, errdata)
+end
+
+function main(_)
+ local clang_options = {stdmodule = true, compiler = "clang", version = CLANG_MIN_VER, after_build = run_xmake_test}
+ local gcc_options = {stdmodule = true, compiler = "gcc", version = GCC_MIN_VER, after_build = run_xmake_test}
+ -- latest mingw gcc 15.1 is broken
+ -- error: F:/msys64/mingw64/include/c++/15.1.0/shared_mutex:105:3: error: 'int std::__glibcxx_rwlock_timedrdlock(pthread_rwlock_t*, const timespec*)' exposes TU-local entity 'int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const timespec*)'
+ -- 105 | __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
+ -- | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ -- In file included from F:/msys64/mingw64/include/c++/15.1.0/x86_64-w64-mingw32/bits/gthr-default.h:35,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/x86_64-w64-mingw32/bits/gthr.h:157,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/ext/atomicity.h:37,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/bits/ios_base.h:41,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/streambuf:45,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/bits/streambuf_iterator.h:37,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/iterator:68,
+ -- from F:/msys64/mingw64/include/c++/15.1.0/x86_64-w64-mingw32/bits/stdc++.h:56:
+ -- F:/msys64/mingw64/include/pthread.h:296:28: note: 'int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const timespec*)' declared with internal linkage
+ -- 296 | WINPTHREAD_RWLOCK_DECL int pthread_rwlock_timedrdlock(pthread_rwlock_t *l, const struct timespec *ts)
+ -- | ^~~~~~~~~~~~~~~~~~~~~~~~~~
+ -- F:/msys64/mingw64/include/c++/15.1.0/shared_mutex:115:3: error: 'int std::__glibcxx_rwlock_timedwrlock(pthread_rwlock_t*, const timespec*)' exposes TU-local entity 'int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const timespec*)'
+ -- 115 | __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock, local gcc_options = {stdmodule = true, compiler = "gcc", version = GCC_MIN_VER}
+ if is_subhost("msys") then
+ gcc_options = nil
+ end
+ local msvc_options = {stdmodule = true, version = MSVC_MIN_VER}
+ run_tests(clang_options, gcc_options, msvc_options)
+end
diff --git a/tests/projects/c++/modules/xmake_tests3/src/foo.cppm b/tests/projects/c++/modules/xmake_tests3/src/foo.cppm
new file mode 100644
index 000000000..dee531b45
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests3/src/foo.cppm
@@ -0,0 +1,7 @@
+export module Foo;
+
+import std;
+
+export void foo() {
+ std::printf("Hello from Foo\n");
+}
diff --git a/tests/projects/c++/modules/xmake_tests3/src/main.cpp b/tests/projects/c++/modules/xmake_tests3/src/main.cpp
new file mode 100644
index 000000000..4e2bb9b57
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests3/src/main.cpp
@@ -0,0 +1,8 @@
+import std;
+import Foo;
+
+int main() {
+ foo();
+ std::printf("Hello from main\n");
+ return 0;
+}
diff --git a/tests/projects/c++/modules/xmake_tests3/test.lua b/tests/projects/c++/modules/xmake_tests3/test.lua
new file mode 100644
index 000000000..e36d560d9
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests3/test.lua
@@ -0,0 +1 @@
+inherit(".test_xmake_test_stdmodules")
diff --git a/tests/projects/c++/modules/xmake_tests3/xmake.lua b/tests/projects/c++/modules/xmake_tests3/xmake.lua
new file mode 100644
index 000000000..67021fcd9
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests3/xmake.lua
@@ -0,0 +1,12 @@
+add_rules("mode.debug", "mode.release")
+set_languages("c++23")
+
+target("module_dep")
+ set_kind("moduleonly")
+ add_files("src/*.cppm")
+
+target("module_target1")
+ set_kind("moduleonly")
+ add_files("src/*.cppm")
+ add_deps("module_dep")
+ add_tests("tests", {kind = "binary", files = "src/main.cpp", build_should_pass = true, run_should_pass = true})
diff --git a/tests/projects/c++/modules/xmake_tests4/.gitignore b/tests/projects/c++/modules/xmake_tests4/.gitignore
new file mode 100644
index 000000000..9d988d246
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/.gitignore
@@ -0,0 +1,4 @@
+.xmake/
+.vscode/
+build/
+uselocalxmake.sh
diff --git a/tests/projects/c++/modules/xmake_tests4/src/main.cpp b/tests/projects/c++/modules/xmake_tests4/src/main.cpp
new file mode 100644
index 000000000..45222c241
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/src/main.cpp
@@ -0,0 +1,5 @@
+import work;
+
+int main() {
+ do_work<int>();
+}
diff --git a/tests/projects/c++/modules/xmake_tests4/src/work.mpp b/tests/projects/c++/modules/xmake_tests4/src/work.mpp
new file mode 100644
index 000000000..27b5f2089
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/src/work.mpp
@@ -0,0 +1,14 @@
+export module work;
+import std;
+
+export
+template<typename T>
+void do_work() {
+ if constexpr (std::is_same_v<T, int>) {
+ std::println("Hello world!");
+ } else if constexpr (std::is_same_v<T, double>) {
+ std::println("Hello test!");
+ } else {
+ std::println("Hello unknown type!");
+ }
+}
diff --git a/tests/projects/c++/modules/xmake_tests4/test.lua b/tests/projects/c++/modules/xmake_tests4/test.lua
new file mode 100644
index 000000000..e36d560d9
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/test.lua
@@ -0,0 +1 @@
+inherit(".test_xmake_test_stdmodules")
diff --git a/tests/projects/c++/modules/xmake_tests4/test/test.cpp b/tests/projects/c++/modules/xmake_tests4/test/test.cpp
new file mode 100644
index 000000000..fae906e76
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/test/test.cpp
@@ -0,0 +1,5 @@
+import work;
+
+int main() {
+ do_work<double>();
+}
diff --git a/tests/projects/c++/modules/xmake_tests4/xmake.lua b/tests/projects/c++/modules/xmake_tests4/xmake.lua
new file mode 100644
index 000000000..43a330fe1
--- /dev/null
+++ b/tests/projects/c++/modules/xmake_tests4/xmake.lua
@@ -0,0 +1,7 @@
+add_rules("mode.debug", "mode.release")
+set_languages("c++23")
+
+target("llvm")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+ add_tests("test", { files = "test/test.cpp", remove_files = "src/main.cpp" })
diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua
index 0e7192d83..85866814a 100644
--- a/xmake/actions/test/main.lua
+++ b/xmake/actions/test/main.lua
@@ -31,6 +31,7 @@ import("private.action.run.runenvs")
import("private.service.remote_build.action", {alias = "remote_build_action"})
import("actions.build.main", {rootdir = os.programdir(), alias = "build_action"})
import("utils.progress")
+import("private.utils.target", {alias = "target_utils"})
-- test target
function _do_test_target(target, opt)
@@ -522,6 +523,7 @@ function get_tests()
if extra.packages then
target_new:add("packages", extra.packages)
end
+ target_utils.config_target(target_new)
testinfo.target = target_new
end
end
diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua
index 8a808e7da..9b69349d0 100644
--- a/xmake/core/sandbox/modules/import/core/project/project.lua
+++ b/xmake/core/sandbox/modules/import/core/project/project.lua
@@ -127,41 +127,10 @@ function sandbox_core_project.check_options()
end
end
--- config target
-function sandbox_core_project._config_target(target, opt)
- for _, rule in ipairs(table.wrap(target:orderules())) do
- local before_config = rule:script("config_before")
- if before_config then
- before_config(target, opt)
- end
- end
-
- for _, rule in ipairs(table.wrap(target:orderules())) do
- local on_config = rule:script("config")
- if on_config then
- on_config(target, opt)
- end
- end
- local on_config = target:script("config")
- if on_config then
- on_config(target, opt)
- end
-
- for _, rule in ipairs(table.wrap(target:orderules())) do
- local after_config = rule:script("config_after")
- if after_config then
- after_config(target, opt)
- end
- end
-end
-
+-- config targets
function sandbox_core_project._config_targets(opt)
- opt = opt or {}
- for _, target in ipairs(table.wrap(project.ordertargets())) do
- if target:is_enabled() then
- sandbox_core_project._config_target(target, opt)
- end
- end
+ import("private.utils.target", {alias = "target_utils"})
+ target_utils.config_targets(opt)
end
-- config targets
diff --git a/xmake/modules/private/utils/target.lua b/xmake/modules/private/utils/target.lua
index d06f1293c..728bd6016 100644
--- a/xmake/modules/private/utils/target.lua
+++ b/xmake/modules/private/utils/target.lua
@@ -50,6 +50,7 @@ end
-- @see https://github.com/xmake-io/xmake/issues/3022
--
-- e.g.
+--
-- for all: add_cxxflags("-g")
-- only for clang: add_cxxflags("clang::-stdlib=libc++")
-- only for clang and multiple flags: add_cxxflags("-stdlib=libc++", "-DFOO", {tools = "clang"})
@@ -104,6 +105,7 @@ function translate_flags_in_tool(target, flagkind, flags)
-- @see https://github.com/xmake-io/xmake/issues/3022
--
-- e.g.
+ --
-- for all: add_cxxflags("-g")
-- only for clang: add_cxxflags("clang::-stdlib=libc++")
-- only for clang and multiple flags: add_cxxflags("-stdlib=libc++", "-DFOO", {tools = "clang"})
@@ -165,3 +167,40 @@ function check_target_toolchains()
end
end
+-- config target
+function config_target(target, opt)
+ for _, rule in ipairs(table.wrap(target:orderules())) do
+ local before_config = rule:script("config_before")
+ if before_config then
+ before_config(target, opt)
+ end
+ end
+
+ for _, rule in ipairs(table.wrap(target:orderules())) do
+ local on_config = rule:script("config")
+ if on_config then
+ on_config(target, opt)
+ end
+ end
+ local on_config = target:script("config")
+ if on_config then
+ on_config(target, opt)
+ end
+
+ for _, rule in ipairs(table.wrap(target:orderules())) do
+ local after_config = rule:script("config_after")
+ if after_config then
+ after_config(target, opt)
+ end
+ end
+end
+
+-- config targets
+function config_targets(opt)
+ opt = opt or {}
+ for _, target in ipairs(table.wrap(project.ordertargets())) do
+ if target:is_enabled() then
+ config_target(target, opt)
+ end
+ end
+end
diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua
index a017b4a32..4f01354fa 100644
--- a/xmake/plugins/project/clang/compile_commands.lua
+++ b/xmake/plugins/project/clang/compile_commands.lua
@@ -276,6 +276,23 @@ function _add_target(jsonfile, target)
os.setenvs(oldenvs)
end
+-- get test targets
+-- https://github.com/xmake-io/xmake/issues/4750
+function _get_test_targets()
+ local test_targets = _g.test_targets
+ if test_targets == nil then
+ test_targets = {}
+ for _, test in pairs(test_action.get_tests()) do
+ local target = test.target
+ if not target:is_phony() then
+ table.insert(test_targets, target)
+ end
+ end
+ _g.test_targets = test_targets
+ end
+ return test_targets
+end
+
-- add targets
function _add_targets(jsonfile)
jsonfile:print("[")
@@ -288,15 +305,19 @@ function _add_targets(jsonfile)
end
end
-- https://github.com/xmake-io/xmake/issues/4750
- for _, test in pairs(test_action.get_tests()) do
- local target = test.target
- if not target:is_phony() then
- _add_target(jsonfile, target)
- end
+ for _, target in ipairs(_get_test_targets()) do
+ _add_target(jsonfile, target)
end
jsonfile:print("]")
end
+-- prepare targets
+function _prepare_targets()
+ target_cmds.prepare_targets()
+ -- https://github.com/xmake-io/xmake/issues/7166
+ target_cmds.prepare_targets(_get_test_targets())
+end
+
-- generate compilation databases for clang-based tools(compile_commands.json)
--
-- references:
@@ -308,7 +329,7 @@ function make(outputdir)
local oldir = os.cd(os.projectdir())
local jsonfile = io.open(path.join(outputdir, "compile_commands.json"), "w")
os.setenv("XMAKE_IN_COMPILE_COMMANDS_PROJECT_GENERATOR", "true")
- target_cmds.prepare_targets()
+ _prepare_targets()
_add_targets(jsonfile)
jsonfile:close()
os.setenv("XMAKE_IN_COMPILE_COMMANDS_PROJECT_GENERATOR", nil)
diff --git a/xmake/plugins/project/utils/target_cmds.lua b/xmake/plugins/project/utils/target_cmds.lua
index 25413912a..7ea3a13a0 100644
--- a/xmake/plugins/project/utils/target_cmds.lua
+++ b/xmake/plugins/project/utils/target_cmds.lua
@@ -27,8 +27,8 @@ import("private.utils.batchcmds")
import("private.action.build.target", {alias = "target_buildutils"})
-- prepare targets
-function prepare_targets()
- local targets_root = target_buildutils.get_root_targets()
+function prepare_targets(targets)
+ local targets_root = targets or target_buildutils.get_root_targets()
target_buildutils.run_targetjobs(targets_root, {job_kind = "prepare", for_generator = true, jobs = os.default_njob()})
end