xref: /qemu/rust/qemu-api/build.rs (revision cb7ada5409f171dae364f206a7fe3ff30fcba7cb)
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 use version_check as rustc;
12 
13 fn main() -> Result<()> {
14     // Placing bindings.inc.rs in the source directory is supported
15     // but not documented or encouraged.
16     let path = env::var("MESON_BUILD_ROOT")
17         .unwrap_or_else(|_| format!("{}/src", env!("CARGO_MANIFEST_DIR")));
18 
19     let file = format!("{}/bindings.inc.rs", path);
20     let file = Path::new(&file);
21     if !Path::new(&file).exists() {
22         panic!(concat!(
23             "No generated C bindings found! If you want to run `cargo`, start a subshell\n",
24             "with `meson devenv`, or point MESON_BUILD_ROOT to the top of the build tree."
25         ));
26     }
27 
28     let out_dir = env::var("OUT_DIR").unwrap();
29     let dest_path = format!("{}/bindings.inc.rs", out_dir);
30     let dest_path = Path::new(&dest_path);
31     if dest_path.symlink_metadata().is_ok() {
32         remove_file(dest_path)?;
33     }
34     symlink_file(file, dest_path)?;
35 
36     // Check for available rustc features
37     if rustc::is_min_version("1.77.0").unwrap_or(false) {
38         println!("cargo:rustc-cfg=has_offset_of");
39     }
40 
41     println!("cargo:rerun-if-changed=build.rs");
42     Ok(())
43 }
44