// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: Apache-2.023use std::process::Command;45/// Get the ld linker search paths6///7/// Cargo overwrites LD_LIBRARY_PATH with rust specific paths. But we need the default system8/// paths in order to find libfdt. So we query `ld` in order to get them.9fn get_ld_search_dirs() -> Vec<String> {10// We need to extract from `ld --verbose` all the search paths.11// For example `ld --verbose | grep SEARCH_DIR | tr -s ' ;' '\n'` returns the following:12// ```13// SEARCH_DIR("=/usr/local/lib/aarch64-linux-gnu")14// SEARCH_DIR("=/lib/aarch64-linux-gnu")15// SEARCH_DIR("=/usr/lib/aarch64-linux-gnu")16// SEARCH_DIR("=/usr/local/lib")17// SEARCH_DIR("=/lib")18// SEARCH_DIR("=/usr/lib")19// SEARCH_DIR("=/usr/aarch64-linux-gnu/lib")20// ```21let cmd = r#"22ld --verbose | grep -oP '(?<=SEARCH_DIR\(\"=)[^"]+(?=\"\);)'23"#;2425Command::new("sh")26.arg("-c")27.arg(cmd)28.output()29.ok()30.and_then(|output| {31if output.status.success() {32return Some(output.stdout);33}34None35})36.and_then(|stdout_bytes| String::from_utf8(stdout_bytes).ok())37.map_or(vec![], |stdout| {38stdout.lines().map(|item| item.to_string()).collect()39})40}4142fn main() {43for ld_search_dir in get_ld_search_dirs() {44println!("cargo:rustc-link-search=native={}", ld_search_dir);45}46}474849