@@ -183,25 +183,25 @@ end
183183
184184-- get llvm sdk resource directory
185185function _get_llvm_resourcedir (toolchain )
186- local llvm_resourcedir = _g ._LLVM_resourceDIR
186+ local llvm_resourcedir = _g ._LLVM_RESOURCE_DIR
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" }) end }
189189 if outdata then
190190 llvm_resourcedir = path .normalize (outdata :trim ())
191191 if not os .isdir (llvm_resourcedir ) then
192192 llvm_resourcedir = nil
193193 end
194194 end
195- _g ._LLVM_resourceDIR = llvm_resourcedir or false
195+ _g ._LLVM_RESOURCE_DIR = llvm_resourcedir or false
196196 end
197197 return llvm_resourcedir or nil
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" }) end }
251261 if outdata then
252262 llvm_targettriple = outdata :trim ()
253263 end
@@ -268,50 +278,75 @@ function get_llvm_dirs(toolchain)
268278 local bindir , libdir , cxxlibdir , includedir , cxxincludedir , resdir , rtdir , rtlink
269279 if rootdir then
270280 bindir = path .join (rootdir , " bin" )
271- if bindir then
272- bindir = os .isdir (bindir ) and bindir or nil
273- end
281+ bindir = os .isdir (bindir ) and bindir or nil
274282
275283 libdir = path .join (rootdir , " lib" )
276- if libdir then
277- libdir = os .isdir (libdir ) and libdir or nil
278- end
284+ libdir = os .isdir (libdir ) and libdir or nil
279285
280286 if libdir then
281- cxxlibdir = libdir and path .join (libdir , " c++" )
282- if cxxlibdir then
287+ cxxlibdir = path .join (libdir , " c++" )
288+ cxxlibdir = os .isdir (cxxlibdir ) and cxxlibdir or nil
289+ if not cxxlibdir then
290+ cxxlibdir = path .join (libdir , _get_llvm_target_triple (toolchain ))
283291 cxxlibdir = os .isdir (cxxlibdir ) and cxxlibdir or nil
284292 end
285293 end
286294
287295 includedir = path .join (rootdir , " include" )
288- if includedir then
289- includedir = os .isdir (includedir ) and includedir or nil
290- end
296+ includedir = os .isdir (includedir ) and includedir or nil
291297
292298 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
299+ cxxincludedir = path .join (includedir , " c++" , " v1" )
300+ cxxincludedir = os .isdir (cxxincludedir ) and cxxincludedir or nil
297301 end
298302
299303 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
304+ rtdir , rtlink , rtlib = _get_llvm_compiler_rtdir_and_link (toolchain )
303305 end
304306
305307 llvm_dirs = {root = rootdir ,
306- bin = bindir ,
307- lib = libdir ,
308- cxxlib = cxxlibdir ,
309- include = includedir ,
310- cxxinclude = cxxincludedir ,
311- res = resdir ,
312- rt = rtdir ,
313- rtlink = rtlink }
308+ bin = bindir ,
309+ lib = libdir ,
310+ cxxlib = cxxlibdir ,
311+ include = includedir ,
312+ cxxinclude = cxxincludedir ,
313+ res = resdir ,
314+ rt = rtdir ,
315+ rtlib = rtlib ,
316+ rtlink = rtlink }
314317 _g .llvm_dirs = llvm_dirs
315318 end
316319 return llvm_dirs
317320end
321+
322+ -- set runenvs for llvm
323+ function set_llvm_runenvs (toolchain )
324+ local pathname = " PATH"
325+ if toolchain :is_plat (" windows" ) then
326+ local curenvs = os .getenvs ()
327+ for k , _ in pairs (curenvs ) do
328+ if pathname :lower () == k :lower () then
329+ pathname = k
330+ break
331+ end
332+ end
333+ end
334+
335+ local dirs = get_llvm_dirs (toolchain )
336+ if dirs then
337+ if dirs .bin then
338+ toolchain :add (" runenvs" , pathname , dirs .bin )
339+ end
340+ for _ , dir in ipairs ({dirs .lib or false , dirs .cxxlib or false , dirs .rtlib or false }) do
341+ if dir then
342+ if toolchain :is_plat (" windows" ) then
343+ toolchain :add (" runenvs" , pathname , dir )
344+ elseif toolchain :is_plat (" linux" , " bsd" , " android" ) then
345+ toolchain :add (" runenvs" , " LD_LIBRARY_PATH" , dir )
346+ elseif toolchain :is_plat (" macosx" , " ios" , " appletvos" , " applexros" , " watchos" ) then
347+ toolchain :add (" runenvs" , " DYLD_LIBRARY_PATH" , dir )
348+ end
349+ end
350+ end
351+ end
352+ end
0 commit comments