Skip to content

Commit

Permalink
Resolve CAIRO_NATIVE_RUNTIME_LIBRARY relative path (#841)
Browse files Browse the repository at this point in the history
* feat(ffi): resolve runtime relative path using current dir

* chore: remove mentions to old runtime variable

* fix: typo

---------

Co-authored-by: Bohdan Ohorodnii <[email protected]>
  • Loading branch information
rodrigo-pino and varex83 authored Oct 16, 2024
1 parent 7c4ff7f commit b5769e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/execution_walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion scripts/bench-hyperfine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
37 changes: 25 additions & 12 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Cow<'static, str>> = {
#[cfg(target_os = "macos")]
{
Expand All @@ -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")]
Expand All @@ -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")]
Expand Down

0 comments on commit b5769e4

Please sign in to comment.