|
1 | 1 | fn main() { |
2 | 2 | if option_env!("DOCS_RS").is_some() { return } |
3 | 3 |
|
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"); |
| 4 | + if dbg!(!probe_mujoco_lib_with_libdir(None)) { |
| 5 | + if let Some(mujoco_lib_dir) = mujoco_lib_directory_from_env("MUJOCO_LIB") { |
| 6 | + assert!( |
| 7 | + dbg!(probe_mujoco_lib_with_libdir(Some(&mujoco_lib_dir))), |
| 8 | + "Failed to link with mujoco library even after setting `MUJOCO_LIB` environment variable!" |
| 9 | + ); |
| 10 | + println!("cargo:rustc-link-search=native={}", mujoco_lib_dir.display()); |
| 11 | + } else { |
| 12 | + panic!("\ |
| 13 | + MuJoCo library not found. Make sure that the mujoco library is installed and, \ |
| 14 | + if its location is non-standard, set the path via the `MUJOCO_LIB` environment variable.\ |
| 15 | + "); |
| 16 | + } |
| 17 | + } |
| 18 | + println!("cargo:rustc-link-lib=mujoco"); |
10 | 19 |
|
11 | 20 | #[cfg(feature = "bindgen")] |
12 | 21 | bindgen(); |
13 | 22 | } |
14 | 23 |
|
| 24 | +fn probe_mujoco_lib_with_libdir(libdir: Option<&std::path::Path>) -> bool { |
| 25 | + let crate_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); |
| 26 | + let vendor_dir = crate_root.join("vendor"); |
| 27 | + |
| 28 | + let probe_c = crate_root.join("probe.c"); |
| 29 | + let vendor_include = vendor_dir.join("include"); |
| 30 | + let vendor_include_mujoco = vendor_include.join("mujoco"); |
| 31 | + |
| 32 | + /* |
| 33 | + * `cc` crate is designed as: |
| 34 | + * |
| 35 | + * > A library for Cargo build scripts to compile a set of C/C++/assembly/CUDA files |
| 36 | + * into a static archive for Cargo to link into the crate being built. |
| 37 | + * |
| 38 | + * However, here we need to probe whether linking with `mujoco` library to an executable |
| 39 | + * succeeds or not, so we manually invoke the compiler with appropriate arguments, |
| 40 | + * using `cc` crate to get the compiler path and kind. |
| 41 | + */ |
| 42 | + |
| 43 | + let cc = cc::Build::new().cargo_metadata(false).get_compiler(); |
| 44 | + let cc_path = cc.path(); |
| 45 | + let cc_args = if cc.is_like_gnu() || cc.is_like_clang() { |
| 46 | + let mut args = vec![ |
| 47 | + probe_c.to_str().unwrap().to_string(), |
| 48 | + format!("-I{}", vendor_include.display()), |
| 49 | + format!("-I{}", vendor_include_mujoco.display()), |
| 50 | + "-o".to_string(), "/dev/null".to_string(), |
| 51 | + ]; |
| 52 | + if let Some(libdir) = libdir { |
| 53 | + args.push(format!("-L{}", libdir.display())); |
| 54 | + } |
| 55 | + args.push("-lmujoco".to_string()); |
| 56 | + args |
| 57 | + } else if cc.is_like_msvc() || cc.is_like_clang_cl() { |
| 58 | + let mut args = vec![ |
| 59 | + probe_c.to_str().unwrap().to_string(), |
| 60 | + format!("/I{}", vendor_include.display()), |
| 61 | + format!("/I{}", vendor_include_mujoco.display()), |
| 62 | + "/Fe:NUL".to_string(), |
| 63 | + ]; |
| 64 | + if let Some(libdir) = libdir { |
| 65 | + args.push(format!("/LIBPATH:{}", libdir.display())); |
| 66 | + } |
| 67 | + args.push("mujoco.lib".to_string()); |
| 68 | + args |
| 69 | + } else { |
| 70 | + panic!("Unsupported compiler: {}", cc_path.display()); |
| 71 | + }; |
| 72 | + |
| 73 | + std::process::Command::new(dbg!(cc_path)) |
| 74 | + .args(dbg!(cc_args)) |
| 75 | + .stdout(std::process::Stdio::inherit()) |
| 76 | + .stderr(std::process::Stdio::inherit()) |
| 77 | + .status() |
| 78 | + .unwrap_or_else(|err| panic!("Failed to invoke compiler to probe mujoco library: {err}")) |
| 79 | + .success() |
| 80 | +} |
| 81 | + |
| 82 | +/// Adds the given path to the library search path for linking, |
| 83 | +/// with resolving the directory path from an environment variable `env` |
| 84 | +/// that may be either the directory path or path of the library file itself. |
| 85 | +fn mujoco_lib_directory_from_env(env: &'static str) -> Option<std::path::PathBuf> { |
| 86 | + let mujoco_lib = match std::env::var(env) { |
| 87 | + Ok(value) => value, |
| 88 | + Err(std::env::VarError::NotPresent) => return None, |
| 89 | + Err(std::env::VarError::NotUnicode(os_str)) => panic!("{env} contains invalid unicode: `{}`", os_str.to_string_lossy()), |
| 90 | + }; |
| 91 | + let mujoco_lib = std::path::Path::new(&mujoco_lib); |
| 92 | + |
| 93 | + Some(if mujoco_lib.is_dir() { |
| 94 | + mujoco_lib.to_owned() |
| 95 | + } else if mujoco_lib.is_file() { |
| 96 | + mujoco_lib |
| 97 | + .parent() |
| 98 | + .unwrap_or_else(|| panic!("{env} must be a valid path to mujoco library file or directory containing it")) |
| 99 | + .to_owned() |
| 100 | + } else { |
| 101 | + panic!("{env} must be a valid path to mujoco library file or directory containing it") |
| 102 | + }) |
| 103 | +} |
| 104 | + |
15 | 105 | #[cfg(feature = "bindgen")] |
16 | 106 | fn bindgen() { |
17 | 107 | #[derive(Debug)] |
|
0 commit comments