From 13a4163d1e90644a3775b61544a9166253995f8e Mon Sep 17 00:00:00 2001 From: Camillo Schenone Date: Sat, 16 Mar 2024 14:36:41 +0100 Subject: [PATCH] feat: added python glue, still needs some tweaking in the building of the dylib --- Cargo.toml | 8 +++++++- build.rs | 45 +++++++++++++++++++++++++++++++++------------ src/ffi/mod.rs | 5 ++++- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aa47726..f6279cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,16 +44,22 @@ rmpv = { version = "1", optional = true } napi = { version = "2", features = ["full"], optional = true } napi-derive = { version="2", optional = true} futures = { version = "0.3.28", optional = true } +# glue (python) +pyo3 = { version = "0.20", features = ["extension-module"], optional = true} +pyo3-asyncio = { version = "0.20", features = ["tokio-runtime"], optional = true } [build-dependencies] # glue (java) flapigen = { version = "0.6.0", optional = true } rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7", optional = true } # glue (js) napi-build = { version = "2", optional = true } +# glue (python) +pyo3-build-config = { version = "0.19.2", optional = true } [features] default = [] lua = ["mlua", "thiserror", "derive_more", "lazy_static", "tracing-subscriber"] java = ["lazy_static", "jni", "jni-sys", "flapigen", "rifgen", "log"] java-artifact = ["java"] # also builds the jar -js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"] \ No newline at end of file +js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"] +python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"] diff --git a/build.rs b/build.rs index 6d89f2b..4ba772a 100644 --- a/build.rs +++ b/build.rs @@ -1,24 +1,37 @@ #[cfg(feature = "js")] extern crate napi_build; +#[cfg(feature = "python")] +extern crate pyo3_build_config; + /// The main method of the buildscript, required by some glue modules. fn main() { - #[cfg(feature = "java")] { + #[cfg(feature = "java")] + { let pkg = "com.codemp.jni".to_string(); let pkg_folder = pkg.replace('.', "/"); // java moment let out_dir = std::env::var("OUT_DIR").expect("cargo did not provide OUT_DIR"); let out_dir = std::path::Path::new(&out_dir); let generated_glue_file = out_dir.join("generated_glue.in"); - let src_dir = std::path::Path::new("src") - .join("ffi") - .join("java"); + let src_dir = std::path::Path::new("src").join("ffi").join("java"); let typemap_file = src_dir.join("typemap.in"); - rifgen::Generator::new(rifgen::TypeCases::CamelCase, rifgen::Language::Java, vec![src_dir]) - .generate_interface(&generated_glue_file); + rifgen::Generator::new( + rifgen::TypeCases::CamelCase, + rifgen::Language::Java, + vec![src_dir], + ) + .generate_interface(&generated_glue_file); // build java source path - let target = out_dir.parent().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); // target/debug + let target = out_dir + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .to_path_buf(); // target/debug let mut java_target = target.clone(); // target/debug/java java_target.push("java"); @@ -37,10 +50,11 @@ fn main() { java_gen.expand_many( "codemp-intellij", &[&generated_glue_file, &typemap_file], - out_dir.join("glue.rs") + out_dir.join("glue.rs"), ); - #[cfg(feature = "java-artifact")] { + #[cfg(feature = "java-artifact")] + { // panic if no jdk std::process::Command::new("javac") .arg("--version") @@ -58,13 +72,14 @@ fn main() { javac_cmd.arg(java_file.path().as_os_str()); } javac_cmd.status().expect("failed to run javac"); - + // jar it! let mut jar_file = target.clone(); // target/debug/codemp-java.jar jar_file.push("codemp-java.jar"); let mut jar_cmd = std::process::Command::new("jar"); - jar_cmd.current_dir(&java_compiled) + jar_cmd + .current_dir(&java_compiled) .arg("cf") .arg(jar_file.as_os_str()); for java_file in java_compiled.read_dir().unwrap().filter_map(|e| e.ok()) { @@ -78,9 +93,15 @@ fn main() { } } - #[cfg(feature = "js")] { + #[cfg(feature = "js")] + { napi_build::setup(); } + + #[cfg(feature = "python")] + { + pyo3_build_config::add_extension_module_link_args(); + } } #[cfg(feature = "java")] diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 1eb0c82..e384afb 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -5,4 +5,7 @@ pub mod java; pub mod lua; #[cfg(feature = "js")] -pub mod js; \ No newline at end of file +pub mod js; + +#[cfg(feature = "python")] +pub mod python;