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++/build_optimization/config.lua | 95 -------------------------- xmake/rules/c++/build_optimization/xmake.lua | 32 --------- xmake/rules/c++/build_sanitizer/config.lua | 98 --------------------------- xmake/rules/c++/build_sanitizer/xmake.lua | 44 ------------ xmake/rules/c++/config/basic.lua | 38 +++++++++++ xmake/rules/c++/config/main.lua | 38 +++++++++++ xmake/rules/c++/config/optimization.lua | 92 +++++++++++++++++++++++++ xmake/rules/c++/config/sanitizer.lua | 97 ++++++++++++++++++++++++++ xmake/rules/c++/openmp/load.lua | 1 + xmake/rules/c++/openmp/xmake.lua | 5 +- xmake/rules/c++/xmake.lua | 24 ++----- xmake/rules/objc++/config/basic.lua | 42 ++++++++++++ xmake/rules/objc++/config/main.lua | 33 +++++++++ xmake/rules/objc++/xmake.lua | 26 +++---- 14 files changed, 358 insertions(+), 307 deletions(-) delete mode 100644 xmake/rules/c++/build_optimization/config.lua delete mode 100644 xmake/rules/c++/build_optimization/xmake.lua delete mode 100644 xmake/rules/c++/build_sanitizer/config.lua delete mode 100644 xmake/rules/c++/build_sanitizer/xmake.lua create mode 100644 xmake/rules/c++/config/basic.lua create mode 100644 xmake/rules/c++/config/main.lua create mode 100644 xmake/rules/c++/config/optimization.lua create mode 100644 xmake/rules/c++/config/sanitizer.lua create mode 100644 xmake/rules/objc++/config/basic.lua create mode 100644 xmake/rules/objc++/config/main.lua diff --git a/xmake/rules/c++/build_optimization/config.lua b/xmake/rules/c++/build_optimization/config.lua deleted file mode 100644 index 686ef76c5..000000000 --- a/xmake/rules/c++/build_optimization/config.lua +++ /dev/null @@ -1,95 +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, Xmake Open Source Community. --- --- @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" 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 - -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 deleted file mode 100644 index 82693035e..000000000 --- a/xmake/rules/c++/build_optimization/xmake.lua +++ /dev/null @@ -1,32 +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, Xmake Open Source Community. --- --- @author ruki --- @file xmake.lua --- - --- define rule: c.build.optimization -rule("c.build.optimization") - on_config(function (target) - import("config")(target, "cc") - end) - --- define rule: c++.build.optimization -rule("c++.build.optimization") - on_config(function (target) - import("config")(target, "cxx") - end) - diff --git a/xmake/rules/c++/build_sanitizer/config.lua b/xmake/rules/c++/build_sanitizer/config.lua deleted file mode 100644 index be5ba7225..000000000 --- a/xmake/rules/c++/build_sanitizer/config.lua +++ /dev/null @@ -1,98 +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, Xmake Open Source Community. --- --- @author ruki --- @file config.lua --- - --- imports -import("core.tool.compiler") -import("core.project.project") -import("lib.detect.find_tool") -import("core.base.semver") - --- add build sanitizer -function _add_build_sanitizer(target, sourcekind, checkmode) - - -- add cflags - local _, cc = target:tool(sourcekind) - local flagnames = { - cc = "cflags", - cxx = "cxxflags", - mm = "mflags", - mxx = "mxflags" - } - local flagname = flagnames[sourcekind] - if flagname and target:has_tool(sourcekind, "cl", "clang", "clangxx", "clang_cl", "gcc", "gxx") then - target:add(flagname, "-fsanitize=" .. checkmode, {force = true}) - end - - -- add ldflags and shflags - -- msvc does not have an fsanitize linker flag, so the 'link' tool is excluded - if target:has_tool("ld", "clang", "clangxx", "gcc", "gxx") then - target:add("ldflags", "-fsanitize=" .. checkmode, {force = true}) - target:add("shflags", "-fsanitize=" .. checkmode, {force = true}) - end -end - -function main(target, sourcekind) - local sanitizer = false - for _, checkmode in ipairs({"address", "thread", "memory", "leak", "undefined"}) do - local enabled = target:policy("build.sanitizer." .. checkmode) - if enabled == nil then - enabled = project.policy("build.sanitizer." .. checkmode) - end - if enabled then - _add_build_sanitizer(target, sourcekind, checkmode) - sanitizer = true - end - end - - if sanitizer then - - -- enable the debug symbols for sanitizer - if not target:get("symbols") then - target:set("symbols", "debug") - end - - -- we need to load runenvs for msvc - -- @see https://github.com/xmake-io/xmake/issues/4176 - if target:is_plat("windows") and target:is_binary() then - if target:has_tool("cxx", "clang_cl") then - local clang_cl = target:toolchain("clang-cl") - if clang_cl then - local envs = clang_cl:runenvs() - local vscmd_ver = envs and envs.VSCMD_VER - if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then - local clang_cl_tool = assert(find_tool("clang-cl", {envs = envs}), "clang-cl not found!") - target:add("runenvs", "PATH", path.directory(clang_cl_tool.program)) - end - end - else - local msvc = target:toolchain("msvc") - if msvc then - local envs = msvc:runenvs() - local vscmd_ver = envs and envs.VSCMD_VER - if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then - local cl = assert(find_tool("cl", {envs = envs}), "cl not found!") - target:add("runenvs", "PATH", path.directory(cl.program)) - end - end - end - end - end -end - diff --git a/xmake/rules/c++/build_sanitizer/xmake.lua b/xmake/rules/c++/build_sanitizer/xmake.lua deleted file mode 100644 index 89149f414..000000000 --- a/xmake/rules/c++/build_sanitizer/xmake.lua +++ /dev/null @@ -1,44 +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, Xmake Open Source Community. --- --- @author ruki --- @file xmake.lua --- - --- define rule: c.build.sanitizer -rule("c.build.sanitizer") - on_config(function (target) - import("config")(target, "cc") - end) - --- define rule: c++.build.sanitizer -rule("c++.build.sanitizer") - on_config(function (target) - import("config")(target, "cxx") - end) - --- define rule: objc.build.sanitizer -rule("objc.build.sanitizer") - on_config(function (target) - import("config")(target, "mm") - end) - --- define rule: objc++.build.sanitizer -rule("objc++.build.sanitizer") - on_config(function (target) - import("config")(target, "mxx") - end) - diff --git a/xmake/rules/c++/config/basic.lua b/xmake/rules/c++/config/basic.lua new file mode 100644 index 000000000..e9c810417 --- /dev/null +++ b/xmake/rules/c++/config/basic.lua @@ -0,0 +1,38 @@ +--!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 basic.lua +-- + +-- main entry +function main(target, sourcekind) + -- enable c++ exceptions by default on Windows + if sourcekind == "cxx" and target:is_plat("windows") and not target:get("exceptions") then + target:set("exceptions", "cxx") + end + + -- https://github.com/xmake-io/xmake/issues/4621 + -- tcc on Windows static library needs special handling + if target:is_plat("windows") and target:is_static() then + local toolname = sourcekind == "cxx" and "cxx" or "cc" + if target:has_tool(toolname, "tcc") then + target:set("extension", ".a") + target:set("prefixname", "lib") + end + end +end + diff --git a/xmake/rules/c++/config/main.lua b/xmake/rules/c++/config/main.lua new file mode 100644 index 000000000..78d1362af --- /dev/null +++ b/xmake/rules/c++/config/main.lua @@ -0,0 +1,38 @@ +--!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 main.lua +-- + +-- imports +import("basic", {alias = "config_basic"}) +import("optimization", {alias = "config_optimization"}) +import("sanitizer", {alias = "config_sanitizer"}) + +-- main entry +function main(target, sourcekind) + + -- config basic configs + config_basic(target, sourcekind) + + -- config optimization configs + config_optimization(target, sourcekind) + + -- config sanitizer configs + config_sanitizer(target, sourcekind) +end + 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 + diff --git a/xmake/rules/c++/config/sanitizer.lua b/xmake/rules/c++/config/sanitizer.lua new file mode 100644 index 000000000..b6fe0df7e --- /dev/null +++ b/xmake/rules/c++/config/sanitizer.lua @@ -0,0 +1,97 @@ +--!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 sanitizer.lua +-- + +-- imports +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) + -- add cflags + local _, cc = target:tool(sourcekind) + local flagnames = { + cc = "cflags", + cxx = "cxxflags", + mm = "mflags", + mxx = "mxflags" + } + local flagname = flagnames[sourcekind] + if flagname and target:has_tool(sourcekind, "cl", "clang", "clangxx", "clang_cl", "gcc", "gxx") then + target:add(flagname, "-fsanitize=" .. checkmode, {force = true}) + end + + -- add ldflags and shflags + -- msvc does not have an fsanitize linker flag, so the 'link' tool is excluded + if target:has_tool("ld", "clang", "clangxx", "gcc", "gxx") then + target:add("ldflags", "-fsanitize=" .. checkmode, {force = true}) + target:add("shflags", "-fsanitize=" .. checkmode, {force = true}) + end +end + +-- main entry +function main(target, sourcekind) + local sanitizer = false + for _, checkmode in ipairs({"address", "thread", "memory", "leak", "undefined"}) do + local enabled = target:policy("build.sanitizer." .. checkmode) + if enabled == nil then + enabled = project.policy("build.sanitizer." .. checkmode) + end + if enabled then + _add_build_sanitizer(target, sourcekind, checkmode) + sanitizer = true + end + end + + if sanitizer then + -- enable the debug symbols for sanitizer + if not target:get("symbols") then + target:set("symbols", "debug") + end + + -- we need to load runenvs for msvc + -- @see https://github.com/xmake-io/xmake/issues/4176 + if target:is_plat("windows") and target:is_binary() then + if target:has_tool("cxx", "clang_cl") then + local clang_cl = target:toolchain("clang-cl") + if clang_cl then + local envs = clang_cl:runenvs() + local vscmd_ver = envs and envs.VSCMD_VER + if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then + local clang_cl_tool = assert(find_tool("clang-cl", {envs = envs}), "clang-cl not found!") + target:add("runenvs", "PATH", path.directory(clang_cl_tool.program)) + end + end + else + local msvc = target:toolchain("msvc") + if msvc then + local envs = msvc:runenvs() + local vscmd_ver = envs and envs.VSCMD_VER + if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then + local cl = assert(find_tool("cl", {envs = envs}), "cl not found!") + target:add("runenvs", "PATH", path.directory(cl.program)) + end + end + end + end + end +end + diff --git a/xmake/rules/c++/openmp/load.lua b/xmake/rules/c++/openmp/load.lua index a1ced6cac..7e4969ff0 100644 --- a/xmake/rules/c++/openmp/load.lua +++ b/xmake/rules/c++/openmp/load.lua @@ -39,3 +39,4 @@ 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 052ad5370..e7a554c5d 100644 --- a/xmake/rules/c++/openmp/xmake.lua +++ b/xmake/rules/c++/openmp/xmake.lua @@ -20,12 +20,13 @@ -- define rule: c.openmp rule("c.openmp") - on_config(function (target) + on_load(function (target) import("load")(target, "cc") end) -- define rule: c++.openmp rule("c++.openmp") - on_config(function (target) + on_load(function (target) import("load")(target, "cxx") end) + diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index e0673837d..23e5f27bc 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -20,31 +20,19 @@ rule("c.build") set_sourcekinds("cc") - add_deps("c.build.pcheader", "c.build.optimization", "c.build.sanitizer") - on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) + add_deps("c.build.pcheader") on_config(function (target) - -- https://github.com/xmake-io/xmake/issues/4621 - if target:is_plat("windows") and target:is_static() and target:has_tool("cc", "tcc") then - target:set("extension", ".a") - target:set("prefixname", "lib") - end + import("rules.c++.config")(target, "cc") end) + on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) rule("c++.build") set_sourcekinds("cxx") - add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization", "c++.build.sanitizer") - on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) + add_deps("c++.build.pcheader", "c++.build.modules") on_config(function (target) - -- enable c++ exceptions by default - if target:is_plat("windows") and not target:get("exceptions") then - target:set("exceptions", "cxx") - end - -- https://github.com/xmake-io/xmake/issues/4621 - if target:is_plat("windows") and target:is_static() and target:has_tool("cxx", "tcc") then - target:set("extension", ".a") - target:set("prefixname", "lib") - end + import("rules.c++.config")(target, "cxx") end) + on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) rule("c") diff --git a/xmake/rules/objc++/config/basic.lua b/xmake/rules/objc++/config/basic.lua new file mode 100644 index 000000000..cf1da1e12 --- /dev/null +++ b/xmake/rules/objc++/config/basic.lua @@ -0,0 +1,42 @@ +--!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 basic.lua +-- + +-- main entry +function main(target, sourcekind) + -- objc basic configs + if sourcekind == "mm" then + -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it + if target:values("objc.build.arc") == false then + target:add("mflags", "-fno-objc-arc") + end + if target:is_plat("macosx", "iphoneos", "watchos") then + target:add("frameworks", "Foundation", "CoreFoundation") + end + elseif sourcekind == "mxx" then + -- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it + if target:values("objc++.build.arc") == false then + target:add("mxxflags", "-fno-objc-arc") + end + if target:is_plat("macosx", "iphoneos", "watchos") then + target:add("frameworks", "Foundation", "CoreFoundation") + end + end +end + diff --git a/xmake/rules/objc++/config/main.lua b/xmake/rules/objc++/config/main.lua new file mode 100644 index 000000000..45755edb8 --- /dev/null +++ b/xmake/rules/objc++/config/main.lua @@ -0,0 +1,33 @@ +--!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 main.lua +-- + +-- imports +import("rules.objc++.config.basic", {rootdir = os.programdir(), alias = "config_basic"}) +import("rules.c++.config", {rootdir = os.programdir(), alias = "config_cxx"}) + +-- main entry +function main(target, sourcekind) + -- handle objc++ basic configs + config_basic(target, sourcekind) + + -- handle c++ configs (optimization, sanitizer, etc.) + config_cxx(target, sourcekind) +end + diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua index fe857deef..2ff0ecb8c 100644 --- a/xmake/rules/objc++/xmake.lua +++ b/xmake/rules/objc++/xmake.lua @@ -21,30 +21,20 @@ -- define rule: objc.build rule("objc.build") set_sourcekinds("mm") - add_deps("objc.build.pcheader", "c.build.optimization", "objc.build.sanitizer") - after_load(function (target) - -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it - if target:values("objc.build.arc") == false then - target:add("mflags", "-fno-objc-arc") - end - if target:is_plat("macosx", "iphoneos", "watchos") then - target:add("frameworks", "Foundation", "CoreFoundation") - end + 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}) -- define rule: objc++.build rule("objc++.build") set_sourcekinds("mxx") - add_deps("objc++.build.pcheader", "c++.build.optimization", "objc++.build.sanitizer") - after_load(function (target) - -- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it - if target:values("objc++.build.arc") == false then - target:add("mxxflags", "-fno-objc-arc") - end - if target:is_plat("macosx", "iphoneos", "watchos") then - target:add("frameworks", "Foundation", "CoreFoundation") - end + 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 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(-) 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 From 9d7699a185cf459b33782fc314c39fdd848de816 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Dec 2025 22:57:33 +0800 Subject: improve basic --- xmake/rules/objc++/config/basic.lua | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/xmake/rules/objc++/config/basic.lua b/xmake/rules/objc++/config/basic.lua index cf1da1e12..4da2eed43 100644 --- a/xmake/rules/objc++/config/basic.lua +++ b/xmake/rules/objc++/config/basic.lua @@ -18,22 +18,16 @@ -- @file basic.lua -- --- main entry function main(target, sourcekind) - -- objc basic configs - if sourcekind == "mm" then - -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it - if target:values("objc.build.arc") == false then - target:add("mflags", "-fno-objc-arc") - end - if target:is_plat("macosx", "iphoneos", "watchos") then - target:add("frameworks", "Foundation", "CoreFoundation") - end - elseif sourcekind == "mxx" then - -- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it - if target:values("objc++.build.arc") == false then - target:add("mxxflags", "-fno-objc-arc") + if sourcekind == "mm" or sourcekind == "mxx" then + -- deprecated, we only need to use `add_mflags("-fno-objc-arc")` or `add_mxxflags("-fno-objc-arc")` to override it + local arc_value = sourcekind == "mm" and target:values("objc.build.arc") or target:values("objc++.build.arc") + if arc_value == false then + local flag_name = sourcekind == "mm" and "mflags" or "mxxflags" + target:add(flag_name, "-fno-objc-arc") end + + -- add frameworks for Apple platforms if target:is_plat("macosx", "iphoneos", "watchos") then target:add("frameworks", "Foundation", "CoreFoundation") end -- cgit v1.3.1 From ca52efa1eff5af56e0dc9197616e709ce1b97a25 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Dec 2025 22:59:28 +0800 Subject: fix rules --- xmake/rules/c++/xmake.lua | 4 ++-- xmake/rules/objc++/xmake.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 23e5f27bc..dbb24451d 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -22,7 +22,7 @@ rule("c.build") set_sourcekinds("cc") add_deps("c.build.pcheader") on_config(function (target) - import("rules.c++.config")(target, "cc") + import("config")(target, "cc") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) @@ -30,7 +30,7 @@ rule("c++.build") set_sourcekinds("cxx") add_deps("c++.build.pcheader", "c++.build.modules") on_config(function (target) - import("rules.c++.config")(target, "cxx") + import("config")(target, "cxx") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua index 321aa131a..23cdc3faa 100644 --- a/xmake/rules/objc++/xmake.lua +++ b/xmake/rules/objc++/xmake.lua @@ -23,7 +23,7 @@ rule("objc.build") set_sourcekinds("mm") add_deps("objc.build.pcheader") on_config(function (target) - import("rules.objc++.config")(target, "mm") + import("config")(target, "mm") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) @@ -32,7 +32,7 @@ rule("objc++.build") set_sourcekinds("mxx") add_deps("objc++.build.pcheader") on_config(function (target) - import("rules.objc++.config")(target, "mxx") + import("config")(target, "mxx") end) on_build_files("private.action.build.object", {jobgraph = true, batch = true, distcc = true}) -- cgit v1.3.1 From 73e234824c235191f430400f4bb7b39421ee1fb4 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Dec 2025 23:25:17 +0800 Subject: update yml --- .github/workflows/solaris.yml | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/solaris.yml b/.github/workflows/solaris.yml index 9e044b036..6f4dbba76 100644 --- a/.github/workflows/solaris.yml +++ b/.github/workflows/solaris.yml @@ -19,17 +19,17 @@ jobs: script: | const fs = require('fs'); const outputFile = process.env.GITHUB_OUTPUT; - + // Always run for release events if (context.eventName === 'release') { fs.appendFileSync(outputFile, `should-run=true\n`); core.info('Release event detected. Will run tests.'); return; } - + // Execution probability (default 50%, can be overridden via env) const probability = parseFloat(process.env.RUN_PROBABILITY || '0.5'); - + // Generate deterministic "random" number based on commit SHA, run ID, and current time // Adding time ensures better randomness while keeping same commit/run consistent const timeSeed = Math.floor(Date.now() / (1000 * 60 * 60)); // Round to hour for consistency @@ -43,7 +43,7 @@ jobs: // Normalize to 0-1 range const random = Math.abs(hash) / 2147483647; const shouldRun = random < probability; - + // Use environment file instead of deprecated set-output fs.appendFileSync(outputFile, `should-run=${shouldRun}\n`); if (shouldRun) { @@ -72,7 +72,15 @@ jobs: with: usesh: true prepare: | - pkgutil -y -i socat git gmake bash gcc4g++ || pkgutil -y -i socat git gmake bash gcc5g++ || pkg install -y developer/gcc || true + # Try OpenCSW first (for older Solaris) + if command -v pkgutil >/dev/null 2>&1; then + pkgutil -U || true + pkgutil -y -i socat git gmake bash gcc4g++ 2>/dev/null || pkgutil -y -i socat git gmake bash gcc5g++ 2>/dev/null || true + fi + # Try IPS (for newer Solaris) + if command -v pkg >/dev/null 2>&1; then + pkg install --accept developer/gcc developer/build/gnu-make developer/versioning/git || true + fi run: | cd $GITHUB_WORKSPACE bash ./configure -- cgit v1.3.1