-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.rs
104 lines (84 loc) · 2.89 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::{
borrow::Cow,
error::Error,
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
process::{self, Command},
};
use jaffi::Jaffi;
fn class_path() -> PathBuf {
PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set")).join("java/classes")
}
fn find_java_files() -> Vec<PathBuf> {
let search_paths: Vec<Cow<'_, Path>> = vec![Cow::from(PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"),
))];
find_files(search_paths, "java")
}
fn find_files(mut search_paths: Vec<Cow<'_, Path>>, extension: &str) -> Vec<PathBuf> {
let mut java_files = Vec::<PathBuf>::new();
while let Some(path) = search_paths.pop() {
if !path.is_dir() {
continue;
}
for dir_entry in path.read_dir().expect("could not read directory") {
let dir_entry = dir_entry.expect("could not open directory");
let path = dir_entry.path();
match dir_entry.file_type().expect("could not read file") {
e if e.is_dir() => {
search_paths.push(path.into());
}
e if e.is_file() => {
if path
.extension()
.map(|ext| ext == extension)
.unwrap_or(false)
{
java_files.push(path);
}
}
_ => (),
}
}
}
java_files
}
fn compile_java() {
let java_files = find_java_files()
.into_iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>();
// create the target dir
let class_path = class_path().display().to_string();
std::fs::create_dir_all(&class_path).expect("failed to create dir");
let mut cmd = Command::new("javac");
cmd.arg("-d")
.arg(&class_path)
.arg("-h")
.arg(class_path)
.args(java_files);
eprintln!("javac: {cmd:?}");
let output = cmd.output().expect("Failed to execute command");
std::io::stderr().write_all(&output.stdout).unwrap();
std::io::stderr().write_all(&output.stderr).unwrap();
eprintln!("java compilations status: {}", output.status);
if !output.status.success() {
panic!("javac failed");
}
eprintln!("successfully compiled java");
}
fn main() -> Result<(), Box<dyn Error>> {
// only need this if you need to compile the java, this is needed for the integration tests...
compile_java();
let class_path = class_path();
let class = "net.bluejekyll.NativeClass";
let output_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set"));
let jaffi = Jaffi::builder()
.classes(vec![Cow::from(class)])
.classpath(vec![Cow::from(class_path)])
.output_dir(Some(Cow::from(output_dir)))
.build();
jaffi.generate()?;
Ok(())
}