diff options
| author | ruki <[email protected]> | 2024-05-11 13:45:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-05-11 13:45:50 +0800 |
| commit | 32bb7f1cfaa79fdd12ca24d4f46ec8cb57143ee5 (patch) | |
| tree | f3cdb6999ddef1f9a65a569c9fff18a8cc10df39 | |
| parent | 5c1dde0048109547f843cf9302cd553ad3375344 (diff) | |
add lto plugin for gcc
| -rw-r--r-- | xmake/rules/c++/build_optimization/config.lua | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/xmake/rules/c++/build_optimization/config.lua b/xmake/rules/c++/build_optimization/config.lua index 6baa26802..f252f78c7 100644 --- a/xmake/rules/c++/build_optimization/config.lua +++ b/xmake/rules/c++/build_optimization/config.lua @@ -21,6 +21,28 @@ -- 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) @@ -37,7 +59,7 @@ function _add_lto_optimization(target, sourcekind) end -- add ldflags and shflags - local _, ld = target:tool("ld") + local program, ld = target:tool("ld") if ld == "link" then target:add("ldflags", "-LTCG") target:add("shflags", "-LTCG") @@ -47,6 +69,14 @@ function _add_lto_optimization(target, sourcekind) elseif ld == "gcc" or ld == "gxx" then 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") |
