Skip to content

Commit

Permalink
support for inner namespace access
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 6, 2025
1 parent f6904d7 commit ae8a596
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 15 deletions.
8 changes: 8 additions & 0 deletions tests/apis/namespace/inner/.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/inner/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/inner/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/inner/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/inner/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/inner/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/inner/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main()
os.exec("xmake -vD")
end
19 changes: 19 additions & 0 deletions tests/apis/namespace/inner/xmake.lua
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 1 addition & 1 deletion xmake/actions/build/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion xmake/actions/build/build_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions xmake/actions/config/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion xmake/actions/package/local/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion xmake/actions/package/remote/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions xmake/core/base/private/instance_deps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions xmake/core/project/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion xmake/plugins/project/make/makefile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion xmake/plugins/project/ninja/build_ninja.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
2 changes: 1 addition & 1 deletion xmake/plugins/project/vsxmake/getinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae8a596

Please sign in to comment.