Skip to content

Commit d1d99cd

Browse files
committed
improve llvm for macos
1 parent a0db85a commit d1d99cd

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

xmake/toolchains/llvm/check.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ import("detect.sdks.find_cross_toolchain")
2828
-- find xcode on macos
2929
function _find_xcode(toolchain)
3030

31+
-- get apple device
32+
local appledev = toolchain:config("appledev") or config.get("appledev")
33+
if appledev and appledev == "simulator" then
34+
appledev = "simulator"
35+
elseif not toolchain:is_plat("macosx") and toolchain:is_arch("i386", "x86_64") then
36+
appledev = "simulator"
37+
end
38+
3139
-- find xcode
3240
local xcode_sdkver = toolchain:config("xcode_sdkver") or config.get("xcode_sdkver")
3341
local xcode = find_xcode(toolchain:config("xcode") or config.get("xcode"), {force = true, verbose = true,
@@ -46,8 +54,14 @@ function _find_xcode(toolchain)
4654
config.set("xcode", xcode.sdkdir, {force = true, readonly = true})
4755
cprint("checking for Xcode directory ... ${color.success}%s", xcode.sdkdir)
4856
end
57+
local target_minver = toolchain:config("target_minver") or config.get("target_minver")
58+
if xcode_sdkver and not target_minver then
59+
target_minver = xcode.target_minver
60+
end
4961
toolchain:config_set("xcode", xcode.sdkdir)
5062
toolchain:config_set("xcode_sdkver", xcode_sdkver)
63+
toolchain:config_set("target_minver", target_minver)
64+
toolchain:config_set("appledev", appledev)
5165
toolchain:configs_save()
5266
cprint("checking for SDK version of Xcode for %s (%s) ... ${color.success}%s", toolchain:plat(), toolchain:arch(), xcode_sdkver)
5367
end

xmake/toolchains/llvm/xmake.lua

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,74 @@ toolchain("llvm")
4848
toolchain:add("runtimes", "MT", "MTd", "MD", "MDd")
4949
end
5050

51-
-- add march flags
52-
local march
51+
-- add target flags
52+
local target
5353
if toolchain:is_plat("windows") and not is_host("windows") then
54-
-- cross-compilation for windows
5554
if toolchain:is_arch("i386", "x86") then
56-
march = "-target i386-pc-windows-msvc"
55+
target = "i386-pc-windows-msvc"
5756
else
58-
march = "-target x86_64-pc-windows-msvc"
57+
target = "x86_64-pc-windows-msvc"
5958
end
59+
elseif toolchain:is_plat("macosx") then
60+
local arch = toolchain:arch()
61+
local target_minver = toolchain:config("target_minver")
62+
local appledev = toolchain:config("appledev")
63+
if target_minver then
64+
target = ("%s-apple-macos%s"):format(arch, target_minver)
65+
if appledev == "catalyst" then
66+
target = ("%s-apple-ios%s-macabi"):format(arch, target_minver)
67+
end
68+
end
69+
elseif toolchain:is_plat("cross") then
70+
local cross = toolchain:cross():gsub("(.*)%-$", "%1")
71+
target = "--target=" .. cross
72+
end
73+
local target_flags
74+
if target then
75+
target_flags = "--target=" .. target
76+
elseif toolchain:is_arch("x86_64", "x64") then
77+
target_flags = "-m64"
78+
elseif toolchain:is_arch("i386", "x86") then
79+
target_flags = "-m32"
80+
end
81+
if target_flags then
82+
toolchain:add("cxflags", target_flags)
83+
toolchain:add("mxflags", target_flags)
84+
toolchain:add("asflags", target_flags)
85+
toolchain:add("ldflags", target_flags)
86+
toolchain:add("shflags", target_flags)
87+
end
88+
89+
-- init flags for platform
90+
if toolchain:is_plat("windows") and not is_host("windows") then
6091
toolchain:add("ldflags", "-fuse-ld=lld")
6192
toolchain:add("shflags", "-fuse-ld=lld")
93+
elseif toolchain:is_plat("macosx") then
94+
local xcode_dir = get_config("xcode")
95+
local xcode_sdkver = toolchain:config("xcode_sdkver")
96+
local xcode_sdkdir = nil
97+
if xcode_dir and xcode_sdkver then
98+
xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
99+
toolchain:add("cxflags", {"-isysroot", xcode_sdkdir})
100+
toolchain:add("mxflags", {"-isysroot", xcode_sdkdir})
101+
toolchain:add("ldflags", {"-isysroot", xcode_sdkdir})
102+
toolchain:add("shflags", {"-isysroot", xcode_sdkdir})
103+
else
104+
-- @see https://github.com/xmake-io/xmake/issues/1179
105+
local macsdk = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
106+
if os.exists(macsdk) then
107+
toolchain:add("cxflags", {"-isysroot", macsdk})
108+
toolchain:add("mxflags", {"-isysroot", macsdk})
109+
toolchain:add("ldflags", {"-isysroot", macsdk})
110+
toolchain:add("shflags", {"-isysroot", macsdk})
111+
end
112+
end
113+
toolchain:add("mxflags", "-fobjc-arc")
62114
elseif toolchain:is_plat("cross") then
63115
local sysroot
64116
local sdkdir = toolchain:sdkdir()
65117
local bindir = toolchain:bindir()
66118
local cross = toolchain:cross():gsub("(.*)%-$", "%1")
67-
march = "--target=" .. cross
68119
if bindir and os.isexec(path.join(bindir, cross .. "-gcc" .. (is_host("windows") and ".exe" or ""))) then
69120
local gcc_toolchain = path.directory(bindir)
70121
toolchain:add("cxflags", "--gcc-toolchain=" .. gcc_toolchain)
@@ -86,41 +137,6 @@ toolchain("llvm")
86137
toolchain:add("ldflags", "--sysroot=" .. sysroot)
87138
toolchain:add("shflags", "--sysroot=" .. sysroot)
88139
end
89-
elseif toolchain:is_arch("x86_64", "x64") then
90-
march = "-m64"
91-
elseif toolchain:is_arch("i386", "x86") then
92-
march = "-m32"
93-
end
94-
if march then
95-
toolchain:add("cxflags", march)
96-
toolchain:add("mxflags", march)
97-
toolchain:add("asflags", march)
98-
toolchain:add("ldflags", march)
99-
toolchain:add("shflags", march)
100-
end
101-
102-
-- init flags for macOS
103-
if toolchain:is_plat("macosx") then
104-
local xcode_dir = get_config("xcode")
105-
local xcode_sdkver = toolchain:config("xcode_sdkver")
106-
local xcode_sdkdir = nil
107-
if xcode_dir and xcode_sdkver then
108-
xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
109-
toolchain:add("cxflags", {"-isysroot", xcode_sdkdir})
110-
toolchain:add("mxflags", {"-isysroot", xcode_sdkdir})
111-
toolchain:add("ldflags", {"-isysroot", xcode_sdkdir})
112-
toolchain:add("shflags", {"-isysroot", xcode_sdkdir})
113-
else
114-
-- @see https://github.com/xmake-io/xmake/issues/1179
115-
local macsdk = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
116-
if os.exists(macsdk) then
117-
toolchain:add("cxflags", {"-isysroot", macsdk})
118-
toolchain:add("mxflags", {"-isysroot", macsdk})
119-
toolchain:add("ldflags", {"-isysroot", macsdk})
120-
toolchain:add("shflags", {"-isysroot", macsdk})
121-
end
122-
end
123-
toolchain:add("mxflags", "-fobjc-arc")
124140
end
125141

126142
-- add bin search library for loading some dependent .dll files windows

0 commit comments

Comments
 (0)