From e81a234fdcb3542403e03e9db0dc2c182eb81e36 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 21 Dec 2024 22:38:14 +0800 Subject: [PATCH 01/44] 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 diff --git a/tests/apis/namespace/basic/.gitignore b/tests/apis/namespace/basic/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..fd52ab01345 --- /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") + From 904e9410660956c149f9bb6dcfc631c2720ac32a Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 21 Dec 2024 22:49:06 +0800 Subject: [PATCH 02/44] add namespace stub --- xmake/core/base/interpreter.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 71250be6d33..95d25685162 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -680,6 +680,8 @@ function interpreter.new() instance:api_register(nil, "add_subdirs", interpreter.api_builtin_add_subdirs) instance:api_register(nil, "add_subfiles", interpreter.api_builtin_add_subfiles) instance:api_register(nil, "set_xmakever", interpreter.api_builtin_set_xmakever) + instance:api_register(nil, "namespace", interpreter.api_builtin_namespace) + instance:api_register(nil, "namespace_end",interpreter.api_builtin_namespace_end) -- register the interpreter interfaces instance:api_register(nil, "interp_save_scope", interpreter.api_interp_save_scope) @@ -1844,6 +1846,14 @@ function interpreter:api_builtin_add_subfiles(...) deprecated.add("includes(%s)", "add_subfiles(%s)", table.concat(files, ", "), table.concat(files, ", ")) end +-- the builtin api: namespace() +function interpreter:api_builtin_namespace(...) +end + +-- the builtin api: namespace_end() +function interpreter:api_builtin_namespace_end(...) +end + -- the interpreter api: interp_save_scope() -- save the current scope function interpreter:api_interp_save_scope() From 32db8fc2f0f2c560ab803acd4c4394466843c0bd Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 22 Dec 2024 22:57:52 +0800 Subject: [PATCH 03/44] 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 diff --git a/tests/apis/namespace/nested/.gitignore b/tests/apis/namespace/nested/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..93d7132f34d --- /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 95d25685162..716ce30041b 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() From 3e07f684154dfd9e7d0c9b14c81320e6e4b203cd Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 22 Dec 2024 23:04:32 +0800 Subject: [PATCH 04/44] update namespace string --- xmake/core/base/interpreter.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 716ce30041b..c8b0873334a 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1854,6 +1854,7 @@ function interpreter:api_builtin_namespace(name, callback) self._NAMESPACE = namespace end table.insert(namespace, name) + self._NAMESPACE_STR = table.concat(namespace, "::") if callback and type(callback) == "function" then callback() self:api_builtin_namespace_end() @@ -1865,6 +1866,7 @@ function interpreter:api_builtin_namespace_end() local namespace = self._NAMESPACE if namespace then table.remove(namespace) + self._NAMESPACE_STR = table.concat(namespace, "::") end end From 4b35267f3de12f93234467e84b08f5c8b7aef281 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 22 Dec 2024 23:11:25 +0800 Subject: [PATCH 05/44] 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 diff --git a/tests/apis/namespace/basic/test.lua b/tests/apis/namespace/basic/test.lua new file mode 100644 index 00000000000..83c0a954648 --- /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 00000000000..83c0a954648 --- /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 c8b0873334a..ccf49f1ec06 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 From 36a07b48ce45afc4f91aa0120aa419eb9b5943d1 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 22:51:24 +0800 Subject: [PATCH 06/44] add root tests --- xmake/core/base/interpreter.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index ccf49f1ec06..4652dfd363c 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -239,11 +239,16 @@ function interpreter:_api_register_xxx_values(scope_kind, action, apifunc, ...) local implementation = function (self, scopes, apiname, ...) -- init root scopes + local namespace = self._NAMESPACE_STR scopes._ROOT = scopes._ROOT or {} -- init current root scope - local root = scopes._ROOT[scope_kind] or {} - scopes._ROOT[scope_kind] = root + local rootkey = scope_kind + if namespace then + rootkey = scope_kind .. "@@" .. namespace .. "::" + end + local root = scopes._ROOT[rootkey] or {} + scopes._ROOT[rootkey] = root -- clear the current scope if be not belong to the current scope kind if scopes._CURRENT and scopes._CURRENT_KIND ~= scope_kind then @@ -926,7 +931,7 @@ end -- { -- scope_kind1 -- { --- "scope_name1" +-- "namespace1::scope_name1" -- { -- -- } @@ -934,7 +939,7 @@ end -- -- scope_kind2 -- { --- "scope_name1" +-- "namespace1::namespace2::scope_name1" -- { -- -- } @@ -955,6 +960,9 @@ function interpreter:api_register_scope(...) local scope_name = scope_args[1] local scope_info = scope_args[2] local namespace = self._NAMESPACE_STR + if scope_name ~= nil and namespace then + scope_name = namespace .. "::" .. scope_name + end -- check invalid scope name, @see https://github.com/xmake-io/xmake/issues/4547 if scope_args.n > 0 and type(scope_name) ~= "string" then @@ -995,6 +1003,8 @@ function interpreter:api_register_scope(...) scopes._ROOT = scopes._ROOT or {} if scope_name ~= nil then scopes._ROOT[scope_kind .. "@@" .. scope_name] = {} + elseif namespace then + scopes._ROOT[scope_kind .. "@@" .. namespace .. "::"] = {} end -- with scope info? translate it @@ -1061,13 +1071,17 @@ end -- { -- scope_kind -- { +-- name1 = {"value3"} +-- } +-- scope_kind@@namespace:: +-- { -- name2 = {"value3"} -- } -- } -- -- scope_kind -- { --- "scope_name" <-- _SCOPES._CURRENT +-- "namespace::scope_name" <-- _SCOPES._CURRENT -- { -- name1 = {"value1"} -- name2 = {"value1", "value2", ...} From d4cbbbb952700a31d17f580a0376b4793de45d7a Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 22:51:28 +0800 Subject: [PATCH 07/44] 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 diff --git a/tests/apis/namespace/root/.gitignore b/tests/apis/namespace/root/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..725fbc6f7d5 --- /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") + From 7d3cfcac2a3ca3e6860d7ba6c9e03850d04dacaa Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 22:54:17 +0800 Subject: [PATCH 08/44] fix namespace end --- xmake/core/base/interpreter.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 4652dfd363c..b579a334984 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -86,7 +86,7 @@ function interpreter._traceback(errors) end -- merge the current root values to the previous scope -function interpreter._merge_root_scope(root, root_prev, override) +function interpreter:_merge_root_scope(root, root_prev, override) -- merge it root_prev = root_prev or {} @@ -118,9 +118,7 @@ end -- fetch the root values to the child values in root scope -- and we will only use the child values if be override mode -function interpreter._fetch_root_scope(root) - - -- fetch it +function interpreter:_fetch_root_scope(root) for scope_kind_and_name, _ in pairs(root or {}) do -- is scope_kind@@scope_name? @@ -551,7 +549,7 @@ function interpreter:_make(scope_kind, deduplicate, enable_filter) if scope_for_kind then -- fetch the root values in root scope first - interpreter._fetch_root_scope(scopes._ROOT) + self:_fetch_root_scope(scopes._ROOT) -- merge results for scope_name, scope in pairs(scope_for_kind) do @@ -1828,12 +1826,12 @@ function interpreter:api_builtin_includes(...) scopes._CURRENT = scope_prev -- fetch the root values in root scopes first - interpreter._fetch_root_scope(scopes._ROOT) + self:_fetch_root_scope(scopes._ROOT) -- restore the previous root scope and merge current root scope -- it will override the previous values if the current values are override mode -- so we priority use the values in subdirs scope - scopes._ROOT = interpreter._merge_root_scope(scopes._ROOT, root_prev, true) + scopes._ROOT = self:_merge_root_scope(scopes._ROOT, root_prev, true) -- get mtime of the file self._PRIVATE._MTIMES[path.relative(file, self._PRIVATE._ROOTDIR)] = os.mtime(file) @@ -1881,7 +1879,11 @@ function interpreter:api_builtin_namespace_end() local namespace = self._NAMESPACE if namespace then table.remove(namespace) + end + if namespace and #namespace > 0 then self._NAMESPACE_STR = table.concat(namespace, "::") + else + self._NAMESPACE_STR = nil end end From d630e9c0ef7819be09caf4b70ec6fbe1f974fbb7 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 23:02:51 +0800 Subject: [PATCH 09/44] fix namespace scope --- xmake/core/base/interpreter.lua | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index b579a334984..d976268c116 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -164,27 +164,15 @@ end -- register scope end: scopename_end() function interpreter:_api_register_scope_end(...) assert(self and self._PUBLIC and self._PRIVATE) - - -- done for _, apiname in ipairs({...}) do - -- check - assert(apiname) - -- register scope api self:api_register(nil, apiname .. "_end", function (self, ...) - - -- check assert(self and self._PRIVATE and apiname) - -- the scopes - local scopes = self._PRIVATE._SCOPES - assert(scopes) - -- enter root scope + local scopes = self._PRIVATE._SCOPES scopes._CURRENT = nil - - -- clear scope kind scopes._CURRENT_KIND = nil end) end @@ -1861,6 +1849,14 @@ end -- the builtin api: namespace() function interpreter:api_builtin_namespace(name, callback) + + -- enter root scope + self:api_interp_save_scope() + local scopes = self._PRIVATE._SCOPES + scopes._CURRENT = nil + scopes._CURRENT_KIND = nil + + -- enter namespace local namespace = self._NAMESPACE if namespace == nil then namespace = {} @@ -1876,6 +1872,7 @@ end -- the builtin api: namespace_end() function interpreter:api_builtin_namespace_end() + assert(self and self._PRIVATE) local namespace = self._NAMESPACE if namespace then table.remove(namespace) @@ -1885,6 +1882,7 @@ function interpreter:api_builtin_namespace_end() else self._NAMESPACE_STR = nil end + self:api_interp_restore_scope() end -- the interpreter api: interp_save_scope() From 7447e9505f48633ea7536fb195c59511d3e93fe1 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 23:46:48 +0800 Subject: [PATCH 10/44] fix root namespace values order --- xmake/core/base/interpreter.lua | 58 +++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index d976268c116..b3c32f280db 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -111,8 +111,6 @@ function interpreter:_merge_root_scope(root, root_prev, override) root_prev[scope_kind_and_name] = scope_values end end - - -- ok? return root_prev end @@ -126,21 +124,55 @@ function interpreter:_fetch_root_scope(root) if #scope_kind_and_name == 2 then local scope_kind = scope_kind_and_name[1] local scope_name = scope_kind_and_name[2] - local scope_values = root[scope_kind .. "@@" .. scope_name] or {} - local scope_root = root[scope_kind] or {} - for name, values in pairs(scope_root) do - if not name:startswith("__override_") then - if scope_root["__override_" .. name] then - if scope_values[name] == nil then - scope_values[name] = values - scope_values["__override_" .. name] = true + + -- we only fetch the root values to the target values, e.g. target@@ns1::ns2::bar" + -- and ignore root namespace values, e.g. target@@ns1::ns2:: + if not scope_name:endswith("::") then + local scope_values = root[scope_kind .. "@@" .. scope_name] or {} + local namespaces = scope_name:split("::", {plain = true}) + table.remove(namespaces) + table.insert(namespaces, 1, "") + + -- add values in global root scope, all namespace root scopes + local namespace + local scope_rootkeys = {} + for idx, namespace_part in ipairs(namespaces) do + local scope_rootkey = scope_kind + if idx ~= 1 then + if not namespace then + namespace = namespace_part + else + namespace = namespace .. "::" .. namespace_part + end + scope_rootkey = scope_kind .. "@@" .. namespace .. "::" + end + table.insert(scope_rootkeys, scope_rootkey) + end + -- we need to add root values in head + -- + -- e.g. + -- add root values to ns1::ns2::bar from target@@ns1::ns2:: + -- add root values to ns1::ns2::bar from target@@ns1:: + -- add root values to ns1::ns2::bar from target + -- + for idx = #scope_rootkeys, 1, -1 do + local scope_rootkey = scope_rootkeys[idx] + local scope_root = root[scope_rootkey] or {} + for name, values in pairs(scope_root) do + if not name:startswith("__override_") then + if scope_root["__override_" .. name] then + if scope_values[name] == nil then + scope_values[name] = values + scope_values["__override_" .. name] = true + end + else + scope_values[name] = table.join(values, scope_values[name] or {}) + end end - else - scope_values[name] = table.join(values, scope_values[name] or {}) end end + root[scope_kind .. "@@" .. scope_name] = scope_values end - root[scope_kind .. "@@" .. scope_name] = scope_values end end end From 4fe6254655ec575fe414d382444d6e6023f9ad70 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 6 Jan 2025 23:48:52 +0800 Subject: [PATCH 11/44] 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 diff --git a/tests/apis/namespace/includes/.gitignore b/tests/apis/namespace/includes/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..1d93e5a9a0e --- /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 00000000000..83c0a954648 --- /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 00000000000..3236277341c --- /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") + From 1aa63e9481937d7c66d33741699bec4bfb7b6fba Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:00:21 +0800 Subject: [PATCH 12/44] fix target path for namespace --- xmake/core/project/target.lua | 62 +++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 1d35476601a..2669875b594 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -55,9 +55,9 @@ local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new a target instance function _instance.new(name, info) local instance = table.inherit(_instance) - instance._NAME = name instance._INFO = info instance._CACHEID = 1 + instance:name_set(name) return instance end @@ -253,9 +253,9 @@ function _instance:_build_deps() self._DEPS = self._DEPS or {} self._ORDERDEPS = self._ORDERDEPS or {} self._INHERITDEPS = self._INHERITDEPS 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()}) -- @see https://github.com/xmake-io/xmake/issues/4689 - instance_deps.load_deps(self, instances, {}, self._INHERITDEPS, {self:name()}, function (t, dep) + instance_deps.load_deps(self, instances, {}, self._INHERITDEPS, {self:fullname()}, function (t, dep) local depinherit = t:extraconf("deps", dep:name(), "inherit") return depinherit == nil or depinherit end) @@ -518,9 +518,9 @@ end -- clone target, @note we can just call it in after_load() function _instance:clone() if not self:_is_loaded() then - os.raise("please call target:clone() in after_load().", self:name()) + os.raise("please call target:clone() in after_load().", self:fullname()) end - local instance = target.new(self:name(), self._INFO:clone()) + local instance = target.new(self:fullname(), self._INFO:clone()) if self._DEPS then instance._DEPS = table.clone(self._DEPS) end @@ -886,7 +886,23 @@ end -- set the target 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 target kind @@ -1371,7 +1387,12 @@ function _instance:objectdir(opt) if not objectdir then objectdir = path.join(config.buildir(), ".objs") end - objectdir = path.join(objectdir, self:name()) + local namespace = self:namespace() + if namespace then + objectdir = path.join(objectdir, (namespace:replace("::", path.sep())), self:name()) + else + objectdir = path.join(objectdir, self:name()) + end -- get root directory of target local intermediate_directory = self:policy("build.intermediate_directory") @@ -1403,7 +1424,12 @@ function _instance:dependir(opt) if not dependir then dependir = path.join(config.buildir(), ".deps") end - dependir = path.join(dependir, self:name()) + local namespace = self:namespace() + if namespace then + dependir = path.join(dependir, (namespace:replace("::", path.sep())), self:name()) + else + dependir = path.join(dependir, self:name()) + end -- get root directory of target local intermediate_directory = self:policy("build.intermediate_directory") @@ -1435,7 +1461,12 @@ function _instance:autogendir(opt) if not autogendir then autogendir = path.join(config.buildir(), ".gens") end - autogendir = path.join(autogendir, self:name()) + local namespace = self:namespace() + if namespace then + autogendir = path.join(autogendir, (namespace:replace("::", path.sep())), self:name()) + else + autogendir = path.join(autogendir, self:name()) + end -- get root directory of target local intermediate_directory = self:policy("build.intermediate_directory") @@ -1533,6 +1564,10 @@ function _instance:targetdir() if mode then targetdir = path.join(targetdir, mode) end + local namespace = self:namespace() + if namespace then + targetdir = path.join(targetdir, (namespace:replace("::", path.sep()))) + end end return targetdir end @@ -1916,7 +1951,8 @@ function _instance:sourcefiles() end if #results == 0 then local sourceinfo = self:sourceinfo("files", file) or {} - utils.warning("%s:%d${clear}: cannot match %s_files(\"%s\") in %s(%s)", sourceinfo.file or "", sourceinfo.line or -1, (removed and "remove" or "add"), file, self:type(), self:name()) + utils.warning("%s:%d${clear}: cannot match %s_files(\"%s\") in %s(%s)", + sourceinfo.file or "", sourceinfo.line or -1, (removed and "remove" or "add"), file, self:type(), self:fullname()) end -- process source files @@ -2478,9 +2514,9 @@ end function _instance:tool(toolkind) -- we cannot get tool in on_load, because target:toolchains() has been not checked in configuration stage. if not self._LOADED_AFTER then - os.raise("we cannot get tool(%s) before target(%s) is loaded, maybe it is called on_load(), please call it in on_config().", toolkind, self:name()) + os.raise("we cannot get tool(%s) before target(%s) is loaded, maybe it is called on_load(), please call it in on_config().", toolkind, self:fullname()) end - return toolchain.tool(self:toolchains(), toolkind, {cachekey = "target_" .. self:name(), plat = self:plat(), arch = self:arch(), + return toolchain.tool(self:toolchains(), toolkind, {cachekey = "target_" .. self:fullname(), plat = self:plat(), arch = self:arch(), before_get = function() -- get program from set_toolset local program = self:get("toolset." .. toolkind) @@ -2517,7 +2553,7 @@ end -- get tool configuration from the toolchains function _instance:toolconfig(name) - return toolchain.toolconfig(self:toolchains(), name, {cachekey = "target_" .. self:name(), plat = self:plat(), arch = self:arch(), + return toolchain.toolconfig(self:toolchains(), name, {cachekey = "target_" .. self:fullname(), plat = self:plat(), arch = self:arch(), after_get = function(toolchain_inst) -- get flags from target.on_xxflags() local script = toolchain_inst:get("target.on_" .. name) From 1ce4271e8b44e6bc3cd7db44be6b52ac538e5176 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:01:19 +0800 Subject: [PATCH 13/44] improve progress info --- xmake/actions/build/kinds/binary.lua | 8 ++++++-- xmake/actions/build/kinds/shared.lua | 8 ++++++-- xmake/actions/build/kinds/static.lua | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/xmake/actions/build/kinds/binary.lua b/xmake/actions/build/kinds/binary.lua index 3acc3bcd9f0..3f67232f77c 100644 --- a/xmake/actions/build/kinds/binary.lua +++ b/xmake/actions/build/kinds/binary.lua @@ -39,9 +39,13 @@ function _do_link_target(target, opt) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - local targetfile = target:targetfile() - progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) + local filename = target:filename() + if target:namespace() then + filename = target:namespace() .. "::" .. filename + end + progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", filename) + local targetfile = target:targetfile() local objectfiles = target:objectfiles() local verbose = option.get("verbose") if verbose then diff --git a/xmake/actions/build/kinds/shared.lua b/xmake/actions/build/kinds/shared.lua index 1b70657664d..cc7aa010cfb 100644 --- a/xmake/actions/build/kinds/shared.lua +++ b/xmake/actions/build/kinds/shared.lua @@ -39,9 +39,13 @@ function _do_link_target(target, opt) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - local targetfile = target:targetfile() - progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) + local filename = target:filename() + if target:namespace() then + filename = target:namespace() .. "::" .. filename + end + progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", filename) + local targetfile = target:targetfile() local objectfiles = target:objectfiles() local verbose = option.get("verbose") if verbose then diff --git a/xmake/actions/build/kinds/static.lua b/xmake/actions/build/kinds/static.lua index 7a116184904..abb6c5f727b 100644 --- a/xmake/actions/build/kinds/static.lua +++ b/xmake/actions/build/kinds/static.lua @@ -39,9 +39,13 @@ function _do_link_target(target, opt) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - local targetfile = target:targetfile() - progress.show(opt.progress, "${color.build.target}archiving.$(mode) %s", path.filename(targetfile)) + local filename = target:filename() + if target:namespace() then + filename = target:namespace() .. "::" .. filename + end + progress.show(opt.progress, "${color.build.target}archiving.$(mode) %s", filename) + local targetfile = target:targetfile() local objectfiles = target:objectfiles() local verbose = option.get("verbose") if verbose then From f6904d729ce7ca99fad237e5e0641ee015b97faf Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:01:52 +0800 Subject: [PATCH 14/44] add namespace info to build object --- xmake/modules/private/action/build/object.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xmake/modules/private/action/build/object.lua b/xmake/modules/private/action/build/object.lua index c4c646cbab5..25bf3385bfc 100644 --- a/xmake/modules/private/action/build/object.lua +++ b/xmake/modules/private/action/build/object.lua @@ -76,7 +76,11 @@ function _do_build_file(target, sourcefile, opt) -- trace progress info if not opt.quiet then - progress.show(opt.progress, "${color.build.object}%scompiling.$(mode) %s", prefix, sourcefile) + local filepath = sourcefile + if target:namespace() then + filepath = target:namespace() .. "::" .. filepath + end + progress.show(opt.progress, "${color.build.object}%scompiling.$(mode) %s", prefix, filepath) end -- trace verbose info From ae8a596c609e01b21caa5825f3fd2eec0bdb7507 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:40:57 +0800 Subject: [PATCH 15/44] 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 diff --git a/tests/apis/namespace/inner/.gitignore b/tests/apis/namespace/inner/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..c18203f2928 --- /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 f4b371de7d8..28a33db78de 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 f5604f0bda4..ad85d8b2a71 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 9f0cc08fd5a..0f69142a54b 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 42133b1428d..3035946ffa9 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 f73126618cc..58c89b302c4 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 17145ac6f52..2c9c1a7e2fc 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 77793d3e50e..f395f5f6b52 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 2669875b594..eed55c0bfa8 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 12b1b77e03c..3d4d5507f97 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 1264ed60c3b..94d6495efc7 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 e566ab160b9..12d6497c0f8 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 From 61812aa80ba743e735368a52aec1c4c5bc4a7f5e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:50:41 +0800 Subject: [PATCH 16/44] 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 diff --git a/tests/apis/namespace/option/.gitignore b/tests/apis/namespace/option/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..10f2d18f5b9 --- /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 a5d3db9bacb..9eb4dfb7183 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 0e4499f57e4..18b9dc363f1 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 f395f5f6b52..a266bf1db6b 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 eed55c0bfa8..03e68629f98 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 bada906d4db..b39fabaf603 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 From 508341ccfe39480a0465de602afc6db09980500c Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:51:07 +0800 Subject: [PATCH 17/44] improve option test --- tests/apis/namespace/option/xmake.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 10f2d18f5b9..1bfbbe3fe43 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") From 55dc4ce3ea49160952bec878188d9a01a4a76f73 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:54:53 +0800 Subject: [PATCH 18/44] 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 diff --git a/tests/apis/namespace/rule/.gitignore b/tests/apis/namespace/rule/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..96b80271d8d --- /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 a266bf1db6b..8afcee6b914 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 75da4da39d2..5d44f7c251f 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 03e68629f98..9d5a9c07a67 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 ae7279e785e..d64a75d62fc 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 0b98a4ed7c4..332e4858d3e 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 From e66776b2980aec6bf3379814aae68882dcfb516e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:57:40 +0800 Subject: [PATCH 19/44] fix cli --- xmake/core/base/cli.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/core/base/cli.lua b/xmake/core/base/cli.lua index 9eb4dfb7183..f9a73759f67 100644 --- a/xmake/core/base/cli.lua +++ b/xmake/core/base/cli.lua @@ -89,10 +89,10 @@ 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) - -- ignore namespace, e.g. `--namespace::opt` + local sep = value:find("[=:]", 3, false) + -- ignore namespace, e.g. `--namespace::opt=` if sep and value:sub(sep, sep + 1) == "::" then - sep = nil + sep = value:find("=", 3, false) end if sep then table.insert(parsed, cli._make_option(value:sub(3, sep - 1), value:sub(sep + 1), false, argv, index)) From bdc9a4cb46d5dd7d46dfa9a0ab2ec29aa9a2e6a8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:59:53 +0800 Subject: [PATCH 20/44] 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 diff --git a/tests/apis/namespace/task/.gitignore b/tests/apis/namespace/task/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..46fa44af3eb --- /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 00000000000..1f9d13e4a6c --- /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 deea228aaf9..1b1093e6855 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 From 27b25397a70585921df496503060e3bf04f0f38b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:13:26 +0800 Subject: [PATCH 21/44] Update task.lua --- xmake/core/base/task.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 1b1093e6855..7258aeb2116 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -481,12 +481,12 @@ function task:name() end -- get the namespace -function _instance:namespace() +function task:namespace() return self._NAMESPACE end -- get the full name -function _instance:fullname() +function task:fullname() local namespace = self:namespace() return namespace and namespace .. "::" .. self:name() or self:name() end @@ -508,8 +508,6 @@ function task:run(...) -- restore the current directory os.cd(curdir) - - -- ok? return ok, errors end From c184146581f86d95536dafcadd64e8ae000341f2 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 22:43:17 +0800 Subject: [PATCH 22/44] 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 diff --git a/tests/apis/namespace/toolchain/.gitignore b/tests/apis/namespace/toolchain/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..588541296c3 --- /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 8afcee6b914..72bec34d5d6 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 9d5a9c07a67..3f187585462 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 29bd5a09f2a..a19413d345e 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") From e5ea7bc6347ce4c4a62468138753cedea663bfa9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 22:45:32 +0800 Subject: [PATCH 23/44] 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 diff --git a/tests/apis/namespace/package/.gitignore b/tests/apis/namespace/package/.gitignore new file mode 100644 index 00000000000..15210576129 --- /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 00000000000..2c00cc6b62f --- /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 00000000000..47d8a7251e3 --- /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 00000000000..1a1fb3425c5 --- /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 00000000000..d2506bca987 --- /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 00000000000..4cf7b5e624a --- /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 00000000000..83c0a954648 --- /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 00000000000..b2bc61d73eb --- /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 068e95ab678..d3032f15bf5 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 30c571592e7..a06ee7a299d 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 From 9021afe5a0fd56abc7683409c921fd60de786588 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 22:50:03 +0800 Subject: [PATCH 24/44] support namespace for xpack --- xmake/core/base/interpreter.lua | 2 -- xmake/plugins/pack/xpack.lua | 26 ++++++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index b3c32f280db..5ae08de3364 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -87,8 +87,6 @@ end -- merge the current root values to the previous scope function interpreter:_merge_root_scope(root, root_prev, override) - - -- merge it root_prev = root_prev or {} for scope_kind_and_name, _ in pairs(root or {}) do -- only merge sub-scope for each kind("target@@xxxx") or __rootkind diff --git a/xmake/plugins/pack/xpack.lua b/xmake/plugins/pack/xpack.lua index 203fcbe6dc9..5cd3431a59f 100644 --- a/xmake/plugins/pack/xpack.lua +++ b/xmake/plugins/pack/xpack.lua @@ -32,13 +32,24 @@ import("filter") import("xpack_component") -- define module -local xpack = xpack or object {_init = {"_name", "_info"}} +local xpack = xpack or object {_init = {"_name", "_info", "_namespace"}} -- get name function xpack:name() return self._name end +-- get namespace +function xpack:namespace() + return self._namespace +end + +-- get fullname +function xpack:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- get values function xpack:get(name) if self._info then @@ -131,7 +142,7 @@ function xpack:targets() local targetnames = self:get("targets") if targetnames then for _, name in ipairs(targetnames) do - local target = project.target(name) + local target = project.target(name, {namespace = self:namespace()}) if target then table.insert(targets, target) else @@ -148,7 +159,7 @@ end function xpack:target(name) local targetnames = self:get("targets") if targetnames and table.contains(table.wrap(targetnames), name) then - return project.target(name) + return project.target(name, {namespace = self:namespace()}) end end @@ -506,7 +517,14 @@ end -- new a xpack, and we need to clone scope info, -- because two different format packages maybe have same scope function _new(name, info) - return xpack {name, info:clone()} + local parts = name:split("::", {plain = true}) + name = parts[#parts] + table.remove(parts) + local namespace + if #parts > 0 then + namespace = table.concat(parts, "::") + end + return xpack {name, info:clone(), namespace} end -- get xpack packages From bd6ded54efbea8fe01040941ce8d39951a8431c9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 23:19:03 +0800 Subject: [PATCH 25/44] 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(-) diff --git a/tests/apis/namespace/package/test.lua b/tests/apis/namespace/package/test.lua index 83c0a954648..bff75b06637 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 b2bc61d73eb..9f01f4bdba4 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 5ae08de3364..2ae021f79be 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 d3032f15bf5..05be6af8cb6 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 a06ee7a299d..30c571592e7 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 From 545c0558aff2883735ae6dddbf831567d29d21f1 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 23:34:46 +0800 Subject: [PATCH 26/44] merge root scope for namespace --- xmake/core/base/interpreter.lua | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 2ae021f79be..6f006efff43 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 and scope_kind ~= "__rootkind" then + if namespace then rootkey = scope_kind .. "@@" .. namespace .. "::" end local root = scopes._ROOT[rootkey] or {} @@ -543,7 +543,6 @@ 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 results = self:_handle(root_scope, deduplicate, enable_filter) @@ -552,8 +551,21 @@ function interpreter:_make(scope_kind, deduplicate, enable_filter) -- get the root scope info without scope kind elseif scope_kind == "root" or scope_kind == nil then - - local root_scope = scopes._ROOT["__rootkind"] + 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 + end + end + empty = false + end + end if root_scope then results = self:_handle(root_scope, deduplicate, enable_filter) end From 76fd3aba7f38972943f3ad60471de04000e68c5a Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 23:37:28 +0800 Subject: [PATCH 27/44] 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(-) diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index 9f01f4bdba4..eac4ab53093 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 6f006efff43..7f43ca3408c 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 72bec34d5d6..b92872d6431 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 From 154345c0c6f52a72418b1d545215ec0693794b73 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 00:05:44 +0800 Subject: [PATCH 28/44] improve comments --- xmake/core/project/project.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index b92872d6431..6bf13824399 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -785,7 +785,10 @@ 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") +-- +-- get root values in project, e.g project.get("name") +-- get root values in target, e.g. project.get("target.name") +-- get root values in specific 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 From 14fff0b1c97238ec498c1a237862d4fe943b8d6c Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 00:53:30 +0800 Subject: [PATCH 29/44] get namespaces in project --- xmake/core/base/interpreter.lua | 30 ++++++++++++++----- xmake/core/project/project.lua | 5 ++++ .../modules/import/core/project/project.lua | 1 + 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 7f43ca3408c..f09f544fc67 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -27,6 +27,7 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") +local hashset = require("base/hashset") local scopeinfo = require("base/scopeinfo") local deprecated = require("base/deprecated") local sandbox = require("sandbox/sandbox") @@ -255,7 +256,7 @@ function interpreter:_api_register_xxx_values(scope_kind, action, apifunc, ...) local implementation = function (self, scopes, apiname, ...) -- init root scopes - local namespace = self._NAMESPACE_STR + local namespace = self._PRIVATE._NAMESPACE_STR scopes._ROOT = scopes._ROOT or {} -- init current root scope @@ -819,6 +820,12 @@ function interpreter:mtimes() return self._PRIVATE._MTIMES end +-- get namespaces +function interpreter:namespaces() + local namespaces = self._PRIVATE._NAMESPACES + return namespaces and namespaces:to_array() +end + -- get filter function interpreter:filter() assert(self and self._PRIVATE) @@ -1006,7 +1013,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 + local namespace = self._PRIVATE._NAMESPACE_STR if scope_name ~= nil and namespace then scope_name = namespace .. "::" .. scope_name end @@ -1918,13 +1925,20 @@ function interpreter:api_builtin_namespace(name, callback) scopes._CURRENT_KIND = nil -- enter namespace - local namespace = self._NAMESPACE + local namespace = self._PRIVATE._NAMESPACE if namespace == nil then namespace = {} - self._NAMESPACE = namespace + self._PRIVATE._NAMESPACE = namespace end table.insert(namespace, name) - self._NAMESPACE_STR = table.concat(namespace, "::") + self._PRIVATE._NAMESPACE_STR = table.concat(namespace, "::") + -- save namespaces + local namespaces = self._PRIVATE._NAMESPACES + if namespaces == nil then + namespaces = hashset.new() + self._PRIVATE._NAMESPACES = namespaces + end + namespaces:insert(self._PRIVATE._NAMESPACE_STR) if callback and type(callback) == "function" then callback() self:api_builtin_namespace_end() @@ -1934,14 +1948,14 @@ end -- the builtin api: namespace_end() function interpreter:api_builtin_namespace_end() assert(self and self._PRIVATE) - local namespace = self._NAMESPACE + local namespace = self._PRIVATE._NAMESPACE if namespace then table.remove(namespace) end if namespace and #namespace > 0 then - self._NAMESPACE_STR = table.concat(namespace, "::") + self._PRIVATE._NAMESPACE_STR = table.concat(namespace, "::") else - self._NAMESPACE_STR = nil + self._PRIVATE._NAMESPACE_STR = nil end self:api_interp_restore_scope() end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 6bf13824399..c283e1e3686 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -828,6 +828,11 @@ function project.version() return project.get("target.version") end +-- get the project namespaces +function project.namespaces() + return project.interpreter():namespaces() +end + -- init default policies -- @see https://github.com/xmake-io/xmake/issues/5527 function project._init_default_policies() diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index b39fabaf603..61b538bb1b0 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -71,6 +71,7 @@ sandbox_core_project.tmpdir = project.tmpdir sandbox_core_project.tmpfile = project.tmpfile sandbox_core_project.is_loaded = project.is_loaded sandbox_core_project.apis = project.apis +sandbox_core_project.namespaces = project.namespaces -- check project options function sandbox_core_project.check_options() From 4ad00835a9070c32b6ce17248e5702ef0eae5a37 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 00:59:28 +0800 Subject: [PATCH 30/44] fix package error --- xmake/core/package/package.lua | 11 +++++++- xmake/core/project/project.lua | 27 +++++++++++++++++++ .../private/action/require/impl/package.lua | 6 +++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 05be6af8cb6..be736d2f0e7 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -3001,7 +3001,16 @@ function package.load_from_project(packagename, project) -- get package info local packageinfo = packages[packagename] - if not packageinfo then + if packageinfo == nil and project.namespaces() then + for _, namespace in ipairs(project.namespaces()) do + packageinfo = packages[namespace .. "::" .. packagename] + if packageinfo then + packagename = namespace .. "::" .. packagename + break + end + end + end + if packageinfo == nil then return end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index c283e1e3686..7a24a298ad5 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -1030,11 +1030,38 @@ function project.requires_str() -- get raw requires requires_str, requires_extra = project.get("requires"), project.get("__extra_requires") + local namespaces = project.namespaces() + if namespaces then + for _, namespace in ipairs(namespaces) do + local ns_requires_str, ns_requires_extra = project.get(namespace .. "::requires"), project.get(namespace .. "::__extra_requires") + if ns_requires_str then + requires_str = table.wrap(requires_str) + table.insert(requires_str, ns_requires_str) + end + if ns_requires_extra then + requires_extra = table.wrap(requires_extra) + table.join2(requires_extra, ns_requires_extra) + end + end + end project._memcache():set("requires_str", requires_str or false) project._memcache():set("requires_extra", requires_extra) -- get raw requireconfs local requireconfs_str, requireconfs_extra = project.get("requireconfs"), project.get("__extra_requireconfs") + if namespaces then + for _, namespace in ipairs(project.namespaces()) do + local ns_requireconfs_str, ns_requireconfs_extra = project.get(namespace .. "::requireconfs"), project.get(namespace .. "::__extra_requireconfs") + if ns_requireconfs_str then + requireconfs_str = table.wrap(requireconfs_str) + table.insert(requireconfs_str, ns_requireconfs_str) + end + if ns_requireconfs_extra then + requireconfs_extra = table.wrap(requireconfs_extra) + table.join2(requireconfs_extra, ns_requireconfs_extra) + end + end + end project._memcache():set("requireconfs_str", requireconfs_str or false) project._memcache():set("requireconfs_extra", requireconfs_extra) end diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 30c571592e7..840814040ad 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -961,6 +961,12 @@ function _load_package(packagename, requireinfo, opt) local package if os.isfile(os.projectfile()) then package = _load_package_from_project(packagename) + if package and package:namespace() then + packagename = package:namespace() .. "::" .. packagename + if displayname then + displayname = package:namespace() .. "::" .. displayname + end + end end -- load package from repositories From 7dec01ba2840236fc975d9213842108ed13e858d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 23:28:02 +0800 Subject: [PATCH 31/44] get packages with namespace --- xmake/core/project/package.lua | 13 ++++++++++++- xmake/core/project/project.lua | 2 +- xmake/core/project/target.lua | 11 +++++++++++ .../action/require/impl/register_packages.lua | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/xmake/core/project/package.lua b/xmake/core/project/package.lua index 70f89a1f878..67000bdc61c 100644 --- a/xmake/core/project/package.lua +++ b/xmake/core/project/package.lua @@ -69,6 +69,12 @@ function _instance:name() return self._NAME end +-- get the full name +function _instance:fullname() + local namespace = self:namespace() + return namespace and namespace .. "::" .. self:name() or self:name() +end + -- get the package version function _instance:version() @@ -107,6 +113,11 @@ function _instance:requirestr() return self:get("__requirestr") end +-- get the namespace +function _instance:namespace() + return self:get("__namespace") +end + -- get the require configuration from the given name -- -- e.g. @@ -369,7 +380,7 @@ end -- we need to sort package set keys by this string -- @see https://github.com/xmake-io/xmake/pull/2971#issuecomment-1290052169 function _instance:__tostring() - return "" + return "" end -- get cache diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 7a24a298ad5..a41ecc0cfcd 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -524,7 +524,7 @@ function project._load_requires() end -- add require info - requires[alias or packagename] = instance + requires[name] = instance end return requires end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 3f187585462..a8796076cac 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -1339,6 +1339,17 @@ function _instance:orderpkgs(opt) if requires then for _, packagename in ipairs(table.wrap(self:get("packages", opt))) 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 table.insert(packages, pkg) end diff --git a/xmake/modules/private/action/require/impl/register_packages.lua b/xmake/modules/private/action/require/impl/register_packages.lua index ed6079f29e9..5f428edf3c7 100644 --- a/xmake/modules/private/action/require/impl/register_packages.lua +++ b/xmake/modules/private/action/require/impl/register_packages.lua @@ -66,6 +66,11 @@ function _register_required_package_libs(instance, required_package, is_deps) required_package:set("__components_orderlist", instance:components_orderlist()) end + -- save namespace + if instance:namespace() then + required_package:set("__namespace", instance:namespace()) + end + -- merge into the components values local required_components = required_package:get("components") if required_components then From 6ef0f9be8bb89a2c8cabd6d950ac104fbf3c88d5 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 23:30:16 +0800 Subject: [PATCH 32/44] improve package name --- xmake/core/package/package.lua | 24 ++++++++++++------------ xmake/plugins/show/info/target.lua | 6 +++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index be736d2f0e7..502a0b0f839 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -90,7 +90,7 @@ function _instance:_memcache() return cache end --- get the package name +-- get the package name without namespace function _instance:name() return self._NAME end @@ -100,12 +100,22 @@ function _instance:namespace() return self._NAMESPACE end --- get the full name +-- get the full name (with namespace) function _instance:fullname() local namespace = self:namespace() return namespace and namespace .. "::" .. self:name() or self:name() end +-- get the display name (with namespace and ~label) +function _instance:displayname() + return self._DISPLAYNAME +end + +-- set the display name +function _instance:displayname_set(fullname) + self._DISPLAYNAME = displayname +end + -- get the type: package function _instance:type() return "package" @@ -1547,16 +1557,6 @@ function _instance:label() return requireinfo and requireinfo.label end --- get the display name -function _instance:displayname() - return self._DISPLAYNAME -end - --- set the display name -function _instance:displayname_set(displayname) - self._DISPLAYNAME = displayname -end - -- invalidate configs function _instance:_invalidate_configs() self._CONFIGS = nil diff --git a/xmake/plugins/show/info/target.lua b/xmake/plugins/show/info/target.lua index e3aa8829d92..871ed321a7c 100644 --- a/xmake/plugins/show/info/target.lua +++ b/xmake/plugins/show/info/target.lua @@ -78,11 +78,11 @@ function _get_values_from_pkgs(target, name) local info = components[component_name] if info then for _, value in ipairs(info[name]) do - values[value] = string.format(" -> package(%s)", pkg:name()) + values[value] = string.format(" -> package(%s)", pkg:fullname()) end else local components_str = table.concat(table.wrap(configinfo.components), ", ") - utils.warning("unknown component(%s) in add_packages(%s, {components = {%s}})", component_name, pkg:name(), components_str) + utils.warning("unknown component(%s) in add_packages(%s, {components = {%s}})", component_name, pkg:fullname(), components_str) end end end @@ -95,7 +95,7 @@ function _get_values_from_pkgs(target, name) else -- get values from the builtin package configs for _, value in ipairs(pkg:get(name)) do - values[value] = string.format(" -> package(%s)", pkg:name()) + values[value] = string.format(" -> package(%s)", pkg:fullname()) end end end From cd44af7b2e7744a2d04c30422118f80a5e0ff68d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 23:31:02 +0800 Subject: [PATCH 33/44] update test --- tests/apis/namespace/package/xmake.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index eac4ab53093..9f01f4bdba4 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") From 34a6d0e15e298c6ed7340013b7570a0c97058c80 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 8 Jan 2025 23:31:24 +0800 Subject: [PATCH 34/44] fix display name --- xmake/core/package/package.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 502a0b0f839..bb12282e1e9 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -112,7 +112,7 @@ function _instance:displayname() end -- set the display name -function _instance:displayname_set(fullname) +function _instance:displayname_set(displayname) self._DISPLAYNAME = displayname end From 26921bb868f343d82c08fcc3f4decccec5b89c45 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:40:54 +0800 Subject: [PATCH 35/44] use tbox namespace --- core/src/xmake/xmake.lua | 7 ++++++- core/xmake.lua | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/xmake/xmake.lua b/core/src/xmake/xmake.lua index f5f2d9699c9..50438de2093 100644 --- a/core/src/xmake/xmake.lua +++ b/core/src/xmake/xmake.lua @@ -2,7 +2,12 @@ target("xmake") set_kind("static") -- add deps - add_deps("sv", "lz4", "tbox") + add_deps("sv", "lz4") + if false then--namespace then + add_deps("tbox::tbox") + else + add_deps("tbox") + end if is_config("runtime", "luajit") then add_deps("luajit") else diff --git a/core/xmake.lua b/core/xmake.lua index 0d282dcdfd3..f27461f548d 100644 --- a/core/xmake.lua +++ b/core/xmake.lua @@ -131,7 +131,14 @@ if is_plat("windows") then end -- add projects -includes("src/sv", "src/lz4", "src/tbox", "src/xmake", "src/cli") +includes("src/sv", "src/lz4", "src/xmake", "src/cli") +if false then --namespace then + namespace("tbox", function () + includes("src/tbox") + end) +else + includes("src/tbox") +end if has_config("lua_cjson") then includes("src/lua-cjson") end From 53f150785e89570e481a50be252df2ab300b6e52 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:41:31 +0800 Subject: [PATCH 36/44] add has_config test --- tests/apis/namespace/option/xmake.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 1bfbbe3fe43..7043caf9dd7 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) From 672112971ac26e4ed5bc5789938a82e8dbc22c86 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:42:04 +0800 Subject: [PATCH 37/44] improve has_config test --- tests/apis/namespace/option/xmake.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/apis/namespace/option/xmake.lua b/tests/apis/namespace/option/xmake.lua index 7043caf9dd7..b39b764f1e1 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) From a74774a809de30e6e473157233aa47bbabb272d4 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:44:44 +0800 Subject: [PATCH 38/44] improve has_config --- xmake/core/base/interpreter.lua | 5 +++++ xmake/core/project/config.lua | 15 +++++++++++---- xmake/core/project/project.lua | 2 +- xmake/core/sandbox/modules/has_config.lua | 6 +++++- .../sandbox/modules/import/core/base/process.lua | 4 ++-- xmake/core/sandbox/sandbox.lua | 11 ----------- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index f09f544fc67..72886e0d2e9 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -820,6 +820,11 @@ function interpreter:mtimes() return self._PRIVATE._MTIMES end +-- get current namespace +function interpreter:namespace() + return self._PRIVATE._NAMESPACE_STR +end + -- get namespaces function interpreter:namespaces() local namespaces = self._PRIVATE._NAMESPACES diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index 650bbc4a6a4..d4d510c0948 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -297,10 +297,17 @@ function config.is_value(name, ...) end -- has the given configs? -function config.has(...) - for _, name in ipairs(table.pack(...)) do - if name and type(name) == "string" and config.get(name) then - return true +function config.has(names, opt) + opt = opt or {} + for _, name in ipairs(names) do + if name and type(name) == "string" then + local value = config.get(name) + if value == nil and opt.namespace then + value = config.get(opt.namespace .. "::" .. name) + end + if value then + return true + end end end return false diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index a41ecc0cfcd..61c1361dbd6 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -113,7 +113,7 @@ end -- some configs are enabled? function project._api_has_config(interp, ...) - return config.has(...) + return config.has(table.pack(...), {namespace = interp:namespace()}) end -- some packages are enabled? diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua index c6b68fc9e4c..1dc36278acd 100644 --- a/xmake/core/sandbox/modules/has_config.lua +++ b/xmake/core/sandbox/modules/has_config.lua @@ -19,5 +19,9 @@ -- -- return module -return require("project/config").has +local config = require("project/config") + +return function (...) + return config.has(table.pack(...)) +end diff --git a/xmake/core/sandbox/modules/import/core/base/process.lua b/xmake/core/sandbox/modules/import/core/base/process.lua index 884ae9ebeb1..531ae0bd6f1 100644 --- a/xmake/core/sandbox/modules/import/core/base/process.lua +++ b/xmake/core/sandbox/modules/import/core/base/process.lua @@ -25,8 +25,8 @@ local raise = require("sandbox/modules/raise") local vformat = require("sandbox/modules/vformat") -- define module -local sandbox_core_base_process = sandbox_core_base_process or {} -local sandbox_core_base_instance = sandbox_core_base_instance or {} +local sandbox_core_base_process = sandbox_core_base_process or {} +local sandbox_core_base_instance = sandbox_core_base_instance or {} sandbox_core_base_process._subprocess = sandbox_core_base_process._subprocess or process._subprocess -- wait subprocess diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua index 1e0817a4eaf..9bad3eb9f10 100644 --- a/xmake/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/sandbox.lua @@ -79,12 +79,8 @@ function sandbox._traceback(errors) else results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline) end - - -- next level = level + 1 end - - -- ok? return results end @@ -118,20 +114,13 @@ end -- new a sandbox instance with the given script function sandbox.new(script, filter, rootdir) - - -- check assert(script) -- new instance local self = sandbox._new() - - -- check assert(self and self._PUBLIC and self._PRIVATE) - -- save filter self._PRIVATE._FILTER = filter - - -- save root directory self._PRIVATE._ROOTDIR = rootdir -- invalid script? From ed685ecd74c5267781713f3bd00ce4e4e40b27d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:50:52 +0800 Subject: [PATCH 39/44] add namespace to sandbox --- xmake/core/base/interpreter.lua | 2 +- xmake/core/base/task.lua | 9 ++------ xmake/core/package/package.lua | 2 +- xmake/core/project/project.lua | 12 +++++++++-- xmake/core/sandbox/modules/has_config.lua | 8 +++++++- xmake/core/sandbox/sandbox.lua | 25 +++++++++++------------ 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 72886e0d2e9..4e5aaf8c798 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -647,7 +647,7 @@ function interpreter:_script(script) end -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, self:filter(), self:scriptdir()) + local instance, errors = sandbox.new(script, {filter = self:filter(), rootdir = self:scriptdir(), namespace = self:namespace()}) if not instance then return nil, errors end diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 7258aeb2116..a3fde191383 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -221,17 +221,12 @@ end -- bind script with a sandbox instance function task._bind_script(interp, script) - - -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, interp:filter(), interp:rootdir()) + local instance, errors = sandbox.new(script, { + filter = interp:filter(), rootdir = interp:rootdir(), namespace = interp:namespace()}) if not instance then return nil, errors end - - -- check assert(instance:script()) - - -- update option script return instance:script() end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index bb12282e1e9..741ca318c71 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2945,7 +2945,7 @@ function package.load_from_system(packagename) end -- make sandbox instance with the given script - instance, errors = sandbox.new(on_install, interp:filter()) + instance, errors = sandbox.new(on_install, {filter = interp:filter(), namespace = interp:namespace()}) if not instance then return nil, errors end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 61c1361dbd6..ee2b55a6360 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -979,8 +979,16 @@ function project.ordertargets() end -- get the given option -function project.option(name) - return project.options()[name] +function project.option(name, opt) + opt = opt or {} + local options = project.options() + if options then + local o = options[name] + if not o and opt.namespace then + o = options[opt.namespace .. "::" .. name] + end + return o + end end -- get options diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua index 1dc36278acd..2cf5a86d03b 100644 --- a/xmake/core/sandbox/modules/has_config.lua +++ b/xmake/core/sandbox/modules/has_config.lua @@ -20,8 +20,14 @@ -- return module local config = require("project/config") +local sandbox = require("sandbox/sandbox") return function (...) - return config.has(table.pack(...)) + local namespace + local instance = sandbox.instance() + if instance then + namespace = instance:namespace() + end + return config.has(table.pack(...), {namespace = namespace}) end diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua index 9bad3eb9f10..2d624da9883 100644 --- a/xmake/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/sandbox.lua @@ -107,21 +107,20 @@ function sandbox._new() -- bind instance to the public script envirnoment instance:bind(instance._PUBLIC) - - -- ok? return instance end -- new a sandbox instance with the given script -function sandbox.new(script, filter, rootdir) - assert(script) +function sandbox.new(script, opt) + opt = opt or {} -- new instance local self = sandbox._new() assert(self and self._PUBLIC and self._PRIVATE) - self._PRIVATE._FILTER = filter - self._PRIVATE._ROOTDIR = rootdir + self._PRIVATE._FILTER = opt.filter + self._PRIVATE._ROOTDIR = opt.rootdir + self._PRIVATE._NAMESPACE = opt.namespace -- invalid script? if type(script) ~= "function" then @@ -178,23 +177,17 @@ function sandbox:fork(script, rootdir) -- init a new sandbox instance local instance = sandbox._new() - - -- check assert(instance and instance._PUBLIC and instance._PRIVATE) - -- inherit the filter instance._PRIVATE._FILTER = self:filter() - - -- inherit the root directory instance._PRIVATE._ROOTDIR = rootdir or self:rootdir() + instance._PRIVATE._NAMESPACE = self:namespace() -- bind public scope if script then setfenv(script, instance._PUBLIC) instance._PRIVATE._SCRIPT = script end - - -- ok? return instance end @@ -246,6 +239,12 @@ function sandbox:rootdir() return self._PRIVATE._ROOTDIR end +-- get current namespace +function sandbox:namespace() + assert(self and self._PRIVATE) + return self._PRIVATE._NAMESPACE +end + -- get current instance in the sandbox modules function sandbox.instance(script) From 94918773a3a33398b2ee0dbf23c00199c164a50c Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:51:57 +0800 Subject: [PATCH 40/44] remove config.has --- xmake/core/project/config.lua | 17 ----------------- xmake/core/project/project.lua | 13 ++++++++++++- xmake/core/sandbox/modules/has_config.lua | 12 +++++++++++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index d4d510c0948..789eada0949 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -296,23 +296,6 @@ function config.is_value(name, ...) return false end --- has the given configs? -function config.has(names, opt) - opt = opt or {} - for _, name in ipairs(names) do - if name and type(name) == "string" then - local value = config.get(name) - if value == nil and opt.namespace then - value = config.get(opt.namespace .. "::" .. name) - end - if value then - return true - end - end - end - return false -end - -- dump the configure function config.dump() if not option.get("quiet") then diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index ee2b55a6360..2fac8f50bb2 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -113,7 +113,18 @@ end -- some configs are enabled? function project._api_has_config(interp, ...) - return config.has(table.pack(...), {namespace = interp:namespace()}) + local names = table.pack(...) + local namespace = interp:namespace() + for _, name in ipairs(names) do + local value = config.get(name) + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + if value then + return true + end + end + return false end -- some packages are enabled? diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua index 2cf5a86d03b..e1538a50456 100644 --- a/xmake/core/sandbox/modules/has_config.lua +++ b/xmake/core/sandbox/modules/has_config.lua @@ -28,6 +28,16 @@ return function (...) if instance then namespace = instance:namespace() end - return config.has(table.pack(...), {namespace = namespace}) + local names = table.pack(...) + for _, name in ipairs(names) do + local value = config.get(name) + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + if value then + return true + end + end + return false end From 279b1f07740e235a0a5fddcf46937b829fd160d2 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:52:22 +0800 Subject: [PATCH 41/44] improve to get config --- xmake/core/project/project.lua | 7 ++++++- xmake/core/sandbox/modules/get_config.lua | 17 +++++++++++++++-- xmake/core/sandbox/modules/has_config.lua | 1 - 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 2fac8f50bb2..57655ee0e53 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -143,7 +143,12 @@ end -- get config from the given name function project._api_get_config(interp, name) - return config.get(name) + local value = config.get(name) + local namespace = interp:namespace() + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + return value end -- add module directories diff --git a/xmake/core/sandbox/modules/get_config.lua b/xmake/core/sandbox/modules/get_config.lua index 7fbd16a589c..fd09c02e3a3 100644 --- a/xmake/core/sandbox/modules/get_config.lua +++ b/xmake/core/sandbox/modules/get_config.lua @@ -18,6 +18,19 @@ -- @file get_config.lua -- --- return module -return require("project/config").get +local config = require("project/config") +local sandbox = require("sandbox/sandbox") + +return function (name) + local namespace + local instance = sandbox.instance() + if instance then + namespace = instance:namespace() + end + local value = config.get(name) + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + return value +end diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua index e1538a50456..4681447542b 100644 --- a/xmake/core/sandbox/modules/has_config.lua +++ b/xmake/core/sandbox/modules/has_config.lua @@ -18,7 +18,6 @@ -- @file has_config.lua -- --- return module local config = require("project/config") local sandbox = require("sandbox/sandbox") From 7a4c72e8385d63e1588f8a1cd8b482cf8825d5fb Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:54:55 +0800 Subject: [PATCH 42/44] improve is_config --- xmake/core/project/config.lua | 35 ++++++++++++------------ xmake/core/project/project.lua | 7 ++++- xmake/core/sandbox/modules/is_config.lua | 17 ++++++++++-- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index 789eada0949..3e53fe3884f 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -52,6 +52,23 @@ function config._use_workingdir() return use_workingdir end +-- the current config is belong to the given config values? +function config._is_value(value, ...) + if value == nil then + return false + end + + value = tostring(value) + for _, v in ipairs(table.pack(...)) do + -- escape '-' + v = tostring(v) + if value == v or value:find("^" .. v:gsub("%-", "%%-") .. "$") then + return true + end + end + return false +end + -- get the current given configuration function config.get(name) local value = nil @@ -278,24 +295,6 @@ function config.is_cross() return is_cross(config.plat(), config.arch()) end --- the current config is belong to the given config values? -function config.is_value(name, ...) - local value = config.get(name) - if value == nil then - return false - end - - value = tostring(value) - for _, v in ipairs(table.pack(...)) do - -- escape '-' - v = tostring(v) - if value == v or value:find("^" .. v:gsub("%-", "%%-") .. "$") then - return true - end - end - return false -end - -- dump the configure function config.dump() if not option.get("quiet") then diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 57655ee0e53..b102f442391 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -108,7 +108,12 @@ end -- the current config is belong to the given config values? function project._api_is_config(interp, name, ...) - return config.is_value(name, ...) + local value = config.get(name) + local namespace = interp:namespace() + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + return config._is_value(value, ...) end -- some configs are enabled? diff --git a/xmake/core/sandbox/modules/is_config.lua b/xmake/core/sandbox/modules/is_config.lua index 017f707f4ef..9cc4ec18b83 100644 --- a/xmake/core/sandbox/modules/is_config.lua +++ b/xmake/core/sandbox/modules/is_config.lua @@ -18,6 +18,19 @@ -- @file is_config.lua -- --- return module -return require("project/config").is_value +local config = require("project/config") +local sandbox = require("sandbox/sandbox") + +return function (name, ...) + local namespace + local instance = sandbox.instance() + if instance then + namespace = instance:namespace() + end + local value = config.get(name) + if value == nil and namespace then + value = config.get(namespace .. "::" .. name) + end + return config._is_value(value, ...) +end From 9c0f92f7cfcabe17fbb56c2f6bc1ea54a7223f82 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:56:25 +0800 Subject: [PATCH 43/44] 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(-) diff --git a/tests/apis/namespace/package/xmake.lua b/tests/apis/namespace/package/xmake.lua index 9f01f4bdba4..c880380718c 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 b102f442391..43271c7b01e 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 8d6c8ec3fb4..cb5d2822ee6 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 From 6317ffd0fe312360eb5f6e079320c01c95870ad0 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:57:17 +0800 Subject: [PATCH 44/44] fix config --- xmake/core/project/config.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index 3e53fe3884f..35ccd91450c 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -277,17 +277,17 @@ end -- the current mode is belong to the given modes? function config.is_mode(...) - return config.is_value("mode", ...) + return config._is_value(config.get("mode"), ...) end -- the current platform is belong to the given platforms? function config.is_plat(...) - return config.is_value("plat", ...) + return config._is_value(config.get("plat"), ...) end -- the current architecture is belong to the given architectures? function config.is_arch(...) - return config.is_value("arch", ...) + return config._is_value(config.get("arch"), ...) end -- is cross-compilation?