From b9ecb6ba01262703fd962353fcc24cf0ee7e9e5f Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 2 Jul 2022 00:54:52 +0800 Subject: add c++ build optimization rule stub --- xmake/rules/c++/build_optimization/load.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 xmake/rules/c++/build_optimization/load.lua (limited to 'xmake/rules/c++/build_optimization/load.lua') diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua new file mode 100644 index 000000000..eb9fe3863 --- /dev/null +++ b/xmake/rules/c++/build_optimization/load.lua @@ -0,0 +1,22 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file load.lua +-- + +function main(target, sourcekind) +end -- cgit v1.3.1 From 343a938f7d45c8593fa3fc42559b21fe5177f916 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 2 Jul 2022 00:01:57 +0800 Subject: add lto flags for gcc/clang --- xmake/core/project/policy.lua | 2 ++ xmake/rules/c++/build_optimization/load.lua | 41 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'xmake/rules/c++/build_optimization/load.lua') 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 -- cgit v1.3.1 From dbdb5bd1fef50c553f017ca473603c049daba4db Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 2 Jul 2022 00:05:54 +0800 Subject: improve lto --- xmake/rules/c++/build_optimization/load.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'xmake/rules/c++/build_optimization/load.lua') diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua index faf91add8..38dc3847b 100644 --- a/xmake/rules/c++/build_optimization/load.lua +++ b/xmake/rules/c++/build_optimization/load.lua @@ -20,6 +20,7 @@ -- imports import("core.tool.compiler") +import("core.project.project") -- add lto optimization function _add_lto_optimization(target, sourcekind) @@ -57,7 +58,8 @@ function _add_lto_optimization(target, sourcekind) end function main(target, sourcekind) - if target:policy("build.optimization.lto") then + if target:policy("build.optimization.lto") or + project.policy("build.optimization.lto") then _add_lto_optimization(target, sourcekind) end end -- cgit v1.3.1 From b53fd8d1ecec8055f5ec351c3ad02923e74b9dfd Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 2 Jul 2022 09:08:51 +0800 Subject: add lto for msvc --- xmake/rules/c++/build_optimization/load.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'xmake/rules/c++/build_optimization/load.lua') diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua index 38dc3847b..b5bdf07a1 100644 --- a/xmake/rules/c++/build_optimization/load.lua +++ b/xmake/rules/c++/build_optimization/load.lua @@ -29,7 +29,7 @@ function _add_lto_optimization(target, sourcekind) local _, cc = target:tool(sourcekind) local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" if cc == "cl" then - -- TODO + target:add(cflag, "-GL") elseif cc == "clang" or cc == "clangxx" then target:add(cflag, "-flto=thin") elseif cc == "gcc" or cc == "gxx" then @@ -39,7 +39,8 @@ function _add_lto_optimization(target, sourcekind) -- add ldflags and shflags local _, ld = target:tool("ld") if ld == "cl" then - -- TODO + target:add("ldflags", "-LTCG") + target:add("shflags", "-LTCG") elseif ld == "clang" or ld == "clangxx" then target:add("ldflags", "-flto=thin") target:add("shflags", "-flto=thin") -- cgit v1.3.1 From bec05e2418d6c2055cd729b7b4e0de7210091473 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 2 Jul 2022 19:20:42 +0800 Subject: fix link for msvc --- xmake/core/package/package.lua | 2 +- xmake/rules/c++/build_optimization/load.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'xmake/rules/c++/build_optimization/load.lua') diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index a9f39e208..9dad30ab1 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1709,7 +1709,7 @@ function _instance:_generate_lto_configs(sourcekind) -- add ldflags and shflags local _, ld = self:tool("ld") - if ld == "cl" then + if ld == "link" then configs.ldflags = "-LTCG" configs.shflags = "-LTCG" elseif ld == "clang" or ld == "clangxx" then diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua index b5bdf07a1..bd1831778 100644 --- a/xmake/rules/c++/build_optimization/load.lua +++ b/xmake/rules/c++/build_optimization/load.lua @@ -38,7 +38,7 @@ function _add_lto_optimization(target, sourcekind) -- add ldflags and shflags local _, ld = target:tool("ld") - if ld == "cl" then + if ld == "link" then target:add("ldflags", "-LTCG") target:add("shflags", "-LTCG") elseif ld == "clang" or ld == "clangxx" then -- cgit v1.3.1