Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb9ef47

Browse files
committed
fix(llvm) fix nf_runtime clang
1 parent7c4af5e commitfb9ef47

File tree

6 files changed

+207
-125
lines changed

6 files changed

+207
-125
lines changed

‎tests/projects/c/llvm_compiler_rt/test.lua‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import("lib.detect.find_tool")
2+
import("core.tool.toolchain")
23
import("utils.ci.is_running", {alias="ci_is_running"})
34

45
functionmain(t)
56
localflags=""
67
ifci_is_running()then
7-
flags="-vD"
8+
flags="-vD"
89
end
910

10-
localcc=find_tool("clang", {version=true})
11-
ifnotccthen
12-
wprint("clang not found, skipping tests")
11+
localllvm=toolchain.load("llvm")
12+
ifnotllvmornotllvm:check()then
13+
wprint("llvm not found, skipping tests")
1314
return
1415
end
1516
os.exec("xmake clean -a")

‎xmake/modules/core/tools/clang.lua‎

Lines changed: 57 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
-- inherit gcc
2222
inherit("gcc")
2323
import("core.language.language")
24+
import("private.utils.toolchain", {alias="toolchain_utils"})
2425

2526
-- init it
2627
functioninit(self)
@@ -191,76 +192,6 @@ function _has_static_libstdcxx(self)
191192
returnhas_static_libstdcxx
192193
end
193194

194-
-- get llvm sdk root directory
195-
function_get_llvm_rootdir(self)
196-
localllvm_rootdir=_g._LLVM_ROOTDIR
197-
ifllvm_rootdir==nilthen
198-
localoutdata=try {function()returnos.iorunv(self:program(), {"-print-resource-dir"}, {envs=self:runenvs()})end }
199-
ifoutdatathen
200-
llvm_rootdir=path.normalize(path.join(outdata:trim(),"..","..",".."))
201-
ifnotos.isdir(llvm_rootdir)then
202-
llvm_rootdir=nil
203-
end
204-
end
205-
_g._LLVM_ROOTDIR=llvm_rootdirorfalse
206-
end
207-
returnllvm_rootdirornil
208-
end
209-
210-
-- get llvm target triple
211-
function_get_llvm_target_triple(self)
212-
localllvm_targettriple=_g._LLVM_TARGETTRIPLE
213-
ifllvm_targettriple==nilthen
214-
localoutdata=try {function()returnos.iorunv(self:program(), {"-print-target-triple"}, {envs=self:runenvs()})end }
215-
ifoutdatathen
216-
llvm_targettriple=outdata:trim()
217-
end
218-
_g._LLVM_TARGETTRIPLE=llvm_targettripleorfalse
219-
end
220-
returnllvm_targettripleornil
221-
end
222-
223-
-- find compiler-rt dir
224-
function_get_llvm_compiler_rtdir(self,target,llvm_rootdir)
225-
import("lib.detect.find_tool")
226-
import("core.base.semver")
227-
228-
locallibdir=path.absolute(path.join(llvm_rootdir,"lib","clang"))
229-
localtarget_triple=_get_llvm_target_triple(self)
230-
231-
localcc=target:tool("cc")
232-
localcc_tool=find_tool(cc, {version=true})
233-
ifcc_toolandcc_tool.versionthen
234-
localversion=semver.new(cc_tool.version):major()
235-
localbasedir=path.join(libdir,version,"lib")
236-
237-
localcompiler_rtdir
238-
-- sometimes llvm is built with -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON, compiler_rt is located in a target-triple subfolder
239-
localtripletdir=path.join(basedir,target_triple)
240-
ifos.isdir(tripletdir)then
241-
compiler_rtdir=tripletdir
242-
else
243-
-- sometimes llvm is built with -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF, compiler_rt is located in a platform name subfolder
244-
iftarget:is_plat("windows")then
245-
compiler_rtdir=os.isdir(path.join(basedir,"windows"))andpath.join(basedir,"windows")
246-
elseiftarget:is_plat("linux")then
247-
compiler_rtdir=os.isdir(path.join(basedir,"linux"))andpath.join(basedir,"linux")
248-
elseiftarget:is_plat("macosx")then
249-
compiler_rtdir=os.isdir(path.join(basedir,"darwin"))andpath.join(basedir,"darwin")
250-
end
251-
end
252-
253-
ifcompiler_rtdirandtarget_triplethen
254-
localarch=target_triple:split("-")[1]
255-
localcompiler_rtlink="clang_rt.builtins-"..arch
256-
ifos.isfile(path.join(compiler_rtdir,compiler_rtlink..".lib"))then
257-
returncompiler_rtdir,path.join(compiler_rtdir,compiler_rtlink..".lib")
258-
end
259-
end
260-
returncompiler_rtdir
261-
end
262-
end
263-
264195
-- make the runtime flag
265196
-- @see https://github.com/xmake-io/xmake/issues/3546
266197
functionnf_runtime(self,runtime,opt)
@@ -291,58 +222,72 @@ function nf_runtime(self, runtime, opt)
291222
}
292223
end
293224
end
294-
ifnotself:is_plat("android")then-- we will set runtimes in android ndk toolchain
295-
maps=mapsor {}
296-
localllvm_rootdir=self:toolchain():sdkdir()
297-
ifnotllvm_rootdirandself:is_plat("windows")then
298-
llvm_rootdir=_get_llvm_rootdir(self)
225+
localtarget=opt.targetoropt
226+
-- llvm on windows still doesn't support autolinking of libc++ and compiler-rt builtins
227+
-- @see https://discourse.llvm.org/t/improve-autolinking-of-compiler-rt-and-libc-on-windows-with-lld-link/71392/10
228+
-- and need manual setting of libc++ headerdirectory
229+
-- @see https://github.com/llvm/llvm-project/issues/79647
230+
localllvm_dirs=toolchain_utils.get_llvm_dirs(self)
231+
232+
ifself:is_plat("windows")andruntime=="c++_shared"then
233+
ifllvm_dirs.binthen
234+
self:add("runenvs","PATHS",llvm_dirs.bin)
299235
end
300-
ifkind=="cxx"then
236+
ifllvm_dirs.rtthen
237+
self:add("runenvs","PATHS",llvm_dirs.rt)
238+
end
239+
end
240+
241+
-- we will set runtimes in android ndk toolchain
242+
ifnotself:is_plat("android")then
243+
maps=mapsor {}
244+
ifkind=="cxx"orkind=="ld"orkind=="sh"then
301245
maps["c++_static"]="-stdlib=libc++"
302246
maps["c++_shared"]="-stdlib=libc++"
303247
maps["stdc++_static"]="-stdlib=libstdc++"
304248
maps["stdc++_shared"]="-stdlib=libstdc++"
305-
-- clang on windows fail to add libc++ includepath when using -stdlib=libc++ so we manually add it
306-
-- @see https://github.com/llvm/llvm-project/issues/79647
307-
ifllvm_rootdirthen
308-
maps["c++_static"]=table.join(maps["c++_static"],"-cxx-isystem"..path.join(llvm_rootdir,"include","c++","v1"))
309-
maps["c++_shared"]=table.join(maps["c++_shared"],"-cxx-isystem"..path.join(llvm_rootdir,"include","c++","v1"))
249+
ifkind=="cxx"then
250+
-- force the toolchain libc++ headers to prevent clang picking the systems one
251+
ifllvm_dirs.cxxincludethen
252+
maps["c++_static"]=table.join(maps["c++_static"],"-cxx-isystem"..llvm_dirs.cxxinclude)
253+
maps["c++_shared"]=table.join(maps["c++_shared"],"-cxx-isystem"..llvm_dirs.cxxinclude)
254+
end
310255
end
311-
elseifkind=="ld"orkind=="sh"then
312-
localtarget=opt.targetoropt
313-
localis_cxx_or_c=targetand (target.sourcekindsandtable.contains(table.wrap(target:sourcekinds()),"cxx","cc"))
314-
ifis_cxx_or_cthen
315-
maps["c++_static"]="-stdlib=libc++"
316-
maps["c++_shared"]="-stdlib=libc++"
317-
maps["stdc++_static"]="-stdlib=libstdc++"
318-
maps["stdc++_shared"]="-stdlib=libstdc++"
319-
-- clang on windows fail to add libc++ librarypath when using -stdlib=libc++ so we manually add it
320-
-- @see https://github.com/llvm/llvm-project/issues/79647
321-
ifllvm_rootdirthen
322-
locallibdir=path.absolute(path.join(llvm_rootdir,"lib"))
323-
maps["c++_static"]=table.join(maps["c++_static"],nf_linkdir(self,libdir))
324-
maps["c++_shared"]=table.join(maps["c++_shared"],nf_linkdir(self,libdir))
256+
end
257+
258+
ifself:is_plat("windows")andlanguage.sourcekinds()[kind]then
259+
-- on windows force link to compiler_rt builtins
260+
ifllvm_dirs.rtandllvm_dirs.rtlinkthen
261+
forname,_inpairs(maps)do
262+
maps[name]=table.join({"-Xclang","--dependent-lib="..llvm_dirs.rtlink},maps[name])
263+
end
264+
end
265+
end
266+
ifkind=="ld"orkind=="sh"then
267+
ifself:is_plat("windows")andllvm_dirs.rtthen
268+
-- on windows force add compiler_rt link directories
269+
forname,_inpairs(maps)do
270+
maps[name]=table.join(nf_linkdir(self,llvm_dirs.rt),maps[name])
271+
maps[name]=table.join("-resource-dir="..llvm_dirs.res,maps[name])
272+
end
273+
end
274+
275+
localis_cxx=targetand (target.sourcekindsandtable.contains(table.wrap(target:sourcekinds()),"cxx"))
276+
ifis_cxxthen
277+
ifllvm_dirs.libthen
278+
maps["c++_static"]=table.join(maps["c++_static"],nf_linkdir(self,llvm_dirs.lib))
279+
maps["c++_shared"]=table.join(maps["c++_shared"],nf_linkdir(self,llvm_dirs.lib))
280+
maps["c++_shared"]=table.join(maps["c++_shared"],nf_rpathdir(self,llvm_dirs.lib))
325281
-- sometimes llvm c++ runtimes are located in c++ subfolder (e.g homebrew llvm)
326-
localcxx_libdir=path.join(libdir,"c++")
327-
ifos.isdir(cxx_libdir)then
328-
maps["c++_static"]=table.join(maps["c++_static"],nf_linkdir(self,cxx_libdir))
329-
maps["c++_shared"]=table.join(maps["c++_shared"],"-L"..nf_linkdir(self,cxx_libdir))
282+
ifllvm_dirs.cxxlibthen
283+
maps["c++_static"]=table.join(maps["c++_static"],nf_linkdir(self,llvm_dirs.cxxlib))
284+
maps["c++_shared"]=table.join(maps["c++_shared"],nf_linkdir(self,llvm_dirs.cxxlib))
285+
maps["c++_shared"]=table.join(maps["c++_shared"],nf_rpathdir(self,llvm_dirs.cxxlib))
330286
end
331-
localcompiler_rtdir,compiler_rtlink=_get_llvm_compiler_rtdir(self,target,llvm_rootdir)
332-
ifcompiler_rtdirthen
333-
forname,_inpairs(maps)do
334-
maps[name]=table.join(nf_linkdir(self,compiler_rtdir),maps[name])
335-
ifcompiler_rtlinkthen
336-
maps[name]=table.join(compiler_rtlink,maps[name])
337-
end
338-
end
287+
ifllvm_dirs.rtthen
288+
maps["c++_shared"]=table.join(maps["c++_shared"],nf_rpathdir(self,llvm_dirs.rt))
339289
end
340290
-- add rpath to avoid the user need to set LD_LIBRARY_PATH by hand
341-
maps["c++_shared"]=table.join(maps["c++_shared"],nf_rpathdir(self,libdir))
342-
ifcompiler_rtdirthen
343-
maps["c++_shared"]=table.join(maps["c++_shared"],nf_rpathdir(self,compiler_rtdir))
344-
maps["MD"]=table.join(maps["MD"],nf_rpathdir(self,compiler_rtdir))
345-
end
346291
iftarget.is_sharedandtarget:is_shared()andtarget.filenameandself:is_plat("macosx","iphoneos","watchos")then
347292
maps["c++_shared"]=table.join(maps["c++_shared"],"-install_name")
348293
maps["c++_shared"]=table.join(maps["c++_shared"],"@rpath/"..target:filename())

‎xmake/modules/private/utils/toolchain.lua‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,137 @@ function map_linkflags_for_package(package, targetkind, sourcekinds, name, value
181181
returnflags
182182
end
183183

184+
-- get llvm sdk resource directory
185+
function_get_llvm_resourcedir(toolchain)
186+
localllvm_resourcedir=_g._LLVM_resourceDIR
187+
ifllvm_resourcedir==nilthen
188+
localoutdata=try {function()returnos.iorunv(toolchain:get("cc"), {"-print-resource-dir"}, {envs=toolchain:runenvs()})end }
189+
ifoutdatathen
190+
llvm_resourcedir=path.normalize(outdata:trim())
191+
ifnotos.isdir(llvm_resourcedir)then
192+
llvm_resourcedir=nil
193+
end
194+
end
195+
_g._LLVM_resourceDIR=llvm_resourcedirorfalse
196+
end
197+
returnllvm_resourcedirornil
198+
end
199+
200+
-- get llvm sdk root directory
201+
function_get_llvm_rootdir(self)
202+
localllvm_rootdir=_g._LLVM_ROOTDIR
203+
ifllvm_rootdir==nilthen
204+
localresourcedir=_get_llvm_resourcedir(self)
205+
ifresourcedirthen
206+
llvm_rootdir=path.normalize(path.join(resourcedir,"..","..",".."))
207+
ifnotos.isdir(llvm_rootdir)then
208+
llvm_rootdir=nil
209+
end
210+
end
211+
_g._LLVM_ROOTDIR=llvm_rootdirorfalse
212+
end
213+
returnllvm_rootdirornil
214+
end
215+
216+
-- find compiler-rt dir
217+
function_get_llvm_compiler_win_rtdir_and_link(self,target)
218+
import("lib.detect.find_tool")
219+
220+
localcc=self:get("cc")
221+
localcc_tool=find_tool(cc, {version=true})
222+
ifcc_toolandcc_tool.versionthen
223+
localresdir=_get_llvm_resourcedir(self)
224+
ifresdirthen
225+
localres_libdir=path.join(resdir,"lib")
226+
-- when -DLLVM_ENABLE_TARGET_RUNTIME_DIR=OFF rtdir is windows/ and rtlink is clang_rt.builtinsi_<arch>.lib
227+
-- when ON rtdir is windows/<target-triple> and rtlink is clang_rt.builtins.lib
228+
localtarget_triple=_get_llvm_target_triple(self)
229+
localarch=target_tripleandtarget_triple:split("-")[1]
230+
231+
localtripletdir=target_tripleandpath.join(res_libdir,"windows",target_triple)
232+
tripletdir=os.isdir(tripletdir)ornil
233+
234+
localrtdir=tripletdirandpath.join("windows",target_triple)or"windows"
235+
ifos.isdir(path.join(res_libdir,rtdir))then
236+
localrtlink="clang_rt.builtins".. (tripletdirand".lib"or ("-"..arch..".lib"))
237+
ifos.isfile(path.join(res_libdir,rtdir,rtlink))then
238+
returnres_libdir,path.join(rtdir,rtlink)
239+
end
240+
end
241+
returnres_libdir
242+
end
243+
end
244+
end
245+
246+
-- get llvm target triple
247+
function_get_llvm_target_triple(self)
248+
localllvm_targettriple=_g._LLVM_TARGETTRIPLE
249+
ifllvm_targettriple==nilthen
250+
localoutdata=try {function()returnos.iorunv(self:program(), {"-print-target-triple"}, {envs=self:runenvs()})end }
251+
ifoutdatathen
252+
llvm_targettriple=outdata:trim()
253+
end
254+
_g._LLVM_TARGETTRIPLE=llvm_targettripleorfalse
255+
end
256+
returnllvm_targettripleornil
257+
end
258+
259+
-- get llvm toolchain dirs
260+
functionget_llvm_dirs(toolchain)
261+
localllvm_dirs=_g.llvm_dirs
262+
ifllvm_dirs==nilthen
263+
localrootdir=toolchain:sdkdir()
264+
ifnotrootdirandtoolchain:is_plat("windows")then
265+
rootdir=_get_llvm_rootdir(toolchain)
266+
end
267+
268+
localbindir,libdir,cxxlibdir,includedir,cxxincludedir,resdir,rtdir,rtlink
269+
ifrootdirthen
270+
bindir=path.join(rootdir,"bin")
271+
ifbindirthen
272+
bindir=os.isdir(bindir)andbindirornil
273+
end
274+
275+
libdir=path.join(rootdir,"lib")
276+
iflibdirthen
277+
libdir=os.isdir(libdir)andlibdirornil
278+
end
279+
280+
iflibdirthen
281+
cxxlibdir=libdirandpath.join(libdir,"c++")
282+
ifcxxlibdirthen
283+
cxxlibdir=os.isdir(cxxlibdir)andcxxlibdirornil
284+
end
285+
end
286+
287+
includedir=path.join(rootdir,"include")
288+
ifincludedirthen
289+
includedir=os.isdir(includedir)andincludedirornil
290+
end
291+
292+
ifincludedirthen
293+
cxxincludedir=includedirandpath.join(includedir,"c++","v1")ornil
294+
ifcxxincludedirthen
295+
cxxincludedir=os.isdir(cxxincludedir)andcxxincludedirornil
296+
end
297+
end
298+
299+
resdir=_get_llvm_resourcedir(toolchain)
300+
iftoolchain:is_plat("windows")then
301+
rtdir,rtlink=_get_llvm_compiler_win_rtdir_and_link(toolchain)
302+
end
303+
end
304+
305+
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 }
314+
_g.llvm_dirs=llvm_dirs
315+
end
316+
returnllvm_dirs
317+
end

‎xmake/rules/utils/compiler_runtime/xmake.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ rule("utils.compiler.runtime")
4444
-- enable vs runtime as MD by default
4545
iftarget:is_plat("windows")andnottarget:get("runtimes")then
4646
localvs_runtime_default=target:policy("build.c++.msvc.runtime")
47-
ifvs_runtime_defaultandtarget:has_tool("cxx","cl","clang","clang_cl")then
47+
ifvs_runtime_defaultandtarget:has_tool("cxx","cl","clang","clangxx","clang_cl")then
4848
ifis_mode("debug")then
4949
vs_runtime_default=vs_runtime_default.."d"
5050
end

‎xmake/toolchains/llvm/check.lua‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ function main(toolchain)
115115
toolchain:config_set("bindir",cross_toolchain.bindir)
116116
toolchain:config_set("sdkdir",cross_toolchain.sdkdir)
117117
else
118-
raise("llvm toolchain not found!")
118+
wprint("llvm toolchain not found!")
119+
returnfalse
119120
end
120121

121122
iftoolchain:is_plat("cross")and (nottoolchain:cross()ortoolchain:cross():match("^%s*$"))then
122-
raise("Missing cross target. Use `--cross=name` to specify.")
123+
wprint("Missing cross target. Use `--cross=name` to specify.")
124+
returnfalse
123125
end
124126

125127
-- attempt to find xcode to pass `-isysroot` on macos

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp