Skip to content

Commit dad8fe9

Browse files
committed
add export filter
1 parent 1dee088 commit dad8fe9

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "bar.h"
2+
3+
int bar::add(int a, int b) {
4+
return a + b;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class bar {
2+
public:
3+
static int add(int a, int b);
4+
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "foo.h"
22

3-
int test::add(int a, int b)
4-
{
3+
int foo::add(int a, int b) {
54
return a + b;
65
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class test
2-
{
1+
class foo {
32
public:
43
static int add(int a, int b);
54
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "foo.h"
2+
#include "bar.h"
23
#include <iostream>
34

4-
using namespace std;
5-
6-
int main(int argc, char** argv)
7-
{
8-
cout << "add(1, 2) = " << test::add(1, 2) << endl;
5+
int main(int argc, char** argv) {
6+
std::cout << "foo::add(1, 2) = " << foo::add(1, 2) << std::endl;
7+
std::cout << "bar::add(1, 2) = " << bar::add(1, 2) << std::endl;
98
return 0;
109
}

tests/projects/c++/shared_library_export_all/xmake.lua

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ target("foo")
55
add_files("src/foo.cpp")
66
add_rules("utils.symbols.export_all", {export_classes = true})
77

8+
target("bar")
9+
set_kind("shared")
10+
add_files("src/bar.cpp")
11+
add_rules("utils.symbols.export_all", {export_filter = function (symbol)
12+
if symbol:find("add", 1, true) then
13+
return true
14+
end
15+
end})
16+
817
target("demo")
918
set_kind("binary")
10-
add_deps("foo")
19+
add_deps("foo", "bar")
1120
add_files("src/main.cpp")
1221

1322

xmake/rules/utils/symbols/export_all/export_all.lua

+21-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
3131
opt = opt or {}
3232
local allsymbols = hashset.new()
3333
local export_classes = opt.export_classes
34+
local export_filter = opt.export_filter
3435
for _, objectfile in ipairs(target:objectfiles()) do
3536
local objectsymbols = try { function () return os.iorunv(dumpbin, {"/symbols", "/nologo", objectfile}) end }
3637
if objectsymbols then
@@ -41,7 +42,11 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
4142
local symbol = line:match(".*External%s+| (.*)")
4243
if symbol then
4344
symbol = symbol:split('%s')[1]
44-
if not symbol:startswith("__") then
45+
if export_filter then
46+
if export_filter(symbol) then
47+
allsymbols:insert(symbol)
48+
end
49+
elseif not symbol:startswith("__") then
4550
-- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992
4651
if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("_DllMain@") then
4752
symbol = symbol:sub(2)
@@ -69,6 +74,7 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
6974
opt = opt or {}
7075
local allsymbols = hashset.new()
7176
local export_classes = opt.export_classes
77+
local export_filter = opt.export_filter
7278
for _, objectfile in ipairs(target:objectfiles()) do
7379
local objectsymbols = try { function () return os.iorunv(objdump, {"--syms", objectfile}) end }
7480
if objectsymbols then
@@ -77,7 +83,11 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
7783
local splitinfo = line:split("%s")
7884
local symbol = splitinfo[#splitinfo]
7985
if symbol then
80-
if not symbol:startswith("__") then
86+
if export_filter then
87+
if export_filter(symbol) then
88+
allsymbols:insert(symbol)
89+
end
90+
elseif not symbol:startswith("__") then
8191
-- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992
8292
if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("_DllMain@") then
8393
symbol = symbol:sub(2)
@@ -120,15 +130,22 @@ function main(target, opt)
120130
-- export c++ class?
121131
local export_classes = target:extraconf("rules", "utils.symbols.export_all", "export_classes")
122132

133+
-- the export filter
134+
local export_filter = target:extraconf("rules", "utils.symbols.export_all", "export_filter")
135+
123136
-- get all symbols
124137
local allsymbols
125138
local msvc = toolchain.load("msvc", {plat = target:plat(), arch = target:arch()})
126139
if msvc:check() then
127140
local dumpbin = assert(find_tool("dumpbin", {envs = msvc:runenvs()}), "dumpbin not found!")
128-
allsymbols = _get_allsymbols_by_dumpbin(target, dumpbin.program, {export_classes = export_classes})
141+
allsymbols = _get_allsymbols_by_dumpbin(target, dumpbin.program, {
142+
export_classes = export_classes,
143+
export_filter = export_filter})
129144
elseif target:has_tool("cc", "clang", "clang_cl", "clangxx", "gcc", "gxx") then
130145
local objdump = assert(find_tool("llvm-objdump") or find_tool("objdump"), "objdump not found!")
131-
allsymbols = _get_allsymbols_by_objdump(target, objdump.program, {export_classes = export_classes})
146+
allsymbols = _get_allsymbols_by_objdump(target, objdump.program, {
147+
export_classes = export_classes,
148+
export_filter = export_filter})
132149
end
133150

134151
-- export all symbols

0 commit comments

Comments
 (0)