xref: /qemu/rust/qemu-api/build.rs (revision 4aed0296b307b6e2e3b7d38ee6c5204cf2dfe1ca)
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             "\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!("{}/bindings.inc.rs", out_dir);
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     // Check for available rustc features
42     if rustc::is_min_version("1.77.0").unwrap_or(false) {
43         println!("cargo:rustc-cfg=has_offset_of");
44     }
45 
46     println!("cargo:rerun-if-changed=build.rs");
47     Ok(())
48 }
49