Skip to content

Commit 50681dd

Browse files
schulzchRalith
authored andcommitted
Rework build.rs to use CARGO_* environment variables, fixed missing "shell32" (windows), and added "buildtime_bindgen" as an optional feature to generate bindings at compile time.
1 parent 353b1e4 commit 50681dd

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ maintenance = { status = "passively-maintained" }
2626

2727
[build-dependencies]
2828
cmake = "0.1"
29+
bindgen = { version = "0.49", optional = true }
2930

3031
[lib]
3132
name = "openvr_sys"
3233
path = "lib.rs"
34+
35+
[features]
36+
default = []
37+
buildtime_bindgen = ["bindgen"]

build.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1+
#[cfg(feature = "buildtime_bindgen")]
2+
extern crate bindgen;
13
extern crate cmake;
24

3-
fn main() {
4-
let mut cfg = cmake::Config::new("openvr");
5-
6-
// Work around broken cmake build
7-
#[cfg(windows)]
8-
cfg.cxxflag("/DWIN32");
5+
use std::env;
96

10-
#[cfg(target_os="macos")]
11-
cfg.define("BUILD_UNIVERSAL", "OFF");
12-
13-
let dst = cfg.build();
7+
fn main() {
8+
let mut config = cmake::Config::new("openvr");
9+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
10+
let target_pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
11+
12+
if target_os == "macos" {
13+
config.define("BUILD_UNIVERSAL", "OFF");
14+
} else if target_os == "windows" {
15+
// Work around broken cmake build.
16+
config.cxxflag("/DWIN32");
17+
}
18+
19+
let dst = config.build();
1420
println!("cargo:rustc-link-search=native={}/lib", dst.display());
1521

16-
#[cfg(all(windows, target_pointer_width = "64"))]
17-
println!("cargo:rustc-link-lib=static=openvr_api64");
18-
19-
#[cfg(not(all(windows, target_pointer_width = "64")))]
20-
println!("cargo:rustc-link-lib=static=openvr_api");
21-
22-
#[cfg(target_os="linux")]
23-
println!("cargo:rustc-link-lib=stdc++");
24-
25-
#[cfg(target_os="macos")]
26-
println!("cargo:rustc-link-lib=c++");
22+
if target_os == "windows" && target_pointer_width == "64" {
23+
println!("cargo:rustc-link-lib=static=openvr_api64");
24+
} else {
25+
println!("cargo:rustc-link-lib=static=openvr_api");
26+
}
27+
28+
if target_os == "linux" {
29+
println!("cargo:rustc-link-lib=stdc++");
30+
} else if target_os == "macos" {
31+
println!("cargo:rustc-link-lib=c++");
32+
} else if target_os == "windows" {
33+
println!("cargo:rustc-link-lib=shell32");
34+
}
35+
36+
// Generate bindings at build time.
37+
#[cfg(feature = "buildtime_bindgen")]
38+
bindgen::builder()
39+
.header("wrapper.hpp")
40+
.constified_enum(".*")
41+
.prepend_enum_name(false)
42+
.generate()
43+
.expect("could not generate bindings")
44+
.write_to_file("bindings.rs")
45+
.expect("could not write bindings.rs");
2746
}

0 commit comments

Comments
 (0)