Debian Codegraph @

build/common.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/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
5 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/;
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 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#;
8
9 use globrust-analyzer cargo glob 0.3.4 crate/::{MatchOptionsrust-analyzer cargo glob 0.3.4 MatchOptions#, Patternrust-analyzer cargo glob 0.3.4 Pattern#};
10
11 //================================================
12 // Commands
13 //================================================
14
15 thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!! {
16 /// The errors encountered by the build script while executing commands.
17 static COMMAND_ERRORSrust-analyzer cargo clang-sys 1.8.1 common/COMMAND_ERRORS.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<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#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>> = RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`][Default]default().();
18 }
19
20 /// Adds an error encountered by the build script while executing a command.
21 fn add_command_errorrust-analyzer cargo clang-sys 1.8.1 common/add_command_error().(name: &str, path: &str, arguments: &[&str], message: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
22 COMMAND_ERRORSrust-analyzer cargo clang-sys 1.8.1 common/COMMAND_ERRORS..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|e| {
23 e.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().()
24 .entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
25 .or_defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Entry<'a, K, V>`]or_default().()
26 .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!!(
27 "couldn't execute `{} {}` (path={}) ({})",
28 name,
29 arguments.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(" "),
30 path,
31 message,
32 ))
33 });
34 }
35
36 /// A struct that prints the errors encountered by the build script while
37 /// executing commands when dropped (unless explictly discarded).
38 ///
39 /// This is handy because we only want to print these errors when the build
40 /// script fails to link to an instance of `libclang`. For example, if
41 /// `llvm-config` couldn't be executed but an instance of `libclang` was found
42 /// anyway we don't want to pollute the build output with irrelevant errors.
43 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
44 pub struct CommandErrorPrinterrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter# {
45 discardrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter#discard.: bool,
46 }
47
48 impl CommandErrorPrinterrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter# {
49 pub fn discardrust-analyzer cargo clang-sys 1.8.1 common/impl#[CommandErrorPrinter]discard().(mut self) {
50 self.discardrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter#discard. = true;
51 }
52 }
53
54 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for CommandErrorPrinterrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter# {
55 fn droprust-analyzer cargo clang-sys 1.8.1 common/impl#[CommandErrorPrinter][Drop]drop().(&mut self) {
56 if self.discardrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter#discard. {
57 return;
58 }
59
60 let errors = COMMAND_ERRORSrust-analyzer cargo clang-sys 1.8.1 common/COMMAND_ERRORS..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|e| e.borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][Clone]clone().());
61
62 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(errors) = errors.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().("llvm-config") {
63 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
64 "cargo:warning=could not execute `llvm-config` one or more \
65 times, if the LLVM_CONFIG_PATH environment variable is set to \
66 a full path to valid `llvm-config` executable it will be used \
67 to try to find an instance of `libclang` on your system: {}",
68 errors
69 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
70 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\"{}\"", e))
71 .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#<_>>()
72 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("\n "),
73 )
74 }
75
76 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(errors) = errors.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().("xcode-select") {
77 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
78 "cargo:warning=could not execute `xcode-select` one or more \
79 times, if a valid instance of this executable is on your PATH \
80 it will be used to try to find an instance of `libclang` on \
81 your system: {}",
82 errors
83 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
84 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\"{}\"", e))
85 .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#<_>>()
86 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("\n "),
87 )
88 }
89 }
90 }
91
92 #[cfg(test)]
93 lazy_staticrust-analyzer cargo lazy_static 1.5.0 crate/::lazy_staticrust-analyzer cargo lazy_static 1.5.0 lazy_static!! {
94 pub static ref RUN_COMMAND_MOCKrust-analyzer cargo clang-sys 1.8.1 common/RUN_COMMAND_MOCK#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<
95 Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&str, &str, &[&str]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + 'static>>,
96 > = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
97 }
98
99 /// Executes a command and returns the `stdout` output if the command was
100 /// successfully executed (errors are added to `COMMAND_ERRORS`).
101 fn run_commandrust-analyzer cargo clang-sys 1.8.1 common/run_command().(name: &str, path: &str, arguments: &[&str]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
102 #[cfg(test)]
103 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(command) = &*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().RUN_COMMAND_MOCKrust-analyzer cargo clang-sys 1.8.1 common/RUN_COMMAND_MOCK..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() {
104 return command(name, path, arguments);
105 }
106
107 let output = match Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(path).argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(arguments).outputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]output().() {
108 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(output) => output,
109 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => {
110 let message = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("error: {}", error);
111 add_command_errorrust-analyzer cargo clang-sys 1.8.1 common/add_command_error().(name, path, arguments, message);
112 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
113 }
114 };
115
116 if output.statusrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Output#status..successrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[ExitStatus]success().() {
117 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_lossy().(&output.stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Output#stdout.).into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().())
118 } else {
119 let message = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("exit code: {}", output.statusrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Output#status.);
120 add_command_errorrust-analyzer cargo clang-sys 1.8.1 common/add_command_error().(name, path, arguments, message);
121 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
122 }
123 }
124
125 /// Executes the `llvm-config` command and returns the `stdout` output if the
126 /// command was successfully executed (errors are added to `COMMAND_ERRORS`).
127 pub fn run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(arguments: &[&str]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
128 let path = 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().("LLVM_CONFIG_PATH").unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| "llvm-config".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
129 run_commandrust-analyzer cargo clang-sys 1.8.1 common/run_command().("llvm-config", &path, arguments)
130 }
131
132 /// Executes the `xcode-select` command and returns the `stdout` output if the
133 /// command was successfully executed (errors are added to `COMMAND_ERRORS`).
134 pub fn run_xcode_selectrust-analyzer cargo clang-sys 1.8.1 common/run_xcode_select().(arguments: &[&str]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
135 run_commandrust-analyzer cargo clang-sys 1.8.1 common/run_command().("xcode-select", "xcode-select", arguments)
136 }
137
138 //================================================
139 // Search Directories
140 //================================================
141 // These search directories are listed in order of
142 // preference, so if multiple `libclang` instances
143 // are found when searching matching directories,
144 // the `libclang` instances from earlier
145 // directories will be preferred (though version
146 // takes precedence over location).
147 //================================================
148
149 /// `libclang` directory patterns for Haiku.
150 const DIRECTORIES_HAIKUrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_HAIKU.: &[&str] = &[
151 "/boot/home/config/non-packaged/develop/lib",
152 "/boot/home/config/non-packaged/lib",
153 "/boot/system/non-packaged/develop/lib",
154 "/boot/system/non-packaged/lib",
155 "/boot/system/develop/lib",
156 "/boot/system/lib",
157 ];
158
159 /// `libclang` directory patterns for Linux (and FreeBSD).
160 const DIRECTORIES_LINUXrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_LINUX.: &[&str] = &[
161 "/usr/local/llvm*/lib*",
162 "/usr/local/lib*/*/*",
163 "/usr/local/lib*/*",
164 "/usr/local/lib*",
165 "/usr/lib*/*/*",
166 "/usr/lib*/*",
167 "/usr/lib*",
168 ];
169
170 /// `libclang` directory patterns for macOS.
171 const DIRECTORIES_MACOSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_MACOS.: &[&str] = &[
172 "/usr/local/opt/llvm*/lib/llvm*/lib",
173 "/Library/Developer/CommandLineTools/usr/lib",
174 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib",
175 "/usr/local/opt/llvm*/lib",
176 ];
177
178 /// `libclang` directory patterns for Windows.
179 ///
180 /// The boolean indicates whether the directory pattern should be used when
181 /// compiling for an MSVC target environment.
182 const DIRECTORIES_WINDOWSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_WINDOWS.: &[(&str, bool)] = &[
183 // LLVM + Clang can be installed using Scoop (https://scoop.sh).
184 // Other Windows package managers install LLVM + Clang to other listed
185 // system-wide directories.
186 ("C:\\Users\\*\\scoop\\apps\\llvm\\current\\lib", true),
187 ("C:\\MSYS*\\MinGW*\\lib", false),
188 ("C:\\Program Files*\\LLVM\\lib", true),
189 ("C:\\LLVM\\lib", true),
190 // LLVM + Clang can be installed as a component of Visual Studio.
191 // https://github.com/KyleMayes/clang-sys/issues/121
192 ("C:\\Program Files*\\Microsoft Visual Studio\\*\\VC\\Tools\\Llvm\\**\\lib", true),
193 ];
194
195 /// `libclang` directory patterns for illumos
196 const DIRECTORIES_ILLUMOSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_ILLUMOS.: &[&str] = &[
197 "/opt/ooce/llvm-*/lib",
198 "/opt/ooce/clang-*/lib",
199 ];
200
201 //================================================
202 // Searching
203 //================================================
204
205 /// Finds the files in a directory that match one or more filename glob patterns
206 /// and returns the paths to and filenames of those files.
207 fn search_directoryrust-analyzer cargo clang-sys 1.8.1 common/search_directory().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, filenames: &[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#<(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#)> {
208 // Escape the specified directory in case it contains characters that have
209 // special meaning in glob patterns (e.g., `[` or `]`).
210 let directory = Patternrust-analyzer cargo glob 0.3.4 Pattern#::escaperust-analyzer cargo glob 0.3.4 impl#[Pattern]escape().(directory.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
211 let directory = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&directory);
212
213 // Join the escaped directory to the filename glob patterns to obtain
214 // complete glob patterns for the files being searched for.
215 let paths = filenames
216 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
217 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(f).to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().());
218
219 // Prevent wildcards from matching path separators to ensure that the search
220 // is limited to the specified directory.
221 let mut options = MatchOptionsrust-analyzer cargo glob 0.3.4 MatchOptions#::newrust-analyzer cargo glob 0.3.4 impl#[MatchOptions]new().();
222 options.require_literal_separatorrust-analyzer cargo glob 0.3.4 MatchOptions#require_literal_separator. = true;
223
224 paths
225 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|p| globrust-analyzer cargo glob 0.3.4 crate/::glob_withrust-analyzer cargo glob 0.3.4 glob_with().(&p, options))
226 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#::okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().)
227 .flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().()
228 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|p| {
229 let path = p.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
230 let filename = path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
231
232 // The `libclang_shared` library has been renamed to `libclang-cpp`
233 // in Clang 10. This can cause instances of this library (e.g.,
234 // `libclang-cpp.so.10`) to be matched by patterns looking for
235 // instances of `libclang`.
236 if filename.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("-cpp.") {
237 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
238 }
239
240 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path][ToOwned]to_owned().(), filename.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))
241 })
242 .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#<_>>()
243 }
244
245 /// Finds the files in a directory (and any relevant sibling directories) that
246 /// match one or more filename glob patterns and returns the paths to and
247 /// filenames of those files.
248 fn search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, filenames: &[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#<(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#)> {
249 let mut results = search_directoryrust-analyzer cargo clang-sys 1.8.1 common/search_directory().(directory, filenames);
250
251 // On Windows, `libclang.dll` is usually found in the LLVM `bin` directory
252 // while `libclang.lib` is usually found in the LLVM `lib` directory. To
253 // keep things consistent with other platforms, only LLVM `lib` directories
254 // are included in the backup search directory globs so we need to search
255 // the LLVM `bin` directory here.
256 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("windows") && directory.ends_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]ends_with().("lib") {
257 let sibling = directory.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("bin");
258 results.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoryrust-analyzer cargo clang-sys 1.8.1 common/search_directory().(&sibling, filenames));
259 }
260
261 results
262 }
263
264 /// Finds the `libclang` static or dynamic libraries matching one or more
265 /// filename glob patterns and returns the paths to and filenames of those files.
266 pub fn search_libclang_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_libclang_directories().(filenames: &[Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#], variable: &str) -> 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#)> {
267 // Search only the path indicated by the relevant environment variable
268 // (e.g., `LIBCLANG_PATH`) if it is set.
269 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) = 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().(variable).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|d| Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&d).to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()) {
270 // Check if the path is a matching file.
271 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent) = path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().() {
272 let filename = path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
273 let libraries = search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(parent, filenames);
274 if libraries.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|(_, f)| f rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().filename) {
275 return vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(parent.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), filename.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())];
276 }
277 }
278
279 // Check if the path is directory containing a matching file.
280 return search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&path, filenames);
281 }
282
283 let mut found = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
284
285 // Search the `bin` and `lib` directories in the directory returned by
286 // `llvm-config --prefix`.
287 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(output) = run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(&["--prefix"]) {
288 let directory = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(output.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Lines<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()).to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().();
289 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("bin"), filenames));
290 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("lib"), filenames));
291 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("lib64"), filenames));
292 }
293
294 // Search the toolchain directory in the directory returned by
295 // `xcode-select --print-path`.
296 if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("macos") {
297 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(output) = run_xcode_selectrust-analyzer cargo clang-sys 1.8.1 common/run_xcode_select().(&["--print-path"]) {
298 let directory = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(output.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Lines<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()).to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().();
299 let directory = directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("Toolchains/XcodeDefault.xctoolchain/usr/lib");
300 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory, filenames));
301 }
302 }
303
304 // Search the directories in the `LD_LIBRARY_PATH` environment variable.
305 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) = 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().("LD_LIBRARY_PATH") {
306 for directory in envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::split_pathsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/split_paths().(&path) {
307 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory, filenames));
308 }
309 }
310
311 // Determine the `libclang` directory patterns.
312 let directories: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> = if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("haiku") {
313 DIRECTORIES_HAIKUrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_HAIKU..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
314 } else 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") {
315 DIRECTORIES_LINUXrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_LINUX..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
316 } else if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("macos") {
317 DIRECTORIES_MACOSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_MACOS..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
318 } else if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("windows") {
319 let msvc = target_envrust-analyzer cargo clang-sys 1.8.1 macros/target_env!!("msvc");
320 DIRECTORIES_WINDOWSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_WINDOWS.
321 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
322 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|d| d.1 || !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().msvc)
323 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|d| d.0)
324 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
325 } else if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("illumos") {
326 DIRECTORIES_ILLUMOSrust-analyzer cargo clang-sys 1.8.1 common/DIRECTORIES_ILLUMOS..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
327 } else {
328 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]
329 };
330
331 // We use temporary directories when testing the build script so we'll
332 // remove the prefixes that make the directories absolute.
333 let directories = if testrust-analyzer cargo clang-sys 1.8.1 macros/test!!() {
334 directories
335 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
336 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|d| d.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('/').or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| d.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("C:\\")).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(d))
337 .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#<_>>()
338 } else {
339 directories
340 };
341
342 // Search the directories provided by the `libclang` directory patterns.
343 let mut options = MatchOptionsrust-analyzer cargo glob 0.3.4 MatchOptions#::newrust-analyzer cargo glob 0.3.4 impl#[MatchOptions]new().();
344 options.case_sensitiverust-analyzer cargo glob 0.3.4 MatchOptions#case_sensitive. = false;
345 options.require_literal_separatorrust-analyzer cargo glob 0.3.4 MatchOptions#require_literal_separator. = true;
346 for directory in directories.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
347 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(directories) = globrust-analyzer cargo glob 0.3.4 crate/::glob_withrust-analyzer cargo glob 0.3.4 glob_with().(directory, options) {
348 for directory in directories.filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#::okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().).filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|p| p.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_dir().()) {
349 found.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(search_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_directories().(&directory, filenames));
350 }
351 }
352 }
353
354 found
355 }