Working example of calling Lean from rust!
This commit is contained in:
1 parent
557454b340
commit
5b6f675cac
5 files changed
+78
-1
No files matched your search
@@ -3,6 +3,12 @@ def hello := "world"
|
||||
@[export mycustomstring]
|
||||
def myCustomString := "KJLKJLKJLKJLKJLJLKLKJ"
|
||||
|
||||
@[export mycustomint]
|
||||
def myCustomInt : UInt32 := 42069
|
||||
|
||||
@[export mycustomintfunction]
|
||||
def myCustomIntFunction (input: UInt32): UInt32 := input + 42069
|
||||
|
||||
structure AST where
|
||||
name: String
|
||||
count: Nat
|
||||
|
||||
@@ -10,6 +10,9 @@ build-shared-lib:
|
||||
|
||||
[doc: "Build the rust executable"]
|
||||
[working-directory: "lean-ffi-experiment-rust"]
|
||||
run-rust:
|
||||
run-rust: build-shared-lib
|
||||
cargo run
|
||||
|
||||
# Run objdump on the output .so
|
||||
objdump:
|
||||
objdump -T .lake/build/lib/libffiexperiment.so
|
||||
@@ -2,5 +2,6 @@
|
||||
name = "lean-ffi-experiment-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,57 @@
|
||||
use std::{env, path::PathBuf, process::Command};
|
||||
|
||||
fn main() {
|
||||
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().to_owned();
|
||||
|
||||
// Lake output dir containing libYourLeanLib.so
|
||||
let lake_lib_dir = root.join(".lake").join("build").join("lib");
|
||||
|
||||
// Replace this with the actual basename:
|
||||
// libmylean.so -> "mylean"
|
||||
// libmy_pkg_MyLib.so -> "my_pkg_MyLib"
|
||||
let lean_lib_name = "ffiexperiment";
|
||||
|
||||
println!("cargo::rustc-link-search=native={}", lake_lib_dir.display());
|
||||
println!("cargo::rustc-link-lib=dylib={lean_lib_name}");
|
||||
|
||||
// Useful so `cargo run` can find the .so at runtime without LD_LIBRARY_PATH.
|
||||
println!(
|
||||
"cargo::rustc-link-arg=-Wl,-rpath,{}",
|
||||
lake_lib_dir.display()
|
||||
);
|
||||
|
||||
// Re-run if the Lean shared library changes.
|
||||
println!(
|
||||
"cargo::rerun-if-changed={}",
|
||||
lake_lib_dir.join(format!("lib{lean_lib_name}.so")).display()
|
||||
);
|
||||
|
||||
// Lean runtime shared libraries.
|
||||
//
|
||||
// This is needed if your Rust executable directly references Lean runtime
|
||||
// functions like lean_initialize_runtime_module.
|
||||
let lean_prefix = Command::new("lean")
|
||||
.arg("--print-prefix")
|
||||
.output()
|
||||
.expect("failed to run `lean --print-prefix`");
|
||||
|
||||
assert!(
|
||||
lean_prefix.status.success(),
|
||||
"`lean --print-prefix` failed"
|
||||
);
|
||||
|
||||
let lean_prefix = String::from_utf8(lean_prefix.stdout).unwrap();
|
||||
let lean_prefix = lean_prefix.trim();
|
||||
|
||||
let lean_lib_dir = PathBuf::from(lean_prefix).join("lib").join("lean");
|
||||
|
||||
println!("cargo::rustc-link-search=native={}", lean_lib_dir.display());
|
||||
println!("cargo::rustc-link-arg=-Wl,-rpath,{}", lean_lib_dir.display());
|
||||
|
||||
// You may need these depending on what your exported Lean code references.
|
||||
// They are commonly needed when initializing/calling Lean from a non-Lean host.
|
||||
// println!("cargo::rustc-link-lib=dylib=Init_shared");
|
||||
// println!("cargo::rustc-link-lib=dylib=leanshared_2");
|
||||
// println!("cargo::rustc-link-lib=dylib=leanshared_1");
|
||||
// println!("cargo::rustc-link-lib=dylib=leanshared");
|
||||
}
|
||||
@@ -1,3 +1,13 @@
|
||||
unsafe extern "C" {
|
||||
fn mycustomintfunction(x: u32) -> u32;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Going to invoke Lean FFI");
|
||||
|
||||
let input = 20;
|
||||
let output = unsafe { mycustomintfunction(input) };
|
||||
|
||||
println!("Input: {input} output: {output}");
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user