build.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2024, Linaro Limited
  2. // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #[cfg(unix)]
  5. use std::os::unix::fs::symlink as symlink_file;
  6. #[cfg(windows)]
  7. use std::os::windows::fs::symlink_file;
  8. use std::{env, fs::remove_file, io::Result, path::Path};
  9. use version_check as rustc;
  10. fn main() -> Result<()> {
  11. // Placing bindings.inc.rs in the source directory is supported
  12. // but not documented or encouraged.
  13. let path = env::var("MESON_BUILD_ROOT")
  14. .unwrap_or_else(|_| format!("{}/src", env!("CARGO_MANIFEST_DIR")));
  15. let file = format!("{}/bindings.inc.rs", path);
  16. let file = Path::new(&file);
  17. if !Path::new(&file).exists() {
  18. panic!(concat!(
  19. "\n",
  20. " No generated C bindings found! Maybe you wanted one of\n",
  21. " `make clippy`, `make rustfmt`, `make rustdoc`?\n",
  22. "\n",
  23. " For other uses of `cargo`, start a subshell with\n",
  24. " `pyvenv/bin/meson devenv`, or point MESON_BUILD_ROOT to\n",
  25. " the top of the build tree."
  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. // Check for available rustc features
  36. if rustc::is_min_version("1.77.0").unwrap_or(false) {
  37. println!("cargo:rustc-cfg=has_offset_of");
  38. }
  39. println!("cargo:rerun-if-changed=build.rs");
  40. Ok(())
  41. }