codemp/build.rs

114 lines
3.2 KiB
Rust
Raw Normal View History

2024-03-10 12:42:56 +01:00
#[cfg(feature = "js")]
extern crate napi_build;
#[cfg(feature = "python")]
extern crate pyo3_build_config;
2024-03-09 23:27:08 +01:00
/// The main method of the buildscript, required by some glue modules.
fn main() {
#[cfg(feature = "java")]
{
2024-03-09 23:27:08 +01:00
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");
2024-03-09 23:27:08 +01:00
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);
2024-03-09 23:27:08 +01:00
// build java source path
let target = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf(); // target/debug
2024-03-09 23:57:20 +01:00
let mut java_target = target.clone(); // target/debug/java
2024-03-09 23:27:08 +01:00
java_target.push("java");
2024-03-09 23:57:20 +01:00
let mut pkg_path = java_target.clone(); // target/debug/java/src/com/codemp/intellij
2024-03-09 23:27:08 +01:00
pkg_path.push("src");
pkg_path.push(pkg_folder);
// delete folder if it exists, then create it
recreate_path(&pkg_path);
// generate java code
let java_cfg = flapigen::JavaConfig::new(pkg_path.clone(), pkg);
let java_gen = flapigen::Generator::new(flapigen::LanguageConfig::JavaConfig(java_cfg))
.rustfmt_bindings(true);
java_gen.expand_many(
"codemp-intellij",
&[&generated_glue_file, &typemap_file],
out_dir.join("glue.rs"),
2024-03-09 23:27:08 +01:00
);
#[cfg(feature = "java-artifact")]
{
// panic if no jdk
std::process::Command::new("javac")
.arg("--version")
.status()
.expect("java not found");
2024-03-09 23:27:08 +01:00
// compile java code
let mut java_compiled = java_target.clone(); // target/debug/java/classes
java_compiled.push("classes");
recreate_path(&java_compiled);
2024-03-09 23:27:08 +01:00
let mut javac_cmd = std::process::Command::new("javac");
javac_cmd.arg("-d").arg(java_compiled.as_os_str());
for java_file in pkg_path.read_dir().unwrap().filter_map(|e| e.ok()) {
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");
2024-03-09 23:57:20 +01:00
let mut jar_cmd = std::process::Command::new("jar");
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()) {
let relative_path = java_file.path().clone();
let relative_path = relative_path.strip_prefix(&java_compiled).unwrap();
jar_cmd.arg(relative_path.as_os_str());
}
jar_cmd.status().expect("failed to run jar!");
2024-03-09 23:27:08 +01:00
println!("cargo:rerun-if-changed={}", generated_glue_file.display());
}
2024-03-09 23:27:08 +01:00
}
2024-03-10 12:42:56 +01:00
#[cfg(feature = "js")]
{
2024-03-10 12:42:56 +01:00
napi_build::setup();
}
#[cfg(feature = "python")]
{
pyo3_build_config::add_extension_module_link_args();
}
2024-03-09 23:27:08 +01:00
}
#[cfg(feature = "java")]
2024-03-09 23:27:08 +01:00
fn recreate_path(path: &std::path::PathBuf) {
if path.exists() {
std::fs::remove_dir_all(path).expect("failed to delete old dir!");
}
std::fs::create_dir_all(path).expect("error while creating folder!");
}