Skip to content

Commit cb90595

Browse files
authored
Merge pull request #5608 from xmake-io/exportall
Improve export all rule
2 parents fa48b0f + b3fa6a3 commit cb90595

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ target("foo")
88
target("bar")
99
set_kind("shared")
1010
add_files("src/bar.cpp")
11-
add_rules("utils.symbols.export_all", {export_filter = function (symbol)
12-
if symbol:find("add", 1, true) then
11+
add_rules("utils.symbols.export_all", {export_filter = function (symbol, opt)
12+
local filepath = opt.sourcefile or opt.objectfile
13+
if filepath and filepath:find("bar.cpp", 1, true) and symbol:find("add", 1, true) then
14+
print("export: %s at %s", symbol, filepath)
1315
return true
1416
end
1517
end})
@@ -20,3 +22,4 @@ target("demo")
2022
add_files("src/main.cpp")
2123

2224

25+

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

+38-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,46 @@ import("core.base.hashset")
2626
import("core.project.depend")
2727
import("utils.progress")
2828

29+
-- It is not very accurate because some rules automatically
30+
-- generate objectfiles and do not save the corresponding sourcefiles.
31+
-- @see https://github.com/xmake-io/xmake/issues/5601
32+
function _get_sourcefiles_map(target, sourcefiles_map)
33+
for _, sourcebatch in pairs(target:sourcebatches()) do
34+
for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
35+
local objectfiles = sourcebatch.objectfiles
36+
if objectfiles then
37+
local objectfile = objectfiles[idx]
38+
if objectfile then
39+
sourcefiles_map[objectfile] = sourcefile
40+
end
41+
end
42+
end
43+
end
44+
local plaindeps = target:get("deps")
45+
if plaindeps then
46+
for _, depname in ipairs(plaindeps) do
47+
local dep = target:dep(depname)
48+
if dep and dep:is_object() then
49+
_get_sourcefiles_map(dep, sourcefiles_map)
50+
end
51+
end
52+
end
53+
end
54+
2955
-- use dumpbin to get all symbols from object files
3056
function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
3157
opt = opt or {}
3258
local allsymbols = hashset.new()
3359
local export_classes = opt.export_classes
3460
local export_filter = opt.export_filter
61+
local sourcefiles_map = {}
62+
if export_filter then
63+
_get_sourcefiles_map(target, sourcefiles_map)
64+
end
3565
for _, objectfile in ipairs(target:objectfiles()) do
3666
local objectsymbols = try { function () return os.iorunv(dumpbin, {"/symbols", "/nologo", objectfile}) end }
3767
if objectsymbols then
68+
local sourcefile = sourcefiles_map[objectfile]
3869
for _, line in ipairs(objectsymbols:split('\n', {plain = true})) do
3970
-- https://docs.microsoft.com/en-us/cpp/build/reference/symbols
4071
-- 008 00000000 SECT3 notype () External | add
@@ -47,7 +78,7 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
4778
symbol = symbol:sub(2)
4879
end
4980
if export_filter then
50-
if export_filter(symbol) then
81+
if export_filter(symbol, {objectfile = objectfile, sourcefile = sourcefile}) then
5182
allsymbols:insert(symbol)
5283
end
5384
elseif not symbol:startswith("__") then
@@ -75,9 +106,14 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
75106
local allsymbols = hashset.new()
76107
local export_classes = opt.export_classes
77108
local export_filter = opt.export_filter
109+
local sourcefiles_map = {}
110+
if export_filter then
111+
_get_sourcefiles_map(target, sourcefiles_map)
112+
end
78113
for _, objectfile in ipairs(target:objectfiles()) do
79114
local objectsymbols = try { function () return os.iorunv(objdump, {"--syms", objectfile}) end }
80115
if objectsymbols then
116+
local sourcefile = sourcefiles_map[objectfile]
81117
for _, line in ipairs(objectsymbols:split('\n', {plain = true})) do
82118
if line:find("(scl 2)", 1, true) then
83119
local splitinfo = line:split("%s")
@@ -88,7 +124,7 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
88124
symbol = symbol:sub(2)
89125
end
90126
if export_filter then
91-
if export_filter(symbol) then
127+
if export_filter(symbol, {objectfile = objectfile, sourcefile = sourcefile}) then
92128
allsymbols:insert(symbol)
93129
end
94130
elseif not symbol:startswith("__") then

0 commit comments

Comments
 (0)