From e81a234fdcb3542403e03e9db0dc2c182eb81e36 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 21 Dec 2024 22:38:14 +0800 Subject: add namespace basic test --- tests/apis/namespace/basic/.gitignore | 8 ++++++++ tests/apis/namespace/basic/src/bar.cpp | 5 +++++ tests/apis/namespace/basic/src/bar.h | 9 +++++++++ tests/apis/namespace/basic/src/foo.cpp | 5 +++++ tests/apis/namespace/basic/src/foo.h | 9 +++++++++ tests/apis/namespace/basic/src/main.cpp | 9 +++++++++ tests/apis/namespace/basic/xmake.lua | 19 +++++++++++++++++++ 7 files changed, 64 insertions(+) create mode 100644 tests/apis/namespace/basic/.gitignore create mode 100644 tests/apis/namespace/basic/src/bar.cpp create mode 100644 tests/apis/namespace/basic/src/bar.h create mode 100644 tests/apis/namespace/basic/src/foo.cpp create mode 100644 tests/apis/namespace/basic/src/foo.h create mode 100644 tests/apis/namespace/basic/src/main.cpp create mode 100644 tests/apis/namespace/basic/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/basic/.gitignore b/tests/apis/namespace/basic/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/basic/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/basic/src/bar.cpp b/tests/apis/namespace/basic/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/basic/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/basic/src/bar.h b/tests/apis/namespace/basic/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/basic/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/basic/src/foo.cpp b/tests/apis/namespace/basic/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/basic/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/basic/src/foo.h b/tests/apis/namespace/basic/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/basic/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/basic/src/main.cpp b/tests/apis/namespace/basic/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/basic/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/basic/xmake.lua b/tests/apis/namespace/basic/xmake.lua new file mode 100644 index 000000000..fd52ab013 --- /dev/null +++ b/tests/apis/namespace/basic/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.debug", "mode.release") + +namespace("ns1", function () + target("foo") + set_kind("static") + add_files("src/foo.cpp") +end) + +namespace("ns2") + target("bar") + set_kind("static") + add_files("src/bar.cpp") +namespace_end() + +target("test") + set_kind("binary") + add_deps("ns1::foo", "ns2::bar") + add_files("src/main.cpp") + -- cgit v1.3.1 From 32db8fc2f0f2c560ab803acd4c4394466843c0bd Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 22 Dec 2024 22:57:52 +0800 Subject: add nested namespace tests --- tests/apis/namespace/nested/.gitignore | 8 ++++++++ tests/apis/namespace/nested/src/bar.cpp | 5 +++++ tests/apis/namespace/nested/src/bar.h | 9 +++++++++ tests/apis/namespace/nested/src/foo.cpp | 5 +++++ tests/apis/namespace/nested/src/foo.h | 9 +++++++++ tests/apis/namespace/nested/src/main.cpp | 9 +++++++++ tests/apis/namespace/nested/xmake.lua | 19 +++++++++++++++++++ xmake/core/base/interpreter.lua | 18 ++++++++++++++++-- 8 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tests/apis/namespace/nested/.gitignore create mode 100644 tests/apis/namespace/nested/src/bar.cpp create mode 100644 tests/apis/namespace/nested/src/bar.h create mode 100644 tests/apis/namespace/nested/src/foo.cpp create mode 100644 tests/apis/namespace/nested/src/foo.h create mode 100644 tests/apis/namespace/nested/src/main.cpp create mode 100644 tests/apis/namespace/nested/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/nested/.gitignore b/tests/apis/namespace/nested/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/nested/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/nested/src/bar.cpp b/tests/apis/namespace/nested/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/nested/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/nested/src/bar.h b/tests/apis/namespace/nested/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/nested/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/nested/src/foo.cpp b/tests/apis/namespace/nested/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/nested/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/nested/src/foo.h b/tests/apis/namespace/nested/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/nested/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/nested/src/main.cpp b/tests/apis/namespace/nested/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/nested/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/nested/xmake.lua b/tests/apis/namespace/nested/xmake.lua new file mode 100644 index 000000000..93d7132f3 --- /dev/null +++ b/tests/apis/namespace/nested/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.debug", "mode.release") + +namespace("ns1", function () + target("foo") + set_kind("static") + add_files("src/foo.cpp") + + namespace("ns2") + target("bar") + set_kind("static") + add_files("src/bar.cpp") + namespace_end() +end) + +target("test") + set_kind("binary") + add_deps("ns1::foo", "ns1::ns2::bar") + add_files("src/main.cpp") + diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 95d256851..716ce3004 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1847,11 +1847,25 @@ function interpreter:api_builtin_add_subfiles(...) end -- the builtin api: namespace() -function interpreter:api_builtin_namespace(...) +function interpreter:api_builtin_namespace(name, callback) + local namespace = self._NAMESPACE + if namespace == nil then + namespace = {} + self._NAMESPACE = namespace + end + table.insert(namespace, name) + if callback and type(callback) == "function" then + callback() + self:api_builtin_namespace_end() + end end -- the builtin api: namespace_end() -function interpreter:api_builtin_namespace_end(...) +function interpreter:api_builtin_namespace_end() + local namespace = self._NAMESPACE + if namespace then + table.remove(namespace) + end end -- the interpreter api: interp_save_scope() -- cgit v1.3.1 From 4b35267f3de12f93234467e84b08f5c8b7aef281 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 22 Dec 2024 23:11:25 +0800 Subject: add test.lua --- tests/apis/namespace/basic/test.lua | 3 +++ tests/apis/namespace/nested/test.lua | 3 +++ xmake/core/base/interpreter.lua | 1 + 3 files changed, 7 insertions(+) create mode 100644 tests/apis/namespace/basic/test.lua create mode 100644 tests/apis/namespace/nested/test.lua (limited to 'tests') diff --git a/tests/apis/namespace/basic/test.lua b/tests/apis/namespace/basic/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/basic/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/nested/test.lua b/tests/apis/namespace/nested/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/nested/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index c8b087333..ccf49f1ec 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -954,6 +954,7 @@ function interpreter:api_register_scope(...) local scope_args = table.pack(...) local scope_name = scope_args[1] local scope_info = scope_args[2] + local namespace = self._NAMESPACE_STR -- check invalid scope name, @see https://github.com/xmake-io/xmake/issues/4547 if scope_args.n > 0 and type(scope_name) ~= "string" then -- cgit v1.3.1 From d4cbbbb952700a31d17f580a0376b4793de45d7a Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 22:51:28 +0800 Subject: add root tests --- tests/apis/namespace/root/.gitignore | 8 ++++++++ tests/apis/namespace/root/src/bar.cpp | 5 +++++ tests/apis/namespace/root/src/bar.h | 9 +++++++++ tests/apis/namespace/root/src/foo.cpp | 5 +++++ tests/apis/namespace/root/src/foo.h | 9 +++++++++ tests/apis/namespace/root/src/main.cpp | 9 +++++++++ tests/apis/namespace/root/test.lua | 3 +++ tests/apis/namespace/root/xmake.lua | 26 ++++++++++++++++++++++++++ 8 files changed, 74 insertions(+) create mode 100644 tests/apis/namespace/root/.gitignore create mode 100644 tests/apis/namespace/root/src/bar.cpp create mode 100644 tests/apis/namespace/root/src/bar.h create mode 100644 tests/apis/namespace/root/src/foo.cpp create mode 100644 tests/apis/namespace/root/src/foo.h create mode 100644 tests/apis/namespace/root/src/main.cpp create mode 100644 tests/apis/namespace/root/test.lua create mode 100644 tests/apis/namespace/root/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/root/.gitignore b/tests/apis/namespace/root/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/root/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/root/src/bar.cpp b/tests/apis/namespace/root/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/root/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/root/src/bar.h b/tests/apis/namespace/root/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/root/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/root/src/foo.cpp b/tests/apis/namespace/root/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/root/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/root/src/foo.h b/tests/apis/namespace/root/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/root/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/root/src/main.cpp b/tests/apis/namespace/root/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/root/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/root/test.lua b/tests/apis/namespace/root/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/root/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/root/xmake.lua b/tests/apis/namespace/root/xmake.lua new file mode 100644 index 000000000..725fbc6f7 --- /dev/null +++ b/tests/apis/namespace/root/xmake.lua @@ -0,0 +1,26 @@ +add_rules("mode.debug", "mode.release") + +add_defines("ROOT") + +namespace("ns1", function () + add_defines("NS1_ROOT") + target("foo") + set_kind("static") + add_files("src/foo.cpp") + add_defines("FOO") + + namespace("ns2", function () + add_defines("NS2_ROOT") + target("bar") + set_kind("static") + add_files("src/bar.cpp") + add_defines("BAR") + end) +end) + +target("test") + set_kind("binary") + add_deps("ns1::foo", "ns1::ns2::bar") + add_files("src/main.cpp") + add_defines("TEST") + -- cgit v1.3.1 From 4fe6254655ec575fe414d382444d6e6023f9ad70 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 23:48:52 +0800 Subject: add includes test --- tests/apis/namespace/includes/.gitignore | 8 ++++++++ tests/apis/namespace/includes/src/bar.cpp | 5 +++++ tests/apis/namespace/includes/src/bar.h | 9 +++++++++ tests/apis/namespace/includes/src/foo.cpp | 5 +++++ tests/apis/namespace/includes/src/foo.h | 9 +++++++++ tests/apis/namespace/includes/src/main.cpp | 9 +++++++++ tests/apis/namespace/includes/src/xmake.lua | 8 ++++++++ tests/apis/namespace/includes/test.lua | 3 +++ tests/apis/namespace/includes/xmake.lua | 20 ++++++++++++++++++++ 9 files changed, 76 insertions(+) create mode 100644 tests/apis/namespace/includes/.gitignore create mode 100644 tests/apis/namespace/includes/src/bar.cpp create mode 100644 tests/apis/namespace/includes/src/bar.h create mode 100644 tests/apis/namespace/includes/src/foo.cpp create mode 100644 tests/apis/namespace/includes/src/foo.h create mode 100644 tests/apis/namespace/includes/src/main.cpp create mode 100644 tests/apis/namespace/includes/src/xmake.lua create mode 100644 tests/apis/namespace/includes/test.lua create mode 100644 tests/apis/namespace/includes/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/includes/.gitignore b/tests/apis/namespace/includes/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/includes/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/includes/src/bar.cpp b/tests/apis/namespace/includes/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/includes/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/includes/src/bar.h b/tests/apis/namespace/includes/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/includes/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/includes/src/foo.cpp b/tests/apis/namespace/includes/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/includes/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/includes/src/foo.h b/tests/apis/namespace/includes/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/includes/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/includes/src/main.cpp b/tests/apis/namespace/includes/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/includes/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/includes/src/xmake.lua b/tests/apis/namespace/includes/src/xmake.lua new file mode 100644 index 000000000..1d93e5a9a --- /dev/null +++ b/tests/apis/namespace/includes/src/xmake.lua @@ -0,0 +1,8 @@ +namespace("ns2", function () + add_defines("NS2_ROOT") + target("bar") + set_kind("static") + add_files("bar.cpp") + add_defines("BAR") +end) + diff --git a/tests/apis/namespace/includes/test.lua b/tests/apis/namespace/includes/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/includes/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/includes/xmake.lua b/tests/apis/namespace/includes/xmake.lua new file mode 100644 index 000000000..323627734 --- /dev/null +++ b/tests/apis/namespace/includes/xmake.lua @@ -0,0 +1,20 @@ +add_rules("mode.debug", "mode.release") + +add_defines("ROOT") + +namespace("ns1", function () + add_defines("NS1_ROOT") + target("foo") + set_kind("static") + add_files("src/foo.cpp") + add_defines("FOO") + + includes("src") +end) + +target("test") + set_kind("binary") + add_deps("ns1::foo", "ns1::ns2::bar") + add_files("src/main.cpp") + add_defines("TEST") + -- cgit v1.3.1 From ae8a596c609e01b21caa5825f3fd2eec0bdb7507 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:40:57 +0800 Subject: support for inner namespace access --- tests/apis/namespace/inner/.gitignore | 8 ++++++++ tests/apis/namespace/inner/src/bar.cpp | 5 +++++ tests/apis/namespace/inner/src/bar.h | 9 +++++++++ tests/apis/namespace/inner/src/foo.cpp | 5 +++++ tests/apis/namespace/inner/src/foo.h | 9 +++++++++ tests/apis/namespace/inner/src/main.cpp | 9 +++++++++ tests/apis/namespace/inner/test.lua | 3 +++ tests/apis/namespace/inner/xmake.lua | 19 +++++++++++++++++++ xmake/actions/build/build.lua | 2 +- xmake/actions/build/build_files.lua | 3 ++- xmake/actions/config/main.lua | 9 ++++----- xmake/actions/package/local/main.lua | 2 +- xmake/actions/package/remote/main.lua | 2 +- xmake/core/base/private/instance_deps.lua | 12 ++++++++++++ xmake/core/project/project.lua | 11 +++++++++-- xmake/core/project/target.lua | 12 +++++++++++- xmake/plugins/project/make/makefile.lua | 2 +- xmake/plugins/project/ninja/build_ninja.lua | 2 +- xmake/plugins/project/vsxmake/getinfo.lua | 2 +- 19 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 tests/apis/namespace/inner/.gitignore create mode 100644 tests/apis/namespace/inner/src/bar.cpp create mode 100644 tests/apis/namespace/inner/src/bar.h create mode 100644 tests/apis/namespace/inner/src/foo.cpp create mode 100644 tests/apis/namespace/inner/src/foo.h create mode 100644 tests/apis/namespace/inner/src/main.cpp create mode 100644 tests/apis/namespace/inner/test.lua create mode 100644 tests/apis/namespace/inner/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/inner/.gitignore b/tests/apis/namespace/inner/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/inner/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/inner/src/bar.cpp b/tests/apis/namespace/inner/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/inner/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/inner/src/bar.h b/tests/apis/namespace/inner/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/inner/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/inner/src/foo.cpp b/tests/apis/namespace/inner/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/inner/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/inner/src/foo.h b/tests/apis/namespace/inner/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/inner/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/inner/src/main.cpp b/tests/apis/namespace/inner/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/inner/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/inner/test.lua b/tests/apis/namespace/inner/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/inner/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/inner/xmake.lua b/tests/apis/namespace/inner/xmake.lua new file mode 100644 index 000000000..c18203f29 --- /dev/null +++ b/tests/apis/namespace/inner/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.debug", "mode.release") + +namespace("ns1", function () + target("foo") + set_kind("static") + add_files("src/foo.cpp") + + namespace("ns2", function() + target("bar") + set_kind("static") + add_files("src/bar.cpp") + end) + + target("test") + set_kind("binary") + add_deps("foo", "ns2::bar") + add_files("src/main.cpp") +end) + diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua index f4b371de7..28a33db78 100644 --- a/xmake/actions/build/build.lua +++ b/xmake/actions/build/build.lua @@ -217,7 +217,7 @@ function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, target, jobrefs, jobrefs[target:name()] = job_build_after jobrefs_before[target:name()] = job_build_before for _, depname in ipairs(target:get("deps")) do - local dep = project.target(depname) + local dep = project.target(depname, {namespace = target:namespace()}) local targetjob = job_build -- @see https://github.com/xmake-io/xmake/discussions/2500 if dep:policy("build.across_targets_in_parallel") == false then diff --git a/xmake/actions/build/build_files.lua b/xmake/actions/build/build_files.lua index f5604f0bd..ad85d8b2a 100644 --- a/xmake/actions/build/build_files.lua +++ b/xmake/actions/build/build_files.lua @@ -106,7 +106,8 @@ function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, jobrefs, target, jobrefs[target:name()] = targetjob_root if not option.get("shallow") then for _, depname in ipairs(target:get("deps")) do - _add_batchjobs_for_target_and_deps(batchjobs, targetjob, jobrefs, project.target(depname), filepatterns) + _add_batchjobs_for_target_and_deps(batchjobs, targetjob, jobrefs, + project.target(depname, {namespace = target:namespace()}), filepatterns) end end end diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua index 9f0cc08fd..0f69142a5 100644 --- a/xmake/actions/config/main.lua +++ b/xmake/actions/config/main.lua @@ -102,12 +102,11 @@ end -- check target function _check_target(target, checked_targets) - if not checked_targets[target:name()] then - checked_targets[target:name()] = target + if not checked_targets[target:fullname()] then + checked_targets[target:fullname()] = target for _, depname in ipairs(target:get("deps")) do - assert(depname ~= target:name(), "the target(%s) cannot depend self!", depname) - local deptarget = project.target(depname) - assert(deptarget, "unknown target(%s) for %s.deps!", depname, target:name()) + local deptarget = project.target(depname, {namespace = target:namespace()}) + assert(deptarget, "unknown target(%s) for %s.deps!", depname, target:fullname()) _check_target(deptarget, checked_targets) end end diff --git a/xmake/actions/package/local/main.lua b/xmake/actions/package/local/main.lua index 42133b142..3035946ff 100644 --- a/xmake/actions/package/local/main.lua +++ b/xmake/actions/package/local/main.lua @@ -30,7 +30,7 @@ import("target.action.install") function _get_librarydeps(target) local librarydeps = {} for _, depname in ipairs(target:get("deps")) do - local dep = project.target(depname) + local dep = project.target(depname, {namespace = target:namespace()}) if not ((target:is_binary() or target:is_shared()) and dep:is_static()) then table.insert(librarydeps, dep:name():lower()) end diff --git a/xmake/actions/package/remote/main.lua b/xmake/actions/package/remote/main.lua index f73126618..58c89b302 100644 --- a/xmake/actions/package/remote/main.lua +++ b/xmake/actions/package/remote/main.lua @@ -30,7 +30,7 @@ import("core.base.bit") function _get_librarydeps(target) local librarydeps = {} for _, depname in ipairs(target:get("deps")) do - local dep = project.target(depname) + local dep = project.target(depname, {namespace = target:namespace()}) if not ((target:is_binary() or target:is_shared()) and dep:is_static()) then table.insert(librarydeps, dep:name():lower()) end diff --git a/xmake/core/base/private/instance_deps.lua b/xmake/core/base/private/instance_deps.lua index 17145ac6f..2c9c1a7e2 100644 --- a/xmake/core/base/private/instance_deps.lua +++ b/xmake/core/base/private/instance_deps.lua @@ -46,6 +46,12 @@ function instance_deps.load_deps(instance, instances, deps, orderdeps, depspath, -- @see https://github.com/xmake-io/xmake/issues/3144 local depname = plaindeps[total + 1 - idx] local depinst = instances[depname] + if depinst == nil and instance.namespace then + local namespace = instance:namespace() + if namespace then + depinst = instances[namespace .. "::" .. depname] + end + end if depinst then local continue_walk = true if walkdep then @@ -79,6 +85,12 @@ function instance_deps._sort_instance(instance, instances, orderinstances, insta instancerefs[instance:name()] = true for _, depname in ipairs(table.wrap(instance:get("deps"))) do local depinst = instances[depname] + if depinst == nil and instance.namespace then + local namespace = instance:namespace() + if namespace then + depinst = instances[namespace .. "::" .. depname] + end + end if depinst then local depspath_sub if depspath then diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 77793d3e5..f395f5f6b 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -911,9 +911,16 @@ function project.is_loaded() end -- get the given target -function project.target(name) +function project.target(name, opt) + opt = opt or {} local targets = project.targets() - return targets and targets[name] + if targets then + local t = targets[name] + if not t and opt.namespace then + t = targets[opt.namespace .. "::" .. name] + end + return t + end end -- add the given target, @note if the target name is the same, it will be replaced diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 2669875b5..eed55c0bf 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -257,6 +257,9 @@ function _instance:_build_deps() -- @see https://github.com/xmake-io/xmake/issues/4689 instance_deps.load_deps(self, instances, {}, self._INHERITDEPS, {self:fullname()}, function (t, dep) local depinherit = t:extraconf("deps", dep:name(), "inherit") + if depinherit == nil then + depinherit = t:extraconf("deps", dep:fullname(), "inherit") + end return depinherit == nil or depinherit end) end @@ -1107,7 +1110,14 @@ end function _instance:dep(name) local deps = self:deps() if deps then - return deps[name] + local dep = deps[name] + if dep == nil then + local namespace = self:namespace() + if namespace then + dep = deps[namespace .. "::" .. name] + end + end + return dep end end diff --git a/xmake/plugins/project/make/makefile.lua b/xmake/plugins/project/make/makefile.lua index 12b1b77e0..3d4d5507f 100644 --- a/xmake/plugins/project/make/makefile.lua +++ b/xmake/plugins/project/make/makefile.lua @@ -547,7 +547,7 @@ function _add_build_target(makefile, target, targetflags, outputdir) -- make dependence for the dependent targets for _, depname in ipairs(target:get("deps")) do - local dep = project.target(depname) + local dep = project.target(depname, {namespace = target:namespace()}) makefile:write(" " .. (dep:is_phony() and depname or _get_relative_unix_path(dep:targetfile(), outputdir))) end diff --git a/xmake/plugins/project/ninja/build_ninja.lua b/xmake/plugins/project/ninja/build_ninja.lua index 1264ed60c..94d6495ef 100644 --- a/xmake/plugins/project/ninja/build_ninja.lua +++ b/xmake/plugins/project/ninja/build_ninja.lua @@ -367,7 +367,7 @@ function _add_build_for_target(ninjafile, target, outputdir) ninjafile:print(" || $") ninjafile:write(" ") for _, dep in ipairs(deps) do - ninjafile:write(" " .. _get_relative_unix_path(project.target(dep):targetfile(), outputdir)) + ninjafile:write(" " .. _get_relative_unix_path(project.target(dep, {namespace = target:namespace()}):targetfile(), outputdir)) end end ninjafile:print("") diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua index e566ab160..12d6497c0 100644 --- a/xmake/plugins/project/vsxmake/getinfo.lua +++ b/xmake/plugins/project/vsxmake/getinfo.lua @@ -585,7 +585,7 @@ function main(outputdir, vsinfo) if target:get("default") == true then table.insert(targetnames, 1, targetname) elseif target:is_binary() then - local first_target = targetnames[1] and project.target(targetnames[1]) + local first_target = targetnames[1] and project.target(targetnames[1], {namespace = target:namespace()}) if not first_target or first_target:get("default") ~= true then table.insert(targetnames, 1, targetname) else -- cgit v1.3.1 From 61812aa80ba743e735368a52aec1c4c5bc4a7f5e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:50:41 +0800 Subject: support for namespace options --- tests/apis/namespace/option/.gitignore | 8 +++ tests/apis/namespace/option/src/bar.cpp | 5 ++ tests/apis/namespace/option/src/bar.h | 9 +++ tests/apis/namespace/option/src/foo.cpp | 5 ++ tests/apis/namespace/option/src/foo.h | 9 +++ tests/apis/namespace/option/src/main.cpp | 9 +++ tests/apis/namespace/option/test.lua | 3 + tests/apis/namespace/option/xmake.lua | 25 ++++++++ xmake/core/base/cli.lua | 6 +- xmake/core/project/option.lua | 68 +++++++++++++++------- xmake/core/project/project.lua | 2 +- xmake/core/project/target.lua | 8 ++- .../modules/import/core/project/project.lua | 8 +-- 13 files changed, 137 insertions(+), 28 deletions(-) create mode 100644 tests/apis/namespace/option/.gitignore create mode 100644 tests/apis/namespace/option/src/bar.cpp create mode 100644 tests/apis/namespace/option/src/bar.h create mode 100644 tests/apis/namespace/option/src/foo.cpp create mode 100644 tests/apis/namespace/option/src/foo.h create mode 100644 tests/apis/namespace/option/src/main.cpp create mode 100644 tests/apis/namespace/option/test.lua create mode 100644 tests/apis/namespace/option/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/option/.gitignore b/tests/apis/namespace/option/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/option/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/option/src/bar.cpp b/tests/apis/namespace/option/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/option/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/option/src/bar.h b/tests/apis/namespace/option/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/option/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/option/src/foo.cpp b/tests/apis/namespace/option/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/option/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/option/src/foo.h b/tests/apis/namespace/option/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/option/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/option/src/main.cpp b/tests/apis/namespace/option/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/option/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/option/test.lua b/tests/apis/namespace/option/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/option/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua new file mode 100644 index 000000000..10f2d18f5 --- /dev/null +++ b/tests/apis/namespace/option/xmake.lua @@ -0,0 +1,25 @@ +add_rules("mode.debug", "mode.release") + +option("opt0", {default = true, defines = "OPT0", description = "option0"}) + +namespace("ns1", function () + option("opt1", {default = true, defines = "NS1_OPT1", description = "option1"}) + + target("foo") + set_kind("static") + add_files("src/foo.cpp") + + namespace("ns2", function() + option("opt2", {default = true, defines = "NS2_OPT2", description = "option2"}) + target("bar") + set_kind("static") + add_files("src/bar.cpp") + end) + + target("test") + set_kind("binary") + add_deps("foo", "ns2::bar") + add_files("src/main.cpp") + add_options("opt0", "opt1", "ns2::opt2") +end) + diff --git a/xmake/core/base/cli.lua b/xmake/core/base/cli.lua index a5d3db9ba..9eb4dfb71 100644 --- a/xmake/core/base/cli.lua +++ b/xmake/core/base/cli.lua @@ -89,7 +89,11 @@ function cli.parsev(argv, flags) table.insert(parsed, cli._make_segment("sep", "--", argv, index, {})) elseif value:startswith("--") then -- "--key:value", "--key=value", "--long-flag" - local sep = value:find("[=:]", 3, false) + local sep = value:find("[=]", 3, false) + -- ignore namespace, e.g. `--namespace::opt` + if sep and value:sub(sep, sep + 1) == "::" then + sep = nil + end if sep then table.insert(parsed, cli._make_option(value:sub(3, sep - 1), value:sub(sep + 1), false, argv, index)) else diff --git a/xmake/core/project/option.lua b/xmake/core/project/option.lua index 0e4499f57..18b9dc363 100644 --- a/xmake/core/project/option.lua +++ b/xmake/core/project/option.lua @@ -44,9 +44,14 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance function _instance.new(name, info) - local instance = table.inherit(_instance) - instance._NAME = name - instance._INFO = info + local instance = table.inherit(_instance) + local parts = name:split("::", {plain = true}) + instance._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + instance._NAMESPACE = table.concat(parts, "::") + end + instance._INFO = info instance._CACHEID = 1 return instance end @@ -63,7 +68,7 @@ function _instance:_save() self:set("check_before", nil) -- save option - option._cache():set(self:name(), self:info()) + option._cache():set(self:fullname(), self:info()) -- restore scripts self:set("check", check) @@ -73,7 +78,7 @@ end -- clear the option info for cache function _instance:_clear() - option._cache():set(self:name(), nil) + option._cache():set(self:fullname(), nil) end -- check snippets @@ -129,7 +134,7 @@ function _instance:_do_check_cxsnippets(snippets) end end if #table.keys(snippets_output) > 1 then - return false, -1, string.format("option(%s): only support for only one snippet with output!", self:name()) + return false, -1, string.format("option(%s): only support for only one snippet with output!", self:fullname()) end end @@ -309,6 +314,10 @@ function _instance:_check() if name:startswith("__") then name = name:sub(3) end + local namespace = self:namespace() + if namespace then + name = namespace .. "::" .. name + end -- trace local result @@ -336,7 +345,7 @@ end function _instance:check() -- the option name - local name = self:name() + local name = self:fullname() -- get default value, TODO: enable will be deprecated local default = self:get("default") @@ -378,24 +387,24 @@ end -- get the option value function _instance:value() - return config.get(self:name()) + return config.get(self:fullname()) end -- set the option value function _instance:set_value(value) - config.set(self:name(), value) + config.set(self:fullname(), value) self:_save() end -- clear the option status and need recheck it function _instance:clear() - config.set(self:name(), nil) + config.set(self:fullname(), nil) self:_clear() end -- this option is enabled? function _instance:enabled() - return config.get(self:name()) + return config.get(self:fullname()) end -- enable or disable this option @@ -409,8 +418,8 @@ function _instance:enable(enabled, opt) opt = opt or {} -- enable or disable this option? - if not config.readonly(self:name()) or opt.force then - config.set(self:name(), enabled, opt) + if not config.readonly(self:fullname()) or opt.force then + config.set(self:fullname(), enabled, opt) end -- save or clear this option in cache @@ -474,7 +483,14 @@ end function _instance:dep(name) local deps = self:deps() if deps then - return deps[name] + local dep = deps[name] + if dep == nil then + local namespace = self:namespace() + if namespace then + dep = deps[namespace .. "::" .. name] + end + end + return dep end end @@ -493,9 +509,20 @@ function _instance:name() return self._NAME end +-- get the namespace +function _instance:namespace() + return self._NAMESPACE +end + +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- get the option description function _instance:description() - return self:get("description") or ("The " .. self:name() .. " option") + return self:get("description") or ("The " .. self:fullname() .. " option") end -- get the cache key @@ -645,13 +672,12 @@ function option.new(name, info) end -- load the option info from the cache -function option.load(name) - - -- check - assert(name) - - -- get info +function option.load(name, opt) + opt = opt or {} local info = option._cache():get(name) + if info == nil and opt.namespace then + info = option._cache():get(opt.namespace .. "::" .. name) + end if info == nil then return end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index f395f5f6b..a266bf1db 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -1191,7 +1191,7 @@ function project.menu() options_by_category[category] = options_by_category[category] or {} -- append option to the current category - options_by_category[category][opt:name()] = opt + options_by_category[category][opt:fullname()] = opt end -- make menu by category diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index eed55c0bf..03e68629f 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -1278,7 +1278,13 @@ function _instance:orderopts(opt) orderopts = {} for _, name in ipairs(table.wrap(self:get("options", opt))) do local opt_ = nil - if config.get(name) then opt_ = option.load(name) end + local enabled = config.get(name) + if enabled == nil and self:namespace() then + enabled = config.get(self:namespace() .. "::" .. name) + end + if enabled then + opt_ = option.load(name, {namespace = self:namespace()}) + end if opt_ then table.insert(orderopts, opt_) end diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index bada906d4..b39fabaf6 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -98,15 +98,15 @@ function sandbox_core_project.check_options() if opt then -- check deps of this option first for _, dep in ipairs(opt:orderdeps()) do - if not checked[dep:name()] then + if not checked[dep:fullname()] then dep:check() - checked[dep:name()] = true + checked[dep:fullname()] = true end end -- check this option - if not checked[opt:name()] then + if not checked[opt:fullname()] then opt:check() - checked[opt:name()] = true + checked[opt:fullname()] = true end end end -- cgit v1.3.1 From 508341ccfe39480a0465de602afc6db09980500c Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:51:07 +0800 Subject: improve option test --- tests/apis/namespace/option/xmake.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 10f2d18f5..1bfbbe3fe 100644 --- a/tests/apis/namespace/option/xmake.lua +++ b/tests/apis/namespace/option/xmake.lua @@ -8,12 +8,14 @@ namespace("ns1", function () target("foo") set_kind("static") add_files("src/foo.cpp") + add_options("opt1") namespace("ns2", function() option("opt2", {default = true, defines = "NS2_OPT2", description = "option2"}) target("bar") set_kind("static") add_files("src/bar.cpp") + add_options("opt2") end) target("test") -- cgit v1.3.1 From 55dc4ce3ea49160952bec878188d9a01a4a76f73 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:54:53 +0800 Subject: add namespace for rule --- tests/apis/namespace/rule/.gitignore | 8 +++++++ tests/apis/namespace/rule/src/bar.cpp | 5 ++++ tests/apis/namespace/rule/src/bar.h | 9 +++++++ tests/apis/namespace/rule/src/foo.cpp | 5 ++++ tests/apis/namespace/rule/src/foo.h | 9 +++++++ tests/apis/namespace/rule/src/main.cpp | 9 +++++++ tests/apis/namespace/rule/test.lua | 3 +++ tests/apis/namespace/rule/xmake.lua | 37 +++++++++++++++++++++++++++++ xmake/core/project/project.lua | 11 ++++++--- xmake/core/project/rule.lua | 24 +++++++++++++++---- xmake/core/project/target.lua | 17 ++++++++----- xmake/modules/private/utils/rule_groups.lua | 3 ++- xmake/plugins/project/utils/target_cmds.lua | 3 ++- 13 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 tests/apis/namespace/rule/.gitignore create mode 100644 tests/apis/namespace/rule/src/bar.cpp create mode 100644 tests/apis/namespace/rule/src/bar.h create mode 100644 tests/apis/namespace/rule/src/foo.cpp create mode 100644 tests/apis/namespace/rule/src/foo.h create mode 100644 tests/apis/namespace/rule/src/main.cpp create mode 100644 tests/apis/namespace/rule/test.lua create mode 100644 tests/apis/namespace/rule/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/rule/.gitignore b/tests/apis/namespace/rule/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/rule/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/rule/src/bar.cpp b/tests/apis/namespace/rule/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/rule/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/rule/src/bar.h b/tests/apis/namespace/rule/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/rule/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/rule/src/foo.cpp b/tests/apis/namespace/rule/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/rule/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/rule/src/foo.h b/tests/apis/namespace/rule/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/rule/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/rule/src/main.cpp b/tests/apis/namespace/rule/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/rule/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/rule/test.lua b/tests/apis/namespace/rule/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/rule/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/rule/xmake.lua b/tests/apis/namespace/rule/xmake.lua new file mode 100644 index 000000000..96b80271d --- /dev/null +++ b/tests/apis/namespace/rule/xmake.lua @@ -0,0 +1,37 @@ +add_rules("mode.debug", "mode.release") + +rule("rule0") + on_load(function (target) + target:add("defines", "RULE0") + end) + +namespace("ns1", function () + rule("rule1") + on_load(function (target) + target:add("defines", "NS1_RULE1") + end) + + target("foo") + set_kind("static") + add_files("src/foo.cpp") + add_rules("rule1") + + namespace("ns2", function() + rule("rule2") + on_load(function (target) + target:add("defines", "NS2_RULE2") + end) + + target("bar") + set_kind("static") + add_files("src/bar.cpp") + add_rules("rule2") + end) + + target("test") + set_kind("binary") + add_deps("foo", "ns2::bar") + add_files("src/main.cpp") + add_rules("rule0", "rule1", "ns2::rule2") +end) + diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index a266bf1db..8afcee6b9 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -383,7 +383,7 @@ function project._load_targets() end rulenames = table.unique(rulenames) for _, rulename in ipairs(rulenames) do - local r = project.rule(rulename) or rule.rule(rulename) + local r = project.rule(rulename, {namespace = t:namespace()}) or rule.rule(rulename) if r then -- only add target rules if r:kind() == "target" then @@ -1087,8 +1087,13 @@ function project.requireslock_version() end -- get the given rule -function project.rule(name) - return project.rules()[name] +function project.rule(name, opt) + opt = opt or {} + local r = project.rules()[name] + if r == nil and opt.namespace then + r = project.rules()[opt.namespace .. "::" .. name] + end + return r end -- get project rules diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua index 75da4da39..5d44f7c25 100644 --- a/xmake/core/project/rule.lua +++ b/xmake/core/project/rule.lua @@ -60,12 +60,12 @@ function _instance:_build_deps() end self._DEPS = self._DEPS or {} self._ORDERDEPS = self._ORDERDEPS or {} - instance_deps.load_deps(self, instances, self._DEPS, self._ORDERDEPS, {self:name()}) + instance_deps.load_deps(self, instances, self._DEPS, self._ORDERDEPS, {self:fullname()}) end -- clone rule function _instance:clone() - local instance = rule.new(self:name(), self._INFO:clone()) + local instance = rule.new(self:fullname(), self._INFO:clone()) instance._DEPS = self._DEPS instance._ORDERDEPS = self._ORDERDEPS instance._PACKAGE = self._PACKAGE @@ -106,7 +106,23 @@ end -- set the rule name function _instance:name_set(name) - self._NAME = name + local parts = name:split("::", {plain = true}) + self._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + self._NAMESPACE = table.concat(parts, "::") + end +end + +-- get the namespace +function _instance:namespace() + return self._NAMESPACE +end + +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() end -- get the rule kind @@ -331,7 +347,7 @@ end function rule.new(name, info, opt) opt = opt or {} local instance = table.inherit(_instance) - instance._NAME = name + instance:name_set(name) instance._INFO = info instance._PACKAGE = opt.package if opt.package then diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 03e68629f..9d5a9c07a 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -75,7 +75,7 @@ end function _instance:_load_rule(ruleinst, suffix) -- init cache - local key = ruleinst:name() .. (suffix and ("_" .. suffix) or "") + local key = ruleinst:fullname() .. (suffix and ("_" .. suffix) or "") local cache = self._RULES_LOADED or {} -- do load @@ -90,7 +90,7 @@ function _instance:_load_rule(ruleinst, suffix) -- before_load has been deprecated if on_load and suffix == "before" then - deprecated.add(ruleinst:name() .. ".on_load", ruleinst:name() .. ".before_load") + deprecated.add(ruleinst:fullname() .. ".on_load", ruleinst:fullname() .. ".before_load") end end @@ -208,7 +208,7 @@ function _instance:_update_filerules() end rulenames = table.unique(rulenames) for _, rulename in ipairs(rulenames) do - local r = target._project() and target._project().rule(rulename) or rule.rule(rulename) + local r = target._project() and target._project().rule(rulename, {namespace = self:namespace()}) or rule.rule(rulename) if r then -- only add target rules if r:kind() == "target" then @@ -1163,7 +1163,11 @@ end -- get target rule from the given rule name function _instance:rule(name) if self._RULES then - return self._RULES[name] + local r = self._RULES[name] + if r == nil and self:namespace() then + r = self._RULES[self:namespace() .. "::" .. name] + end + return r end end @@ -1173,7 +1177,7 @@ end -- it will be replaced in the target:rules() and target:orderules(), but will be not replaced globally in the project.rules() function _instance:rule_add(r) self._RULES = self._RULES or {} - self._RULES[r:name()] = r + self._RULES[r:fullname()] = r self._ORDERULES = nil end @@ -1750,7 +1754,8 @@ function _instance:filerules(sourcefile) if filerules then override = filerules.override for _, rulename in ipairs(table.wrap(filerules)) do - local r = target._project().rule(rulename) or rule.rule(rulename) or self:rule(rulename) + local r = target._project().rule(rulename, {namespace = self:namespace()}) or + rule.rule(rulename) or self:rule(rulename) if r then table.insert(rules, r) end diff --git a/xmake/modules/private/utils/rule_groups.lua b/xmake/modules/private/utils/rule_groups.lua index ae7279e78..d64a75d62 100644 --- a/xmake/modules/private/utils/rule_groups.lua +++ b/xmake/modules/private/utils/rule_groups.lua @@ -27,7 +27,8 @@ import("core.project.project") -- get rule -- @note we need to get rule from target first, because we maybe will inject and replace builtin rule in target function get_rule(target, rulename) - local ruleinst = assert(target:rule(rulename) or project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) + local ruleinst = assert(target:rule(rulename) or project.rule(rulename, {namespace = target:namespace()}) or + rule.rule(rulename), "unknown rule: %s", rulename) return ruleinst end diff --git a/xmake/plugins/project/utils/target_cmds.lua b/xmake/plugins/project/utils/target_cmds.lua index 0b98a4ed7..332e4858d 100644 --- a/xmake/plugins/project/utils/target_cmds.lua +++ b/xmake/plugins/project/utils/target_cmds.lua @@ -64,7 +64,8 @@ function get_target_buildcmd_files(target, cmds, sourcebatch, opt) -- get rule local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!") - local ruleinst = assert(target:rule(rulename) or project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) + local ruleinst = assert(target:rule(rulename) or project.rule(rulename, {namespace = target:namespace()}) or + rule.rule(rulename), "unknown rule: %s", rulename) local ignored_rules = hashset.from(opt.ignored_rules or {}) if ignored_rules:has(ruleinst:name()) then return -- cgit v1.3.1 From bdc9a4cb46d5dd7d46dfa9a0ab2ec29aa9a2e6a8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:59:53 +0800 Subject: add namespace for task --- tests/apis/namespace/task/.gitignore | 8 ++++++++ tests/apis/namespace/task/test.lua | 5 +++++ tests/apis/namespace/task/xmake.lua | 22 ++++++++++++++++++++++ xmake/core/base/task.lua | 20 ++++++++++++++++++-- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/apis/namespace/task/.gitignore create mode 100644 tests/apis/namespace/task/test.lua create mode 100644 tests/apis/namespace/task/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/task/.gitignore b/tests/apis/namespace/task/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/task/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/task/test.lua b/tests/apis/namespace/task/test.lua new file mode 100644 index 000000000..46fa44af3 --- /dev/null +++ b/tests/apis/namespace/task/test.lua @@ -0,0 +1,5 @@ +function main() + os.exec("xmake task0") + os.exec("xmake ns1::task1") + os.exec("xmake ns1::ns2::task2") +end diff --git a/tests/apis/namespace/task/xmake.lua b/tests/apis/namespace/task/xmake.lua new file mode 100644 index 000000000..1f9d13e4a --- /dev/null +++ b/tests/apis/namespace/task/xmake.lua @@ -0,0 +1,22 @@ +task("task0") + set_menu {options = {}} + on_run(function () + print("task0") + end) + +namespace("ns1", function () + task("task1") + set_menu {options = {}} + on_run(function () + print("NS1_TASK1") + end) + + namespace("ns2", function() + task("task2") + set_menu {options = {}} + on_run(function () + print("NS2_TASK2") + end) + end) +end) + diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index deea228aa..1b1093e68 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -373,7 +373,12 @@ end -- new a task instance function task.new(name, info) local instance = table.inherit(task) - instance._NAME = name + local parts = name:split("::", {plain = true}) + instance._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + instance._NAMESPACE = table.concat(parts, "::") + end instance._INFO = info return instance end @@ -475,13 +480,24 @@ function task:name() return self._NAME end +-- get the namespace +function _instance:namespace() + return self._NAMESPACE +end + +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- run given task function task:run(...) -- check local on_run = self:get("run") if not on_run then - return false, string.format("task(\"%s\"): no run script, please call on_run() first!", self:name()) + return false, string.format("task(\"%s\"): no run script, please call on_run() first!", self:fullname()) end -- save the current directory -- cgit v1.3.1 From c184146581f86d95536dafcadd64e8ae000341f2 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 22:43:17 +0800 Subject: add namespace for toolchain --- tests/apis/namespace/toolchain/.gitignore | 8 +++++++ tests/apis/namespace/toolchain/src/bar.cpp | 5 ++++ tests/apis/namespace/toolchain/src/bar.h | 9 ++++++++ tests/apis/namespace/toolchain/src/foo.cpp | 5 ++++ tests/apis/namespace/toolchain/src/foo.h | 9 ++++++++ tests/apis/namespace/toolchain/src/main.cpp | 9 ++++++++ tests/apis/namespace/toolchain/test.lua | 3 +++ tests/apis/namespace/toolchain/xmake.lua | 36 +++++++++++++++++++++++++++++ xmake/core/project/project.lua | 4 ++++ xmake/core/project/target.lua | 1 + xmake/core/tool/toolchain.lua | 18 ++++++++++++++- 11 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/apis/namespace/toolchain/.gitignore create mode 100644 tests/apis/namespace/toolchain/src/bar.cpp create mode 100644 tests/apis/namespace/toolchain/src/bar.h create mode 100644 tests/apis/namespace/toolchain/src/foo.cpp create mode 100644 tests/apis/namespace/toolchain/src/foo.h create mode 100644 tests/apis/namespace/toolchain/src/main.cpp create mode 100644 tests/apis/namespace/toolchain/test.lua create mode 100644 tests/apis/namespace/toolchain/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/toolchain/.gitignore b/tests/apis/namespace/toolchain/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/toolchain/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/toolchain/src/bar.cpp b/tests/apis/namespace/toolchain/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/toolchain/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/toolchain/src/bar.h b/tests/apis/namespace/toolchain/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/toolchain/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/toolchain/src/foo.cpp b/tests/apis/namespace/toolchain/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/toolchain/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/toolchain/src/foo.h b/tests/apis/namespace/toolchain/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/toolchain/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/toolchain/src/main.cpp b/tests/apis/namespace/toolchain/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/toolchain/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/toolchain/test.lua b/tests/apis/namespace/toolchain/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/toolchain/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/toolchain/xmake.lua b/tests/apis/namespace/toolchain/xmake.lua new file mode 100644 index 000000000..588541296 --- /dev/null +++ b/tests/apis/namespace/toolchain/xmake.lua @@ -0,0 +1,36 @@ + +toolchain("toolchain0") + on_load(function (toolchain) + toolchain:add("defines", "TOOLCHAIN0") + end) + +namespace("ns1", function () + toolchain("toolchain1") + on_load(function (toolchain) + toolchain:add("defines", "NS1_TOOLCHAIN1") + end) + + target("foo") + set_kind("static") + add_files("src/foo.cpp") + set_toolchains("toolchain1") + + namespace("ns2", function() + toolchain("toolchain2") + on_load(function (toolchain) + toolchain:add("defines", "NS2_TOOLCHAIN2") + end) + + target("bar") + set_kind("static") + add_files("src/bar.cpp") + set_toolchains("toolchain2") + end) + + target("test") + set_kind("binary") + add_deps("foo", "ns2::bar") + add_files("src/main.cpp") + set_toolchains("toolchain0", "toolchain1", "ns2::toolchain2") +end) + diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 8afcee6b9..72bec34d5 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -1112,8 +1112,12 @@ end -- get the given toolchain function project.toolchain(name, opt) + opt = opt or {} local toolchain_name = toolchain.parsename(name) -- we need to ignore `@packagename` local info = project._toolchains()[toolchain_name] + if info == nil and opt.namespace then + info = project._toolchains()[opt.namespace .. "::" .. toolchain_name] + end if info then return toolchain.load_withinfo(name, info, opt) end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 9d5a9c07a..3f1875854 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2495,6 +2495,7 @@ function _instance:toolchains() local toolchain_opt = table.copy(self:extraconf("toolchains", name)) toolchain_opt.arch = self:arch() toolchain_opt.plat = self:plat() + toolchain_opt.namespace = self:namespace() local toolchain_inst, errors = toolchain.load(name, toolchain_opt) -- attempt to load toolchain from project if not toolchain_inst and target._project() then diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index 29bd5a09f..a19413d34 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -42,7 +42,12 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance function _instance.new(name, info, cachekey, is_builtin, configs) local instance = table.inherit(_instance) - instance._NAME = name + local parts = name:split("::", {plain = true}) + instance._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + instance._NAMESPACE = table.concat(parts, "::") + end instance._INFO = info instance._IS_BUILTIN = is_builtin instance._CACHE = toolchain._localcache() @@ -68,6 +73,17 @@ function _instance:name() return self._NAME end +-- get the namespace +function _instance:namespace() + return self._NAMESPACE +end + +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- get toolchain platform function _instance:plat() return self._PLAT or self:config("plat") -- cgit v1.3.1 From e5ea7bc6347ce4c4a62468138753cedea663bfa9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 22:45:32 +0800 Subject: add package for namespace --- tests/apis/namespace/package/.gitignore | 8 ++++ tests/apis/namespace/package/src/bar.cpp | 5 +++ tests/apis/namespace/package/src/bar.h | 9 +++++ tests/apis/namespace/package/src/foo.cpp | 5 +++ tests/apis/namespace/package/src/foo.h | 9 +++++ tests/apis/namespace/package/src/main.cpp | 9 +++++ tests/apis/namespace/package/test.lua | 3 ++ tests/apis/namespace/package/xmake.lua | 44 ++++++++++++++++++++++ xmake/core/package/package.lua | 19 +++++++++- .../private/action/require/impl/package.lua | 3 ++ 10 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/apis/namespace/package/.gitignore create mode 100644 tests/apis/namespace/package/src/bar.cpp create mode 100644 tests/apis/namespace/package/src/bar.h create mode 100644 tests/apis/namespace/package/src/foo.cpp create mode 100644 tests/apis/namespace/package/src/foo.h create mode 100644 tests/apis/namespace/package/src/main.cpp create mode 100644 tests/apis/namespace/package/test.lua create mode 100644 tests/apis/namespace/package/xmake.lua (limited to 'tests') diff --git a/tests/apis/namespace/package/.gitignore b/tests/apis/namespace/package/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/apis/namespace/package/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/apis/namespace/package/src/bar.cpp b/tests/apis/namespace/package/src/bar.cpp new file mode 100644 index 000000000..2c00cc6b6 --- /dev/null +++ b/tests/apis/namespace/package/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/tests/apis/namespace/package/src/bar.h b/tests/apis/namespace/package/src/bar.h new file mode 100644 index 000000000..47d8a7251 --- /dev/null +++ b/tests/apis/namespace/package/src/bar.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int sub(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/package/src/foo.cpp b/tests/apis/namespace/package/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/apis/namespace/package/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/apis/namespace/package/src/foo.h b/tests/apis/namespace/package/src/foo.h new file mode 100644 index 000000000..d2506bca9 --- /dev/null +++ b/tests/apis/namespace/package/src/foo.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int add(int a, int b); + +#ifdef __cplusplus +} +#endif diff --git a/tests/apis/namespace/package/src/main.cpp b/tests/apis/namespace/package/src/main.cpp new file mode 100644 index 000000000..4cf7b5e62 --- /dev/null +++ b/tests/apis/namespace/package/src/main.cpp @@ -0,0 +1,9 @@ +#include "foo.h" +#include "bar.h" +#include + +int main(int argc, char** argv) { + std::cout << "add(1, 2) = " << add(1, 2) << std::endl; + std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; + return 0; +} diff --git a/tests/apis/namespace/package/test.lua b/tests/apis/namespace/package/test.lua new file mode 100644 index 000000000..83c0a9546 --- /dev/null +++ b/tests/apis/namespace/package/test.lua @@ -0,0 +1,3 @@ +function main() + os.exec("xmake -vD") +end diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua new file mode 100644 index 000000000..b2bc61d73 --- /dev/null +++ b/tests/apis/namespace/package/xmake.lua @@ -0,0 +1,44 @@ + +add_requires("package0") + +package("package0") + on_fetch(function (package) + return {defines = "PACKAGE0"} + end) + +namespace("ns1", function () + + add_requires("package1") + + package("package1") + on_fetch(function (package) + return {defines = "NS1_PACKAGE1"} + end) + + target("foo") + set_kind("static") + add_files("src/foo.cpp") + add_packages("package1") + + namespace("ns2", function() + + add_requires("package2") + + package("package2") + on_fetch(function (package) + return {defines = "NS2_PACKAGE2"} + end) + + target("bar") + set_kind("static") + add_files("src/bar.cpp") + add_packages("package2") + end) + + target("test") + set_kind("binary") + add_deps("foo", "ns2::bar") + add_files("src/main.cpp") + add_packages("package0", "package1", "ns2::package2") +end) + diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 068e95ab6..d3032f15b 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -56,7 +56,12 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") function _instance.new(name, info, opt) opt = opt or {} local instance = table.inherit(_instance) - instance._NAME = name + local parts = name:split("::", {plain = true}) + instance._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + instance._NAMESPACE = table.concat(parts, "::") + end instance._INFO = info instance._REPO = opt.repo instance._SCRIPTDIR = opt.scriptdir and path.absolute(opt.scriptdir) @@ -78,6 +83,17 @@ function _instance:name() return self._NAME end +-- get the namespace +function _instance:namespace() + return self._NAMESPACE +end + +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- get the type: package function _instance:type() return "package" @@ -1276,6 +1292,7 @@ function _instance:toolchains() local toolchain_opt = project and project.extraconf("target.toolchains", name) or {} toolchain_opt.plat = self:plat() toolchain_opt.arch = self:arch() + toolchain_opt.namespace = self:namespace() local toolchain_inst, errors = toolchain.load(name, toolchain_opt) if not toolchain_inst and project then toolchain_inst = project.toolchain(name, toolchain_opt) diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 30c571592..a06ee7a29 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -1064,6 +1064,9 @@ function _load_package(packagename, requireinfo, opt) end _memcache():set2("packageids", packagename, (packageid or 0) + 1) end + if displayname and package:namespace() then + displayname = package:namespace() .. "::" .. displayname + end package:displayname_set(displayname) -- disable parallelize if the package cache directory conflicts -- cgit v1.3.1 From bd6ded54efbea8fe01040941ce8d39951a8431c9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 23:19:03 +0800 Subject: fix package with 3rd namespace --- tests/apis/namespace/package/test.lua | 2 +- tests/apis/namespace/package/xmake.lua | 21 ++++++++++++--------- xmake/core/base/interpreter.lua | 2 +- xmake/core/package/package.lua | 22 +++++++++++++++++----- .../private/action/require/impl/package.lua | 3 --- 5 files changed, 31 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/apis/namespace/package/test.lua b/tests/apis/namespace/package/test.lua index 83c0a9546..bff75b066 100644 --- a/tests/apis/namespace/package/test.lua +++ b/tests/apis/namespace/package/test.lua @@ -1,3 +1,3 @@ function main() - os.exec("xmake -vD") + os.exec("xmake -vD -y") end diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index b2bc61d73..9f01f4bdb 100644 --- a/tests/apis/namespace/package/xmake.lua +++ b/tests/apis/namespace/package/xmake.lua @@ -1,19 +1,21 @@ -add_requires("package0") +add_requires("package0", {system = false}) package("package0") - on_fetch(function (package) - return {defines = "PACKAGE0"} + on_load(function (package) + package:add("defines", "PACKAGE0") end) + on_install(function (package) end) namespace("ns1", function () - add_requires("package1") + add_requires("package1", {system = false}) package("package1") - on_fetch(function (package) - return {defines = "NS1_PACKAGE1"} + on_load(function (package) + package:add("defines", "NS1_PACKAGE1") end) + on_install(function (package) end) target("foo") set_kind("static") @@ -22,12 +24,13 @@ namespace("ns1", function () namespace("ns2", function() - add_requires("package2") + add_requires("package2", {system = false}) package("package2") - on_fetch(function (package) - return {defines = "NS2_PACKAGE2"} + on_load(function (package) + package:add("defines", "NS2_PACKAGE2") end) + on_install(function (package) end) target("bar") set_kind("static") diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 5ae08de33..2ae021f79 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -260,7 +260,7 @@ function interpreter:_api_register_xxx_values(scope_kind, action, apifunc, ...) -- init current root scope local rootkey = scope_kind - if namespace then + if namespace and scope_kind ~= "__rootkind" then rootkey = scope_kind .. "@@" .. namespace .. "::" end local root = scopes._ROOT[rootkey] or {} diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index d3032f15b..05be6af8c 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -55,12 +55,24 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance function _instance.new(name, info, opt) opt = opt or {} - local instance = table.inherit(_instance) local parts = name:split("::", {plain = true}) - instance._NAME = parts[#parts] - table.remove(parts) - if #parts > 0 then - instance._NAMESPACE = table.concat(parts, "::") + local instance = table.inherit(_instance) + local managers = package._memcache():get("managers") + if managers == nil and #parts == 2 then + managers = hashset.new() + for _, dir in ipairs(os.dirs(path.join(os.programdir(), "modules/package/manager/*"))) do + managers:insert(path.filename(dir)) + end + package._memcache():set("managers", managers) + end + if #parts == 2 and managers and managers:has(parts[1]) then + instance._NAME = name + else + instance._NAME = parts[#parts] + table.remove(parts) + if #parts > 0 then + instance._NAMESPACE = table.concat(parts, "::") + end end instance._INFO = info instance._REPO = opt.repo diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index a06ee7a29..30c571592 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -1064,9 +1064,6 @@ function _load_package(packagename, requireinfo, opt) end _memcache():set2("packageids", packagename, (packageid or 0) + 1) end - if displayname and package:namespace() then - displayname = package:namespace() .. "::" .. displayname - end package:displayname_set(displayname) -- disable parallelize if the package cache directory conflicts -- cgit v1.3.1 From 76fd3aba7f38972943f3ad60471de04000e68c5a Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 23:37:28 +0800 Subject: improve project.get --- tests/apis/namespace/package/xmake.lua | 3 +++ xmake/core/base/interpreter.lua | 41 +++++++++++++++++++++++++--------- xmake/core/project/project.lua | 1 + 3 files changed, 34 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index 9f01f4bdb..eac4ab530 100644 --- a/tests/apis/namespace/package/xmake.lua +++ b/tests/apis/namespace/package/xmake.lua @@ -1,4 +1,5 @@ +set_version("1.0.0") add_requires("package0", {system = false}) package("package0") @@ -9,6 +10,7 @@ package("package0") namespace("ns1", function () + set_version("1.0.1") add_requires("package1", {system = false}) package("package1") @@ -24,6 +26,7 @@ namespace("ns1", function () namespace("ns2", function() + set_version("1.0.2") add_requires("package2", {system = false}) package("package2") diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 6f006efff..7f43ca340 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -543,8 +543,25 @@ function interpreter:_make(scope_kind, deduplicate, enable_filter) local results = {} local scope_opt = {interpreter = self, deduplicate = deduplicate, enable_filter = enable_filter} if scope_kind and scope_kind:startswith("root.") then - local root_scope = scopes._ROOT[scope_kind:sub(6)] - if root_scope then + local root_scope = {} + local empty = true + local kind_prefix = scope_kind:sub(6) + for kind, scope in pairs(scopes._ROOT) do + if kind:startswith(kind_prefix) then + local namespace = kind:match(kind_prefix .. "@@(.+)::") + if namespace or kind == kind_prefix then + for k, v in pairs(scope) do + if namespace then + root_scope[namespace .. "::" .. k] = v + else + root_scope[k] = v + end + end + end + empty = false + end + end + if root_scope and not empty then results = self:_handle(root_scope, deduplicate, enable_filter) end return scopeinfo.new(scope_kind, results, scope_opt) @@ -553,20 +570,22 @@ function interpreter:_make(scope_kind, deduplicate, enable_filter) elseif scope_kind == "root" or scope_kind == nil then local root_scope = {} local empty = true - for scopekind, scope in pairs(scopes._ROOT) do - if scopekind:startswith("__rootkind") then - local namespace = scopekind:match("__rootkind@@(.+)::") - for k, v in pairs(scope) do - if namespace then - root_scope[namespace .. "::" .. k] = v - else - root_scope[k] = v + for kind, scope in pairs(scopes._ROOT) do + if kind:startswith("__rootkind") then + local namespace = kind:match("__rootkind@@(.+)::") + if namespace or kind == "__rootkind" then + for k, v in pairs(scope) do + if namespace then + root_scope[namespace .. "::" .. k] = v + else + root_scope[k] = v + end end end empty = false end end - if root_scope then + if root_scope and not empty then results = self:_handle(root_scope, deduplicate, enable_filter) end return scopeinfo.new(scope_kind, results, scope_opt) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 72bec34d5..b92872d64 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -785,6 +785,7 @@ function project.filelock() end -- get the root configuration +-- and we get values in namespace, e.g. project.get("ns1::ns2::name"), project.get("target.ns1::ns2::name") function project.get(name) local rootinfo if name and name:startswith("target.") then -- cgit v1.3.1 From cd44af7b2e7744a2d04c30422118f80a5e0ff68d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 23:31:02 +0800 Subject: update test --- tests/apis/namespace/package/xmake.lua | 3 --- 1 file changed, 3 deletions(-) (limited to 'tests') diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index eac4ab530..9f01f4bdb 100644 --- a/tests/apis/namespace/package/xmake.lua +++ b/tests/apis/namespace/package/xmake.lua @@ -1,5 +1,4 @@ -set_version("1.0.0") add_requires("package0", {system = false}) package("package0") @@ -10,7 +9,6 @@ package("package0") namespace("ns1", function () - set_version("1.0.1") add_requires("package1", {system = false}) package("package1") @@ -26,7 +24,6 @@ namespace("ns1", function () namespace("ns2", function() - set_version("1.0.2") add_requires("package2", {system = false}) package("package2") -- cgit v1.3.1 From 53f150785e89570e481a50be252df2ab300b6e52 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:41:31 +0800 Subject: add has_config test --- tests/apis/namespace/option/xmake.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tests') diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 1bfbbe3fe..7043caf9d 100644 --- a/tests/apis/namespace/option/xmake.lua +++ b/tests/apis/namespace/option/xmake.lua @@ -9,6 +9,9 @@ namespace("ns1", function () set_kind("static") add_files("src/foo.cpp") add_options("opt1") + if has_config("opt1") then + add_defines("HAS_NS1_OPT1") + end namespace("ns2", function() option("opt2", {default = true, defines = "NS2_OPT2", description = "option2"}) @@ -16,6 +19,9 @@ namespace("ns1", function () set_kind("static") add_files("src/bar.cpp") add_options("opt2") + if has_config("opt2") then + add_defines("HAS_NS2_OPT2") + end end) target("test") @@ -23,5 +29,14 @@ namespace("ns1", function () add_deps("foo", "ns2::bar") add_files("src/main.cpp") add_options("opt0", "opt1", "ns2::opt2") + if has_config("opt0") then + add_defines("HAS_OPT0") + end + if has_config("opt1") then + add_defines("HAS_NS1_OPT1") + end + if has_config("ns2::opt2") then + add_defines("HAS_NS2_OPT2") + end end) -- cgit v1.3.1 From 672112971ac26e4ed5bc5789938a82e8dbc22c86 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:42:04 +0800 Subject: improve has_config test --- tests/apis/namespace/option/xmake.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 7043caf9d..b39b764f1 100644 --- a/tests/apis/namespace/option/xmake.lua +++ b/tests/apis/namespace/option/xmake.lua @@ -29,14 +29,16 @@ namespace("ns1", function () add_deps("foo", "ns2::bar") add_files("src/main.cpp") add_options("opt0", "opt1", "ns2::opt2") - if has_config("opt0") then - add_defines("HAS_OPT0") - end - if has_config("opt1") then - add_defines("HAS_NS1_OPT1") - end - if has_config("ns2::opt2") then - add_defines("HAS_NS2_OPT2") - end + on_load(function (target) + if has_config("opt0") then + target:add("defines", "HAS_OPT0") + end + if has_config("opt1") then + target:add("defines", "HAS_NS1_OPT1") + end + if has_config("ns2::opt2") then + target:add("defines", "HAS_NS2_OPT2") + end + end) end) -- cgit v1.3.1 From 9c0f92f7cfcabe17fbb56c2f6bc1ea54a7223f82 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:56:25 +0800 Subject: improve has_package --- tests/apis/namespace/package/xmake.lua | 17 +++++++++++++++++ xmake/core/project/project.lua | 15 +++++++++++++-- xmake/core/sandbox/modules/has_package.lua | 15 +++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index 9f01f4bdb..c88038071 100644 --- a/tests/apis/namespace/package/xmake.lua +++ b/tests/apis/namespace/package/xmake.lua @@ -21,6 +21,9 @@ namespace("ns1", function () set_kind("static") add_files("src/foo.cpp") add_packages("package1") + if has_package("package1") then + add_defines("HAS_PACKAGE1") + end namespace("ns2", function() @@ -36,6 +39,9 @@ namespace("ns1", function () set_kind("static") add_files("src/bar.cpp") add_packages("package2") + if has_package("package2") then + add_defines("HAS_PACKAGE2") + end end) target("test") @@ -43,5 +49,16 @@ namespace("ns1", function () add_deps("foo", "ns2::bar") add_files("src/main.cpp") add_packages("package0", "package1", "ns2::package2") + on_load(function (target) + if has_package("package0") then + target:add("defines", "HAS_PACKAGE0") + end + if has_package("package1") then + target:add("defines", "HAS_PACKAGE1") + end + if has_package("ns2::package2") then + target:add("defines", "HAS_PACKAGE2") + end + end) end) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index b102f4423..43271c7b0 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -137,8 +137,19 @@ function project._api_has_package(interp, ...) -- only for loading targets local requires = project._memcache():get("requires") if requires then - for _, name in ipairs(table.pack(...)) do - local pkg = requires[name] + for _, packagename in ipairs(table.pack(...)) do + local pkg = requires[packagename] + -- attempt to get package with namespace + if pkg == nil and packagename:find("::", 1, true) then + local parts = packagename:split("::", {plain = true}) + local namespace_pkg = requires[parts[#parts]] + if namespace_pkg and namespace_pkg:namespace() then + local fullname = namespace_pkg:fullname() + if fullname:endswith(packagename) then + pkg = namespace_pkg + end + end + end if pkg and pkg:enabled() then return true end diff --git a/xmake/core/sandbox/modules/has_package.lua b/xmake/core/sandbox/modules/has_package.lua index 8d6c8ec3f..cb5d2822e 100644 --- a/xmake/core/sandbox/modules/has_package.lua +++ b/xmake/core/sandbox/modules/has_package.lua @@ -23,8 +23,19 @@ return function (...) require("sandbox/modules/import/core/sandbox/module").import("core.project.project") local requires = project.required_packages() if requires then - for _, name in ipairs(table.join(...)) do - local pkg = requires[name] + for _, packagename in ipairs(table.join(...)) do + local pkg = requires[packagename] + -- attempt to get package with namespace + if pkg == nil and packagename:find("::", 1, true) then + local parts = packagename:split("::", {plain = true}) + local namespace_pkg = requires[parts[#parts]] + if namespace_pkg and namespace_pkg:namespace() then + local fullname = namespace_pkg:fullname() + if fullname:endswith(packagename) then + pkg = namespace_pkg + end + end + end if pkg and pkg:enabled() then return true end -- cgit v1.3.1