summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-12-22 22:57:52 +0800
committerruki <[email protected]>2025-01-06 18:12:52 +0800
commit32db8fc2f0f2c560ab803acd4c4394466843c0bd (patch)
treeaf0715559a42fee42530bec5786e32b1f78601ca
parent904e9410660956c149f9bb6dcfc631c2720ac32a (diff)
add nested namespace tests
-rw-r--r--tests/apis/namespace/nested/.gitignore8
-rw-r--r--tests/apis/namespace/nested/src/bar.cpp5
-rw-r--r--tests/apis/namespace/nested/src/bar.h9
-rw-r--r--tests/apis/namespace/nested/src/foo.cpp5
-rw-r--r--tests/apis/namespace/nested/src/foo.h9
-rw-r--r--tests/apis/namespace/nested/src/main.cpp9
-rw-r--r--tests/apis/namespace/nested/xmake.lua19
-rw-r--r--xmake/core/base/interpreter.lua18
8 files changed, 80 insertions, 2 deletions
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 <iostream>
+
+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()