Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/src/libfdt-bindings/build.rs
1956 views
1
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: Apache-2.0
3
4
use std::process::Command;
5
6
/// Get the ld linker search paths
7
///
8
/// Cargo overwrites LD_LIBRARY_PATH with rust specific paths. But we need the default system
9
/// paths in order to find libfdt. So we query `ld` in order to get them.
10
fn get_ld_search_dirs() -> Vec<String> {
11
// We need to extract from `ld --verbose` all the search paths.
12
// For example `ld --verbose | grep SEARCH_DIR | tr -s ' ;' '\n'` returns the following:
13
// ```
14
// SEARCH_DIR("=/usr/local/lib/aarch64-linux-gnu")
15
// SEARCH_DIR("=/lib/aarch64-linux-gnu")
16
// SEARCH_DIR("=/usr/lib/aarch64-linux-gnu")
17
// SEARCH_DIR("=/usr/local/lib")
18
// SEARCH_DIR("=/lib")
19
// SEARCH_DIR("=/usr/lib")
20
// SEARCH_DIR("=/usr/aarch64-linux-gnu/lib")
21
// ```
22
let cmd = r#"
23
ld --verbose | grep -oP '(?<=SEARCH_DIR\(\"=)[^"]+(?=\"\);)'
24
"#;
25
26
Command::new("sh")
27
.arg("-c")
28
.arg(cmd)
29
.output()
30
.ok()
31
.and_then(|output| {
32
if output.status.success() {
33
return Some(output.stdout);
34
}
35
None
36
})
37
.and_then(|stdout_bytes| String::from_utf8(stdout_bytes).ok())
38
.map_or(vec![], |stdout| {
39
stdout.lines().map(|item| item.to_string()).collect()
40
})
41
}
42
43
fn main() {
44
for ld_search_dir in get_ld_search_dirs() {
45
println!("cargo:rustc-link-search=native={}", ld_search_dir);
46
}
47
}
48
49