Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions xmake/toolchains/clang/load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +82 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This early return for native builds (host_arch == target_arch) is problematic. It causes the toolchain to skip crucial configuration steps that follow, such as setting up the Visual Studio environment for clang on Windows. This breaks native builds with the clang toolchain on Windows.

The logic should be restructured to only print a message for cross-compilation, without exiting early for native builds, allowing the subsequent configuration to always run.

    if host_arch ~= target_arch and 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
Expand Down
29 changes: 2 additions & 27 deletions xmake/toolchains/clang/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
31 changes: 31 additions & 0 deletions xmake/toolchains/gcc/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ toolchain("gcc" .. suffix)
end)

on_load(function (toolchain)
import("core.base.option")

-- add march flags
local march
Expand All @@ -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
Comment on lines +70 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and to make the logic more explicit, it's better to separate the early exit for native compilation from the verbose logging for cross-compilation. The elseif here implicitly depends on the failure of the first if condition, which can be slightly confusing to read.

        if host_arch == target_arch then
            -- Early exit for native compilation, as the rest of the logic is for cross-compilation.
            return
        end

        if 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()
Loading