Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enables hot reloading of dylibs on macos #10

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,22 @@ impl<'a> Build<'a> {
pub fn load(self) -> Result<TempLibrary, LoadError> {
let dylib_path = self.dylib_path();
let tmp_path = self.tmp_dylib_path();
let tmp_dir = tmp_path.parent().expect("temp dylib path has no parent");

// If the library already exists, load it.
loop {
if tmp_path.exists() {
// This is some voodoo to enable reloading of dylib on mac os
if cfg!(target_os = "macos") {
std::process::Command::new("install_name_tool")
.current_dir(tmp_dir)
.arg("-id")
.arg("''")
.arg(tmp_path.file_name().expect("temp dylib path has no file name"))
.output()
.expect("ls command failed to start");
}

let lib = libloading::Library::new(&tmp_path)
.map(Some)
.map_err(|err| LoadError::Library { err })?;
Expand All @@ -368,9 +380,7 @@ impl<'a> Build<'a> {
let tmp = TempLibrary { build_timestamp, path, lib };
return Ok(tmp);
}

// Copy the dylib to the tmp location.
let tmp_dir = tmp_path.parent().expect("temp dylib path has no parent");
std::fs::create_dir_all(tmp_dir).map_err(|err| LoadError::Io { err })?;
std::fs::copy(&dylib_path, &tmp_path).map_err(|err| LoadError::Io { err })?;
}
Expand Down