diff options
| author | ruki <[email protected]> | 2024-05-19 23:15:53 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-05-19 23:15:53 +0800 |
| commit | dad8fe9b2acda84bfbead2a05a8b0d4b504d6479 (patch) | |
| tree | 610cc5fb68689e2ab35fec5d61330ec9d6f159cb | |
| parent | 1dee088e701ace1ea4cdfe904a152caddfc55f42 (diff) | |
add export filter
7 files changed, 46 insertions, 14 deletions
diff --git a/tests/projects/c++/shared_library_export_all/src/bar.cpp b/tests/projects/c++/shared_library_export_all/src/bar.cpp new file mode 100644 index 000000000..a3065ed8b --- /dev/null +++ b/tests/projects/c++/shared_library_export_all/src/bar.cpp @@ -0,0 +1,5 @@ +#include "bar.h" + +int bar::add(int a, int b) { + return a + b; +} diff --git a/tests/projects/c++/shared_library_export_all/src/bar.h b/tests/projects/c++/shared_library_export_all/src/bar.h new file mode 100644 index 000000000..687386e0a --- /dev/null +++ b/tests/projects/c++/shared_library_export_all/src/bar.h @@ -0,0 +1,4 @@ +class bar { +public: + static int add(int a, int b); +}; diff --git a/tests/projects/c++/shared_library_export_all/src/foo.cpp b/tests/projects/c++/shared_library_export_all/src/foo.cpp index 3ab50856c..46635f458 100644 --- a/tests/projects/c++/shared_library_export_all/src/foo.cpp +++ b/tests/projects/c++/shared_library_export_all/src/foo.cpp @@ -1,6 +1,5 @@ #include "foo.h" -int test::add(int a, int b) -{ +int foo::add(int a, int b) { return a + b; } diff --git a/tests/projects/c++/shared_library_export_all/src/foo.h b/tests/projects/c++/shared_library_export_all/src/foo.h index f671c7d32..e27636b94 100644 --- a/tests/projects/c++/shared_library_export_all/src/foo.h +++ b/tests/projects/c++/shared_library_export_all/src/foo.h @@ -1,5 +1,4 @@ -class test -{ +class foo { public: static int add(int a, int b); }; diff --git a/tests/projects/c++/shared_library_export_all/src/main.cpp b/tests/projects/c++/shared_library_export_all/src/main.cpp index e48f4730e..cc95897ee 100644 --- a/tests/projects/c++/shared_library_export_all/src/main.cpp +++ b/tests/projects/c++/shared_library_export_all/src/main.cpp @@ -1,10 +1,9 @@ #include "foo.h" +#include "bar.h" #include <iostream> -using namespace std; - -int main(int argc, char** argv) -{ - cout << "add(1, 2) = " << test::add(1, 2) << endl; +int main(int argc, char** argv) { + std::cout << "foo::add(1, 2) = " << foo::add(1, 2) << std::endl; + std::cout << "bar::add(1, 2) = " << bar::add(1, 2) << std::endl; return 0; } diff --git a/tests/projects/c++/shared_library_export_all/xmake.lua b/tests/projects/c++/shared_library_export_all/xmake.lua index fa25fac50..73159d4b5 100644 --- a/tests/projects/c++/shared_library_export_all/xmake.lua +++ b/tests/projects/c++/shared_library_export_all/xmake.lua @@ -5,9 +5,18 @@ target("foo") add_files("src/foo.cpp") add_rules("utils.symbols.export_all", {export_classes = true}) +target("bar") + set_kind("shared") + add_files("src/bar.cpp") + add_rules("utils.symbols.export_all", {export_filter = function (symbol) + if symbol:find("add", 1, true) then + return true + end + end}) + target("demo") set_kind("binary") - add_deps("foo") + add_deps("foo", "bar") add_files("src/main.cpp") diff --git a/xmake/rules/utils/symbols/export_all/export_all.lua b/xmake/rules/utils/symbols/export_all/export_all.lua index 4842546f3..9376e557e 100644 --- a/xmake/rules/utils/symbols/export_all/export_all.lua +++ b/xmake/rules/utils/symbols/export_all/export_all.lua @@ -31,6 +31,7 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt) opt = opt or {} local allsymbols = hashset.new() local export_classes = opt.export_classes + local export_filter = opt.export_filter for _, objectfile in ipairs(target:objectfiles()) do local objectsymbols = try { function () return os.iorunv(dumpbin, {"/symbols", "/nologo", objectfile}) end } if objectsymbols then @@ -41,7 +42,11 @@ function _get_allsymbols_by_dumpbin(target, dumpbin, opt) local symbol = line:match(".*External%s+| (.*)") if symbol then symbol = symbol:split('%s')[1] - if not symbol:startswith("__") then + if export_filter then + if export_filter(symbol) then + allsymbols:insert(symbol) + end + elseif not symbol:startswith("__") then -- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992 if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("_DllMain@") then symbol = symbol:sub(2) @@ -69,6 +74,7 @@ function _get_allsymbols_by_objdump(target, objdump, opt) opt = opt or {} local allsymbols = hashset.new() local export_classes = opt.export_classes + local export_filter = opt.export_filter for _, objectfile in ipairs(target:objectfiles()) do local objectsymbols = try { function () return os.iorunv(objdump, {"--syms", objectfile}) end } if objectsymbols then @@ -77,7 +83,11 @@ function _get_allsymbols_by_objdump(target, objdump, opt) local splitinfo = line:split("%s") local symbol = splitinfo[#splitinfo] if symbol then - if not symbol:startswith("__") then + if export_filter then + if export_filter(symbol) then + allsymbols:insert(symbol) + end + elseif not symbol:startswith("__") then -- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992 if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("_DllMain@") then symbol = symbol:sub(2) @@ -120,15 +130,22 @@ function main(target, opt) -- export c++ class? local export_classes = target:extraconf("rules", "utils.symbols.export_all", "export_classes") + -- the export filter + local export_filter = target:extraconf("rules", "utils.symbols.export_all", "export_filter") + -- get all symbols local allsymbols local msvc = toolchain.load("msvc", {plat = target:plat(), arch = target:arch()}) if msvc:check() then local dumpbin = assert(find_tool("dumpbin", {envs = msvc:runenvs()}), "dumpbin not found!") - allsymbols = _get_allsymbols_by_dumpbin(target, dumpbin.program, {export_classes = export_classes}) + allsymbols = _get_allsymbols_by_dumpbin(target, dumpbin.program, { + export_classes = export_classes, + export_filter = export_filter}) elseif target:has_tool("cc", "clang", "clang_cl", "clangxx", "gcc", "gxx") then local objdump = assert(find_tool("llvm-objdump") or find_tool("objdump"), "objdump not found!") - allsymbols = _get_allsymbols_by_objdump(target, objdump.program, {export_classes = export_classes}) + allsymbols = _get_allsymbols_by_objdump(target, objdump.program, { + export_classes = export_classes, + export_filter = export_filter}) end -- export all symbols |
