diff options
| author | ruki <[email protected]> | 2025-01-09 00:56:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-01-09 00:56:25 +0800 |
| commit | 9c0f92f7cfcabe17fbb56c2f6bc1ea54a7223f82 (patch) | |
| tree | 555f80b76b9007e7dcbba42c405a7376abbd5545 | |
| parent | 7a4c72e8385d63e1588f8a1cd8b482cf8825d5fb (diff) | |
improve has_package
| -rw-r--r-- | tests/apis/namespace/package/xmake.lua | 17 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 15 | ||||
| -rw-r--r-- | 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 9f01f4bdb..c88038071 100644 --- a/tests/apis/namespace/package/xmake.lua +++ b/tests/apis/namespace/package/xmake.lua @@ -21,6 +21,9 @@ namespace("ns1", function () set_kind("static") add_files("src/foo.cpp") add_packages("package1") + if has_package("package1") then + add_defines("HAS_PACKAGE1") + end namespace("ns2", function() @@ -36,6 +39,9 @@ namespace("ns1", function () set_kind("static") add_files("src/bar.cpp") add_packages("package2") + if has_package("package2") then + add_defines("HAS_PACKAGE2") + end end) target("test") @@ -43,5 +49,16 @@ namespace("ns1", function () add_deps("foo", "ns2::bar") add_files("src/main.cpp") add_packages("package0", "package1", "ns2::package2") + on_load(function (target) + if has_package("package0") then + target:add("defines", "HAS_PACKAGE0") + end + if has_package("package1") then + target:add("defines", "HAS_PACKAGE1") + end + if has_package("ns2::package2") then + target:add("defines", "HAS_PACKAGE2") + end + end) end) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index b102f4423..43271c7b0 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -137,8 +137,19 @@ function project._api_has_package(interp, ...) -- only for loading targets local requires = project._memcache():get("requires") if requires then - for _, name in ipairs(table.pack(...)) do - local pkg = requires[name] + for _, packagename in ipairs(table.pack(...)) do + local pkg = requires[packagename] + -- attempt to get package with namespace + if pkg == nil and packagename:find("::", 1, true) then + local parts = packagename:split("::", {plain = true}) + local namespace_pkg = requires[parts[#parts]] + if namespace_pkg and namespace_pkg:namespace() then + local fullname = namespace_pkg:fullname() + if fullname:endswith(packagename) then + pkg = namespace_pkg + end + end + end if pkg and pkg:enabled() then return true end diff --git a/xmake/core/sandbox/modules/has_package.lua b/xmake/core/sandbox/modules/has_package.lua index 8d6c8ec3f..cb5d2822e 100644 --- a/xmake/core/sandbox/modules/has_package.lua +++ b/xmake/core/sandbox/modules/has_package.lua @@ -23,8 +23,19 @@ return function (...) require("sandbox/modules/import/core/sandbox/module").import("core.project.project") local requires = project.required_packages() if requires then - for _, name in ipairs(table.join(...)) do - local pkg = requires[name] + for _, packagename in ipairs(table.join(...)) do + local pkg = requires[packagename] + -- attempt to get package with namespace + if pkg == nil and packagename:find("::", 1, true) then + local parts = packagename:split("::", {plain = true}) + local namespace_pkg = requires[parts[#parts]] + if namespace_pkg and namespace_pkg:namespace() then + local fullname = namespace_pkg:fullname() + if fullname:endswith(packagename) then + pkg = namespace_pkg + end + end + end if pkg and pkg:enabled() then return true end |
