diff options
| author | ruki <[email protected]> | 2024-05-11 16:11:10 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-05-11 16:11:10 +0800 |
| commit | 6b447fe2a874c79cc681daf8c7d934ad10da7a22 (patch) | |
| tree | 2772c2a11f3b804c921982f06ec4564bf3219c83 | |
| parent | 32bb7f1cfaa79fdd12ca24d4f46ec8cb57143ee5 (diff) | |
load lto plugin
| -rw-r--r-- | xmake/rules/c++/build_optimization/config.lua | 29 | ||||
| -rw-r--r-- | xmake/toolchains/cross/check.lua | 30 | ||||
| -rw-r--r-- | xmake/toolchains/cross/load.lua | 11 |
3 files changed, 38 insertions, 32 deletions
diff --git a/xmake/rules/c++/build_optimization/config.lua b/xmake/rules/c++/build_optimization/config.lua index f252f78c7..357c3cf76 100644 --- a/xmake/rules/c++/build_optimization/config.lua +++ b/xmake/rules/c++/build_optimization/config.lua @@ -21,28 +21,6 @@ -- imports import("core.tool.compiler") import("core.project.project") -import("lib.detect.find_file") - --- get liblto_plugin.so path of gcc -function _get_gcc_liblto_plugin_path(target, gcc) - local cachekey = "lto_plugin_" .. gcc - local plugin_path = target:data(cachekey) - if plugin_path == nil then - local outdata = try { function() return os.iorunv(gcc, {"-print-prog-name=lto-wrapper"}) end } - if outdata then - local lto_plugindir = path.directory(outdata:trim()) - if os.isdir(lto_plugindir) then - if is_host("windows") then - plugin_path = find_file("liblto_plugin*.dll", lto_plugindir) - else - plugin_path = find_file("liblto_plugin.so", lto_plugindir) - end - end - end - target:data_set(cachekey, plugin_path or false) - end - return plugin_path or nil -end -- add lto optimization function _add_lto_optimization(target, sourcekind) @@ -70,13 +48,6 @@ function _add_lto_optimization(target, sourcekind) target:add("ldflags", "-flto") target:add("shflags", "-flto") - -- @see https://github.com/xmake-io/xmake/issues/5015 - -- add lto_plugin.so to ar - local lto_plugin = _get_gcc_liblto_plugin_path(target, program) - if lto_plugin then - target:add("arflags", {"--plugin", lto_plugin}, {force = true, expand = false}) - end - -- to use the link-time optimizer, -flto and optimization options should be specified at compile time and during the final link. -- @see https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html local optimize = target:get("optimize") diff --git a/xmake/toolchains/cross/check.lua b/xmake/toolchains/cross/check.lua index 815b95fd7..6f1df0f9a 100644 --- a/xmake/toolchains/cross/check.lua +++ b/xmake/toolchains/cross/check.lua @@ -21,6 +21,27 @@ -- imports import("core.project.config") import("detect.sdks.find_cross_toolchain") +import("lib.detect.find_file") + +-- find liblto_plugin.so path for gcc +function _find_gcc_liblto_plugin_path(cross_toolchain) + local gcc = find_file((cross_toolchain.cross or "*-") .. "gcc", cross_toolchain.bindir) + if gcc then + local plugin_path + local outdata = try { function() return os.iorunv(gcc, {"-print-prog-name=lto-wrapper"}) end } + if outdata then + local lto_plugindir = path.directory(outdata:trim()) + if os.isdir(lto_plugindir) then + if is_host("windows") then + plugin_path = find_file("liblto_plugin*.dll", lto_plugindir) + else + plugin_path = find_file("liblto_plugin.so", lto_plugindir) + end + end + end + return plugin_path + end +end -- check the cross toolchain function main(toolchain) @@ -47,6 +68,7 @@ function main(toolchain) end end end + if cross_toolchain then toolchain:config_set("cross", cross_toolchain.cross) toolchain:config_set("bindir", cross_toolchain.bindir) @@ -56,6 +78,14 @@ function main(toolchain) if not config.get("target_os") then config.set("target_os", "linux", {readonly = true, force = true}) end + + -- find lto_plugin.so path for gcc + -- @see https://github.com/xmake-io/xmake/issues/5015 + -- https://github.com/xmake-io/xmake/issues/5051 + local lto_plugin = _find_gcc_liblto_plugin_path(cross_toolchain) + if lto_plugin then + toolchain:config_set("lto_plugin", lto_plugin) + end else raise("cross toolchain not found!") end diff --git a/xmake/toolchains/cross/load.lua b/xmake/toolchains/cross/load.lua index 540d12111..ba59a2b84 100644 --- a/xmake/toolchains/cross/load.lua +++ b/xmake/toolchains/cross/load.lua @@ -23,8 +23,6 @@ import("core.project.config") -- load the cross toolchain function main(toolchain) - - -- get cross prefix local cross = toolchain:cross() or "" -- add toolset @@ -38,7 +36,6 @@ function main(toolchain) toolchain:add("toolset", "as", cross .. "gcc", cross .. "clang") toolchain:add("toolset", "ld", cross .. "g++", cross .. "gcc", cross .. "clang++", cross .. "clang") toolchain:add("toolset", "sh", cross .. "g++", cross .. "gcc", cross .. "clang++", cross .. "clang") - -- old gcc need gcc-ar for lto, @see https://github.com/xmake-io/xmake/issues/5015 toolchain:add("toolset", "ar", cross .. "ar") toolchain:add("toolset", "ranlib", cross .. "ranlib") toolchain:add("toolset", "strip", cross .. "strip") @@ -48,5 +45,13 @@ function main(toolchain) if bindir and is_host("windows") then toolchain:add("runenvs", "PATH", bindir) end + + -- add lto_plugin.so path for gcc + -- @see https://github.com/xmake-io/xmake/issues/5015 + -- https://github.com/xmake-io/xmake/issues/5051 + local lto_plugin = toolchain:config("lto_plugin") + if lto_plugin then + toolchain:add("arflags", {"--plugin", lto_plugin}) + end end |
