185185function _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)
198198end
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)
214214end
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
244254end
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)
257267end
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
0 commit comments