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