build.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use std::env;
  2. use std::io::Write;
  3. use std::path::PathBuf;
  4. use std::process::Command;
  5. fn main() {
  6. let n_threads = std::thread::available_parallelism()
  7. .unwrap()
  8. .get()
  9. .to_string();
  10. let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
  11. let build_dir = &cargo_dir.join("build");
  12. std::fs::create_dir_all(build_dir).unwrap();
  13. env::set_current_dir(build_dir).unwrap();
  14. // Generate CMake cache files
  15. let b = Command::new("cmake")
  16. .arg("-DARCH=native")
  17. .arg("..")
  18. .output()
  19. .expect("Failed to generate Makefile with CMake");
  20. std::io::stdout().write_all(&b.stdout).unwrap();
  21. std::io::stderr().write_all(&b.stderr).unwrap();
  22. assert!(b.status.success());
  23. // Build the library
  24. let b = Command::new("cmake")
  25. .arg("--build")
  26. .arg(".")
  27. .arg("--config")
  28. .arg("Release")
  29. .arg("-j")
  30. .arg(n_threads)
  31. .output()
  32. .expect("Failed to build RandomX library with CMake");
  33. std::io::stdout().write_all(&b.stdout).unwrap();
  34. std::io::stderr().write_all(&b.stderr).unwrap();
  35. assert!(b.status.success());
  36. env::set_current_dir(cargo_dir).unwrap();
  37. // Tell cargo how to find the static library
  38. println!(
  39. "cargo:rustc-link-search=native={}",
  40. build_dir.to_string_lossy()
  41. );
  42. println!("cargo:rustc-link-lib=static=randomx");
  43. let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or("linux".to_string());
  44. let dylib_name = match target_os.as_str() {
  45. "freebsd" | "macos" | "ios" => "c++", // FreeBSD, MacOS, and iOS use "c++",
  46. "windows" => "msvcrt", // Use MSVC runtime on Windows
  47. _ => "stdc++", // Default for other systems (Linux, etc.)
  48. };
  49. println!("cargo:rustc-link-lib=dylib={}", dylib_name);
  50. if cfg!(target_os = "windows") {
  51. println!("cargo:rustc-link-lib=advapi32");
  52. }
  53. // The bindgen::Builder is the main entry point
  54. // to bindgen, and lets you build up options for
  55. // the resulting bindings.
  56. let bindings = bindgen::Builder::default()
  57. // The input header we would like to generate
  58. // bindings for.
  59. .header("src/randomx.h")
  60. // Tell cargo to invalidate the built crate whenever any of the
  61. // included header files changed.
  62. .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
  63. // Finish the builder and generate the bindings.
  64. .generate()
  65. // Unwrap the Result and panic on failure.
  66. .expect("Unable to generate bindings");
  67. // Write the bindings to the $OUT_DIR/bindings.rs file.
  68. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
  69. bindings
  70. .write_to_file(out_path.join("bindings.rs"))
  71. .expect("Couldn't write bindings!");
  72. }