From 9966baf23bc953789e4bdb41d065d82abbcf2dd9 Mon Sep 17 00:00:00 2001 From: Francesco Guastella Date: Tue, 7 Oct 2025 17:51:05 +0200 Subject: [PATCH] improve(toolchain): enhance gcc/clang support for cross-compilation The builtin gcc and clang toolchains did not handle cross-compilation correctly, as they were missing proper cross-prefix support. This patch updates their on_load functions to add the appropriate prefixing, aligning behavior with expected cross-compilation setups. --- xmake/toolchains/clang/load.lua | 37 ++++++++++++++++++++++++++++++-- xmake/toolchains/clang/xmake.lua | 29 ++----------------------- xmake/toolchains/gcc/xmake.lua | 31 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 29 deletions(-) diff --git a/xmake/toolchains/clang/load.lua b/xmake/toolchains/clang/load.lua index 7b50ac526f1..8a5acf0a51a 100644 --- a/xmake/toolchains/clang/load.lua +++ b/xmake/toolchains/clang/load.lua @@ -51,13 +51,46 @@ function _add_vsenv(toolchain, name, curenvs) end function main(toolchain, suffix) + import("core.base.option") + import("core.project.project") + + if project.policy("build.optimization.lto") then + toolchain:set("toolset", "ar", "llvm-ar" .. suffix) + toolchain:set("toolset", "ranlib", "llvm-ranlib" .. suffix) + end + + local march + if toolchain:is_arch("x86_64", "x64", "arm64") then + march = "-m64" + elseif toolchain:is_arch("i386", "x86", "i686") then + march = "-m32" + end + if march then + toolchain:add("cxflags", march) + toolchain:add("mxflags", march) + toolchain:add("asflags", march) + toolchain:add("ldflags", march) + toolchain:add("shflags", march) + end + if toolchain:is_plat("windows") then + toolchain:add("runtimes", "MT", "MTd", "MD", "MDd") + end + + local host_arch = os.arch() + local target_arch = toolchain:arch() + + if host_arch == target_arch then + -- Early exit: prevents further configuration of this toolchain + return + elseif option.get("verbose") then + cprint("${bright yellow}cross compiling from %s to %s", host_arch, target_arch) + end + local target if toolchain:is_arch("x86_64", "x64") then target = "x86_64" - march = "-m64" elseif toolchain:is_arch("i386", "x86", "i686") then target = "i686" - march = "-m32" elseif toolchain:is_arch("arm64", "aarch64") then target = "aarch64" elseif toolchain:is_arch("arm") then diff --git a/xmake/toolchains/clang/xmake.lua b/xmake/toolchains/clang/xmake.lua index b454afa869f..c7a7663980a 100644 --- a/xmake/toolchains/clang/xmake.lua +++ b/xmake/toolchains/clang/xmake.lua @@ -58,33 +58,8 @@ toolchain("clang" .. suffix) end) on_load(function (toolchain) - import("core.project.project") - - if project.policy("build.optimization.lto") then - toolchain:set("toolset", "ar", "llvm-ar" .. suffix) - toolchain:set("toolset", "ranlib", "llvm-ranlib" .. suffix) - end - - local march - if toolchain:is_arch("x86_64", "x64") then - march = "-m64" - elseif toolchain:is_arch("i386", "x86") then - march = "-m32" - end - if march then - toolchain:add("cxflags", march) - toolchain:add("mxflags", march) - toolchain:add("asflags", march) - toolchain:add("ldflags", march) - toolchain:add("shflags", march) - end - if toolchain:is_plat("windows") then - toolchain:add("runtimes", "MT", "MTd", "MD", "MDd") - end - if toolchain:is_plat("windows", "mingw") then - local rootdir = path.join(path.directory(os.scriptdir()), "clang") - import("load", {rootdir = rootdir})(toolchain, suffix) - end + local rootdir = path.join(path.directory(os.scriptdir()), "clang") + import("load", {rootdir = rootdir})(toolchain, suffix) end) end toolchain_clang() diff --git a/xmake/toolchains/gcc/xmake.lua b/xmake/toolchains/gcc/xmake.lua index 502f10c3b77..243ead06bd1 100644 --- a/xmake/toolchains/gcc/xmake.lua +++ b/xmake/toolchains/gcc/xmake.lua @@ -47,6 +47,7 @@ toolchain("gcc" .. suffix) end) on_load(function (toolchain) + import("core.base.option") -- add march flags local march @@ -62,6 +63,36 @@ toolchain("gcc" .. suffix) toolchain:add("ldflags", march) toolchain:add("shflags", march) end + + local host_arch = os.arch() + local target_arch = toolchain:arch() + + if host_arch == target_arch then + -- Early exit: prevents further configuration of this toolchain + return + elseif option.get("verbose") then + cprint("${bright yellow}cross compiling from %s to %s", host_arch, target_arch) + end + + local target + if toolchain:is_arch("x86_64", "x64") then + target = "x86_64" + elseif toolchain:is_arch("i386", "x86", "i686") then + target = "i686" + elseif toolchain:is_arch("arm64", "aarch64") then + target = "aarch64" + elseif toolchain:is_arch("arm") then + target = "armv7" + end + + -- TODO: Add support for more platforms, such as mingw. + if target and toolchain:is_plat("linux") then + target = target .. "-linux-gnu-" + toolchain:set("toolset", "cc", target .. "gcc" .. suffix) + toolchain:set("toolset", "cxx", target .. "g++" .. suffix, "gcc" .. suffix) + toolchain:set("toolset", "ld", target .. "g++" .. suffix, "gcc" .. suffix) + toolchain:set("toolset", "sh", target .. "g++" .. suffix, "gcc" .. suffix) + end end) end toolchain_gcc()