summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-02 00:01:57 +0800
committerruki <[email protected]>2022-07-02 00:01:57 +0800
commit343a938f7d45c8593fa3fc42559b21fe5177f916 (patch)
treeaf47abaa3ebd2068cd55dc593e43aff050e546e1
parentb9ecb6ba01262703fd962353fcc24cf0ee7e9e5f (diff)
add lto flags for gcc/clang
-rw-r--r--xmake/core/project/policy.lua2
-rw-r--r--xmake/rules/c++/build_optimization/load.lua41
2 files changed, 43 insertions, 0 deletions
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index e6a20aef9..e73f67a83 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -49,6 +49,8 @@ function policy.policies()
["build.ccache"] = {description = "Enable C/C++ build cache.", type = "boolean"},
-- Enable build warning output, it's disabled by default and we need `xmake -w/-vD` to look at it.
["build.warning"] = {description = "Enable build warning output.", type = "boolean"},
+ -- Enable LTO linker-time optimization for c/c++ building.
+ ["build.optimization.lto"] = {description = "Enable LTO linker-time optimization for c/c++ building.", type = "boolean"},
-- preprocessor configuration for ccache/distcc, we can disable linemarkers to speed up preprocess
["preprocessor.linemarkers"] = {description = "Enable linemarkers for preprocessor.", default = true, type = "boolean"},
-- preprocessor configuration for ccache/distcc, we can disable it to avoid cache object file with __DATE__, __TIME__
diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua
index eb9fe3863..faf91add8 100644
--- a/xmake/rules/c++/build_optimization/load.lua
+++ b/xmake/rules/c++/build_optimization/load.lua
@@ -18,5 +18,46 @@
-- @file load.lua
--
+-- imports
+import("core.tool.compiler")
+
+-- add lto optimization
+function _add_lto_optimization(target, sourcekind)
+
+ -- add cflags
+ local _, cc = target:tool(sourcekind)
+ local cflag = sourcekind == "cxx" and "cxxflags" or "cflags"
+ if cc == "cl" then
+ -- TODO
+ elseif cc == "clang" or cc == "clangxx" then
+ target:add(cflag, "-flto=thin")
+ elseif cc == "gcc" or cc == "gxx" then
+ target:add(cflag, "-flto")
+ end
+
+ -- add ldflags and shflags
+ local _, ld = target:tool("ld")
+ if ld == "cl" then
+ -- TODO
+ elseif ld == "clang" or ld == "clangxx" then
+ target:add("ldflags", "-flto=thin")
+ target:add("shflags", "-flto=thin")
+ elseif ld == "gcc" or ld == "gxx" then
+ target:add("ldflags", "-flto")
+ target:add("shflags", "-flto")
+ -- 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")
+ if optimize then
+ local optimize_flags = compiler.map_flags(sourcekind == "cc" and "c" or "cxx", "optimize", optimize)
+ target:add("ldflags", optimize_flags)
+ target:add("shflags", optimize_flags)
+ end
+ end
+end
+
function main(target, sourcekind)
+ if target:policy("build.optimization.lto") then
+ _add_lto_optimization(target, sourcekind)
+ end
end