forked from zonyitoo/libsodium-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
349 lines (302 loc) · 10.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
extern crate cc;
#[cfg(windows)]
extern crate flate2;
extern crate pkg_config;
#[macro_use]
extern crate unwrap;
#[cfg(windows)]
extern crate tar;
#[cfg(windows)]
extern crate vcpkg;
#[cfg(windows)]
extern crate zip;
use std::env;
#[cfg(unix)]
use std::process::{Command, Stdio};
const VERSION: &'static str = "1.0.16";
fn main() {
println!("cargo:rerun-if-env-changed=SODIUM_LIB_DIR");
println!("cargo:rerun-if-env-changed=SODIUM_STATIC");
println!("cargo:rerun-if-env-changed=SODIUM_BUILD_STATIC");
if probe_libsodium_vcpkg() {
return;
}
// Use library provided by environ
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",
};
println!("cargo:rustc-link-lib={0}=sodium", mode);
return;
}
if let None = env::var_os("SODIUM_BUILD_STATIC") {
// Uses system-wide libsodium
match pkg_config::find_library("libsodium") {
Ok(..) => return,
Err(..) => panic!(
"Missing libsodium library in your system, \
try to use SODIUM_BUILD_STATIC=yes to build it from source"
),
}
}
println!("Building libsodium {} from source", VERSION);
build();
}
#[cfg(windows)]
fn probe_libsodium_vcpkg() -> bool {
vcpkg::probe_package("libsodium").is_ok()
}
#[cfg(not(windows))]
fn probe_libsodium_vcpkg() -> bool {
false
}
#[cfg(windows)]
fn get_install_dir() -> String {
use std::env;
unwrap!(env::var("OUT_DIR")) + "/installed"
}
#[cfg(windows)]
fn check_powershell_version() {
let mut check_ps_version_cmd = ::std::process::Command::new("powershell");
let check_ps_version_output = check_ps_version_cmd
.arg("-Command")
.arg("If ($PSVersionTable.PSVersion.Major -lt 4) { exit 1 }")
.output()
.unwrap_or_else(|error| {
panic!("Failed to run powershell command: {}", error);
});
if !check_ps_version_output.status.success() {
panic!(
"\n{:?}\n{}\n{}\nYou must have Powershell v4.0 or greater installed.\n\n",
check_ps_version_cmd,
String::from_utf8_lossy(&check_ps_version_output.stdout),
String::from_utf8_lossy(&check_ps_version_output.stderr)
);
}
}
#[cfg(windows)]
fn download_compressed_file() -> String {
use std::process::Command;
let basename = "libsodium-".to_string() + VERSION;
let zip_filename = if cfg!(target_env = "msvc") {
basename.clone() + "-msvc.zip"
} else {
basename.clone() + "-mingw.tar.gz"
};
let url = "https://download.libsodium.org/libsodium/releases/".to_string() + &zip_filename;
let zip_path = get_install_dir() + "/" + &zip_filename;
let mut command = "([Net.ServicePointManager]::SecurityProtocol = 'Tls12') -and \
((New-Object System.Net.WebClient).DownloadFile(\""
.to_string() + &url + "\", \"" + &zip_path + "\"))";
let mut download_cmd = Command::new("powershell");
let mut download_output = download_cmd
.arg("-Command")
.arg(&command)
.output()
.unwrap_or_else(|error| {
panic!("Failed to run powershell download command: {}", error);
});
if download_output.status.success() {
return zip_path;
}
let fallback_url = "https://raw.githubusercontent.com/maidsafe/QA/master/appveyor/".to_string()
+ &zip_filename;
println!(
"cargo:warning=Failed to download libsodium from {}. Falling back to MaidSafe mirror \
at {}",
url, fallback_url
);
command = "([Net.ServicePointManager]::SecurityProtocol = 'Tls12') -and \
((New-Object System.Net.WebClient).DownloadFile(\""
.to_string() + &fallback_url + "\", \"" + &zip_path + "\"))";
download_cmd = Command::new("powershell");
download_output = download_cmd
.arg("-Command")
.arg(&command)
.output()
.unwrap_or_else(|error| {
panic!("Failed to run powershell download command: {}", error);
});
if !download_output.status.success() {
panic!(
"\n{:?}\n{}\n{}\n",
download_cmd,
String::from_utf8_lossy(&download_output.stdout),
String::from_utf8_lossy(&download_output.stderr)
);
}
zip_path
}
#[cfg(all(windows, target_env = "msvc"))]
fn build() {
const S_IFDIR: ::std::os::raw::c_int = 16384;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;
use zip::ZipArchive;
check_powershell_version();
// Download zip file
let install_dir = get_install_dir();
let lib_install_dir = Path::new(&install_dir).join("lib");
unwrap!(fs::create_dir_all(&lib_install_dir));
let zip_path = download_compressed_file();
// Unpack the zip file
let zip_file = unwrap!(File::open(&zip_path));
let mut zip_archive = unwrap!(ZipArchive::new(zip_file));
// Extract just the appropriate version of libsodium.lib and headers to the install path. For
// now, only handle MSVC 2015.
let arch_path = if cfg!(target_pointer_width = "32") {
Path::new("Win32")
} else if cfg!(target_pointer_width = "64") {
Path::new("x64")
} else {
panic!("target_pointer_width not 32 or 64")
};
let unpacked_lib = arch_path.join("Release/v140/static/libsodium.lib");
for i in 0..zip_archive.len() {
let mut entry = unwrap!(zip_archive.by_index(i));
let entry_name = entry.name().to_string();
let entry_path = Path::new(&entry_name);
let opt_install_path = if entry_path.starts_with("include") {
let is_dir = (unwrap!(entry.unix_mode()) & S_IFDIR as u32) != 0;
if is_dir {
let _ = fs::create_dir(&Path::new(&install_dir).join(entry_path));
None
} else {
Some(Path::new(&install_dir).join(entry_path))
}
} else if entry_path == unpacked_lib {
Some(lib_install_dir.join("libsodium.lib"))
} else {
None
};
if let Some(full_install_path) = opt_install_path {
let mut buffer = Vec::with_capacity(entry.size() as usize);
assert_eq!(entry.size(), unwrap!(entry.read_to_end(&mut buffer)) as u64);
let mut file = unwrap!(File::create(&full_install_path));
unwrap!(file.write_all(&buffer));
}
}
// Clean up
let _ = fs::remove_file(zip_path);
println!("cargo:rustc-link-lib=static=libsodium");
println!(
"cargo:rustc-link-search=native={}",
lib_install_dir.display()
);
println!("cargo:include={}/include", install_dir);
}
#[cfg(all(windows, not(target_env = "msvc")))]
fn build() {
use flate2::read::GzDecoder;
use std::fs::{self, File};
use std::path::Path;
use tar::Archive;
check_powershell_version();
// Download gz tarball
let install_dir = get_install_dir();
let lib_install_dir = Path::new(&install_dir).join("lib");
unwrap!(fs::create_dir_all(&lib_install_dir));
let gz_path = download_compressed_file();
// Unpack the tarball
let gz_archive = unwrap!(File::open(&gz_path));
let gz_decoder = unwrap!(GzDecoder::new(gz_archive));
let mut archive = Archive::new(gz_decoder);
// Extract just the appropriate version of libsodium.a and headers to the install path
let arch_path = if cfg!(target_pointer_width = "32") {
Path::new("libsodium-win32")
} else if cfg!(target_pointer_width = "64") {
Path::new("libsodium-win64")
} else {
panic!("target_pointer_width not 32 or 64")
};
let unpacked_include = arch_path.join("include");
let unpacked_lib = arch_path.join("lib\\libsodium.a");
let entries = unwrap!(archive.entries());
for entry_result in entries {
let mut entry = unwrap!(entry_result);
let entry_path = unwrap!(entry.path()).to_path_buf();
let full_install_path = if entry_path.starts_with(&unpacked_include) {
let include_file = unwrap!(entry_path.strip_prefix(arch_path));
Path::new(&install_dir).join(include_file)
} else if entry_path == unpacked_lib {
lib_install_dir.join("libsodium.a")
} else {
continue;
};
unwrap!(entry.unpack(full_install_path));
}
// Clean up
let _ = fs::remove_file(gz_path);
println!("cargo:rustc-link-lib=static=sodium");
println!(
"cargo:rustc-link-search=native={}",
lib_install_dir.display()
);
println!("cargo:include={}/include", install_dir);
}
#[cfg(unix)]
fn build() {
use std::fs;
use std::path::Path;
// Build one by ourselves
let cargo_dir = unwrap!(env::var("CARGO_MANIFEST_DIR"));
let output_dir = unwrap!(env::var("OUT_DIR"));
let src = Path::new(&cargo_dir[..]);
let dst = Path::new(&output_dir[..]);
let target = unwrap!(env::var("TARGET"));
let root = src.join("libsodium");
let mut autogen_cmd = Command::new("sh");
autogen_cmd.arg("-c");
let ccmd = format!("{}", root.join("autogen.sh").display());
autogen_cmd.arg(&ccmd);
run(autogen_cmd.current_dir(&root));
let _ = fs::remove_dir_all(&dst.join("include"));
let _ = fs::remove_dir_all(&dst.join("lib"));
let build_dir = dst.join("build");
let _ = fs::remove_dir_all(&build_dir);
fs::create_dir(&build_dir).unwrap();
let mut configure_cmd = Command::new("sh");
configure_cmd.arg("-c");
let cmd_path = format!("{}", root.join("configure").display());
let dst_path = format!("{}", dst.display());
let mut ccmd = format!(
"{} --prefix={} --disable-shared --enable-static=yes",
cmd_path, dst_path
);
if target.contains("android") {
ccmd += " --disable-soname-versions";
}
configure_cmd.arg(&ccmd);
run(configure_cmd.current_dir(&build_dir));
run(Command::new(make())
.arg(&format!("-j{}", env::var("NUM_JOBS").unwrap()))
.current_dir(&build_dir));
run(Command::new(make())
.arg(&format!("-j{}", env::var("NUM_JOBS").unwrap()))
.arg("install")
.current_dir(&build_dir));
println!("cargo:rustc-link-lib=static=sodium");
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:root={}", dst.display());
fn make() -> &'static str {
if cfg!(target_os = "freebsd") {
"gmake"
} else {
"make"
}
}
}
#[cfg(unix)]
fn run(cmd: &mut Command) {
println!("running: {:?}", cmd);
assert!(
unwrap!(
cmd.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
).success()
);
}