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