diff --git a/.gitignore b/.gitignore index ad67955..0728338 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,8 @@ target # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + + +# Added by cargo + +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a7d488a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "flux-rt" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2a2ddde --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "flux-rt" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/bro.py b/bro.py new file mode 100644 index 0000000..0ebd357 --- /dev/null +++ b/bro.py @@ -0,0 +1,4 @@ +def hello(): + print("Hello, bro!") + +hello() \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..17fdd7f --- /dev/null +++ b/build.rs @@ -0,0 +1,13 @@ +fn main() { + let lib_dir = std::process::Command::new("python3") + .arg("-c") + .arg("import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") + .output() + .expect("failed to run python3") + .stdout; + + let lib_dir = String::from_utf8(lib_dir).unwrap(); + println!("cargo:rustc-link-search=native={}", lib_dir.trim()); + + println!("cargo:rustc-link-lib=python3.11"); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ad7e296 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,21 @@ +use std::{ffi::CString, fs, path::Path}; + +#[link(name = "python3.13")] +extern "C" { + fn Py_Initialize(); + fn PyRun_SimpleString(command: *const i8) -> i32; + fn Py_Finalize(); +} + +pub fn run_py(path: &Path) { + let code = fs::read_to_string(path).expect("Failed to read python script"); + + unsafe { + Py_Initialize(); + + let code = CString::new(code.as_str()).unwrap(); + PyRun_SimpleString(code.as_ptr()); + + Py_Finalize(); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e1ac8bd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,6 @@ +use std::path::Path; +use flux_rt::run_py; + +fn main() { + run_py(Path::new("bro.py")); +}