This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
133 lines (112 loc) · 3.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
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
// This file is part of paperd, the PaperMC server daemon
// Copyright (C) 2019 Kyle Wood (DemonWav)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 only.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use flate2::write::GzEncoder;
use flate2::Compression;
use std::env;
use std::fs::OpenOptions;
use std::io::copy;
use std::io::{BufReader, Read};
use std::process::{Command, Stdio};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let cargo_loc = env::var("CARGO").unwrap();
let profile = env::var("PROFILE").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let num_jobs = env::var("NUM_JOBS").unwrap();
let is_release = profile == "release";
let is_mac = target_os == "macos";
let extension = if is_mac { "dylib" } else { "so" };
build_jni(&cargo_loc, is_release, &out_dir, &num_jobs);
let lib_file_name = if is_release {
"release/libpaperd_jni"
} else {
"debug/libpaperd_jni"
};
let lib_file = format!("{}/{}.{}", out_dir, lib_file_name, extension);
if is_release {
strip(lib_file.as_str(), is_mac);
}
compress(lib_file.as_str());
println!("cargo:rustc-env=PAPERD_JNI_LIB={}.gz", lib_file);
}
fn build_jni(cargo_loc: &str, is_release: bool, out_dir: &str, num_jobs: &str) {
let mut command = Command::new(cargo_loc);
let mut command =
command
.current_dir("paperd-jni")
.args(&["build", "-j", num_jobs, "--target-dir", out_dir]);
if is_release {
command.arg("--release");
}
execute(&mut command);
}
fn strip(lib_file: &str, is_mac: bool) {
if is_mac {
let mut command = Command::new("strip");
let mut command = command.args(&["-x", lib_file]);
execute(&mut command);
} else {
let nm_process = Command::new("nm")
.args(&["--extern-only", lib_file])
.stdout(Stdio::piped())
.spawn()
.unwrap();
let mut output = String::new();
nm_process
.stdout
.unwrap()
.read_to_string(&mut output)
.unwrap();
let symbols: Vec<&str> = output
.lines()
.filter_map(|line| {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 3 {
return None;
}
let part = parts[2];
return if part.starts_with("Java_com_destroystokyo_paper") {
Some(part)
} else {
None
};
})
.collect();
let mut command = Command::new("strip");
for symbol in symbols {
command.args(&["-K", symbol]);
}
command.arg(lib_file);
execute(&mut command);
}
}
fn compress(lib_file: &str) {
let output_file = format!("{}.gz", lib_file);
let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(output_file)
.unwrap();
let mut encoder = GzEncoder::new(file, Compression::best());
let source_file = OpenOptions::new().read(true).open(lib_file).unwrap();
let mut input = BufReader::new(source_file);
copy(&mut input, &mut encoder).unwrap();
}
fn execute(cmd: &mut Command) {
if cmd.spawn().unwrap().wait().unwrap().code().unwrap() != 0 {
panic!("Failed to execute command");
}
}