-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
144 lines (117 loc) · 3.97 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::fs;
use std::fs::File;
use std::process::Command;
use android_tools::d8::get_d8_path;
use cmd_lib::run_cmd;
fn main() {
generate_bytecode();
// download_jre();
// extract_stdlib();
// strip_stdlib();
// dex_stdlib();
dex_entrypoint();
dex_stdlib()
}
fn dex_entrypoint() {
let last_sha256_path = "runtime/build/Entrypoint.java.sha256";
let current_sha256 = calculate_sha256("runtime/Entrypoint.java");
if File::open("runtime/build/entrypoint.dex").is_ok() {
let last_sha256 = fs::read_to_string(last_sha256_path);
if let Ok(sha) = last_sha256 {
if current_sha256 == sha {
return;
}
}
}
let d8 = get_d8_path();
run_cmd!(
cd runtime;
mkdir -p build;
javac -d build Entrypoint.java &> javac_output.txt;
cd build;
jar cvf entrypoint.jar me &> jar_output.txt;
$d8 entrypoint.jar --output entrypoint.dex.zip;
unzip entrypoint.dex.zip;
mv classes.dex entrypoint.dex;
).expect("Unable to dex entrypoint");
fs::write(last_sha256_path, current_sha256).expect("Unable to write sha256");
}
fn calculate_sha256(path: &str) -> String {
let data = fs::read(path).expect(&format!("Unable to read {:?}", path));
sha256::digest(&data)
}
fn generate_bytecode() {
// let bytecode = Path::new("data/bytecode.txt");
// let file = File::open(bytecode).expect(&format!("Unable to open {:?}", bytecode));
// for Ok(line) in BufReader::new(file).lines() {
// let mut parts = line.split(" ");
// let struct_type = parts.next();
//
// // let formats = vec![];
// // let instructions = vec![];
//
// // match struct_type {
// // None => continue,
// // Some("format") => {
// // formats.push(parse_formats(parts))
// // }
// // Some("op") => {
// //
// // }
// // }
// }
}
struct Format {
name: String,
fields: Vec<Field>,
}
struct Field {}
// fn parse_formats(p0: Split<&str>) -> T {
// todo!()
// }
fn strip_stdlib() {
let mut command = std::process::Command::new("proguard");
let _ = &command.arg("@proguard-runtime.pro");
let output = command.output().expect("Unable to execute proguard");
assert!(output.status.success());
}
fn download_jre() {
fs::create_dir_all("toolkit/jre").expect("Unable to create jre directory");
let mut downloader = downloader::downloader::Builder::default()
.download_folder("toolkit/".as_ref())
.build()
.expect("Unable to create downloader");
let download = downloader::download::Download::new("https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jre_x86-32_windows_hotspot_8u392b08.zip");
downloader
.download(&[download])
.expect("Unable to download jre");
}
fn extract_stdlib() {
if File::open("toolkit/jre/lib/rt.jar").is_ok() {
return;
}
let path = "toolkit/OpenJDK8U-jre_x86-32_windows_hotspot_8u392b08.zip";
let file =
File::open(path)
.expect(format!("Unable to open {}", path).as_str());
zip_extract::extract(file, "toolkit/jre".as_ref(), true)
.expect("Unable to extract jre");
}
fn dex_libcore() {
}
fn dex_stdlib() {
if File::open("toolkit/runtime.dex").is_ok() {
return;
}
let d8 = get_d8_path();
let dex_zip_path = "toolkit/runtime.dex.zip";
let mut command = Command::new(d8);
let _ = &command.arg("toolkit/runtime.jar");
let _ = &command.arg("--output");
let _ = &command.arg(dex_zip_path);
let _ = command.output().expect("Unable to execute d8");
let dex = File::open(dex_zip_path).expect(format!("Unable to open {}", dex_zip_path).as_str());
zip_extract::extract(dex, "toolkit".as_ref(), true)
.expect("Unable to extract dex");
fs::rename("toolkit/classes.dex", "toolkit/runtime.dex").expect("Unable to rename dex");
}