|
1 | | -use std::fs::File; |
2 | 1 | use std::path::{Path, PathBuf}; |
3 | 2 | use std::process::{Command, Stdio}; |
4 | 3 | use std::time::{SystemTime, UNIX_EPOCH}; |
5 | 4 | use std::{env, fs}; |
6 | | -use std::thread; |
7 | 5 |
|
8 | 6 | /// A helper macro to `unwrap` a result except also print out details like: |
9 | 7 | /// |
@@ -189,123 +187,6 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool { |
189 | 187 | } |
190 | 188 | } |
191 | 189 |
|
192 | | -#[must_use] |
193 | | -pub struct NativeLibBoilerplate { |
194 | | - pub src_dir: PathBuf, |
195 | | - pub out_dir: PathBuf, |
196 | | -} |
197 | | - |
198 | | -impl NativeLibBoilerplate { |
199 | | - /// On macOS we don't want to ship the exact filename that compiler-rt builds. |
200 | | - /// This conflicts with the system and ours is likely a wildly different |
201 | | - /// version, so they can't be substituted. |
202 | | - /// |
203 | | - /// As a result, we rename it here but we need to also use |
204 | | - /// `install_name_tool` on macOS to rename the commands listed inside of it to |
205 | | - /// ensure it's linked against correctly. |
206 | | - pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) { |
207 | | - if env::var("TARGET").unwrap() != "x86_64-apple-darwin" { |
208 | | - return |
209 | | - } |
210 | | - |
211 | | - let dir = self.out_dir.join("build/lib/darwin"); |
212 | | - let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name); |
213 | | - let src = dir.join(&format!("lib{}.dylib", name)); |
214 | | - let new_name = format!("lib__rustc__{}.dylib", name); |
215 | | - let dst = dir.join(&new_name); |
216 | | - |
217 | | - println!("{} => {}", src.display(), dst.display()); |
218 | | - fs::rename(&src, &dst).unwrap(); |
219 | | - let status = Command::new("install_name_tool") |
220 | | - .arg("-id") |
221 | | - .arg(format!("@rpath/{}", new_name)) |
222 | | - .arg(&dst) |
223 | | - .status() |
224 | | - .expect("failed to execute `install_name_tool`"); |
225 | | - assert!(status.success()); |
226 | | - } |
227 | | -} |
228 | | - |
229 | | -impl Drop for NativeLibBoilerplate { |
230 | | - fn drop(&mut self) { |
231 | | - if !thread::panicking() { |
232 | | - t!(File::create(self.out_dir.join("rustbuild.timestamp"))); |
233 | | - } |
234 | | - } |
235 | | -} |
236 | | - |
237 | | -// Perform standard preparations for native libraries that are build only once for all stages. |
238 | | -// Emit rerun-if-changed and linking attributes for Cargo, check if any source files are |
239 | | -// updated, calculate paths used later in actual build with CMake/make or C/C++ compiler. |
240 | | -// If Err is returned, then everything is up-to-date and further build actions can be skipped. |
241 | | -// Timestamps are created automatically when the result of `native_lib_boilerplate` goes out |
242 | | -// of scope, so all the build actions should be completed until then. |
243 | | -pub fn native_lib_boilerplate( |
244 | | - src_dir: &Path, |
245 | | - out_name: &str, |
246 | | - link_name: &str, |
247 | | - search_subdir: &str, |
248 | | -) -> Result<NativeLibBoilerplate, ()> { |
249 | | - rerun_if_changed_anything_in_dir(src_dir); |
250 | | - |
251 | | - let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(|| |
252 | | - env::var_os("OUT_DIR").unwrap()); |
253 | | - let out_dir = PathBuf::from(out_dir).join(out_name); |
254 | | - t!(fs::create_dir_all(&out_dir)); |
255 | | - if link_name.contains('=') { |
256 | | - println!("cargo:rustc-link-lib={}", link_name); |
257 | | - } else { |
258 | | - println!("cargo:rustc-link-lib=static={}", link_name); |
259 | | - } |
260 | | - println!( |
261 | | - "cargo:rustc-link-search=native={}", |
262 | | - out_dir.join(search_subdir).display() |
263 | | - ); |
264 | | - |
265 | | - let timestamp = out_dir.join("rustbuild.timestamp"); |
266 | | - if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(src_dir, ×tamp) { |
267 | | - Ok(NativeLibBoilerplate { |
268 | | - src_dir: src_dir.to_path_buf(), |
269 | | - out_dir, |
270 | | - }) |
271 | | - } else { |
272 | | - Err(()) |
273 | | - } |
274 | | -} |
275 | | - |
276 | | -pub fn sanitizer_lib_boilerplate(sanitizer_name: &str) |
277 | | - -> Result<(NativeLibBoilerplate, String), ()> |
278 | | -{ |
279 | | - let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() { |
280 | | - "x86_64-unknown-linux-gnu" => ( |
281 | | - format!("clang_rt.{}-x86_64", sanitizer_name), |
282 | | - "build/lib/linux", |
283 | | - false, |
284 | | - ), |
285 | | - "x86_64-apple-darwin" => ( |
286 | | - format!("clang_rt.{}_osx_dynamic", sanitizer_name), |
287 | | - "build/lib/darwin", |
288 | | - true, |
289 | | - ), |
290 | | - _ => return Err(()), |
291 | | - }; |
292 | | - let to_link = if apple { |
293 | | - format!("dylib=__rustc__{}", link_name) |
294 | | - } else { |
295 | | - format!("static={}", link_name) |
296 | | - }; |
297 | | - // This env var is provided by rustbuild to tell us where `compiler-rt` |
298 | | - // lives. |
299 | | - let dir = env::var_os("RUST_COMPILER_RT_ROOT").unwrap(); |
300 | | - let lib = native_lib_boilerplate( |
301 | | - dir.as_ref(), |
302 | | - sanitizer_name, |
303 | | - &to_link, |
304 | | - search_path, |
305 | | - )?; |
306 | | - Ok((lib, link_name)) |
307 | | -} |
308 | | - |
309 | 190 | fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool { |
310 | 191 | t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| { |
311 | 192 | let meta = t!(e.metadata()); |
|
0 commit comments