Skip to content

Commit 8768d2a

Browse files
authored
Fix MuJoCo dependency setup (#98)
* Remove rpath feature and update docs * Cleanup * review * review * fix word * Fix filename * remove absolute * Fix docs * Update message * Changelog * backslashes * Update error messages * Improve messages
1 parent 4c07d9c commit 8768d2a

File tree

6 files changed

+130
-182
lines changed

6 files changed

+130
-182
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mujoco-rs"
3-
version = "2.1.0+mj-3.3.7"
3+
version = "2.1.1+mj-3.3.7"
44
edition = "2024"
55
license = "MIT OR Apache-2.0"
66
description = "A high-level Rust wrapper around the MuJoCo C library, with a native viewer (re-)written in Rust."
@@ -26,7 +26,6 @@ cpp-viewer = []
2626
ffi-regenerate = ["dep:regex", "dep:bindgen"] # Generate the ffi bindings. Only used for updating the committed ``mujoco_c`` module.
2727

2828
auto-download-mujoco = ["dep:ureq", "dep:flate2", "dep:tar", "dep:zip", "dep:sha2"]
29-
use-rpath = []
3029

3130
default = ["viewer", "viewer-ui", "renderer"]
3231

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ The main features on top of MuJoCo include
3030
- Automatic allocation and cleanup.
3131
- Lifetime guarantees.
3232

33-
- Automatic download and setup of MuJoCo.
3433
- Methods as function wrappers.
3534
- Easy manipulation of simulation data via attribute views.
3635
- High-level model editing.
@@ -47,12 +46,7 @@ Screenshot of the built-in Rust viewer. Showing scene from [MuJoCo's menagerie](
4746

4847
## Optional Cargo features
4948
Optional Cargo features can be enabled:
50-
51-
- ``auto-download-mujoco``: MuJoCo dependency will be automatically downloaded and configured.
52-
53-
- This is only available on Linux and Windows.
5449

55-
- ``use-rpath``: On Linux and MacOS, when dynamically linking, set the RPATH of the final binary.
5650
- ``viewer``: enables the Rust-native MuJoCo viewer.
5751

5852
- ``viewer-ui``: enables the (additional) user UI within the viewer.
@@ -61,6 +55,9 @@ Optional Cargo features can be enabled:
6155
This requires static linking to a modified fork of MuJoCo, as described in :ref:`installation`.
6256
- ``renderer``: enables offscreen rendering for writing RGB and
6357
depth data to memory or file.
58+
- ``auto-download-mujoco``: MuJoCo dependency will be automatically downloaded to the specified path.
59+
60+
- This is only available on Linux and Windows.
6461

6562
By default, ``viewer``, ``viewer-ui`` and ``renderer`` are enabled.
6663

build.rs

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ fn main() {
110110
let path_lib_dir = PathBuf::from(path);
111111
let path_lib_dir_display = path_lib_dir.display();
112112

113+
if path_lib_dir.is_relative() {
114+
panic!("{MUJOCO_STATIC_LIB_PATH_VAR} must be an absolute path ('{path_lib_dir_display}' is not).");
115+
}
116+
113117
let path_lib_file = if cfg!(target_os = "windows") {
114118
path_lib_dir.join("mujoco.lib")
115119
} else {
@@ -120,7 +124,7 @@ fn main() {
120124
if !path_lib_file.is_file() {
121125
panic!(
122126
"{MUJOCO_STATIC_LIB_PATH_VAR} must be path to the 'lib/' subdirectory (i.e., 'mujoco-x.x.x/lib/') --- \
123-
'{path_lib_dir_display}' does not appear to contain '{}'.",
127+
'{path_lib_dir_display}' does not appear to contain '{}' or it doesn't exist.",
124128
path_lib_file.file_name().unwrap().to_str().unwrap()
125129
);
126130
}
@@ -152,6 +156,10 @@ fn main() {
152156
let path_lib_dir = PathBuf::from(path);
153157
let path_lib_dir_display = path_lib_dir.display();
154158

159+
if path_lib_dir.is_relative() {
160+
panic!("{MUJOCO_DYN_LIB_PATH_VAR} must be an absolute path ('{path_lib_dir_display}' is not).");
161+
}
162+
155163
let path_lib_file = if cfg!(target_os = "windows") {
156164
path_lib_dir.join("mujoco.lib")
157165
} else if cfg!(target_os = "macos") {
@@ -164,31 +172,14 @@ fn main() {
164172
if !path_lib_file.is_file() {
165173
panic!(
166174
"{MUJOCO_DYN_LIB_PATH_VAR} must be path to the 'lib/' subdirectory (i.e., 'mujoco-x.x.x/lib/') --- \
167-
'{path_lib_dir_display}' does not appear to contain '{}'.",
175+
'{path_lib_dir_display}' does not appear to contain '{}' or it doesn't exist.",
168176
path_lib_file.file_name().unwrap().to_str().unwrap()
169177
);
170178
}
171179

172180
println!("cargo:rustc-link-search={}", path_lib_dir_display);
173181
println!("cargo:rustc-link-lib=mujoco");
174182

175-
// Set the RPATH
176-
#[cfg(feature = "use-rpath")]
177-
{
178-
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path_lib_dir_display);
179-
#[cfg(target_os = "linux")]
180-
if path_lib_dir.is_relative() {
181-
let rpath = PathBuf::from("$ORIGIN").join(&path_lib_dir);
182-
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", rpath.display());
183-
}
184-
185-
#[cfg(target_os = "macos")]
186-
if path_lib_dir.is_relative() {
187-
let rpath = PathBuf::from("@loader_path").join(&path_lib_dir);
188-
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", rpath.display());
189-
}
190-
}
191-
192183
// Copy the DLL on Windows, if a relative path is given.
193184
// Otherwise assume the DLL is discoverable through PATH.
194185
#[cfg(target_os = "windows")]
@@ -223,7 +214,7 @@ fn main() {
223214
\n---------------- ^^^ pkg-config output ^^^ ----------------\n\
224215
\n=================================================================================================\
225216
\nUnable to locate MuJoCo via pkg-config and neither {MUJOCO_STATIC_LIB_PATH_VAR} nor {MUJOCO_DYN_LIB_PATH_VAR} is set and the 'auto-download-mujoco' Cargo feature is disabled.\
226-
\nConsider enabling automatic download of MuJoCo: 'cargo build --features \"auto-download-mujoco use-rpath\"'.\
217+
\nConsider enabling automatic download of MuJoCo: 'cargo add mujoco-rs --features \"auto-download-mujoco\"'.\
227218
\n================================================================================================="
228219
);
229220

@@ -249,7 +240,7 @@ fn main() {
249240
#[cfg(target_os = "windows")]
250241
panic!(
251242
"Unable to locate MuJoCo because 'auto-download-mujoco' Cargo feature is disabled and neither {MUJOCO_STATIC_LIB_PATH_VAR} nor {MUJOCO_DYN_LIB_PATH_VAR} is set.\
252-
\nConsider enabling automatic download of MuJoCo: 'cargo build --features \"auto-download-mujoco\"'."
243+
\nConsider enabling automatic download of MuJoCo: 'cargo add mujoco-rs --features \"auto-download-mujoco\"'."
253244
);
254245

255246
// On Linux and Windows try to automatically download as a fallback.
@@ -292,19 +283,26 @@ fn main() {
292283
let download_hash_url = format!("{download_url}.sha256");
293284

294285
// Obtain the download directory from MUJOCO_DOWNLOAD_PATH_VAR.
295-
// If not given, assume the current working directory. In the latter case,
296-
// also assume that the MuJoCo DLL needs to be copied to the current working
297-
// directory, otherwise assume the user will manually add its directory to PATH.
298-
#[allow(unused)] // copy_dll is only relevant to Windows
299-
let (download_dir, copy_dll) = if let Ok(value) =
300-
std::env::var(MUJOCO_DOWNLOAD_PATH_VAR)
301-
{
302-
(PathBuf::from(value), false)
286+
let download_dir = PathBuf::from(std::env::var(MUJOCO_DOWNLOAD_PATH_VAR).unwrap_or_else(|_| {
287+
let os_example = if cfg!(unix) {
288+
format!("e.g., export {MUJOCO_DOWNLOAD_PATH_VAR}=\"$(realpath .)\"")
289+
} else {
290+
format!("e.g., $env:{MUJOCO_DOWNLOAD_PATH_VAR}=\"/full/absolute/path/\"")
291+
};
292+
panic!(
293+
"when Cargo feature 'auto-download-mujoco' is enabled, {MUJOCO_DOWNLOAD_PATH_VAR} must be set to \
294+
an absolute path, where MuJoCo will be extracted --- \
295+
{os_example}",
296+
);
297+
}));
298+
299+
if download_dir.is_relative() {
300+
panic!(
301+
"{MUJOCO_DOWNLOAD_PATH_VAR} must be an absolute path to the location \
302+
where MuJoCo will be downloaded and extracted (was given '{}').",
303+
download_dir.display()
304+
)
303305
}
304-
else
305-
{
306-
(PathBuf::from("."), true)
307-
};
308306

309307
// The name of the downloaded archive file.
310308
let download_path = download_dir.join(download_url.rsplit_once("/").unwrap().1);
@@ -365,7 +363,7 @@ fn main() {
365363

366364
/* Extraction */
367365
#[cfg(target_os = "windows")]
368-
extract_windows(&download_path, &outdirname, copy_dll);
366+
extract_windows(&download_path, &outdirname);
369367

370368
#[cfg(target_os = "linux")]
371369
extract_linux(&download_path);
@@ -380,23 +378,7 @@ fn main() {
380378
);
381379

382380
let libdir_path = outdirname.join("lib");
383-
let libdir_path_display = libdir_path.display();
384-
385-
// Set RPATH on Linux targets and rerun the script if the .so is changed.
386-
#[cfg(target_os = "linux")]
387-
{
388-
// Set the RPATH
389-
#[cfg(feature = "use-rpath")]
390-
{
391-
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", libdir_path_display);
392-
if libdir_path.is_relative() {
393-
let rpath = PathBuf::from("$ORIGIN").join(&libdir_path);
394-
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", rpath.display());
395-
}
396-
}
397-
}
398-
399-
println!("cargo:rustc-link-search={}", libdir_path_display);
381+
println!("cargo:rustc-link-search={}", libdir_path.display());
400382
println!("cargo:rustc-link-lib=mujoco");
401383
}
402384
}
@@ -407,7 +389,7 @@ fn main() {
407389

408390
#[cfg(target_os = "windows")]
409391
#[cfg(feature = "auto-download-mujoco")]
410-
fn extract_windows(filename: &Path, outdirname: &Path, copy_mujoco_dll: bool) {
392+
fn extract_windows(filename: &Path, outdirname: &Path) {
411393
let file = File::open(filename).unwrap_or_else(|err|
412394
panic!("failed to open archive '{}' ({err}).", filename.display())
413395
);
@@ -445,12 +427,6 @@ fn extract_windows(filename: &Path, outdirname: &Path, copy_mujoco_dll: bool) {
445427
);
446428
}
447429
}
448-
449-
if copy_mujoco_dll {
450-
if let Err(err) = std::fs::copy(outdirname.join("bin").join("mujoco.dll"), "mujoco.dll") {
451-
println!("cargo:warning=failed to copy mujoco.dll to the current working directory ({err})");
452-
}
453-
}
454430
}
455431

456432
#[cfg(target_os = "linux")]

docs/guide/source/changelog.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This means that any incompatible changes increase the major version (**Y**.x.x).
1616
This also includes breaking changes that MuJoCo itself introduced, thus even an
1717
update of MuJoCo alone can increase the major version.
1818

19-
2.1.0
19+
2.1.0 / 2.1.1 (MuJoCo 3.3.7)
2020
================================
2121
- Option to automatically pull MuJoCo.
2222
- pkg-config support for Linux and MacOS. Note that this is not officially supported by MuJoCo.
@@ -26,8 +26,7 @@ update of MuJoCo alone can increase the major version.
2626

2727
- New Cargo features:
2828

29-
- ``auto-download-mujoco``: will automatically download and setup MuJoCo (Windows and Linux).
30-
- ``use-rpath``: will embed the path to the MuJoCo C library into the RPATH of the binary (Linux and MacOS).
29+
- ``auto-download-mujoco``: will automatically download MuJoCo (Windows and Linux).
3130

3231
2.0.1 (MuJoCo 3.3.7)
3332
================================

docs/guide/source/index.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ Main features
2626
MuJoCo-rs tries to stay close to the MuJoCo's C API, with a few additional features for ease of use.
2727
The main features on top of MuJoCo include:
2828

29-
- Automatic download and setup of MuJoCo:
30-
31-
- When the ``auto-download-mujoco`` Cargo feature is enabled.
32-
3329
- Safe wrappers around structs:
3430

3531
- Automatic allocation and cleanup.
@@ -57,12 +53,7 @@ For installation, see :ref:`installation`.
5753
Optional Cargo features
5854
=======================
5955
Optional Cargo features can be enabled:
60-
61-
- ``auto-download-mujoco``: MuJoCo dependency will be automatically downloaded and configured.
6256

63-
- This is only available on Linux and Windows.
64-
65-
- ``use-rpath``: On Linux and MacOS, when dynamically linking, set the RPATH of the final binary.
6657
- ``viewer``: enables the Rust-native MuJoCo viewer.
6758

6859
- ``viewer-ui``: enables the (additional) user UI within the viewer.
@@ -71,6 +62,9 @@ Optional Cargo features can be enabled:
7162
This requires static linking to a modified fork of MuJoCo, as described in :ref:`installation`.
7263
- ``renderer``: enables offscreen rendering for writing RGB and
7364
depth data to memory or file.
65+
- ``auto-download-mujoco``: MuJoCo dependency will be automatically downloaded to the specified path.
66+
67+
- This is only available on Linux and Windows.
7468

7569
By default, ``viewer``, ``viewer-ui`` and ``renderer`` are enabled.
7670

0 commit comments

Comments
 (0)