Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for namespace #6001

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/apis/namespace/basic/.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/basic/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/basic/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/basic/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/basic/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/basic/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/basic/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/basic/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")
end)

namespace("ns2")
target("bar")
set_kind("static")
add_files("src/bar.cpp")
namespace_end()

target("test")
set_kind("binary")
add_deps("ns1::foo", "ns2::bar")
add_files("src/main.cpp")

8 changes: 8 additions & 0 deletions tests/apis/namespace/nested/.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/nested/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/nested/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/nested/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/nested/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/nested/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/nested/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/nested/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")
target("bar")
set_kind("static")
add_files("src/bar.cpp")
namespace_end()
end)

target("test")
set_kind("binary")
add_deps("ns1::foo", "ns1::ns2::bar")
add_files("src/main.cpp")

27 changes: 27 additions & 0 deletions xmake/core/base/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ function interpreter.new()
instance:api_register(nil, "add_subdirs", interpreter.api_builtin_add_subdirs)
instance:api_register(nil, "add_subfiles", interpreter.api_builtin_add_subfiles)
instance:api_register(nil, "set_xmakever", interpreter.api_builtin_set_xmakever)
instance:api_register(nil, "namespace", interpreter.api_builtin_namespace)
instance:api_register(nil, "namespace_end",interpreter.api_builtin_namespace_end)

-- register the interpreter interfaces
instance:api_register(nil, "interp_save_scope", interpreter.api_interp_save_scope)
Expand Down Expand Up @@ -952,6 +954,7 @@ function interpreter:api_register_scope(...)
local scope_args = table.pack(...)
local scope_name = scope_args[1]
local scope_info = scope_args[2]
local namespace = self._NAMESPACE_STR

-- check invalid scope name, @see https://github.com/xmake-io/xmake/issues/4547
if scope_args.n > 0 and type(scope_name) ~= "string" then
Expand Down Expand Up @@ -1838,6 +1841,30 @@ function interpreter:api_builtin_add_subfiles(...)
deprecated.add("includes(%s)", "add_subfiles(%s)", table.concat(files, ", "), table.concat(files, ", "))
end

-- the builtin api: namespace()
function interpreter:api_builtin_namespace(name, callback)
local namespace = self._NAMESPACE
if namespace == nil then
namespace = {}
self._NAMESPACE = namespace
end
table.insert(namespace, name)
self._NAMESPACE_STR = table.concat(namespace, "::")
if callback and type(callback) == "function" then
callback()
self:api_builtin_namespace_end()
end
end

-- the builtin api: namespace_end()
function interpreter:api_builtin_namespace_end()
local namespace = self._NAMESPACE
if namespace then
table.remove(namespace)
self._NAMESPACE_STR = table.concat(namespace, "::")
end
end

-- the interpreter api: interp_save_scope()
-- save the current scope
function interpreter:api_interp_save_scope()
Expand Down
Loading