Skip to content

Commit

Permalink
add package for namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 7, 2025
1 parent c184146 commit e5ea7bc
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/apis/namespace/package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


5 changes: 5 additions & 0 deletions tests/apis/namespace/package/src/bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "bar.h"

int sub(int a, int b) {
return a - b;
}
9 changes: 9 additions & 0 deletions tests/apis/namespace/package/src/bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef __cplusplus
extern "C" {
#endif

int sub(int a, int b);

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions tests/apis/namespace/package/src/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "foo.h"

int add(int a, int b) {
return a + b;
}
9 changes: 9 additions & 0 deletions tests/apis/namespace/package/src/foo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef __cplusplus
extern "C" {
#endif

int add(int a, int b);

#ifdef __cplusplus
}
#endif
9 changes: 9 additions & 0 deletions tests/apis/namespace/package/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "foo.h"
#include "bar.h"
#include <iostream>

int main(int argc, char** argv) {
std::cout << "add(1, 2) = " << add(1, 2) << std::endl;
std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl;
return 0;
}
3 changes: 3 additions & 0 deletions tests/apis/namespace/package/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main()
os.exec("xmake -vD")
end
44 changes: 44 additions & 0 deletions tests/apis/namespace/package/xmake.lua
Original file line number Diff line number Diff line change
@@ -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)

19 changes: 18 additions & 1 deletion xmake/core/package/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions xmake/modules/private/action/require/impl/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5ea7bc

Please sign in to comment.