Skip to content

Commit 8f318f5

Browse files
committed
(llvm) add runtime dirs to runenvs (for asan and libunwind)
1 parent fb9ef47 commit 8f318f5

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

xmake/modules/core/tools/clang.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function nf_runtime(self, runtime, opt)
227227
-- @see https://discourse.llvm.org/t/improve-autolinking-of-compiler-rt-and-libc-on-windows-with-lld-link/71392/10
228228
-- and need manual setting of libc++ headerdirectory
229229
-- @see https://github.com/llvm/llvm-project/issues/79647
230-
local llvm_dirs = toolchain_utils.get_llvm_dirs(self)
230+
local llvm_dirs = toolchain_utils.get_llvm_dirs(self:toolchain(), self:toolchain():sdkdir())
231231

232232
if self:is_plat("windows") and runtime == "c++_shared" then
233233
if llvm_dirs.bin then

xmake/modules/private/utils/toolchain.lua

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ end
185185
function _get_llvm_resourcedir(toolchain)
186186
local llvm_resourcedir = _g._LLVM_resourceDIR
187187
if llvm_resourcedir == nil then
188-
local outdata = try { function() return os.iorunv(toolchain:get("cc"), {"-print-resource-dir"}, {envs = toolchain:runenvs()}) end }
188+
local outdata = try { function() return os.iorunv(toolchain:tool("cc"), {"-print-resource-dir"}, {envs = toolchain:runenvs()}) end }
189189
if outdata then
190190
llvm_resourcedir = path.normalize(outdata:trim())
191191
if not os.isdir(llvm_resourcedir) then
@@ -198,10 +198,10 @@ function _get_llvm_resourcedir(toolchain)
198198
end
199199

200200
-- get llvm sdk root directory
201-
function _get_llvm_rootdir(self)
201+
function _get_llvm_rootdir(toolchain)
202202
local llvm_rootdir = _g._LLVM_ROOTDIR
203203
if llvm_rootdir == nil then
204-
local resourcedir = _get_llvm_resourcedir(self)
204+
local resourcedir = _get_llvm_resourcedir(toolchain)
205205
if resourcedir then
206206
llvm_rootdir = path.normalize(path.join(resourcedir, "..", "..", ".."))
207207
if not os.isdir(llvm_rootdir) then
@@ -214,40 +214,50 @@ function _get_llvm_rootdir(self)
214214
end
215215

216216
-- find compiler-rt dir
217-
function _get_llvm_compiler_win_rtdir_and_link(self, target)
217+
function _get_llvm_compiler_rtdir_and_link(toolchain)
218218
import("lib.detect.find_tool")
219219

220-
local cc = self:get("cc")
220+
local cc = toolchain:tool("cc")
221221
local cc_tool = find_tool(cc, {version = true})
222222
if cc_tool and cc_tool.version then
223-
local resdir = _get_llvm_resourcedir(self)
223+
local resdir = _get_llvm_resourcedir(toolchain)
224224
if resdir then
225225
local res_libdir = path.join(resdir, "lib")
226226
-- when -DLLVM_ENABLE_TARGET_RUNTIME_DIR=OFF rtdir is windows/ and rtlink is clang_rt.builtinsi_<arch>.lib
227227
-- when ON rtdir is windows/<target-triple> and rtlink is clang_rt.builtins.lib
228-
local target_triple = _get_llvm_target_triple(self)
228+
local target_triple = _get_llvm_target_triple(toolchain)
229229
local arch = target_triple and target_triple:split("-")[1]
230230

231+
local plat
232+
if toolchain:is_plat("windows") then
233+
plat = "windows"
234+
elseif toolchain:is_plat("linux") then
235+
plat = "linux"
236+
elseif toolchain:is_plat("macosx", "ios", "watchos", "appletvos", "applexros") then
237+
plat = "darwin"
238+
end
239+
240+
231241
local tripletdir = target_triple and path.join(res_libdir, "windows", target_triple)
232242
tripletdir = os.isdir(tripletdir) or nil
233243

234-
local rtdir = tripletdir and path.join("windows", target_triple) or "windows"
235-
if os.isdir(path.join(res_libdir, rtdir)) then
244+
local rtdir = tripletdir and path.join(plat, target_triple) or plat
245+
if os.isdir(path.join(res_libdir, rtdir)) and toolchain:is_plat("windows") then
236246
local rtlink = "clang_rt.builtins" .. (tripletdir and ".lib" or ("-" .. arch .. ".lib"))
237247
if os.isfile(path.join(res_libdir, rtdir, rtlink)) then
238-
return res_libdir, path.join(rtdir, rtlink)
248+
return res_libdir, path.join(rtdir, rtlink), path.join(res_libdir, rtdir)
239249
end
240250
end
241-
return res_libdir
251+
return res_libdir, nil, path.join(res_libdir, rtdir)
242252
end
243253
end
244254
end
245255

246256
-- get llvm target triple
247-
function _get_llvm_target_triple(self)
257+
function _get_llvm_target_triple(toolchain)
248258
local llvm_targettriple = _g._LLVM_TARGETTRIPLE
249259
if llvm_targettriple == nil then
250-
local outdata = try { function() return os.iorunv(self:program(), {"-print-target-triple"}, {envs = self:runenvs()}) end }
260+
local outdata = try { function() return os.iorunv(toolchain:tool("cc"), {"-print-target-triple"}, {envs = toolchain:runenvs()}) end }
251261
if outdata then
252262
llvm_targettriple = outdata:trim()
253263
end
@@ -257,49 +267,40 @@ function _get_llvm_target_triple(self)
257267
end
258268

259269
-- get llvm toolchain dirs
260-
function get_llvm_dirs(toolchain)
270+
function get_llvm_dirs(toolchain, rootdir)
261271
local llvm_dirs = _g.llvm_dirs
262272
if llvm_dirs == nil then
263-
local rootdir = toolchain:sdkdir()
264273
if not rootdir and toolchain:is_plat("windows") then
265274
rootdir = _get_llvm_rootdir(toolchain)
266275
end
267276

268277
local bindir, libdir, cxxlibdir, includedir, cxxincludedir, resdir, rtdir, rtlink
269278
if rootdir then
270279
bindir = path.join(rootdir, "bin")
271-
if bindir then
272-
bindir = os.isdir(bindir) and bindir or nil
273-
end
280+
bindir = os.isdir(bindir) and bindir or nil
274281

275282
libdir = path.join(rootdir, "lib")
276-
if libdir then
277-
libdir = os.isdir(libdir) and libdir or nil
278-
end
283+
libdir = os.isdir(libdir) and libdir or nil
279284

280285
if libdir then
281-
cxxlibdir = libdir and path.join(libdir, "c++")
282-
if cxxlibdir then
286+
cxxlibdir = path.join(libdir, "c++")
287+
cxxlibdir = os.isdir(cxxlibdir) and cxxlibdir or nil
288+
if not cxxlibdir then
289+
cxxlibdir = path.join(libdir, _get_llvm_target_triple(toolchain))
283290
cxxlibdir = os.isdir(cxxlibdir) and cxxlibdir or nil
284291
end
285292
end
286293

287294
includedir = path.join(rootdir, "include")
288-
if includedir then
289-
includedir = os.isdir(includedir) and includedir or nil
290-
end
295+
includedir = os.isdir(includedir) and includedir or nil
291296

292297
if includedir then
293-
cxxincludedir = includedir and path.join(includedir, "c++", "v1") or nil
294-
if cxxincludedir then
295-
cxxincludedir = os.isdir(cxxincludedir) and cxxincludedir or nil
296-
end
298+
cxxincludedir = path.join(includedir, "c++", "v1")
299+
cxxincludedir = os.isdir(cxxincludedir) and cxxincludedir or nil
297300
end
298301

299302
resdir = _get_llvm_resourcedir(toolchain)
300-
if toolchain:is_plat("windows") then
301-
rtdir, rtlink = _get_llvm_compiler_win_rtdir_and_link(toolchain)
302-
end
303+
rtdir, rtlink, rtlib = _get_llvm_compiler_rtdir_and_link(toolchain)
303304
end
304305

305306
llvm_dirs = {root = rootdir,
@@ -310,6 +311,7 @@ function get_llvm_dirs(toolchain)
310311
cxxinclude = cxxincludedir,
311312
res = resdir,
312313
rt = rtdir,
314+
rtlib = rtlib,
313315
rtlink = rtlink }
314316
_g.llvm_dirs = llvm_dirs
315317
end

xmake/toolchains/clang/load.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import("detect.sdks.find_vstudio")
2222
import("detect.sdks.find_mingw")
2323
import("core.project.config")
24+
import("private.utils.toolchain", {alias = "toolchain_utils"})
2425

2526
-- add the given vs environment
2627
function _add_vsenv(toolchain, name, curenvs)
@@ -66,6 +67,22 @@ function main(toolchain, suffix)
6667
target = "armv7"
6768
end
6869

70+
local dirs = toolchain_utils.get_llvm_dirs(toolchain, toolchain:sdkdir())
71+
if dirs then
72+
if dirs.bin then
73+
toolchain:add("runenvs", dirs.bin)
74+
end
75+
if dirs.lib then
76+
toolchain:add("runenvs", dirs.lib)
77+
end
78+
if dirs.cxxlib then
79+
toolchain:add("runenvs", dirs.cxxlib)
80+
end
81+
if dirs.rtlib then
82+
toolchain:add("runenvs", dirs.rtlib)
83+
end
84+
end
85+
6986
if toolchain:is_plat("windows") then
7087
target = target .. "-windows-msvc"
7188

xmake/toolchains/llvm/xmake.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ toolchain("llvm")
5454
toolchain:add("runtimes", "MT", "MTd", "MD", "MDd")
5555
end
5656

57-
local dirs = toolchain_utils.get_llvm_dirs(toolchain)
58-
if dirs and dirs.rt then
59-
toolchain:add("runenvs", dirs.rt)
57+
local dirs = toolchain_utils.get_llvm_dirs(toolchain, toolchain:sdkdir())
58+
if dirs then
59+
if dirs.bin then
60+
toolchain:add("runenvs", dirs.bin)
61+
end
62+
if dirs.lib then
63+
toolchain:add("runenvs", dirs.lib)
64+
end
65+
if dirs.cxxlib then
66+
toolchain:add("runenvs", dirs.cxxlib)
67+
end
68+
if dirs.rtlib then
69+
toolchain:add("runenvs", dirs.rtlib)
70+
end
6071
end
6172

6273
-- add target flags

0 commit comments

Comments
 (0)