OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 //! This build.rs generates some parameters for Cargo which are necessary to bui
ld. | |
6 //! Particularly, it searches for libsystem_thunks.a. | |
7 | |
8 use std::env; | |
9 use std::ffi::OsStr; | |
10 use std::fs; | |
11 use std::path::Path; | |
12 use std::path::PathBuf; | |
13 use std::vec::Vec; | |
14 | |
15 /// Searches a directory recursively for libsystem_thunks.a | |
16 /// and returns the path if found, or None if not found. | |
17 fn search_output(p: &Path, search_file: &OsStr) -> Option<PathBuf> { | |
18 let mut dirs = Vec::new(); | |
19 let paths = fs::read_dir(p).unwrap(); | |
20 for dir_entry in paths { | |
21 let path = dir_entry.unwrap().path(); | |
22 if path.is_dir() { | |
23 dirs.push(path); | |
24 } else { | |
25 let found = match path.file_name() { | |
26 Some(name) => name == search_file, | |
27 None => false, | |
28 }; | |
29 if found { | |
30 return Some(p.to_path_buf()); | |
31 } | |
32 } | |
33 } | |
34 for path in dirs.iter() { | |
35 match search_output(path.as_path(), search_file) { | |
36 Some(thunks_path) => return Some(thunks_path), | |
37 None => continue, | |
38 } | |
39 } | |
40 None | |
41 } | |
42 | |
43 fn main() { | |
44 let mojo_out_dir = Path::new(env!("MOJO_OUT_DIR")); | |
45 let embed = match env::var("MOJO_RUST_NO_EMBED") { | |
46 Ok(_) => false, | |
47 Err(_) => true, | |
48 }; | |
49 if mojo_out_dir.is_dir() { | |
50 if !embed { | |
51 match search_output(mojo_out_dir, OsStr::new("libsystem_thunks.a"))
{ | |
52 Some(path) => { | |
53 println!("cargo:rustc-link-lib=static=system_thunks"); | |
54 println!("cargo:rustc-link-search=native={}", path.display()
); | |
55 } | |
56 None => panic!("Failed to find system_thunks."), | |
57 } | |
58 } else { | |
59 println!("cargo:rustc-link-lib=stdc++"); | |
60 match search_output(mojo_out_dir, OsStr::new("libvalidation_parser.a
")) { | |
61 Some(path) => { | |
62 println!("cargo:rustc-link-lib=static=validation_parser"); | |
63 println!("cargo:rustc-link-search=native={}", path.display()
); | |
64 } | |
65 None => panic!("Failed to find validation_parser."), | |
66 } | |
67 match search_output(mojo_out_dir, OsStr::new("librust_embedder.a"))
{ | |
68 Some(path) => { | |
69 println!("cargo:rustc-link-lib=static=rust_embedder"); | |
70 println!("cargo:rustc-link-search=native={}", path.display()
); | |
71 } | |
72 None => panic!("Failed to find rust_embedder."), | |
73 } | |
74 } | |
75 } else { | |
76 panic!("MOJO_OUT_DIR is not a valid directory."); | |
77 } | |
78 } | |
OLD | NEW |