Skip to content

Commit f227db3

Browse files
authored
Merge pull request #4804 from Arthapz/fix-toolchain-ld-path
Fix toolchain ld path
2 parents de4416c + 6da651c commit f227db3

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

xmake/modules/core/tools/clang.lua

+33-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ end
195195
function _get_llvm_rootdir(self)
196196
local llvm_rootdir = _g._LLVM_ROOTDIR
197197
if llvm_rootdir == nil then
198-
local outdata = try { function() return os.iorun(self:program() .. " -print-resource-dir") end }
198+
local outdata = try { function() return os.iorunv(self:program(), {"-print-resource-dir"}, {envs = self:runenvs()}) end }
199199
if outdata then
200200
llvm_rootdir = path.normalize(path.join(outdata:trim(), "..", "..", ".."))
201201
if not os.isdir(llvm_rootdir) then
@@ -207,6 +207,19 @@ function _get_llvm_rootdir(self)
207207
return llvm_rootdir or nil
208208
end
209209

210+
-- get llvm target triple
211+
function _get_llvm_target_triple(self)
212+
local llvm_targettriple = _g._LLVM_TARGETTRIPLE
213+
if llvm_targettriple == nil then
214+
local outdata = try { function() return os.iorunv(self:program(), {"-print-target-triple"}, {envs = self:runenvs()}) end }
215+
if outdata then
216+
llvm_targettriple = outdata:trim()
217+
end
218+
_g._LLVM_TARGETTRIPLE = llvm_targettriple or false
219+
end
220+
return llvm_targettriple or nil
221+
end
222+
210223
-- make the runtime flag
211224
-- @see https://github.com/xmake-io/xmake/issues/3546
212225
function nf_runtime(self, runtime, opt)
@@ -267,8 +280,25 @@ function nf_runtime(self, runtime, opt)
267280
llvm_rootdir = _get_llvm_rootdir(self)
268281
end
269282
if llvm_rootdir then
270-
maps["c++_static"] = table.join(maps["c++_static"], "-L" .. path.join(llvm_rootdir, "lib"))
271-
maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. path.join(llvm_rootdir, "lib"))
283+
local libdir = path.absolute(path.join(llvm_rootdir, "lib"))
284+
maps["c++_static"] = table.join(maps["c++_static"], "-L" .. libdir)
285+
maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. libdir)
286+
-- sometimes llvm runtimes are located in a target-triple subfolder
287+
local target_triple = _get_llvm_target_triple(self)
288+
local triple_libdir = (target_triple and os.isdir(path.join(libdir, target_triple))) and path.join(libdir, target_triple)
289+
if triple_libdir then
290+
maps["c++_static"] = table.join(maps["c++_static"], "-L" .. triple_libdir)
291+
maps["c++_shared"] = table.join(maps["c++_shared"], "-L" .. triple_libdir)
292+
end
293+
-- add rpath to avoid the user need to set LD_LIBRARY_PATH by hand
294+
maps["c++_shared"] = table.join(maps["c++_shared"], nf_rpathdir(self, libdir))
295+
if triple_libdir then
296+
maps["c++_shared"] = table.join(maps["c++_shared"], nf_rpathdir(self, triple_libdir))
297+
end
298+
if target:is_shared() and self:is_plat("macosx", "iphoneos", "watchos") then
299+
maps["c++_shared"] = table.join(maps["c++_shared"], "-install_name")
300+
maps["c++_shared"] = table.join(maps["c++_shared"], "@rpath/" .. target:filename())
301+
end
272302
end
273303
if runtime:endswith("_static") and _has_static_libstdcxx(self) then
274304
maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++")

0 commit comments

Comments
 (0)