| 1 | // Copyright 2024, Linaro Limited |
| 2 | // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | #[cfg(unix)] |
| 6 | use std::os::unix::fs::symlink as symlink_file; |
| 7 | #[cfg(windows)] |
| 8 | use std::os::windows::fs::symlink_file; |
| 9 | use std::{env, fs::remove_file, io::Result, path::Path}; |
| 10 | |
| 11 | fn main() -> Result<()> { |
| 12 | let manifest_dir = env!("CARGO_MANIFEST_DIR"); |
| 13 | let root = env::var("MESON_BUILD_ROOT").expect(concat!( |
| 14 | "\n", |
| 15 | " MESON_BUILD_ROOT not found. Maybe you wanted one of\n", |
| 16 | " `make clippy`, `make rustfmt`, `make rustdoc`?\n", |
| 17 | "\n", |
| 18 | " For other uses of `cargo`, start a subshell with\n", |
| 19 | " `pyvenv/bin/meson devenv`, or point MESON_BUILD_ROOT to\n", |
| 20 | " the top of the build tree." |
| 21 | )); |
| 22 | |
| 23 | let sub = get_rust_subdir(manifest_dir).unwrap(); |
| 24 | let file = format!("{root}/{sub}/bindings.inc.rs"); |
| 25 | let file = Path::new(&file); |
| 26 | |
| 27 | if !file.exists() { |
| 28 | panic!(concat!( |
| 29 | "\n", |
| 30 | " No generated C bindings found! Run `make` first; or maybe you\n", |
| 31 | " wanted one of `make clippy`, `make rustfmt`, `make rustdoc`?\n", |
| 32 | )); |
| 33 | } |
| 34 | |
| 35 | let out_dir = env::var("OUT_DIR").unwrap(); |
| 36 | let dest_path = format!("{out_dir}/bindings.inc.rs"); |
| 37 | let dest_path = Path::new(&dest_path); |
| 38 | if dest_path.symlink_metadata().is_ok() { |
| 39 | remove_file(dest_path)?; |
| 40 | } |
| 41 | symlink_file(file, dest_path)?; |
| 42 | |
| 43 | println!("cargo:rerun-if-changed=build.rs"); |
| 44 | Ok(()) |
| 45 | } |
| 46 | |
| 47 | fn get_rust_subdir(path: &str) -> Option<&str> { |
| 48 | path.find("/rust").map(|index| &path[index + 1..]) |
| 49 | } |