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

Resolve CAIRO_NATIVE_RUNTIME_LIBRARY relative path #841

Merged
Merged
Show file tree
Hide file tree
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
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 @@ -219,6 +220,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 @@ -236,14 +259,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 @@ -261,14 +279,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
Loading