This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathbuild.rs
298 lines (278 loc) · 10.5 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::env;
use std::path::PathBuf;
#[cfg(feature = "libsodium-bundled")]
use std::process::Command;
#[cfg(all(feature = "libsodium-bundled", not(target_os = "windows")))]
const LIBSODIUM_NAME: &'static str = "libsodium-1.0.18";
#[cfg(all(feature = "libsodium-bundled", not(target_os = "windows")))]
const LIBSODIUM_URL: &'static str =
"https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz";
// skip the build script when building doc on docs.rs
#[cfg(feature = "docs-rs")]
fn main() {}
#[cfg(not(feature = "docs-rs"))]
fn main() {
#[cfg(feature = "libsodium-bundled")]
download_and_install_libsodium();
#[cfg(not(feature = "libsodium-bundled"))]
{
println!("cargo:rerun-if-env-changed=SODIUM_LIB_DIR");
println!("cargo:rerun-if-env-changed=SODIUM_STATIC");
}
// add libsodium link options
if let Ok(lib_dir) = env::var("SODIUM_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
let mode = match env::var_os("SODIUM_STATIC") {
Some(_) => "static",
None => "dylib",
};
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib={0}=libsodium", mode);
} else {
println!("cargo:rustc-link-lib={0}=sodium", mode);
}
} else {
// the static linking doesn't work if libsodium is installed
// under '/usr' dir, in that case use the environment variables
// mentioned above
pkg_config::Config::new()
.atleast_version("1.0.18")
.statik(true)
.probe("libsodium")
.unwrap();
}
// add liblz4 link options
if let Ok(lib_dir) = env::var("LZ4_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=static=liblz4");
} else {
println!("cargo:rustc-link-lib=static=lz4");
}
} else {
// build lz4 static library
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
if !out_dir.join("liblz4.a").exists() {
let mut compiler = cc::Build::new();
compiler
.file("vendor/lz4/lz4.c")
.file("vendor/lz4/lz4frame.c")
.file("vendor/lz4/lz4hc.c")
.file("vendor/lz4/xxhash.c")
.define("XXH_NAMESPACE", "LZ4_")
.opt_level(3)
.debug(false)
.pic(true)
.shared_flag(false);
if !cfg!(windows) {
compiler.static_flag(true);
}
compiler.compile("liblz4.a");
}
}
}
// This downloads function and builds the libsodium from source for linux and
// unix targets.
// The steps are taken from the libsodium installation instructions:
// https://libsodium.gitbook.io/doc/installation
// effectively:
// $ ./configure
// $ make && make check
// $ sudo make install
#[cfg(all(feature = "libsodium-bundled", not(target_os = "windows")))]
fn download_and_install_libsodium() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let source_dir = out_dir.join(LIBSODIUM_NAME);
let prefix_dir = out_dir.join("libsodium");
let sodium_lib_dir = prefix_dir.join("lib");
let src_file_name = format!("{}.tar.gz", LIBSODIUM_NAME);
// check if command tools exist
Command::new("curl")
.arg("--version")
.output()
.expect("curl not found");
Command::new("tar")
.arg("--version")
.output()
.expect("tar not found");
Command::new("gpg")
.arg("--version")
.output()
.expect("gpg not found");
Command::new("make")
.arg("--version")
.output()
.expect("make not found");
if !source_dir.exists() {
// download source code file
let output = Command::new("curl")
.current_dir(&out_dir)
.args(&[LIBSODIUM_URL, "-sSfL", "-o", &src_file_name])
.output()
.expect("failed to download libsodium");
assert!(output.status.success());
// download signature file
let sig_file_name = format!("{}.sig", src_file_name);
let sig_url = format!("{}.sig", LIBSODIUM_URL);
let output = Command::new("curl")
.current_dir(&out_dir)
.args(&[&sig_url, "-sSfL", "-o", &sig_file_name])
.output()
.expect("failed to download libsodium signature file");
assert!(output.status.success());
// import libsodium author's public key
let output = Command::new("gpg")
.arg("--import")
.arg("libsodium.gpg.key")
.output()
.expect("failed to import libsodium author's gpg key");
assert!(output.status.success());
// verify signature
let output = Command::new("gpg")
.current_dir(&out_dir)
.arg("--verify")
.arg(&sig_file_name)
.output()
.expect("failed to verify libsodium file");
assert!(output.status.success());
// unpack source code files
let output = Command::new("tar")
.current_dir(&out_dir)
.args(&["zxf", &src_file_name])
.output()
.expect("failed to unpack libsodium");
assert!(output.status.success());
}
if !sodium_lib_dir.exists() {
let configure = source_dir.join("./configure");
let output = Command::new(&configure)
.current_dir(&source_dir)
.args(&[std::path::Path::new("--prefix"), &prefix_dir])
.output()
.expect("failed to execute configure");
assert!(output.status.success());
let output = Command::new("make")
.current_dir(&source_dir)
.output()
.expect("failed to execute make");
assert!(output.status.success());
let output = Command::new("make")
.current_dir(&source_dir)
.arg("install")
.output()
.expect("failed to execute make install");
assert!(output.status.success());
}
assert!(
&sodium_lib_dir.exists(),
"libsodium lib directory was not created."
);
env::set_var("SODIUM_LIB_DIR", &sodium_lib_dir);
env::set_var("SODIUM_STATIC", "true");
}
// This downloads function and builds the libsodium from source for windows msvc target.
// The binaries are pre-compiled, so we simply download and link.
// The binary is compressed in zip format.
#[cfg(all(
feature = "libsodium-bundled",
target_os = "windows",
target_env = "msvc"
))]
fn download_and_install_libsodium() {
use std::fs;
use std::fs::OpenOptions;
use std::io;
use std::path::PathBuf;
#[cfg(target_env = "msvc")]
static LIBSODIUM_ZIP: &'static str = "https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-msvc.zip";
#[cfg(target_env = "mingw")]
static LIBSODIUM_ZIP: &'static str = "https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-mingw.tar.gz";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let sodium_lib_dir = out_dir.join("libsodium");
if !sodium_lib_dir.exists() {
fs::create_dir(&sodium_lib_dir).unwrap();
}
let sodium_lib_file_path = sodium_lib_dir.join("libsodium.lib");
if !sodium_lib_file_path.exists() {
let mut tmpfile = tempfile::tempfile().unwrap();
reqwest::get(LIBSODIUM_ZIP)
.unwrap()
.copy_to(&mut tmpfile)
.unwrap();
let mut zip = zip::ZipArchive::new(tmpfile).unwrap();
#[cfg(target_arch = "x86_64")]
let mut lib = zip
.by_name("x64/Release/v142/static/libsodium.lib")
.unwrap();
#[cfg(target_arch = "x86")]
let mut lib = zip
.by_name("Win32/Release/v142/static/libsodium.lib")
.unwrap();
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
compile_error!("Bundled libsodium is only supported on x86 or x86_64 target architecture.");
let mut libsodium_file = OpenOptions::new()
.create(true)
.write(true)
.open(&sodium_lib_file_path)
.unwrap();
io::copy(&mut lib, &mut libsodium_file).unwrap();
}
assert!(
&sodium_lib_dir.exists(),
"libsodium lib directory was not created."
);
env::set_var("SODIUM_LIB_DIR", &sodium_lib_dir);
env::set_var("SODIUM_STATIC", "true");
}
// This downloads function and builds the libsodium from source for windows mingw target.
// The binaries are pre-compiled, so we simply download and link.
// The binary is compressed in tar.gz format.
#[cfg(all(
feature = "libsodium-bundled",
target_os = "windows",
target_env = "gnu"
))]
fn download_and_install_libsodium() {
use libflate::non_blocking::gzip::Decoder;
use std::fs;
use std::fs::OpenOptions;
use std::io;
use std::path::PathBuf;
use tar::Archive;
static LIBSODIUM_ZIP: &'static str = "https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-mingw.tar.gz";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let sodium_lib_dir = out_dir.join("libsodium");
if !sodium_lib_dir.exists() {
fs::create_dir(&sodium_lib_dir).unwrap();
}
let sodium_lib_file_path = sodium_lib_dir.join("libsodium.lib");
if !sodium_lib_file_path.exists() {
let response = reqwest::get(LIBSODIUM_ZIP).unwrap();
let decoder = Decoder::new(response);
let mut ar = Archive::new(decoder);
#[cfg(target_arch = "x86_64")]
let filename = PathBuf::from("libsodium-win64/lib/libsodium.a");
#[cfg(target_arch = "x86")]
let filename = PathBuf::from("libsodium-win32/lib/libsodium.a");
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
compile_error!("Bundled libsodium is only supported on x86 or x86_64 target architecture.");
for file in ar.entries().unwrap() {
let mut f = file.unwrap();
if f.path().unwrap() == *filename {
let mut libsodium_file = OpenOptions::new()
.create(true)
.write(true)
.open(&sodium_lib_file_path)
.unwrap();
io::copy(&mut f, &mut libsodium_file).unwrap();
break;
}
}
}
assert!(
&sodium_lib_dir.exists(),
"libsodium lib directory was not created."
);
env::set_var("SODIUM_LIB_DIR", &sodium_lib_dir);
env::set_var("SODIUM_STATIC", "true");
}