Skip to content

Commit

Permalink
improve has_package
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 8, 2025
1 parent 7a4c72e commit 9c0f92f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
17 changes: 17 additions & 0 deletions tests/apis/namespace/package/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -36,12 +39,26 @@ 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")
set_kind("binary")
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)

15 changes: 13 additions & 2 deletions xmake/core/project/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions xmake/core/sandbox/modules/has_package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c0f92f

Please sign in to comment.