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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use cc;
use std::{env, fs, path::{Path, PathBuf}, process::Command};
fn run_command(mut command: Command, desc: &str) {
println!("running {:?}", command);
let status = command.status().unwrap();
if !status.success() {
panic!(
"
Error {}:
Command: {:?}
Exit status: {}
",
desc, command, status
);
}
}
fn cp_r(src: &Path, dst: &Path) {
for f in fs::read_dir(src).unwrap() {
let f = f.unwrap();
let path = f.path();
let name = path.file_name().unwrap();
if name.to_str() == Some(".git") {
continue;
}
let dst = dst.join(name);
if f.file_type().unwrap().is_dir() {
fs::create_dir_all(&dst).unwrap();
cp_r(&path, &dst);
} else {
let _ = fs::remove_file(&dst);
fs::copy(&path, &dst).unwrap();
}
}
}
pub struct Artifacts {
pub include_dir: PathBuf,
pub lib_dir: PathBuf,
pub bin_dir: PathBuf,
pub libs: Vec<String>,
}
impl Artifacts {
pub fn print_cargo_metadata(&self) {
println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
for lib in self.libs.iter() {
println!("cargo:rustc-link-lib=static={}", lib);
}
println!("cargo:include={}", self.include_dir.display());
println!("cargo:lib={}", self.lib_dir.display());
}
}
pub struct Builder {
build_dir: PathBuf,
install_dir: PathBuf,
target: String,
host: String,
is_force: bool,
}
impl Builder {
pub fn default() -> Builder {
let out_dir = env::var_os("OUT_DIR").unwrap().into_string().unwrap();
let target = env::var("TARGET").ok().unwrap();
let host = env::var("HOST").ok().unwrap();
Builder::new(&out_dir, &target, &host, false)
}
pub fn new(out_dir: &str, target: &str, host: &str, is_force: bool) -> Builder {
let base_dir = PathBuf::from(out_dir.to_owned()).join("tassl-build");
Builder {
build_dir: base_dir.join("build"),
install_dir: base_dir.join("install"),
target: target.to_owned(),
host: host.to_owned(),
is_force
}
}
#[cfg(target_os = "linux")]
fn get_configure(&self) -> Command {
let mut configure = Command::new("sh");
configure.arg("./config");
configure.arg(&format!("--prefix={}", self.install_dir.display()));
configure
}
#[cfg(target_os = "macos")]
fn get_configure(&self) -> Command {
let perl_program = env::var("OPENSSL_SRC_PERL").unwrap_or(
env::var("PERL").unwrap_or("perl".to_string())
);
let mut configure = Command::new(perl_program);
configure
.arg("./Configure")
.arg(&format!("--prefix={}", self.install_dir.display()))
.arg("no-dso")
.arg("no-shared")
.arg("no-tests")
.arg("no-comp")
.arg("no-zlib")
.arg("no-zlib-dynamic")
.arg("--libdir=lib")
.arg("no-legacy");
let os = match self.target.as_str() {
"aarch64-apple-darwin" => "darwin64-arm64-cc",
"i686-apple-darwin" => "darwin-i386-cc",
"x86_64-apple-darwin" => "darwin64-x86_64-cc",
_ => panic!("Don't know how to configure TASSL for {}", &self.target),
};
configure.arg(os);
let mut cc = cc::Build::new();
cc.target(&self.target).host(&self.host).warnings(false).opt_level(2);
let compiler = cc.get_compiler();
configure.env("CC", compiler.path());
let path = compiler.path().to_str().unwrap();
configure.env_remove("CROSS_COMPILE");
if path.ends_with("-gcc") {
let path = &path[..path.len() - 4];
if env::var_os("RANLIB").is_none() {
configure.env("RANLIB", format!("{}-ranlib", path));
}
if env::var_os("AR").is_none() {
configure.env("AR", format!("{}-ar", path));
}
}
let mut skip_next = false;
for arg in compiler.args() {
if self.target.contains("apple") {
if arg == "-arch" {
skip_next = true;
continue;
}
}
if skip_next {
skip_next = false;
continue;
}
configure.arg(arg);
}
configure
}
#[cfg(all(not(target_os = "macos"), not(target_os = "linux")))]
pub fn build(&self) -> Artifacts {
panic!("Not support {:?} yet.", self.target);
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn build(&self) -> Artifacts {
if self.install_dir.exists() {
if self.is_force {
fs::remove_dir_all(&self.install_dir).unwrap();
} else {
return Artifacts {
lib_dir: self.install_dir.join("lib"),
bin_dir: self.install_dir.join("bin"),
include_dir: self.install_dir.join("include"),
libs: vec!["ssl".to_string(), "crypto".to_string()],
};
}
}
if self.build_dir.exists() {
fs::remove_dir_all(&self.build_dir).unwrap();
}
let current_work_dir = self.build_dir.join("src");
fs::create_dir_all(¤t_work_dir).unwrap();
let source_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("TASSL");
cp_r(&source_dir, ¤t_work_dir);
let mut configure = self.get_configure();
configure.current_dir(¤t_work_dir);
run_command(configure, "configuring TASSL build");
let mut depend = Command::new("make");
depend.arg("depend").current_dir(¤t_work_dir);
run_command(depend, "building TASSL dependencies");
let mut build = Command::new("make");
build.arg("build_libs").current_dir(¤t_work_dir);
if let Some(s) = env::var_os("CARGO_MAKEFLAGS") {
build.env("MAKEFLAGS", s);
}
run_command(build, "building TASSL");
let mut install = Command::new("make");
install.arg("install").current_dir(¤t_work_dir);
run_command(install, "installing TASSL");
fs::remove_dir_all(¤t_work_dir).unwrap();
Artifacts {
lib_dir: self.install_dir.join("lib"),
bin_dir: self.install_dir.join("bin"),
include_dir: self.install_dir.join("include"),
libs: vec!["ssl".to_string(), "crypto".to_string()],
}
}
}