From dd1e63c09b84c0fd0b886503da3cc468ef7ea0a5 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 29 Jan 2023 13:31:18 +0800 Subject: add lto for ldc2 --- xmake/rules/c++/build_optimization/config.lua | 66 +++++++++++++++++++++++++ xmake/rules/c++/build_optimization/load.lua | 66 ------------------------- xmake/rules/c++/build_optimization/xmake.lua | 4 +- xmake/rules/dlang/build_optimization/config.lua | 47 ++++++++++++++++++ xmake/rules/dlang/build_optimization/xmake.lua | 24 +++++++++ xmake/rules/dlang/xmake.lua | 1 + 6 files changed, 140 insertions(+), 68 deletions(-) create mode 100644 xmake/rules/c++/build_optimization/config.lua delete mode 100644 xmake/rules/c++/build_optimization/load.lua create mode 100644 xmake/rules/dlang/build_optimization/config.lua create mode 100644 xmake/rules/dlang/build_optimization/xmake.lua diff --git a/xmake/rules/c++/build_optimization/config.lua b/xmake/rules/c++/build_optimization/config.lua new file mode 100644 index 000000000..6baa26802 --- /dev/null +++ b/xmake/rules/c++/build_optimization/config.lua @@ -0,0 +1,66 @@ +--!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 config.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") + +-- 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 + target:add(cflag, "-GL") + 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 == "link" then + 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") + 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") or + project.policy("build.optimization.lto") then + _add_lto_optimization(target, sourcekind) + end +end diff --git a/xmake/rules/c++/build_optimization/load.lua b/xmake/rules/c++/build_optimization/load.lua deleted file mode 100644 index bd1831778..000000000 --- a/xmake/rules/c++/build_optimization/load.lua +++ /dev/null @@ -1,66 +0,0 @@ ---!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 --- - --- imports -import("core.tool.compiler") -import("core.project.project") - --- 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 - target:add(cflag, "-GL") - 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 == "link" then - 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") - 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") or - project.policy("build.optimization.lto") then - _add_lto_optimization(target, sourcekind) - end -end diff --git a/xmake/rules/c++/build_optimization/xmake.lua b/xmake/rules/c++/build_optimization/xmake.lua index c08b74141..d4df5df98 100644 --- a/xmake/rules/c++/build_optimization/xmake.lua +++ b/xmake/rules/c++/build_optimization/xmake.lua @@ -21,12 +21,12 @@ -- define rule: c.build.optimization rule("c.build.optimization") on_config(function (target) - import("load")(target, "cc") + import("config")(target, "cc") end) -- define rule: c++.build.optimization rule("c++.build.optimization") on_config(function (target) - import("load")(target, "cxx") + import("config")(target, "cxx") end) diff --git a/xmake/rules/dlang/build_optimization/config.lua b/xmake/rules/dlang/build_optimization/config.lua new file mode 100644 index 000000000..6de97dfdd --- /dev/null +++ b/xmake/rules/dlang/build_optimization/config.lua @@ -0,0 +1,47 @@ +--!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 config.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") + +-- add lto optimization +function _add_lto_optimization(target) + if target:has_tool("dc", "ldc2") and target:has_tool("dcld", "ldc2") then + target:add("dcflags", "-flto=thin", {force = true}) + target:add("ldflags", "-flto=thin", {force = true}) + target:add("shflags", "-flto=thin", {force = true}) + -- 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("dc", "optimize", optimize) + target:add("ldflags", optimize_flags) + target:add("shflags", optimize_flags) + end + end +end + +function main(target) + if target:policy("build.optimization.lto") or + project.policy("build.optimization.lto") then + _add_lto_optimization(target) + end +end diff --git a/xmake/rules/dlang/build_optimization/xmake.lua b/xmake/rules/dlang/build_optimization/xmake.lua new file mode 100644 index 000000000..89a4a1e8c --- /dev/null +++ b/xmake/rules/dlang/build_optimization/xmake.lua @@ -0,0 +1,24 @@ +--!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 xmake.lua +-- + +-- define rule: dlang.build.optimization +rule("dlang.build.optimization") + on_config("config") + diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua index cebbaef3e..7ffb8c42f 100644 --- a/xmake/rules/dlang/xmake.lua +++ b/xmake/rules/dlang/xmake.lua @@ -20,6 +20,7 @@ rule("dlang.build") set_sourcekinds("dc") + add_deps("dlang.build.optimization") on_build_files("private.action.build.object", {batch = true}) on_load(function (target) local toolchains = target:get("toolchains") or get_config("toolchain") -- cgit v1.3.1