Skip to content

Commit

Permalink
get sourcefile for export all
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Sep 12, 2024
1 parent c72f5f0 commit e22e69c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 3 additions & 2 deletions tests/projects/c++/shared_library_export_all/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ target("bar")
set_kind("shared")
add_files("src/bar.cpp")
add_rules("utils.symbols.export_all", {export_filter = function (symbol, opt)
local objectfile = opt.objectfile
if objectfile:find("bar.cpp", 1, true) and symbol:find("add", 1, true) then
local filepath = opt.sourcefile or opt.objectfile
if filepath and filepath:find("bar.cpp", 1, true) and symbol:find("add", 1, true) then
print("export: %s at %s", symbol, filepath)
return true
end
end})
Expand Down
41 changes: 39 additions & 2 deletions xmake/rules/utils/symbols/export_all/export_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ import("core.base.hashset")
import("core.project.depend")
import("utils.progress")

-- It is not very accurate because some rules automatically
-- generate objectfiles and do not save the corresponding sourcefiles.
-- @see https://github.com/xmake-io/xmake/issues/5601
function _get_sourcefile_from_objectfile(target, objectfile)
local sourcefile
for _, sourcebatch in pairs(target:sourcebatches()) do
local sourcefiles = sourcebatch.sourcefiles
if sourcefiles then
for idx, obj in ipairs(sourcebatch.objectfiles) do
if obj == objectfile then
sourcefile = sourcefiles[idx]
break
end
end
end
end
if not sourcefile then
for _, dep in ipairs(target:orderdeps()) do
if dep:is_object() then
sourcefile = _get_sourcefile_from_objectfile(dep, objectfile)
if sourcefile then
break
end
end
end
end
return sourcefile
end

-- use dumpbin to get all symbols from object files
function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
opt = opt or {}
Expand All @@ -35,6 +64,10 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
for _, objectfile in ipairs(target:objectfiles()) do
local objectsymbols = try { function () return os.iorunv(dumpbin, {"/symbols", "/nologo", objectfile}) end }
if objectsymbols then
local sourcefile
if export_filter then
sourcefile = _get_sourcefile_from_objectfile(target, objectfile)
end
for _, line in ipairs(objectsymbols:split('\n', {plain = true})) do
-- https://docs.microsoft.com/en-us/cpp/build/reference/symbols
-- 008 00000000 SECT3 notype () External | add
Expand All @@ -47,7 +80,7 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt)
symbol = symbol:sub(2)
end
if export_filter then
if export_filter(symbol, {objectfile = objectfile}) then
if export_filter(symbol, {objectfile = objectfile, sourcefile = sourcefile}) then
allsymbols:insert(symbol)
end
elseif not symbol:startswith("__") then
Expand Down Expand Up @@ -78,6 +111,10 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
for _, objectfile in ipairs(target:objectfiles()) do
local objectsymbols = try { function () return os.iorunv(objdump, {"--syms", objectfile}) end }
if objectsymbols then
local sourcefile
if export_filter then
sourcefile = _get_sourcefile_from_objectfile(target, objectfile)
end
for _, line in ipairs(objectsymbols:split('\n', {plain = true})) do
if line:find("(scl 2)", 1, true) then
local splitinfo = line:split("%s")
Expand All @@ -88,7 +125,7 @@ function _get_allsymbols_by_objdump(target, objdump, opt)
symbol = symbol:sub(2)
end
if export_filter then
if export_filter(symbol, {objectfile = objectfile}) then
if export_filter(symbol, {objectfile = objectfile, sourcefile = sourcefile}) then
allsymbols:insert(symbol)
end
elseif not symbol:startswith("__") then
Expand Down

0 comments on commit e22e69c

Please sign in to comment.