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 file = if let Ok(root) = env::var("MESON_BUILD_ROOT") { 13 format!("{root}/rust/qemu-api/bindings.inc.rs") 14 } else { 15 // Placing bindings.inc.rs in the source directory is supported 16 // but not documented or encouraged. 17 format!("{}/src/bindings.inc.rs", env!("CARGO_MANIFEST_DIR")) 18 }; 19 20 let file = Path::new(&file); 21 if !Path::new(&file).exists() { 22 panic!(concat!( 23 "\n", 24 " No generated C bindings found! Maybe you wanted one of\n", 25 " `make clippy`, `make rustfmt`, `make rustdoc`?\n", 26 "\n", 27 " For other uses of `cargo`, start a subshell with\n", 28 " `pyvenv/bin/meson devenv`, or point MESON_BUILD_ROOT to\n", 29 " the top of the build tree." 30 )); 31 } 32 33 let out_dir = env::var("OUT_DIR").unwrap(); 34 let dest_path = format!("{out_dir}/bindings.inc.rs"); 35 let dest_path = Path::new(&dest_path); 36 if dest_path.symlink_metadata().is_ok() { 37 remove_file(dest_path)?; 38 } 39 symlink_file(file, dest_path)?; 40 41 println!("cargo:rerun-if-changed=build.rs"); 42 Ok(()) 43 } 44