libsodium-ffi 0.2.1

Native bindings (FFI) to the libsodium library
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use std::env;
use std::path::PathBuf;
#[cfg(unix)]
use std::process::{Command, Stdio};

use bindgen;
use pkg_config;
use unwrap::unwrap;
use vcpkg;

const VERSION: &'static str = "1.0.18";

#[cfg(target_env = "msvc")]
const SODIUM_LINK_NAME: &str = "libsodium";
#[cfg(not(target_env = "msvc"))]
const SODIUM_LINK_NAME: &str = "sodium";

fn main() {
    build_libsodium();
}

fn generate_bindings(include_dirs: &[PathBuf]) {
    println!("Invoking bindgen with search paths {:?}", include_dirs);

    let mut builder = bindgen::Builder::default().header("sodium_wrapper.h");
    for p in include_dirs {
        builder = builder.clang_arg(format!("-I{}", p.display()));
    }
    let bindings = builder.generate().expect("Unable to generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings.rs");
}

fn build_libsodium() {
    println!("cargo:rerun-if-env-changed=SODIUM_LIB_DIR");
    println!("cargo:rerun-if-env-changed=SODIUM_INCLUDE_DIR");
    println!("cargo:rerun-if-env-changed=SODIUM_STATIC");
    println!("cargo:rerun-if-env-changed=SODIUM_BUILD_STATIC");

    if probe_libsodium_vcpkg() {
        return;
    }

    let is_static = match env::var_os("SODIUM_STATIC") {
        Some(_) => {
            println!("cargo:rustc-link-lib=static={}", SODIUM_LINK_NAME);
            true
        }
        None => {
            println!("cargo:rustc-link-lib=dylib={}", SODIUM_LINK_NAME);
            false
        }
    };
    // Use library provided by environ
    if let Ok(lib_dir) = env::var("SODIUM_LIB_DIR") {
        let kind = if is_static { "static" } else { "native" };
        println!("cargo:rustc-link-search={}={}", kind, lib_dir);

        match env::var("SODIUM_INCLUDE_DIR") {
            Err(..) => panic!("You have to provide SODIUM_LIB_DIR and SODIUM_INCLUDE_DIR together"),
            Ok(include_dir) => {
                generate_bindings(&vec![PathBuf::from(include_dir)]);
            }
        }

        return;
    }

    // Build from source
    if let Some(..) = env::var_os("SODIUM_BUILD_STATIC") {
        println!("Building libsodium {} from source", VERSION);

        build();

        let mut include_dir = PathBuf::from(unwrap!(env::var("OUT_DIR")));
        include_dir.push("include");
        generate_bindings(&vec![include_dir]);
    } else {
        // Uses system-wide libsodium
        // For windows, check vcpkg first
        if cfg!(windows) {
            match vcpkg::find_package("libsodium") {
                Ok(lib) => {
                    generate_bindings(&lib.include_paths);
                }
                Err(..) => {
                    println!("cargo:warning=Failed to find \"libsodium\" in vcpkg, falling back to pkg-config");
                }
            }
        }

        // Uses pkg-config
        match pkg_config::find_library("libsodium") {
            Ok(lib) => {
                generate_bindings(&lib.include_paths);
            }
            Err(..) => panic!(
                "Missing libsodium library in your system, try to use SODIUM_BUILD_STATIC=yes to build it from source"
            ),
        }
    }
}

#[cfg(windows)]
fn probe_libsodium_vcpkg() -> bool {
    vcpkg::probe_package("libsodium").is_ok() || vcpkg::probe_package("libsodium:x64-windows-static").is_ok()
}

#[cfg(not(windows))]
fn probe_libsodium_vcpkg() -> bool {
    false
}

fn get_install_dir() -> PathBuf {
    PathBuf::from(unwrap!(env::var("OUT_DIR")))
}

#[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() -> PathBuf {
    use std::process::Command;

    let basename = format!("libsodium-{}", VERSION);
    let zip_filename = if cfg!(target_env = "msvc") {
        format!("{}-msvc.zip", basename)
    } else {
        format!("{}-mingw.tar.gz", basename)
    };
    let url = format!(
        "https://download.libsodium.org/libsodium/releases/{}",
        zip_filename
    );

    println!("Downloading {} from {}", zip_filename, url);

    let mut zip_path = get_install_dir();
    zip_path.push(&zip_filename);
    let command = format!(
        "([Net.ServicePointManager]::SecurityProtocol = 'Tls12') -and ((New-Object System.Net.WebClient).DownloadFile(\"{}\", \"{}\"))",
        url, zip_path.display()
    );
    let mut download_cmd = Command::new("powershell");
    download_cmd.arg("-Command").arg(&command);
    println!("Running command: {:?}", download_cmd);
    let download_output = download_cmd.output().unwrap_or_else(|error| {
        panic!("Failed to run powershell download command: {}", error);
    });
    if download_output.status.success() {
        println!(
            "Finished donwload {}, saved to {}",
            zip_filename,
            zip_path.display()
        );
        return zip_path;
    }

    let fallback_url = format!(
        "https://raw.githubusercontent.com/maidsafe/QA/master/appveyor/{}",
        zip_filename
    );
    println!(
        "cargo:warning=Failed to download libsodium from {}. Falling back to MaidSafe mirror at {}",
        url, fallback_url
    );

    let command = format!(
        "([Net.ServicePointManager]::SecurityProtocol = 'Tls12') -and ((New-Object System.Net.WebClient).DownloadFile(\"{}\", \"{}\"))",
        fallback_url, zip_path.display()
    );
    let mut download_cmd = Command::new("powershell");
    download_cmd.arg("-Command").arg(&command);
    println!("Running command: {:?}", download_cmd);
    let download_output = download_cmd.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)
        );
    }

    println!(
        "Finished donwload {}, saved to {}",
        zip_filename,
        zip_path.display()
    );
    zip_path
}

#[cfg(unix)]
fn download_compressed_file() -> PathBuf {
    use curl::easy::Easy;
    use std::fs::File;
    use std::io::Write;

    let zip_filename = format!("libsodium-{}.tar.gz", VERSION);
    let url = format!(
        "https://download.libsodium.org/libsodium/releases/{}",
        zip_filename
    );

    println!("Downloading {} from {}", zip_filename, url);

    let mut gz_path = get_install_dir();
    gz_path.push(&zip_filename);

    // Download to .tar.gz
    let mut zf = unwrap!(File::create(&gz_path));

    let mut easy = Easy::new();
    unwrap!(easy.url(&url));
    unwrap!(easy.write_function(move |data| {
        let n = unwrap!(zf.write(data));
        Ok(n)
    }));
    unwrap!(easy.perform());

    println!(
        "Finished download {}, saved to {}",
        zip_filename,
        gz_path.display()
    );

    gz_path
}

#[cfg(all(windows, target_env = "msvc"))]
fn build() {
    use std::fs::{self, File};
    use std::io::{self, 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 = install_dir.join("lib");
    let include_install_dir = install_dir.join("include");

    // Create out/lib & out/include
    unwrap!(fs::create_dir_all(&lib_install_dir));
    unwrap!(fs::create_dir_all(&include_install_dir));

    // Download pre-built library
    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("libsodium/Win32")
    } else if cfg!(target_pointer_width = "64") {
        Path::new("libsodium/x64")
    } else {
        panic!("target_pointer_width not 32 or 64")
    };

    // The library path inside the archive
    let unpacked_lib = arch_path.join("Release/v140/static/libsodium.lib");

    println!("Searching for lib {}", unpacked_lib.display());

    // Include path prefix
    let unpacked_include = Path::new("libsodium/include");

    println!("Searching for headers from {}", unpacked_include.display());

    fn copy_file<R: Read, W: Write>(r: &mut R, w: &mut W) -> io::Result<()> {
        let mut buf = [0u8; 10240];
        loop {
            let n = r.read(&mut buf)?;
            if n == 0 {
                break;
            }
            w.write_all(&buf[..n])?;
        }
        Ok(())
    }

    for i in 0..zip_archive.len() {
        let mut entry = unwrap!(zip_archive.by_index(i));
        let entry_name = entry.name();
        let entry_path = Path::new(entry_name);

        // 1. Deal with library
        if entry_path == unpacked_lib {
            // Write to lib_install_dir
            let lib_file_path = lib_install_dir.join("libsodium.lib");
            let mut lib_file = unwrap!(File::create(&lib_file_path));
            unwrap!(copy_file(&mut entry, &mut lib_file));

            println!("Unpacked lib to {}", lib_file_path.display());
        }
        // 2. include path
        else if entry_path.starts_with(unpacked_include) {
            // Copy them into include_install_dir
            let relative_path = unwrap!(entry_path.strip_prefix(&unpacked_include));
            let include_file_path = include_install_dir.join(&relative_path);
            if entry.is_dir() {
                let _ = fs::create_dir(&include_file_path);
            } else {
                let mut include_file = unwrap!(File::create(&include_file_path));
                unwrap!(copy_file(&mut entry, &mut include_file));
            }

            println!("Unpacked header to {}", include_file_path.display());
        }
    }

    // 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.display());
}

#[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 = install_dir.join("lib");
    let include_install_dir = install_dir.join("include");

    // Create out/lib & out/include
    unwrap!(fs::create_dir_all(&lib_install_dir));
    unwrap!(fs::create_dir_all(&include_install_dir));

    // Download pre-built library
    let gz_path = download_compressed_file();

    // Unpack the tarball
    let gz_archive = unwrap!(File::open(&gz_path));
    let gz_decoder = 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_lib = arch_path.join("lib\\libsodium.a");
    println!("Searching for lib {}", unpacked_lib.display());

    let unpacked_include = arch_path.join("include");
    println!("Searching for headers from {}", unpacked_include.display());

    for entry_result in unwrap!(archive.entries()) {
        let mut entry = unwrap!(entry_result);
        let entry_path = unwrap!(entry.path()).to_path_buf();

        // 1. Include path
        if entry_path.starts_with(&unpacked_include) {
            let relative_path = unwrap!(entry_path.strip_prefix(&unpacked_include));
            let install_path = include_install_dir.join(&relative_path);
            unwrap!(entry.unpack(&install_path));

            println!("Unpacked header to {}", install_path.display());
        }
        // 2. Lib path
        else if entry_path == unpacked_lib {
            let install_path = lib_install_dir.join("libsodium.a");
            unwrap!(entry.unpack(&install_path));

            println!("Unpacked lib to {}", install_path.display());
        }
    }

    // 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.display());
}

#[cfg(unix)]
fn build() {
    use flate2::read::GzDecoder;
    use std::fs::{self, File};
    use std::path::Path;
    use tar::Archive;

    let gz_path = download_compressed_file();

    // Unpack the tarball
    let gz_archive = unwrap!(File::open(&gz_path));
    let gz_decoder = GzDecoder::new(gz_archive);
    let mut archive = Archive::new(gz_decoder);

    // Build one by ourselves
    let output_dir = unwrap!(env::var("OUT_DIR"));

    let dst = Path::new(&output_dir[..]);

    // Unpack to ${OUTPUT_DIR}/libsodium-VERSION
    unwrap!(archive.unpack(&dst));

    let root = dst.join(format!("libsodium-{}", VERSION));

    println!("Finished unpack to {}", root.display());

    let target = unwrap!(env::var("TARGET"));

    let include_dir = dst.join("include");

    let _ = fs::remove_dir_all(&include_dir);
    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:include={}", include_dir.display());
    println!("cargo:root={}", dst.display());

    fn make() -> &'static str {
        if cfg!(target_os = "freebsd") {
            "gmake"
        } else {
            "make"
        }
    }

    fn run(cmd: &mut Command) {
        println!("Run command: {:?}", cmd);
        assert!(unwrap!(cmd
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .status())
        .success());
    }
}