@@ -5,10 +5,86 @@ using Printf
55using Libdl
66
77
8- const ext = joinpath (@__DIR__ , " .." , " deps" , " ext.jl" )
9- isfile (ext) || error (" LLVM.jl has not been built, please run Pkg.build(\" LLVM\" )." )
10- include (ext)
11- const libllvm = libllvm_path
8+ # # discovery
9+
10+ using Libdl
11+
12+ VERSION >= v " 0.7.0-DEV.2576" || error (" This version of LLVM.jl requires Julia 0.7" )
13+
14+ let
15+ # find LLVM library
16+
17+ libllvm_paths = filter (Libdl. dllist ()) do lib
18+ occursin (" LLVM" , basename (lib))
19+ end
20+ if isempty (libllvm_paths)
21+ error ("""
22+ Cannot find the LLVM library loaded by Julia.
23+ Please use a version of Julia that has been built with USE_LLVM_SHLIB=1 (like the official binaries).
24+ If you are, please file an issue and attach the output of `Libdl.dllist()`.""" )
25+ end
26+ if length (libllvm_paths) > 1
27+ error ("""
28+ Multiple LLVM libraries loaded by Julia.
29+ Please file an issue and attach the output of `Libdl.dllist()`.""" )
30+ end
31+ global const libllvm = first (libllvm_paths)
32+ Base. include_dependency (libllvm)
33+
34+ global const libllvm_version = Base. libllvm_version:: VersionNumber
35+
36+ # figure out the supported targets by looking at initialization routines
37+ lib = Libdl. dlopen (libllvm)
38+ llvm_targets = [:AArch64 , :AMDGPU , :ARC , :ARM , :AVR , :BPF , :Hexagon , :Lanai , :MSP430 ,
39+ :Mips , :NVPTX , :PowerPC , :RISCV , :Sparc , :SystemZ , :WebAssembly , :X86 ,
40+ :XCore ]
41+ @show global const libllvm_targets = filter (llvm_targets) do target
42+ sym = Libdl. dlsym_e (lib, Symbol (" LLVMInitialize$(target) Target" ))
43+ sym != = nothing
44+ end
45+ # TODO : figure out the name of the native target
46+
47+ @debug " Found LLVM v$libllvm_version at $libllvm with support for $(join (libllvm_targets, " , " )) "
48+
49+
50+ # find appropriate LLVM.jl wrapper
51+
52+ vercmp_match (a,b) = a. major== b. major && a. minor== b. minor
53+ vercmp_compat (a,b) = a. major> b. major || (a. major== b. major && a. minor>= b. minor)
54+
55+ llvmjl_wrappers_path = joinpath (@__DIR__ , " .." , " lib" )
56+ Base. include_dependency (llvmjl_wrappers_path)
57+
58+ llvmjl_wrappers = filter (path-> isdir (joinpath (llvmjl_wrappers_path, path)),
59+ readdir (llvmjl_wrappers_path))
60+ @assert ! isempty (llvmjl_wrappers)
61+
62+ matching_wrappers = filter (wrapper-> vercmp_match (libllvm_version,
63+ VersionNumber (wrapper)),
64+ llvmjl_wrappers)
65+ global const llvmjl_wrapper = if ! isempty (matching_wrappers)
66+ @assert length (matching_wrappers) == 1
67+ matching_wrappers[1 ]
68+ else
69+ compatible_wrappers = filter (wrapper-> vercmp_compat (libllvm_version,
70+ VersionNumber (wrapper)),
71+ llvmjl_wrappers)
72+ isempty (compatible_wrappers) && error (" Could not find any compatible wrapper for LLVM $(libllvm_version) " )
73+ last (compatible_wrappers)
74+ end
75+
76+ @debug " Using LLVM.jl wrapper for LLVM v$llvmjl_wrapper "
77+
78+
79+ # backwards-compatible flags
80+
81+ global const libllvm_system = false
82+
83+ global const configured = true
84+ end
85+
86+
87+ # # source code includes
1288
1389include (" util/types.jl" )
1490
@@ -49,14 +125,19 @@ include("interop.jl")
49125
50126include (" deprecated.jl" )
51127
128+
129+ # # initialization
130+
52131function __init__ ()
53132 libllvm_paths = filter (Libdl. dllist ()) do lib
54133 occursin (" LLVM" , basename (lib))
55134 end
56135 if length (libllvm_paths) > 1
57136 # NOTE: this still allows switching to a non-USE_LLVM_SHLIB version, but
58137 # there's no way to detect that since the new libLLVM is loaded before this...
59- error (" LLVM.jl and Julia are using different LLVM libraries, please re-run Pkg.build(\" LLVM\" )." )
138+ cachefile = Base. compilecache (Base. PkgId (LLVM))
139+ rm (cachefile)
140+ error (" Your set-up changed, and LLVM.jl needs to be reconfigured. Please load the package again." )
60141 end
61142
62143 _install_handlers ()
0 commit comments