diff --git a/Cargo.toml b/Cargo.toml index 250d89e..4d4b7c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openvr_sys" -version = "2.1.0" +version = "2.1.1" edition = "2021" rust-version = "1.82.0" authors = [ diff --git a/build.rs b/build.rs index 0092022..4981d66 100644 --- a/build.rs +++ b/build.rs @@ -1,13 +1,31 @@ -extern crate bindgen; -extern crate cmake; - +use bindgen; +use cmake; use std::env; +use std::path::PathBuf; fn main() { - let mut config = cmake::Config::new("openvr"); + let out_dir = PathBuf::from(env::var("OUT_DIR").expect("Missing OUT_DIR env var")); + + println!("cargo:rerun-if-changed=wrapper.hpp"); + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); let target_pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap(); + // Configure cmake to place build output in OUT_DIR + let out_dir_str = out_dir.to_string_lossy().into_owned(); + let mut config = cmake::Config::new("openvr"); + let config = config + .define("CMAKE_LIBRARY_OUTPUT_DIRECTORY", &out_dir_str) + .define("CMAKE_ARCHIVE_OUTPUT_DIRECTORY", &out_dir_str) + .define("CMAKE_RUNTIME_OUTPUT_DIRECTORY", &out_dir_str) + .define("CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG", &out_dir_str) + .define("CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE", &out_dir_str) + .define("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG", &out_dir_str) + .define("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE", &out_dir_str) + .define("CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG", &out_dir_str) + .define("CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE", &out_dir_str) + .out_dir(&out_dir); + if target_os == "macos" { config.define("BUILD_UNIVERSAL", "OFF"); } else if target_os == "windows" { @@ -16,6 +34,7 @@ fn main() { } let dst = config.build(); + println!("cargo:rustc-link-search=native={}/lib", dst.display()); if target_os == "windows" && target_pointer_width == "64" { @@ -32,12 +51,13 @@ fn main() { println!("cargo:rustc-link-lib=shell32"); } + // Generate bindings and write them into OUT_DIR bindgen::builder() .header("wrapper.hpp") .constified_enum(".*") .prepend_enum_name(false) .generate() .expect("could not generate bindings") - .write_to_file("bindings.rs") + .write_to_file(out_dir.join("bindings.rs")) .expect("could not write bindings.rs"); } diff --git a/lib.rs b/lib.rs index 1849409..73357e8 100644 --- a/lib.rs +++ b/lib.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)] -include!("bindings.rs"); +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); #[cfg(target_os = "macos")] #[link(name = "Foundation", kind = "framework")]