|
1 | | -use std::{env, io::BufRead, path::Path, process::{Command, Stdio}}; |
| 1 | +fn main() { |
| 2 | + if option_env!("DOCS_RS").is_some() { return } |
2 | 3 |
|
3 | | -#[derive(Debug)] |
4 | | -struct TrimUnderscoreCallbacks; |
5 | | -impl bindgen::callbacks::ParseCallbacks for TrimUnderscoreCallbacks { |
6 | | - fn item_name(&self, item_info: bindgen::callbacks::ItemInfo) -> Option<String> { |
7 | | - /* |
8 | | - This finally oversets non-suffixed name (like `mjData`) |
9 | | - to/over its original name (like `mjData_`). |
10 | | - */ |
11 | | - item_info.name.strip_suffix('_').map(str::to_owned) |
12 | | - } |
| 4 | + let mujoco_dir = std::env::var("MUJOCO_DIR").expect("MUJOCO_DIR environment variable is not set"); |
| 5 | + let mujoco_dir = std::path::Path::new(&mujoco_dir).canonicalize().expect("MUJOCO_DIR is not a valid path"); |
| 6 | + let mujoco_lib = mujoco_dir.join("lib").to_str().unwrap().to_owned(); |
| 7 | + |
| 8 | + println!("cargo:rustc-link-search={mujoco_lib}"); |
| 9 | + println!("cargo:rustc-link-lib=dylib=mujoco"); |
| 10 | + |
| 11 | + #[cfg(feature = "bindgen")] |
| 12 | + bindgen(mujoco_dir); |
13 | 13 | } |
14 | 14 |
|
15 | | -#[derive(Debug)] |
16 | | -struct MakeMjnConstantsCallbacks; |
17 | | -impl bindgen::callbacks::ParseCallbacks for MakeMjnConstantsCallbacks { |
18 | | - fn enum_variant_behavior( |
19 | | - &self, |
20 | | - _enum_name: Option<&str>, |
21 | | - original_variant_name: &str, |
22 | | - _variant_value: bindgen::callbacks::EnumVariantValue, |
23 | | - ) -> Option<bindgen::callbacks::EnumVariantCustomBehavior> { |
24 | | - /* |
25 | | - This generates const like: |
26 | | - ``` |
27 | | - pub const mjNTEXROLE: mjtTextureRole = mjtTextureRole::mjNTEXROLE; |
28 | | - ``` |
29 | | - at module top for `mjN*` variants. |
30 | | - */ |
31 | | - original_variant_name.starts_with("mjN").then_some(bindgen::callbacks::EnumVariantCustomBehavior::Constify) |
| 15 | +#[cfg(feature = "bindgen")] |
| 16 | +fn bindgen(mujoco_dir: impl AsRef<std::path::Path>) { |
| 17 | + #[derive(Debug)] |
| 18 | + struct TrimUnderscoreCallbacks; |
| 19 | + impl bindgen::callbacks::ParseCallbacks for TrimUnderscoreCallbacks { |
| 20 | + fn item_name(&self, item_info: bindgen::callbacks::ItemInfo) -> Option<String> { |
| 21 | + /* |
| 22 | + This finally oversets non-suffixed name (like `mjData`) |
| 23 | + to/over its original name (like `mjData_`). |
| 24 | + */ |
| 25 | + item_info.name.strip_suffix('_').map(str::to_owned) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + #[derive(Debug)] |
| 30 | + struct MakeMjnConstantsCallbacks; |
| 31 | + impl bindgen::callbacks::ParseCallbacks for MakeMjnConstantsCallbacks { |
| 32 | + fn enum_variant_behavior( |
| 33 | + &self, |
| 34 | + _enum_name: Option<&str>, |
| 35 | + original_variant_name: &str, |
| 36 | + _variant_value: bindgen::callbacks::EnumVariantValue, |
| 37 | + ) -> Option<bindgen::callbacks::EnumVariantCustomBehavior> { |
| 38 | + /* |
| 39 | + This generates const like: |
| 40 | + ``` |
| 41 | + pub const mjNTEXROLE: mjtTextureRole = mjtTextureRole::mjNTEXROLE; |
| 42 | + ``` |
| 43 | + at module top for `mjN*` variants. |
| 44 | + */ |
| 45 | + original_variant_name.starts_with("mjN").then_some(bindgen::callbacks::EnumVariantCustomBehavior::Constify) |
| 46 | + } |
32 | 47 | } |
33 | | -} |
34 | | - |
35 | | -fn main() { |
36 | | - if option_env!("DOCS_RS").is_some() { return } |
37 | 48 |
|
38 | 49 | /* |
39 | | - * The hand-process step after `bindgen` generation assumes that |
| 50 | + * The hand-processing step after `bindgen` generation requires |
40 | 51 | * `cargo fmt` (and then it's automatically applied to the |
41 | 52 | * bindgen's raw output, and the hand-processing correctly works). |
42 | 53 | * This is a **requirement** for the build script to continue. |
43 | 54 | */ |
44 | 55 | assert!( |
45 | | - Command::new("cargo").args(["help", "fmt"]).stdout(Stdio::null()).status().is_ok_and(|s| s.success()), |
| 56 | + std::process::Command::new("cargo") |
| 57 | + .args(["help", "fmt"]) |
| 58 | + .stdout(std::process::Stdio::null()) |
| 59 | + .status() |
| 60 | + .is_ok_and(|s| s.success()), |
46 | 61 | "`cargo fmt` is not available; This build script can't continue without it." |
47 | 62 | ); |
48 | 63 |
|
49 | | - let src_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); |
| 64 | + let mujoco_dir = mujoco_dir.as_ref(); |
| 65 | + let mujoco_include = mujoco_dir.join("include").to_str().unwrap().to_owned(); |
| 66 | + let mujoco_include_mujoco = mujoco_dir.join("include").join("mujoco").to_str().unwrap().to_owned(); |
| 67 | + |
| 68 | + let src_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); |
50 | 69 | let bindgen_h = src_dir.join("bindgen.h").to_str().unwrap().to_owned(); |
51 | 70 | let bindgen_rs = src_dir.join("bindgen.rs").to_str().unwrap().to_owned(); |
52 | 71 |
|
53 | 72 | println!("cargo:rerun-if-changed={bindgen_h}"); |
54 | 73 |
|
55 | | - let mujoco_dir = std::env::var("MUJOCO_DIR").expect("MUJOCO_DIR environment variable is not set"); |
56 | | - let mujoco_dir = Path::new(&mujoco_dir).canonicalize().expect("MUJOCO_DIR is not a valid path"); |
57 | | - let mujoco_lib = mujoco_dir.join("lib").to_str().unwrap().to_owned(); |
58 | | - let mujoco_include = mujoco_dir.join("include").to_str().unwrap().to_owned(); |
59 | | - let mujoco_include_mujoco = mujoco_dir.join("include").join("mujoco").to_str().unwrap().to_owned(); |
60 | | - |
61 | | - println!("cargo:rustc-link-search={mujoco_lib}"); |
62 | | - println!("cargo:rustc-link-lib=dylib=mujoco"); |
63 | | - |
64 | 74 | let mut bindings = Vec::new(); |
65 | 75 | bindgen::builder() |
66 | 76 | .header(bindgen_h) |
@@ -98,8 +108,7 @@ fn main() { |
98 | 108 |
|
99 | 109 | using bindgen, so we do them manually... |
100 | 110 | */ |
101 | | - let bindings = bindings |
102 | | - .lines() |
| 111 | + let bindings = std::io::BufRead::lines(&*bindings) |
103 | 112 | .map(Result::unwrap) |
104 | 113 | .fold(Vec::with_capacity(bindings.len()), |mut new, line| { |
105 | 114 | if line.starts_with("pub struct mjt") { |
|
0 commit comments