From 5c4b98ecca5a0c437a1bb5679e4046f752ed1a42 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Dec 2025 22:49:56 +0800 Subject: improve c++/objc++ rules --- xmake/rules/c++/config/optimization.lua | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 xmake/rules/c++/config/optimization.lua (limited to 'xmake/rules/c++/config/optimization.lua') diff --git a/xmake/rules/c++/config/optimization.lua b/xmake/rules/c++/config/optimization.lua new file mode 100644 index 000000000..44b6ce755 --- /dev/null +++ b/xmake/rules/c++/config/optimization.lua @@ -0,0 +1,92 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file optimization.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") + +-- add lto optimization +function main(target, sourcekind) + if not (target:policy("build.optimization.lto") or project.policy("build.optimization.lto")) then + return + end + + -- 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" or cc == "clang_cl" then + target:add(cflag, "-flto=thin") + elseif cc == "gcc" or cc == "gxx" then + target:add(cflag, "-flto") + end + + -- add ldflags and shflags + local program, 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") + + -- On Darwin, when using -flto along with -g and compiling and linking in separate steps, + -- you also need to pass -Wl,-object_path_lto,.o at the linking step to instruct + -- the ld64 linker not to delete the temporary object file generated during Link Time Optimization + -- (this flag is automatically passed to the linker by Clang if compilation and linking are done in a single step). + -- + -- This allows debugging the executable as well as generating the .dSYM bundle using dsymutil(1). + -- + -- @see https://github.com/xmake-io/xmake/issues/7029 + -- https://clang.llvm.org/docs/CommandGuide/clang.html + if target:is_plat("macosx", "iphoneos", "watchos") then + local lto_objectfile = target:objectfile(target:targetfile() .. ".lto") + target:add("ldflags", "-Wl,-object_path_lto," .. lto_objectfile) + target:add("shflags", "-Wl,-object_path_lto," .. lto_objectfile) + end + 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 + + local program, ar = target:tool("ar") + if cc == "clang_cl" then + if ld == "link" then + wprint([[Unsupported toolset(%s) for lto, please use `set_toolset("ld", "lld-link")`]], ld) + target:set("toolset", "ld", "lld-link") + target:set("toolset", "sh", "lld-link") + end + if ar ~= "llvm-ar" and ar ~= "llvm_ar" then + wprint([[Unsupported toolset(%s) for lto, please use `set_toolset("ar", "llvm-ar")`]], ar) + target:set("toolset", "ar", "llvm-ar") + end + end +end + -- cgit v1.3.1 From a3f3678d1705dac4106dc95883d264534db159e2 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Dec 2025 22:52:26 +0800 Subject: improve objc configs --- xmake/rules/c++/config/optimization.lua | 31 ++++++++++++++++++++++++------- xmake/rules/c++/config/sanitizer.lua | 2 -- xmake/rules/c++/openmp/load.lua | 1 - xmake/rules/c++/openmp/xmake.lua | 5 ++--- xmake/rules/objc++/config/main.lua | 10 +++++++--- xmake/rules/objc++/xmake.lua | 2 -- 6 files changed, 33 insertions(+), 18 deletions(-) (limited to 'xmake/rules/c++/config/optimization.lua') diff --git a/xmake/rules/c++/config/optimization.lua b/xmake/rules/c++/config/optimization.lua index 44b6ce755..4332bd833 100644 --- a/xmake/rules/c++/config/optimization.lua +++ b/xmake/rules/c++/config/optimization.lua @@ -23,14 +23,16 @@ import("core.tool.compiler") import("core.project.project") -- add lto optimization -function main(target, sourcekind) - if not (target:policy("build.optimization.lto") or project.policy("build.optimization.lto")) then - return - end - +function _add_lto_optimization(target, sourcekind) -- add cflags local _, cc = target:tool(sourcekind) - local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" + local flagnames = { + cc = "cflags", + cxx = "cxxflags", + mm = "mflags", + mxx = "mxxflags" + } + local cflag = flagnames[sourcekind] or (sourcekind == "cxx" and "cxxflags" or "cflags") if cc == "cl" then target:add(cflag, "-GL") elseif cc == "clang" or cc == "clangxx" or cc == "clang_cl" then @@ -70,7 +72,14 @@ function main(target, sourcekind) -- @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) + local lang_map = { + cc = "c", + cxx = "cxx", + mm = "c", + mxx = "cxx" + } + local lang = lang_map[sourcekind] or "cxx" + local optimize_flags = compiler.map_flags(lang, "optimize", optimize) target:add("ldflags", optimize_flags) target:add("shflags", optimize_flags) end @@ -90,3 +99,11 @@ function main(target, sourcekind) end end +-- main entry +function main(target, sourcekind) + -- handle lto optimization + 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++/config/sanitizer.lua b/xmake/rules/c++/config/sanitizer.lua index b6fe0df7e..aeea8492f 100644 --- a/xmake/rules/c++/config/sanitizer.lua +++ b/xmake/rules/c++/config/sanitizer.lua @@ -22,7 +22,6 @@ import("core.project.project") import("lib.detect.find_tool") import("core.base.semver") -import("core.base.path") -- add build sanitizer function _add_build_sanitizer(target, sourcekind, checkmode) @@ -47,7 +46,6 @@ function _add_build_sanitizer(target, sourcekind, checkmode) end end --- main entry function main(target, sourcekind) local sanitizer = false for _, checkmode in ipairs({"address", "thread", "memory", "leak", "undefined"}) do diff --git a/xmake/rules/c++/openmp/load.lua b/xmake/rules/c++/openmp/load.lua index 7e4969ff0..a1ced6cac 100644 --- a/xmake/rules/c++/openmp/load.lua +++ b/xmake/rules/c++/openmp/load.lua @@ -39,4 +39,3 @@ function main(target, sourcekind) target:add(flag_name, "-Qopenmp") end end - diff --git a/xmake/rules/c++/openmp/xmake.lua b/xmake/rules/c++/openmp/xmake.lua index e7a554c5d..052ad5370 100644 --- a/xmake/rules/c++/openmp/xmake.lua +++ b/xmake/rules/c++/openmp/xmake.lua @@ -20,13 +20,12 @@ -- define rule: c.openmp rule("c.openmp") - on_load(function (target) + on_config(function (target) import("load")(target, "cc") end) -- define rule: c++.openmp rule("c++.openmp") - on_load(function (target) + on_config(function (target) import("load")(target, "cxx") end) - diff --git a/xmake/rules/objc++/config/main.lua b/xmake/rules/objc++/config/main.lua index 45755edb8..43d2b0096 100644 --- a/xmake/rules/objc++/config/main.lua +++ b/xmake/rules/objc++/config/main.lua @@ -20,14 +20,18 @@ -- imports import("rules.objc++.config.basic", {rootdir = os.programdir(), alias = "config_basic"}) -import("rules.c++.config", {rootdir = os.programdir(), alias = "config_cxx"}) +import("rules.c++.config.optimization", {rootdir = os.programdir(), alias = "config_optimization"}) +import("rules.c++.config.sanitizer", {rootdir = os.programdir(), alias = "config_sanitizer"}) -- main entry function main(target, sourcekind) -- handle objc++ basic configs config_basic(target, sourcekind) - -- handle c++ configs (optimization, sanitizer, etc.) - config_cxx(target, sourcekind) + -- handle optimization.lto + config_optimization(target, sourcekind) + + -- handle sanitizer + config_sanitizer(target, sourcekind) end diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua index 2ff0ecb8c..321aa131a 100644 --- a/xmake/rules/objc++/xmake.lua +++ b/xmake/rules/objc++/xmake.lua @@ -23,7 +23,6 @@ rule("objc.build") set_sourcekinds("mm") add_deps("objc.build.pcheader") on_config(function (target) - -- handle all configs import("rules.objc++.config")(target, "mm") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) @@ -33,7 +32,6 @@ rule("objc++.build") set_sourcekinds("mxx") add_deps("objc++.build.pcheader") on_config(function (target) - -- handle all configs import("rules.objc++.config")(target, "mxx") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) -- cgit v1.3.1