diff --git a/docs/execution_walkthrough.md b/docs/execution_walkthrough.md index fd6b645bb..d5b78d26f 100644 --- a/docs/execution_walkthrough.md +++ b/docs/execution_walkthrough.md @@ -206,7 +206,7 @@ Builtin stats: BuiltinStats { bitwise: 1, ec_op: 0, range_check: 1, pedersen: 0, ## The Cairo Native runtime Sometimes we need to use stuff that would be too complicated or error-prone to implement in MLIR, but that we have readily available from Rust. That's when we use the runtime library. -When using the JIT it'll be automatically linked (if compiled with support for it, which is enabled by default). If using the AOT, the `CAIRO_NATIVE_RUNTIME_LIBDIR` environment variable will have to be modified to point to the directory that contains `libcairo_native_runtime.a`, which is built and placed in said folder by `make build`. +When using the JIT it'll be automatically linked (if compiled with support for it, which is enabled by default). If using the AOT, the `CAIRO_NATIVE_RUNTIME_LIBRARY` environment variable will have to be modified to point to the `libcairo_native_runtime.a` file, which is built and placed in said folder by `make build`. Although it's implemented in Rust, its functions use the C ABI and have Rust's name mangling disabled. This means that to the extern observer it's technically indistinguishible from a library written in C. By doing this we're making the functions callable from MLIR. diff --git a/scripts/bench-hyperfine.sh b/scripts/bench-hyperfine.sh index 47e9453c6..180120c79 100755 --- a/scripts/bench-hyperfine.sh +++ b/scripts/bench-hyperfine.sh @@ -90,7 +90,7 @@ run_bench() { -o "$OUTPUT_DIR/$base_name-march-native" \ >> /dev/stderr - CAIRO_NATIVE_RUNTIME_LIBDIR="$ROOT_DIR/target/release" hyperfine \ + hyperfine \ --warmup 3 \ --export-markdown "$OUTPUT_DIR/$base_name.md" \ --export-json "$OUTPUT_DIR/$base_name.json" \ diff --git a/src/ffi.rs b/src/ffi.rs index 1d1a7f412..09cccbbcf 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -29,6 +29,7 @@ use melior::ir::{Module, Type, TypeLike}; use mlir_sys::{mlirLLVMStructTypeGetElementType, mlirTranslateModuleToLLVMIR}; use std::{ borrow::Cow, + env, ffi::{CStr, CString}, io::Write, mem::MaybeUninit, @@ -222,6 +223,28 @@ pub fn object_to_shared_lib(object: &[u8], output_filename: &Path) -> Result<()> } } + let runtime_library_path = if let Ok(extra_dir) = std::env::var("CAIRO_NATIVE_RUNTIME_LIBRARY") + { + let path = Path::new(&extra_dir); + if path.is_absolute() { + extra_dir + } else { + let mut absolute_path = env::current_dir() + .expect("Failed to get the current directory") + .join(path); + absolute_path = absolute_path + .canonicalize() + .expect("Failed to cannonicalize path"); + String::from( + absolute_path + .to_str() + .expect("Absolute path contains non-utf8 characters"), + ) + } + } else { + String::from("libcairo_native_runtime.a") + }; + let args: Vec> = { #[cfg(target_os = "macos")] { @@ -239,14 +262,9 @@ pub fn object_to_shared_lib(object: &[u8], output_filename: &Path) -> Result<()> "-o".into(), Cow::from(output_path), "-lSystem".into(), + Cow::from(runtime_library_path), ]); - if let Ok(extra_dir) = std::env::var("CAIRO_NATIVE_RUNTIME_LIBRARY") { - args.extend([Cow::from(extra_dir)]); - } else { - args.extend(["libcairo_native_runtime.a".into()]); - } - args } #[cfg(target_os = "linux")] @@ -264,14 +282,9 @@ pub fn object_to_shared_lib(object: &[u8], output_filename: &Path) -> Result<()> Cow::from(output_path), "-lc".into(), Cow::from(file_path), + Cow::from(runtime_library_path), ]); - if let Ok(extra_dir) = std::env::var("CAIRO_NATIVE_RUNTIME_LIBRARY") { - args.extend([Cow::from(extra_dir)]); - } else { - args.extend(["libcairo_native_runtime.a".into()]); - } - args } #[cfg(target_os = "windows")]