feat: added python glue, still needs some tweaking in the building of the dylib

This commit is contained in:
Camillo Schenone 2024-03-16 14:36:41 +01:00
parent f0003f04bd
commit 13a4163d1e
3 changed files with 44 additions and 14 deletions

View file

@ -44,12 +44,17 @@ rmpv = { version = "1", optional = true }
napi = { version = "2", features = ["full"], optional = true } napi = { version = "2", features = ["full"], optional = true }
napi-derive = { version="2", optional = true} napi-derive = { version="2", optional = true}
futures = { version = "0.3.28", 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] [build-dependencies]
# glue (java) # glue (java)
flapigen = { version = "0.6.0", optional = true } flapigen = { version = "0.6.0", optional = true }
rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7", optional = true } rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7", optional = true }
# glue (js) # glue (js)
napi-build = { version = "2", optional = true } napi-build = { version = "2", optional = true }
# glue (python)
pyo3-build-config = { version = "0.19.2", optional = true }
[features] [features]
default = [] default = []
@ -57,3 +62,4 @@ lua = ["mlua", "thiserror", "derive_more", "lazy_static", "tracing-subscriber"]
java = ["lazy_static", "jni", "jni-sys", "flapigen", "rifgen", "log"] java = ["lazy_static", "jni", "jni-sys", "flapigen", "rifgen", "log"]
java-artifact = ["java"] # also builds the jar java-artifact = ["java"] # also builds the jar
js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"] js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"]
python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"]

View file

@ -1,24 +1,37 @@
#[cfg(feature = "js")] #[cfg(feature = "js")]
extern crate napi_build; extern crate napi_build;
#[cfg(feature = "python")]
extern crate pyo3_build_config;
/// The main method of the buildscript, required by some glue modules. /// The main method of the buildscript, required by some glue modules.
fn main() { fn main() {
#[cfg(feature = "java")] { #[cfg(feature = "java")]
{
let pkg = "com.codemp.jni".to_string(); let pkg = "com.codemp.jni".to_string();
let pkg_folder = pkg.replace('.', "/"); // java moment 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::env::var("OUT_DIR").expect("cargo did not provide OUT_DIR");
let out_dir = std::path::Path::new(&out_dir); let out_dir = std::path::Path::new(&out_dir);
let generated_glue_file = out_dir.join("generated_glue.in"); let generated_glue_file = out_dir.join("generated_glue.in");
let src_dir = std::path::Path::new("src") let src_dir = std::path::Path::new("src").join("ffi").join("java");
.join("ffi")
.join("java");
let typemap_file = src_dir.join("typemap.in"); let typemap_file = src_dir.join("typemap.in");
rifgen::Generator::new(rifgen::TypeCases::CamelCase, rifgen::Language::Java, vec![src_dir]) rifgen::Generator::new(
rifgen::TypeCases::CamelCase,
rifgen::Language::Java,
vec![src_dir],
)
.generate_interface(&generated_glue_file); .generate_interface(&generated_glue_file);
// build java source path // 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 let mut java_target = target.clone(); // target/debug/java
java_target.push("java"); java_target.push("java");
@ -37,10 +50,11 @@ fn main() {
java_gen.expand_many( java_gen.expand_many(
"codemp-intellij", "codemp-intellij",
&[&generated_glue_file, &typemap_file], &[&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 // panic if no jdk
std::process::Command::new("javac") std::process::Command::new("javac")
.arg("--version") .arg("--version")
@ -64,7 +78,8 @@ fn main() {
jar_file.push("codemp-java.jar"); jar_file.push("codemp-java.jar");
let mut jar_cmd = std::process::Command::new("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("cf")
.arg(jar_file.as_os_str()); .arg(jar_file.as_os_str());
for java_file in java_compiled.read_dir().unwrap().filter_map(|e| e.ok()) { 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(); napi_build::setup();
} }
#[cfg(feature = "python")]
{
pyo3_build_config::add_extension_module_link_args();
}
} }
#[cfg(feature = "java")] #[cfg(feature = "java")]

View file

@ -6,3 +6,6 @@ pub mod lua;
#[cfg(feature = "js")] #[cfg(feature = "js")]
pub mod js; pub mod js;
#[cfg(feature = "python")]
pub mod python;