diff options
| author | ruki <[email protected]> | 2025-05-14 14:44:51 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-14 14:44:51 +0800 |
| commit | a68eff4a47e81b8ea430ee4f4a86d1fd8ccc7df9 (patch) | |
| tree | c0dd102305af8f462c5fcf22af5fa5fe6a937b32 | |
| parent | 29ee4b47144829d80fbb7c3fe429696fba94394c (diff) | |
| parent | bb8609c50ec1d82c50af24455ab904c4f0b7cd27 (diff) | |
Merge pull request #6435 from Arthapz/fix-namespaced-module-dep
(C++ modules support) fix _patch_sourcebatch when target and deps is in the same namespace
| -rw-r--r-- | tests/projects/c++/modules/namespace/src/hello.mpp | 14 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/src/hello_impl.cpp | 23 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/src/main.cpp | 9 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/src/mod.mpp | 5 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/src/mod_impl.cpp | 8 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/test.lua | 1 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace/xmake.lua | 15 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/src/hello.mpp | 14 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/src/hello_impl.cpp | 23 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/src/main.cpp | 9 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/src/mod.mpp | 5 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/src/mod_impl.cpp | 8 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/test.lua | 1 | ||||
| -rw-r--r-- | tests/projects/c++/modules/namespace2/xmake.lua | 17 | ||||
| -rw-r--r-- | xmake/rules/c++/modules/scanner.lua | 7 |
15 files changed, 158 insertions, 1 deletions
diff --git a/tests/projects/c++/modules/namespace/src/hello.mpp b/tests/projects/c++/modules/namespace/src/hello.mpp new file mode 100644 index 000000000..abac9fcb0 --- /dev/null +++ b/tests/projects/c++/modules/namespace/src/hello.mpp @@ -0,0 +1,14 @@ +export module hello; + +export namespace hello { + extern int data__; + void say_hello(); + + class say { + public: + say(int data); + void hello(); + private: + int data_; + }; +} diff --git a/tests/projects/c++/modules/namespace/src/hello_impl.cpp b/tests/projects/c++/modules/namespace/src/hello_impl.cpp new file mode 100644 index 000000000..064afebad --- /dev/null +++ b/tests/projects/c++/modules/namespace/src/hello_impl.cpp @@ -0,0 +1,23 @@ +module; +#include <iostream> + +module hello; +import mod; + +void inner() { + std::cout << "hello world! data: " + << mod::foo() << std::endl; +} + +namespace hello { + int data__; + void say_hello() { ::inner(); } + + say::say(int data) : data_{data} { + } + + void say::hello() { + hello::data__ = data_; + ::inner(); + } +} diff --git a/tests/projects/c++/modules/namespace/src/main.cpp b/tests/projects/c++/modules/namespace/src/main.cpp new file mode 100644 index 000000000..93cc427a4 --- /dev/null +++ b/tests/projects/c++/modules/namespace/src/main.cpp @@ -0,0 +1,9 @@ +import hello; + +int main() { + hello::data__ = 123; + hello::say_hello(); + hello::say(sizeof(hello::say)).hello(); + return 0; +} + diff --git a/tests/projects/c++/modules/namespace/src/mod.mpp b/tests/projects/c++/modules/namespace/src/mod.mpp new file mode 100644 index 000000000..ac30a31c8 --- /dev/null +++ b/tests/projects/c++/modules/namespace/src/mod.mpp @@ -0,0 +1,5 @@ +export module mod; + +export namespace mod { + int foo(); +} diff --git a/tests/projects/c++/modules/namespace/src/mod_impl.cpp b/tests/projects/c++/modules/namespace/src/mod_impl.cpp new file mode 100644 index 000000000..1adf6570e --- /dev/null +++ b/tests/projects/c++/modules/namespace/src/mod_impl.cpp @@ -0,0 +1,8 @@ +module mod; +import hello; + +namespace mod { + int foo() { + return hello::data__; + } +} diff --git a/tests/projects/c++/modules/namespace/test.lua b/tests/projects/c++/modules/namespace/test.lua new file mode 100644 index 000000000..7717f8049 --- /dev/null +++ b/tests/projects/c++/modules/namespace/test.lua @@ -0,0 +1 @@ +inherit(".test_base") diff --git a/tests/projects/c++/modules/namespace/xmake.lua b/tests/projects/c++/modules/namespace/xmake.lua new file mode 100644 index 000000000..aefa1ba09 --- /dev/null +++ b/tests/projects/c++/modules/namespace/xmake.lua @@ -0,0 +1,15 @@ +add_rules("mode.release", "mode.debug") +set_languages("c++20") + +namespace("foo", function() + target("dep") + set_kind("static") + add_files("src/hello.mpp", "src/mod.mpp", {public = true}) + add_files("src/hello_impl.cpp", "src/mod_impl.cpp") + + target("binary") + set_kind("binary") + add_files("src/main.cpp") + add_deps("dep") +end) + diff --git a/tests/projects/c++/modules/namespace2/src/hello.mpp b/tests/projects/c++/modules/namespace2/src/hello.mpp new file mode 100644 index 000000000..abac9fcb0 --- /dev/null +++ b/tests/projects/c++/modules/namespace2/src/hello.mpp @@ -0,0 +1,14 @@ +export module hello; + +export namespace hello { + extern int data__; + void say_hello(); + + class say { + public: + say(int data); + void hello(); + private: + int data_; + }; +} diff --git a/tests/projects/c++/modules/namespace2/src/hello_impl.cpp b/tests/projects/c++/modules/namespace2/src/hello_impl.cpp new file mode 100644 index 000000000..064afebad --- /dev/null +++ b/tests/projects/c++/modules/namespace2/src/hello_impl.cpp @@ -0,0 +1,23 @@ +module; +#include <iostream> + +module hello; +import mod; + +void inner() { + std::cout << "hello world! data: " + << mod::foo() << std::endl; +} + +namespace hello { + int data__; + void say_hello() { ::inner(); } + + say::say(int data) : data_{data} { + } + + void say::hello() { + hello::data__ = data_; + ::inner(); + } +} diff --git a/tests/projects/c++/modules/namespace2/src/main.cpp b/tests/projects/c++/modules/namespace2/src/main.cpp new file mode 100644 index 000000000..93cc427a4 --- /dev/null +++ b/tests/projects/c++/modules/namespace2/src/main.cpp @@ -0,0 +1,9 @@ +import hello; + +int main() { + hello::data__ = 123; + hello::say_hello(); + hello::say(sizeof(hello::say)).hello(); + return 0; +} + diff --git a/tests/projects/c++/modules/namespace2/src/mod.mpp b/tests/projects/c++/modules/namespace2/src/mod.mpp new file mode 100644 index 000000000..ac30a31c8 --- /dev/null +++ b/tests/projects/c++/modules/namespace2/src/mod.mpp @@ -0,0 +1,5 @@ +export module mod; + +export namespace mod { + int foo(); +} diff --git a/tests/projects/c++/modules/namespace2/src/mod_impl.cpp b/tests/projects/c++/modules/namespace2/src/mod_impl.cpp new file mode 100644 index 000000000..1adf6570e --- /dev/null +++ b/tests/projects/c++/modules/namespace2/src/mod_impl.cpp @@ -0,0 +1,8 @@ +module mod; +import hello; + +namespace mod { + int foo() { + return hello::data__; + } +} diff --git a/tests/projects/c++/modules/namespace2/test.lua b/tests/projects/c++/modules/namespace2/test.lua new file mode 100644 index 000000000..7717f8049 --- /dev/null +++ b/tests/projects/c++/modules/namespace2/test.lua @@ -0,0 +1 @@ +inherit(".test_base") diff --git a/tests/projects/c++/modules/namespace2/xmake.lua b/tests/projects/c++/modules/namespace2/xmake.lua new file mode 100644 index 000000000..4f124c5ac --- /dev/null +++ b/tests/projects/c++/modules/namespace2/xmake.lua @@ -0,0 +1,17 @@ +add_rules("mode.release", "mode.debug") +set_languages("c++20") + +namespace("bar", function() + target("dep") + set_kind("static") + add_files("src/hello.mpp", "src/mod.mpp", {public = true}) + add_files("src/hello_impl.cpp", "src/mod_impl.cpp") +end) + +namespace("foo", function() + target("binary") + set_kind("binary") + add_files("src/main.cpp") + add_deps("bar::dep") +end) + diff --git a/xmake/rules/c++/modules/scanner.lua b/xmake/rules/c++/modules/scanner.lua index 263b5725a..fe9b9c63d 100644 --- a/xmake/rules/c++/modules/scanner.lua +++ b/xmake/rules/c++/modules/scanner.lua @@ -296,7 +296,11 @@ function _get_targetdeps_modules(target) fileconfig.undefines = table.join(fileconfig.undefines or {}, dep:get("undefines") or {}) fileconfig.includedirs = table.join(fileconfig.includedirs or {}, dep:get("includedirs") or {}) if not dep:is_phony() then - fileconfig.external = dep:fullname() + if target:namespace() == dep:namespace() then + fileconfig.external = dep:name() + else + fileconfig.external = dep:fullname() + end fileconfig.bmionly = not dep:is_moduleonly() end if not modules[sourcefile] then @@ -358,6 +362,7 @@ function _patch_sourcebatch(target, sourcebatch) local strict = target:policy("build.c++.modules.reuse.strict") or target:policy("build.c++.modules.tryreuse.discriminate_on_defines") local dep = target:dep(fileconfig.external) + assert(dep, "dep target <%s> for <%s>", fileconfig.external, target:fullname()) local can_reuse = nocheck or _are_flags_compatible(target, dep, sourcefile, {strict = strict}) if can_reuse then |
