diff options
| author | ruki <[email protected]> | 2025-05-21 08:30:15 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-21 08:30:15 +0800 |
| commit | 25b22ca42b29389dd328003c5c6acc8d8af746ba (patch) | |
| tree | be5ac57829e2306e10696393c5a71c7ac80b652f | |
| parent | 4d26f861b9f3890bd677a72c7cddd98465f2d670 (diff) | |
| parent | adefc665f025751b078238cf4a07ae8751f4b83b (diff) | |
Merge pull request #6471 from xmake-io/llvm
improve llvm cross-compilation
| -rw-r--r-- | xmake/toolchains/llvm/check.lua | 19 | ||||
| -rw-r--r-- | xmake/toolchains/llvm/xmake.lua | 98 |
2 files changed, 74 insertions, 43 deletions
diff --git a/xmake/toolchains/llvm/check.lua b/xmake/toolchains/llvm/check.lua index a398d26df..933146fbf 100644 --- a/xmake/toolchains/llvm/check.lua +++ b/xmake/toolchains/llvm/check.lua @@ -28,6 +28,14 @@ import("detect.sdks.find_cross_toolchain") -- find xcode on macos function _find_xcode(toolchain) + -- get apple device + local appledev = toolchain:config("appledev") or config.get("appledev") + if appledev and appledev == "simulator" then + appledev = "simulator" + elseif not toolchain:is_plat("macosx") and toolchain:is_arch("i386", "x86_64") then + appledev = "simulator" + end + -- find xcode local xcode_sdkver = toolchain:config("xcode_sdkver") or config.get("xcode_sdkver") local xcode = find_xcode(toolchain:config("xcode") or config.get("xcode"), {force = true, verbose = true, @@ -46,8 +54,14 @@ function _find_xcode(toolchain) config.set("xcode", xcode.sdkdir, {force = true, readonly = true}) cprint("checking for Xcode directory ... ${color.success}%s", xcode.sdkdir) end + local target_minver = toolchain:config("target_minver") or config.get("target_minver") + if xcode_sdkver and not target_minver then + target_minver = xcode.target_minver + end toolchain:config_set("xcode", xcode.sdkdir) toolchain:config_set("xcode_sdkver", xcode_sdkver) + toolchain:config_set("target_minver", target_minver) + toolchain:config_set("appledev", appledev) toolchain:configs_save() cprint("checking for SDK version of Xcode for %s (%s) ... ${color.success}%s", toolchain:plat(), toolchain:arch(), xcode_sdkver) end @@ -84,7 +98,7 @@ function main(toolchain) end -- find cross toolchain from external envirnoment - local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir, cross = cross}) + local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir}) if not cross_toolchain then -- find it from packages for _, package in ipairs(toolchain:packages()) do @@ -98,7 +112,7 @@ function main(toolchain) end end if cross_toolchain then - toolchain:config_set("cross", cross_toolchain.cross) + toolchain:config_set("cross", cross) toolchain:config_set("bindir", cross_toolchain.bindir) toolchain:config_set("sdkdir", cross_toolchain.sdkdir) toolchain:configs_save() @@ -116,3 +130,4 @@ function main(toolchain) end return cross_toolchain end + diff --git a/xmake/toolchains/llvm/xmake.lua b/xmake/toolchains/llvm/xmake.lua index 57638e4f6..0cd42f525 100644 --- a/xmake/toolchains/llvm/xmake.lua +++ b/xmake/toolchains/llvm/xmake.lua @@ -48,27 +48,73 @@ toolchain("llvm") toolchain:add("runtimes", "MT", "MTd", "MD", "MDd") end - -- add march flags - local march + -- add target flags + local target if toolchain:is_plat("windows") and not is_host("windows") then - -- cross-compilation for windows if toolchain:is_arch("i386", "x86") then - march = "-target i386-pc-windows-msvc" + target = "i386-pc-windows-msvc" else - march = "-target x86_64-pc-windows-msvc" + target = "x86_64-pc-windows-msvc" end - toolchain:add("ldflags", "-fuse-ld=lld") - toolchain:add("shflags", "-fuse-ld=lld") + elseif toolchain:is_plat("macosx") then + local arch = toolchain:arch() + local target_minver = toolchain:config("target_minver") + local appledev = toolchain:config("appledev") + if target_minver then + target = ("%s-apple-macos%s"):format(arch, target_minver) + if appledev == "catalyst" then + target = ("%s-apple-ios%s-macabi"):format(arch, target_minver) + end + end + elseif toolchain:is_plat("cross") then + target = toolchain:cross():gsub("(.*)%-$", "%1") + end + local target_flags + if target then + target_flags = "--target=" .. target elseif toolchain:is_arch("x86_64", "x64") then - march = "-m64" + target_flags = "-m64" elseif toolchain:is_arch("i386", "x86") then - march = "-m32" + target_flags = "-m32" + end + if target_flags then + toolchain:add("cxflags", target_flags) + toolchain:add("mxflags", target_flags) + toolchain:add("asflags", target_flags) + toolchain:add("ldflags", target_flags) + toolchain:add("shflags", target_flags) + end + + -- init flags for platform + if toolchain:is_plat("windows") and not is_host("windows") then + toolchain:add("ldflags", "-fuse-ld=lld") + toolchain:add("shflags", "-fuse-ld=lld") + elseif toolchain:is_plat("macosx") then + local xcode_dir = get_config("xcode") + local xcode_sdkver = toolchain:config("xcode_sdkver") + local xcode_sdkdir = nil + if xcode_dir and xcode_sdkver then + xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk" + toolchain:add("cxflags", {"-isysroot", xcode_sdkdir}) + toolchain:add("mxflags", {"-isysroot", xcode_sdkdir}) + toolchain:add("ldflags", {"-isysroot", xcode_sdkdir}) + toolchain:add("shflags", {"-isysroot", xcode_sdkdir}) + else + -- @see https://github.com/xmake-io/xmake/issues/1179 + local macsdk = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" + if os.exists(macsdk) then + toolchain:add("cxflags", {"-isysroot", macsdk}) + toolchain:add("mxflags", {"-isysroot", macsdk}) + toolchain:add("ldflags", {"-isysroot", macsdk}) + toolchain:add("shflags", {"-isysroot", macsdk}) + end + end + toolchain:add("mxflags", "-fobjc-arc") elseif toolchain:is_plat("cross") then local sysroot local sdkdir = toolchain:sdkdir() local bindir = toolchain:bindir() local cross = toolchain:cross():gsub("(.*)%-$", "%1") - march = "--target=" .. cross if bindir and os.isexec(path.join(bindir, cross .. "-gcc" .. (is_host("windows") and ".exe" or ""))) then local gcc_toolchain = path.directory(bindir) toolchain:add("cxflags", "--gcc-toolchain=" .. gcc_toolchain) @@ -91,37 +137,6 @@ toolchain("llvm") toolchain:add("shflags", "--sysroot=" .. sysroot) end 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 - - -- init flags for macOS - if toolchain:is_plat("macosx") then - local xcode_dir = get_config("xcode") - local xcode_sdkver = toolchain:config("xcode_sdkver") - local xcode_sdkdir = nil - if xcode_dir and xcode_sdkver then - xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk" - toolchain:add("cxflags", {"-isysroot", xcode_sdkdir}) - toolchain:add("mxflags", {"-isysroot", xcode_sdkdir}) - toolchain:add("ldflags", {"-isysroot", xcode_sdkdir}) - toolchain:add("shflags", {"-isysroot", xcode_sdkdir}) - else - -- @see https://github.com/xmake-io/xmake/issues/1179 - local macsdk = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" - if os.exists(macsdk) then - toolchain:add("cxflags", {"-isysroot", macsdk}) - toolchain:add("mxflags", {"-isysroot", macsdk}) - toolchain:add("ldflags", {"-isysroot", macsdk}) - toolchain:add("shflags", {"-isysroot", macsdk}) - end - end - toolchain:add("mxflags", "-fobjc-arc") - end -- add bin search library for loading some dependent .dll files windows local bindir = toolchain:bindir() @@ -129,3 +144,4 @@ toolchain("llvm") toolchain:add("runenvs", "PATH", bindir) end end) + |
