Skip to content

Commit 89a011e

Browse files
authored
Add macOS support to stable-mir-json (#97)
- Add operating system detection in record_ld_library_path function - Use DYLD_LIBRARY_PATH on macOS instead of LD_LIBRARY_PATH - Include Rust toolchain path in default library path for macOS - Add DYLD_LIBRARY_PATH export in generated shell scripts for macOS - Maintain backward compatibility with Linux and other systems
1 parent 62a239d commit 89a011e

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

src/bin/cargo_stable_mir_json.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ fn add_run_script(smir_json_dir: &Path, ld_library_path: &Path, profile: Profile
165165
"export LD_LIBRARY_PATH={}",
166166
ld_library_path.display(),
167167
)?;
168+
169+
#[cfg(target_os = "macos")]
170+
{
171+
// Also set DYLD_LIBRARY_PATH on macOS
172+
writeln!(
173+
run_script,
174+
"export DYLD_LIBRARY_PATH={}",
175+
ld_library_path.display(),
176+
)?;
177+
}
178+
168179
writeln!(run_script)?;
169180
writeln!(
170181
run_script,
@@ -178,19 +189,50 @@ fn add_run_script(smir_json_dir: &Path, ld_library_path: &Path, profile: Profile
178189
}
179190

180191
fn record_ld_library_path(smir_json_dir: &Path) -> Result<PathBuf> {
181-
const LOADER_PATH: &str = "LD_LIBRARY_PATH";
182-
if let Some(paths) = env::var_os(LOADER_PATH) {
183-
// Note: kani filters the LD_LIBRARY_PATH, not sure why as it is working locally as is
184-
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?;
185-
186-
match paths.to_str() {
187-
Some(ld_library_path) => {
188-
writeln!(ld_library_file, "{}", ld_library_path)?;
189-
Ok(ld_library_path.into())
192+
#[cfg(target_os = "macos")]
193+
{
194+
// macOS: Check DYLD_LIBRARY_PATH or use default path
195+
if let Some(paths) = env::var_os("DYLD_LIBRARY_PATH") {
196+
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?;
197+
match paths.to_str() {
198+
Some(ld_library_path) => {
199+
writeln!(ld_library_file, "{}", ld_library_path)?;
200+
Ok(ld_library_path.into())
201+
}
202+
None => bail!("Couldn't cast DYLD_LIBRARY_PATH to str"),
203+
}
204+
} else {
205+
// Use default macOS library path including Rust toolchain
206+
let rustup_home = env::var("HOME").unwrap_or_else(|_| "/Users".to_string());
207+
let rust_toolchain_path = format!(
208+
"{}/.rustup/toolchains/nightly-2024-11-29-aarch64-apple-darwin/lib",
209+
rustup_home
210+
);
211+
let default_path = format!("{}:/usr/local/lib:/usr/lib", rust_toolchain_path);
212+
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?;
213+
writeln!(ld_library_file, "{}", default_path)?;
214+
Ok(default_path.into())
215+
}
216+
}
217+
218+
#[cfg(not(target_os = "macos"))]
219+
{
220+
// Linux and other systems: Check LD_LIBRARY_PATH
221+
if let Some(paths) = env::var_os("LD_LIBRARY_PATH") {
222+
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?;
223+
match paths.to_str() {
224+
Some(ld_library_path) => {
225+
writeln!(ld_library_file, "{}", ld_library_path)?;
226+
Ok(ld_library_path.into())
227+
}
228+
None => bail!("Couldn't cast LD_LIBRARY_PATH to str"),
190229
}
191-
None => bail!("Couldn't cast LD_LIBRARY_PATH to str"),
230+
} else {
231+
// Use default Linux library path
232+
let default_path = "/usr/local/lib:/usr/lib";
233+
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?;
234+
writeln!(ld_library_file, "{}", default_path)?;
235+
Ok(default_path.into())
192236
}
193-
} else {
194-
bail!("Couldn't read LD_LIBRARY_PATH from env"); // This should be unreachable
195237
}
196238
}

0 commit comments

Comments
 (0)