Debian Codegraph @

src/support.rs

raw symbols SCIP
1 // SPDX-License-Identifier: Apache-2.0
2
3 //! Provides helper functionality.
4
5 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#};
6 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#;
7 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/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/};
8
9 use globrust-analyzer cargo glob 0.3.4 crate/::{selfrust-analyzer cargo glob 0.3.4 crate/, Patternrust-analyzer cargo glob 0.3.4 Pattern#};
10
11 use libcrust-analyzer cargo libc 0.2.189 crate/::c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#;
12
13 use superrust-analyzer cargo clang-sys 1.8.1 crate/::CXVersionrust-analyzer cargo clang-sys 1.8.1 CXVersion#;
14
15 //================================================
16 // Structs
17 //================================================
18
19 /// A `clang` executable.
20 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
21 pub struct Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang# {
22 /// The path to this `clang` executable.
23 pub pathrust-analyzer cargo clang-sys 1.8.1 support/Clang#path.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
24 /// The version of this `clang` executable if it could be parsed.
25 pub versionrust-analyzer cargo clang-sys 1.8.1 support/Clang#version.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CXVersionrust-analyzer cargo clang-sys 1.8.1 CXVersion#>,
26 /// The directories searched by this `clang` executable for C headers if
27 /// they could be parsed.
28 pub c_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/Clang#c_search_paths.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<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#>>,
29 /// The directories searched by this `clang` executable for C++ headers if
30 /// they could be parsed.
31 pub cpp_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/Clang#cpp_search_paths.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<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#>>,
32 }
33
34 impl Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang# {
35 fn newrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang]new().(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, args: &[Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#]) -> Selfrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang] {
36 Selfrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang] {
37 pathrust-analyzer cargo clang-sys 1.8.1 support/Clang#path.: path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
38 versionrust-analyzer cargo clang-sys 1.8.1 support/Clang#version.: parse_versionrust-analyzer cargo clang-sys 1.8.1 support/parse_version().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()),
39 c_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/Clang#c_search_paths.: parse_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/parse_search_paths().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), "c", args),
40 cpp_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/Clang#cpp_search_paths.: parse_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/parse_search_paths().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), "c++", args),
41 }
42 }
43
44 /// Returns a `clang` executable if one can be found.
45 ///
46 /// If the `CLANG_PATH` environment variable is set, that is the instance of
47 /// `clang` used. Otherwise, a series of directories are searched. First, if
48 /// a path is supplied, that is the first directory searched. Then, the
49 /// directory returned by `llvm-config --bindir` is searched. On macOS
50 /// systems, `xcodebuild -find clang` will next be queried. Last, the
51 /// directories in the system's `PATH` are searched.
52 ///
53 /// ## Cross-compilation
54 ///
55 /// If target arguments are provided (e.g., `--target` followed by a target
56 /// like `x86_64-unknown-linux-gnu`) then this method will prefer a
57 /// target-prefixed instance of `clang` (e.g.,
58 /// `x86_64-unknown-linux-gnu-clang` for the above example).
59 pub fn findrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang]find().(path: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, args: &[Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang#> {
60 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().("CLANG_PATH") {
61 let p = 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().(&path);
62 if p.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() && is_executablerust-analyzer cargo clang-sys 1.8.1 support/is_executable().(p).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(false) {
63 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang#::newrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang]new().(p, args));
64 } else {
65 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("`CLANG_PATH` env var set but is not a full path to an executable");
66 }
67 }
68
69 // Determine the cross-compilation target, if any.
70
71 let mut target = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
72 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#args.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
73 if (args[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."-target" || args[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."-target") && i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().args.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
74 target = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&args[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
75 }
76 }
77
78 // Collect the paths to search for a `clang` executable in.
79
80 let mut paths = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
81
82 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path) = path {
83 paths.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(path.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
84 }
85
86 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) = run_llvm_configrust-analyzer cargo clang-sys 1.8.1 support/run_llvm_config().(&["--bindir"]) {
87 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(line) = path.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().() {
88 paths.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(line.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
89 }
90 }
91
92 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "macos") {
93 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((path, _)) = runrust-analyzer cargo clang-sys 1.8.1 support/run().("xcodebuild", &["-find", "clang"]) {
94 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(line) = path.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().() {
95 paths.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(line.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
96 }
97 }
98 }
99
100 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().("PATH") {
101 paths.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(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));
102 }
103
104 // First, look for a target-prefixed `clang` executable.
105
106 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(target) = target {
107 let default = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}-clang{}", target, 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/::EXE_SUFFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/EXE_SUFFIX.);
108 let versioned = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}-clang-[0-9]*{}", target, 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/::EXE_SUFFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/EXE_SUFFIX.);
109 let patterns = &[&default[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()., &versioned[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().];
110 for path in &paths {
111 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path) = findrust-analyzer cargo clang-sys 1.8.1 support/find().(path, patterns) {
112 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang#::newrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang]new().(path, args));
113 }
114 }
115 }
116
117 // Otherwise, look for any other `clang` executable.
118
119 let default = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("clang{}", 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/::EXE_SUFFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/EXE_SUFFIX.);
120 let versioned = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("clang-[0-9]*{}", 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/::EXE_SUFFIXrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/EXE_SUFFIX.);
121 let patterns = &[&default[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()., &versioned[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().];
122 for path in paths {
123 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path) = findrust-analyzer cargo clang-sys 1.8.1 support/find().(&path, patterns) {
124 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Clangrust-analyzer cargo clang-sys 1.8.1 support/Clang#::newrust-analyzer cargo clang-sys 1.8.1 support/impl#[Clang]new().(path, args));
125 }
126 }
127
128 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
129 }
130 }
131
132 //================================================
133 // Functions
134 //================================================
135
136 /// Returns the first match to the supplied glob patterns in the supplied
137 /// directory if there are any matches.
138 fn findrust-analyzer cargo clang-sys 1.8.1 support/find().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, patterns: &[&str]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
139 // Escape the directory in case it contains characters that have special
140 // meaning in glob patterns (e.g., `[` or `]`).
141 let directory = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(directory) = directory.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().() {
142 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().(&Patternrust-analyzer cargo glob 0.3.4 Pattern#::escaperust-analyzer cargo glob 0.3.4 impl#[Pattern]escape().(directory)).to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path][ToOwned]to_owned().()
143 } else {
144 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
145 };
146
147 for pattern in patterns {
148 let pattern = directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(pattern).to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().().into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().();
149 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path) = globrust-analyzer cargo glob 0.3.4 crate/::globrust-analyzer cargo glob 0.3.4 glob().(&pattern).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()..filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|p| p.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()).nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().() {
150 if path.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() && is_executablerust-analyzer cargo clang-sys 1.8.1 support/is_executable().(&path).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(false) {
151 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path);
152 }
153 }
154 }
155
156 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
157 }
158
159 #[cfg(unix)]
160 fn is_executablerust-analyzer cargo clang-sys 1.8.1 support/is_executable().(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#<bool> {
161 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#;
162 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#;
163
164 let path = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(path.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().().as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
165 unsafe { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(libcrust-analyzer cargo libc 0.2.189 crate/::accessrust-analyzer cargo libc 0.2.189 access().(path.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), libcrust-analyzer cargo libc 0.2.189 crate/::X_OKrust-analyzer cargo libc 0.2.189 unix/linux_like/X_OK.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0) }
166 }
167
168 #[cfg(not(unix))]
169 fn is_executable(_: &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#<bool> {
170 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true)
171 }
172
173 /// Attempts to run an executable, returning the `stdout` and `stderr` output if
174 /// successful.
175 fn runrust-analyzer cargo clang-sys 1.8.1 support/run().(executable: &str, arguments: &[&str]) -> 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#, 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#> {
176 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().(executable)
177 .argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(arguments)
178 .outputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]output().()
179 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|o| {
180 let stdout = 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().(&o.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().();
181 let stderr = 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().(&o.stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Output#stderr.).into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().();
182 (stdout, stderr)
183 })
184 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("could not run executable `{}`: {}", executable, e))
185 }
186
187 /// Runs `clang`, returning the `stdout` and `stderr` output.
188 fn run_clangrust-analyzer cargo clang-sys 1.8.1 support/run_clang().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, arguments: &[&str]) -> (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#) {
189 runrust-analyzer cargo clang-sys 1.8.1 support/run().(&path.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().(), arguments).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
190 }
191
192 /// Runs `llvm-config`, returning the `stdout` output if successful.
193 fn run_llvm_configrust-analyzer cargo clang-sys 1.8.1 support/run_llvm_config().(arguments: &[&str]) -> 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#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
194 let config = 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".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
195 runrust-analyzer cargo clang-sys 1.8.1 support/run().(&config, arguments).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|(o, _)| o)
196 }
197
198 /// Parses a version number if possible, ignoring trailing non-digit characters.
199 fn parse_version_numberrust-analyzer cargo clang-sys 1.8.1 support/parse_version_number().(number: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#> {
200 number
201 .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().()
202 .take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|c| c.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_digit().())
203 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>()
204 .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()
205 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
206 }
207
208 /// Parses the version from the output of a `clang` executable if possible.
209 fn parse_versionrust-analyzer cargo clang-sys 1.8.1 support/parse_version().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CXVersionrust-analyzer cargo clang-sys 1.8.1 CXVersion#> {
210 let output = run_clangrust-analyzer cargo clang-sys 1.8.1 support/run_clang().(path, &["--version"]).0;
211 let start = output.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().("version ")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().8;
212 let mut numbers = output[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('.');
213 let major = numbers.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(parse_version_numberrust-analyzer cargo clang-sys 1.8.1 support/parse_version_number().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
214 let minor = numbers.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(parse_version_numberrust-analyzer cargo clang-sys 1.8.1 support/parse_version_number().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
215 let subminor = numbers.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(parse_version_numberrust-analyzer cargo clang-sys 1.8.1 support/parse_version_number().).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0);
216 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CXVersionrust-analyzer cargo clang-sys 1.8.1 CXVersion# {
217 Majorrust-analyzer cargo clang-sys 1.8.1 CXVersion#Major.: major,
218 Minorrust-analyzer cargo clang-sys 1.8.1 CXVersion#Minor.: minor,
219 Subminorrust-analyzer cargo clang-sys 1.8.1 CXVersion#Subminor.: subminor,
220 })
221 }
222
223 /// Parses the search paths from the output of a `clang` executable if possible.
224 fn parse_search_pathsrust-analyzer cargo clang-sys 1.8.1 support/parse_search_paths().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, language: &str, args: &[Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<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#>> {
225 let mut clang_args = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["-E", "-x", language, "-", "-v"];
226 clang_args.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s));
227 let output = run_clangrust-analyzer cargo clang-sys 1.8.1 support/run_clang().(path, &clang_args).1;
228 let start = output.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().("#include <...> search starts here:")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().34;
229 let end = output.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().("End of search list.")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
230 let paths = output[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("(framework directory)", "");
231 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(
232 paths
233 .linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().()
234 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|l| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().l.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().())
235 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|l| 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().(l.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().()).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
236 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
237 )
238 }