Debian Codegraph @

build/dynamic.rs

raw symbols SCIP
1 // SPDX-License-Identifier: Apache-2.0
2
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#};
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#};
7
8 use superrust-analyzer cargo clang-sys 1.8.1 crate/::commonrust-analyzer cargo clang-sys 1.8.1 common/;
9
10 //================================================
11 // Validation
12 //================================================
13
14 /// Extracts the ELF class from the ELF header in a shared library.
15 fn parse_elf_headerrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_elf_header().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u8> {
16 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
17 let mut buffer = [0; 5];
18 file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
19 if buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T]`][`PartialEq<[U; N]>`]eq().[127, 69, 76, 70] {
20 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().)
21 } else {
22 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, "invalid ELF header"))
23 }
24 }
25
26 /// Extracts the magic number and machine type from the PE header in a shared library.
27 fn parse_pe_headerrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_pe_header().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(u16, u16)> {
28 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
29
30 // Extract the header offset.
31 let mut buffer = [0; 4];
32 let start = SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(0x3C);
33 file.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Seek]seek().(start)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
34 file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
35 let offset = i32::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]from_le_bytes().(buffer);
36
37 // Check the validity of the header.
38 file.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Seek]seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(offset as u64))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
39 file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
40 if buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]ne().[80, 69, 0, 0] {
41 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, "invalid PE header"));
42 }
43
44 // Extract the magic number.
45 let mut buffer = [0; 2];
46 file.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Seek]seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(20))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
47 file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
48 let magic_number = u16::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]from_le_bytes().(buffer);
49
50 // Extract the machine type.
51 let mut buffer = [0; 2];
52 file.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Seek]seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().22))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53 file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
54 let machine_type = u16::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]from_le_bytes().(buffer);
55
56 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((magic_number, machine_type));
57 }
58
59 /// Checks that a `libclang` shared library matches the target platform.
60 fn validate_libraryrust-analyzer cargo clang-sys 1.8.1 dynamic/validate_library().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
61 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("linux") || target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("freebsd") {
62 let class = parse_elf_headerrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_elf_header().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
63
64 if target_pointer_widthrust-analyzer cargo clang-sys 1.8.1 macros/target_pointer_width!!("32") && class rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().1 {
65 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("invalid ELF class (64-bit)".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
66 }
67
68 if target_pointer_widthrust-analyzer cargo clang-sys 1.8.1 macros/target_pointer_width!!("64") && class rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().2 {
69 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("invalid ELF class (32-bit)".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
70 }
71
72 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
73 } else if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("windows") {
74 let (magic, machine_type) = parse_pe_headerrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_pe_header().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
75
76 if target_pointer_widthrust-analyzer cargo clang-sys 1.8.1 macros/target_pointer_width!!("32") && magic rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().267 {
77 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("invalid DLL (64-bit)".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
78 }
79
80 if target_pointer_widthrust-analyzer cargo clang-sys 1.8.1 macros/target_pointer_width!!("64") && magic rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]ne().523 {
81 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("invalid DLL (32-bit)".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
82 }
83
84 let arch_mismatch = match machine_type {
85 0x014C if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().target_archrust-analyzer cargo clang-sys 1.8.1 macros/target_arch!!("x86") => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("x86"),
86 0x8664 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().target_archrust-analyzer cargo clang-sys 1.8.1 macros/target_arch!!("x86_64") => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("x86-64"),
87 0xAA64 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().target_archrust-analyzer cargo clang-sys 1.8.1 macros/target_arch!!("aarch64") => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("ARM64"),
88 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
89 };
90
91 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(arch) = arch_mismatch {
92 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("invalid DLL ({arch})"))
93 } else {
94 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
95 }
96 } else {
97 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
98 }
99 }
100
101 //================================================
102 // Searching
103 //================================================
104
105 /// Extracts the version components in a `libclang` shared library filename.
106 fn parse_versionrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_version().(filename: &str) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> {
107 let version = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version) = filename.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("libclang.so.") {
108 version
109 } else if filename.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("libclang-") {
110 &filename[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().9..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#filename.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
111 } else {
112 return vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
113 };
114
115 version.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('.').maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| s.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(0)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
116 }
117
118 /// Finds `libclang` shared libraries and returns the paths to, filenames of,
119 /// and versions of those shared libraries.
120 fn search_libclang_directoriesrust-analyzer cargo clang-sys 1.8.1 dynamic/search_libclang_directories().(runtime: bool) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32>)>, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
121 let mut files = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
122 "{}clang{}",
123 envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::constsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/::DLL_PREFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/DLL_PREFIX.,
124 envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::constsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/::DLL_SUFFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/DLL_SUFFIX.
125 )];
126
127 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("linux") {
128 // Some Linux distributions don't create a `libclang.so` symlink, so we
129 // need to look for versioned files (e.g., `libclang-3.9.so`).
130 files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("libclang-*.so".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
131
132 // Some Linux distributions don't create a `libclang.so` symlink and
133 // don't have versioned files as described above, so we need to look for
134 // suffix versioned files (e.g., `libclang.so.1`). However, `ld` cannot
135 // link to these files, so this will only be included when linking at
136 // runtime.
137 if runtime {
138 files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("libclang.so.*".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
139 files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("libclang-*.so.*".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
140 }
141 }
142
143 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("freebsd") || target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("haiku") || target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("netbsd") || target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("openbsd") {
144 // Some BSD distributions don't create a `libclang.so` symlink either,
145 // but use a different naming scheme for versioned files (e.g.,
146 // `libclang.so.7.0`).
147 files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("libclang.so.*".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
148 }
149
150 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("windows") {
151 // The official LLVM build uses `libclang.dll` on Windows instead of
152 // `clang.dll`. However, unofficial builds such as MinGW use `clang.dll`.
153 files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("libclang.dll".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
154 }
155
156 // Find and validate `libclang` shared libraries and collect the versions.
157 let mut valid = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
158 let mut invalid = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
159 for (directory, filename) in commonrust-analyzer cargo clang-sys 1.8.1 common/::search_libclang_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_libclang_directories().(&files, "LIBCLANG_PATH") {
160 let path = directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(&filename);
161 match validate_libraryrust-analyzer cargo clang-sys 1.8.1 dynamic/validate_library().(&path) {
162 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => {
163 let version = parse_versionrust-analyzer cargo clang-sys 1.8.1 dynamic/parse_version().(&filename);
164 valid.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((directory, filename, version))
165 }
166 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(message) => invalid.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("({}: {})", path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), message)),
167 }
168 }
169
170 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().valid.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
171 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(valid);
172 }
173
174 let message = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
175 "couldn't find any valid shared libraries matching: [{}], set the \
176 `LIBCLANG_PATH` environment variable to a path where one of these files \
177 can be found (invalid: [{}])",
178 files
179 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
180 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("'{}'", f))
181 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>()
182 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(", "),
183 invalid.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(", "),
184 );
185
186 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(message)
187 }
188
189 /// Finds the "best" `libclang` shared library and returns the directory and
190 /// filename of that library.
191 pub fn findrust-analyzer cargo clang-sys 1.8.1 dynamic/find().(runtime: bool) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
192 search_libclang_directoriesrust-analyzer cargo clang-sys 1.8.1 dynamic/search_libclang_directories().(runtime)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
193 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
194 // We want to find the `libclang` shared library with the highest
195 // version number, hence `max_by_key` below.
196 //
197 // However, in the case where there are multiple such `libclang` shared
198 // libraries, we want to use the order in which they appeared in the
199 // list returned by `search_libclang_directories` as a tiebreaker since
200 // that function returns `libclang` shared libraries in descending order
201 // of preference by how they were found.
202 //
203 // `max_by_key`, perhaps surprisingly, returns the *last* item with the
204 // maximum key rather than the first which results in the opposite of
205 // the tiebreaking behavior we want. This is easily fixed by reversing
206 // the list first.
207 .revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().()
208 .max_by_keyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#max_by_key().(|f| &f.2)
209 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().()
210 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(path, filename, _)| (path, filename))
211 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| "unreachable".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
212 }
213
214 //================================================
215 // Linking
216 //================================================
217
218 /// Finds and links to a `libclang` shared library.
219 #[cfg(not(feature = "runtime"))]
220 pub fn link() {
221 let cep = commonrust-analyzer cargo clang-sys 1.8.1 common/::CommandErrorPrinterrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter#::default();
222
223 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/;
224
225 let (directory, filename) = findrust-analyzer cargo clang-sys 1.8.1 dynamic/find().(false).unwrap();
226 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-search={}", directory.display());
227
228 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(all(target_os = "windows", target_env = "msvc")) {
229 // Find the `libclang` stub static library required for the MSVC
230 // toolchain.
231 let lib = if !directory.ends_with("bin") {
232 directory
233 } else {
234 directory.parent().unwrap().join("lib")
235 };
236
237 if lib.join("libclang.lib").exists() {
238 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-search={}", lib.display());
239 } else if lib.join("libclang.dll.a").exists() {
240 // MSYS and MinGW use `libclang.dll.a` instead of `libclang.lib`.
241 // It is linkable with the MSVC linker, but Rust doesn't recognize
242 // the `.a` suffix, so we need to copy it with a different name.
243 //
244 // FIXME: Maybe we can just hardlink or symlink it?
245 let out = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("OUT_DIR").unwrap();
246 fs::copy(
247 lib.join("libclang.dll.a"),
248 Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::new(&out).join("libclang.lib"),
249 )
250 .unwrap();
251 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-search=native={}", out);
252 } else {
253 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!(
254 "using '{}', so 'libclang.lib' or 'libclang.dll.a' must be \
255 available in {}",
256 filename,
257 lib.display(),
258 );
259 }
260
261 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-lib=dylib=libclang");
262 } else {
263 let name = filename.trim_start_matches("lib");
264
265 // Strip extensions and trailing version numbers (e.g., the `.so.7.0` in
266 // `libclang.so.7.0`).
267 let name = match name.find(".dylib").or_else(|| name.find(".so")) {
268 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index) => &name[0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#index],
269 None => name,
270 };
271
272 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-lib=dylib={}", name);
273 }
274
275 cep.discard();
276 }