185185function _get_llvm_resourcedir (toolchain )
186186local llvm_resourcedir = _g ._LLVM_resourceDIR
187187if 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 }
189189if outdata then
190190llvm_resourcedir = path .normalize (outdata :trim ())
191191if 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 )
202202local llvm_rootdir = _g ._LLVM_ROOTDIR
203203if llvm_rootdir == nil then
204- local resourcedir = _get_llvm_resourcedir (self )
204+ local resourcedir = _get_llvm_resourcedir (toolchain )
205205if resourcedir then
206206llvm_rootdir = path .normalize (path .join (resourcedir ," .." ," .." ," .." ))
207207if 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 )
218218import (" lib.detect.find_tool" )
219219
220- local cc = self : get (" cc" )
220+ local cc = toolchain : tool (" cc" )
221221local cc_tool = find_tool (cc , {version = true })
222222if cc_tool and cc_tool .version then
223- local resdir = _get_llvm_resourcedir (self )
223+ local resdir = _get_llvm_resourcedir (toolchain )
224224if resdir then
225225local 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 )
229229local 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+
231241local tripletdir = target_triple and path .join (res_libdir ," windows" ,target_triple )
232242tripletdir = 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
236246local rtlink = " clang_rt.builtins" .. (tripletdir and " .lib" or (" -" .. arch .. " .lib" ))
237247if 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 )
239249end
240250end
241- return res_libdir
251+ return res_libdir , nil , path . join ( res_libdir , rtdir )
242252end
243253end
244254end
245255
246256-- get llvm target triple
247- function _get_llvm_target_triple (self )
257+ function _get_llvm_target_triple (toolchain )
248258local llvm_targettriple = _g ._LLVM_TARGETTRIPLE
249259if 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 }
251261if outdata then
252262llvm_targettriple = outdata :trim ()
253263end
@@ -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 )
261271local llvm_dirs = _g .llvm_dirs
262272if llvm_dirs == nil then
263- local rootdir = toolchain :sdkdir ()
264273if not rootdir and toolchain :is_plat (" windows" )then
265274rootdir = _get_llvm_rootdir (toolchain )
266275end
267276
268277local bindir ,libdir ,cxxlibdir ,includedir ,cxxincludedir ,resdir ,rtdir ,rtlink
269278if rootdir then
270279bindir = 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
275282libdir = 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
280285if 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 ))
283290cxxlibdir = os .isdir (cxxlibdir )and cxxlibdir or nil
284291end
285292end
286293
287294includedir = 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
292297if 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
297300end
298301
299302resdir = _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 )
303304end
304305
305306llvm_dirs = {root = rootdir ,
@@ -310,6 +311,7 @@ function get_llvm_dirs(toolchain)
310311cxxinclude = cxxincludedir ,
311312res = resdir ,
312313rt = rtdir ,
314+ rtlib = rtlib ,
313315rtlink = rtlink }
314316_g .llvm_dirs = llvm_dirs
315317end