1 // Copyright 2018 Kyle Mayes
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::env;
16 use std::fs::File;
17 use std::io::{self, Error, ErrorKind, Read, Seek, SeekFrom};
18 use std::path::{Path, PathBuf};
19
20 use super::common;
21
22 /// Returns the ELF class from the ELF header in the supplied file.
parse_elf_header(path: &Path) -> io::Result<u8>23 fn parse_elf_header(path: &Path) -> io::Result<u8> {
24 let mut file = File::open(path)?;
25 let mut buffer = [0; 5];
26 file.read_exact(&mut buffer)?;
27 if buffer[..4] == [127, 69, 76, 70] {
28 Ok(buffer[4])
29 } else {
30 Err(Error::new(ErrorKind::InvalidData, "invalid ELF header"))
31 }
32 }
33
34 /// Returns the magic number from the PE header in the supplied file.
parse_pe_header(path: &Path) -> io::Result<u16>35 fn parse_pe_header(path: &Path) -> io::Result<u16> {
36 let mut file = File::open(path)?;
37
38 // Determine the header offset.
39 let mut buffer = [0; 4];
40 let start = SeekFrom::Start(0x3C);
41 file.seek(start)?;
42 file.read_exact(&mut buffer)?;
43 let offset = i32::from_le_bytes(buffer);
44
45 // Determine the validity of the header.
46 file.seek(SeekFrom::Start(offset as u64))?;
47 file.read_exact(&mut buffer)?;
48 if buffer != [80, 69, 0, 0] {
49 return Err(Error::new(ErrorKind::InvalidData, "invalid PE header"));
50 }
51
52 // Find the magic number.
53 let mut buffer = [0; 2];
54 file.seek(SeekFrom::Current(20))?;
55 file.read_exact(&mut buffer)?;
56 Ok(u16::from_le_bytes(buffer))
57 }
58
59 /// Validates the header for the supplied `libclang` shared library.
validate_header(path: &Path) -> Result<(), String>60 fn validate_header(path: &Path) -> Result<(), String> {
61 if cfg!(any(target_os = "freebsd", target_os = "linux")) {
62 let class = parse_elf_header(path).map_err(|e| e.to_string())?;
63
64 if cfg!(target_pointer_width = "32") && class != 1 {
65 return Err("invalid ELF class (64-bit)".into());
66 }
67
68 if cfg!(target_pointer_width = "64") && class != 2 {
69 return Err("invalid ELF class (32-bit)".into());
70 }
71
72 Ok(())
73 } else if cfg!(target_os = "windows") {
74 let magic = parse_pe_header(path).map_err(|e| e.to_string())?;
75
76 if cfg!(target_pointer_width = "32") && magic != 267 {
77 return Err("invalid DLL (64-bit)".into());
78 }
79
80 if cfg!(target_pointer_width = "64") && magic != 523 {
81 return Err("invalid DLL (32-bit)".into());
82 }
83
84 Ok(())
85 } else {
86 Ok(())
87 }
88 }
89
90 /// Returns the components of the version in the supplied `libclang` shared
91 // library filename.
parse_version(filename: &str) -> Vec<u32>92 fn parse_version(filename: &str) -> Vec<u32> {
93 let version = if let Some(version) = filename.strip_prefix("libclang.so.") {
94 version
95 } else if filename.starts_with("libclang-") {
96 &filename[9..filename.len() - 3]
97 } else {
98 return vec![];
99 };
100
101 version.split('.').map(|s| s.parse().unwrap_or(0)).collect()
102 }
103
104 /// Returns the paths to, the filenames, and the versions of the `libclang`
105 // shared libraries.
search_libclang_directories(runtime: bool) -> Result<Vec<(PathBuf, String, Vec<u32>)>, String>106 fn search_libclang_directories(runtime: bool) -> Result<Vec<(PathBuf, String, Vec<u32>)>, String> {
107 let mut files = vec![format!(
108 "{}clang{}",
109 env::consts::DLL_PREFIX,
110 env::consts::DLL_SUFFIX
111 )];
112
113 if cfg!(target_os = "linux") {
114 // Some Linux distributions don't create a `libclang.so` symlink, so we
115 // need to look for versioned files (e.g., `libclang-3.9.so`).
116 files.push("libclang-*.so".into());
117
118 // Some Linux distributions don't create a `libclang.so` symlink and
119 // don't have versioned files as described above, so we need to look for
120 // suffix versioned files (e.g., `libclang.so.1`). However, `ld` cannot
121 // link to these files, so this will only be included when linking at
122 // runtime.
123 if runtime {
124 files.push("libclang.so.*".into());
125 files.push("libclang-*.so.*".into());
126 }
127 }
128
129 if cfg!(any(
130 target_os = "openbsd",
131 target_os = "freebsd",
132 target_os = "netbsd",
133 target_os = "haiku"
134 )) {
135 // Some BSD distributions don't create a `libclang.so` symlink either,
136 // but use a different naming scheme for versioned files (e.g.,
137 // `libclang.so.7.0`).
138 files.push("libclang.so.*".into());
139 }
140
141 if cfg!(target_os = "windows") {
142 // The official LLVM build uses `libclang.dll` on Windows instead of
143 // `clang.dll`. However, unofficial builds such as MinGW use `clang.dll`.
144 files.push("libclang.dll".into());
145 }
146
147 // Validate the `libclang` shared libraries and collect the versions.
148 let mut valid = vec![];
149 let mut invalid = vec![];
150 for (directory, filename) in common::search_libclang_directories(&files, "LIBCLANG_PATH") {
151 let path = directory.join(&filename);
152 match validate_header(&path) {
153 Ok(()) => {
154 let version = parse_version(&filename);
155 valid.push((directory, filename, version))
156 }
157 Err(message) => invalid.push(format!("({}: {})", path.display(), message)),
158 }
159 }
160
161 if !valid.is_empty() {
162 return Ok(valid);
163 }
164
165 let message = format!(
166 "couldn't find any valid shared libraries matching: [{}], set the \
167 `LIBCLANG_PATH` environment variable to a path where one of these files \
168 can be found (invalid: [{}])",
169 files
170 .iter()
171 .map(|f| format!("'{}'", f))
172 .collect::<Vec<_>>()
173 .join(", "),
174 invalid.join(", "),
175 );
176
177 Err(message)
178 }
179
180 /// Returns the directory and filename of the "best" available `libclang` shared
181 /// library.
find(runtime: bool) -> Result<(PathBuf, String), String>182 pub fn find(runtime: bool) -> Result<(PathBuf, String), String> {
183 search_libclang_directories(runtime)?
184 .iter()
185 // We want to find the `libclang` shared library with the highest
186 // version number, hence `max_by_key` below.
187 //
188 // However, in the case where there are multiple such `libclang` shared
189 // libraries, we want to use the order in which they appeared in the
190 // list returned by `search_libclang_directories` as a tiebreaker since
191 // that function returns `libclang` shared libraries in descending order
192 // of preference by how they were found.
193 //
194 // `max_by_key`, perhaps surprisingly, returns the *last* item with the
195 // maximum key rather than the first which results in the opposite of
196 // the tiebreaking behavior we want. This is easily fixed by reversing
197 // the list first.
198 .rev()
199 .max_by_key(|f| &f.2)
200 .cloned()
201 .map(|(path, filename, _)| (path, filename))
202 .ok_or_else(|| "unreachable".into())
203 }
204
205 /// Find and link to `libclang` dynamically.
206 #[cfg(not(feature = "runtime"))]
link()207 pub fn link() {
208 let cep = common::CommandErrorPrinter::default();
209
210 use std::fs;
211
212 let (directory, filename) = find(false).unwrap();
213 println!("cargo:rustc-link-search={}", directory.display());
214
215 if cfg!(all(target_os = "windows", target_env = "msvc")) {
216 // Find the `libclang` stub static library required for the MSVC
217 // toolchain.
218 let lib = if !directory.ends_with("bin") {
219 directory
220 } else {
221 directory.parent().unwrap().join("lib")
222 };
223
224 if lib.join("libclang.lib").exists() {
225 println!("cargo:rustc-link-search={}", lib.display());
226 } else if lib.join("libclang.dll.a").exists() {
227 // MSYS and MinGW use `libclang.dll.a` instead of `libclang.lib`.
228 // It is linkable with the MSVC linker, but Rust doesn't recognize
229 // the `.a` suffix, so we need to copy it with a different name.
230 //
231 // FIXME: Maybe we can just hardlink or symlink it?
232 let out = env::var("OUT_DIR").unwrap();
233 fs::copy(
234 lib.join("libclang.dll.a"),
235 Path::new(&out).join("libclang.lib"),
236 )
237 .unwrap();
238 println!("cargo:rustc-link-search=native={}", out);
239 } else {
240 panic!(
241 "using '{}', so 'libclang.lib' or 'libclang.dll.a' must be \
242 available in {}",
243 filename,
244 lib.display(),
245 );
246 }
247
248 println!("cargo:rustc-link-lib=dylib=libclang");
249 } else {
250 let name = filename.trim_start_matches("lib");
251
252 // Strip extensions and trailing version numbers (e.g., the `.so.7.0` in
253 // `libclang.so.7.0`).
254 let name = match name.find(".dylib").or_else(|| name.find(".so")) {
255 Some(index) => &name[0..index],
256 None => name,
257 };
258
259 println!("cargo:rustc-link-lib=dylib={}", name);
260 }
261
262 cep.discard();
263 }
264