Skip to content

Commit

Permalink
Add Windows support (MSVC)
Browse files Browse the repository at this point in the history
Update README
  • Loading branch information
suhr committed Nov 8, 2018
1 parent 0be9734 commit 9661968
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 45 deletions.
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sodiumoxide
===========
# sodiumoxide

[![Build Status](https://travis-ci.org/sodiumoxide/sodiumoxide.svg?branch=master)](https://travis-ci.org/sodiumoxide/sodiumoxide)
[![Latest Version](https://img.shields.io/crates/v/sodiumoxide.svg)](https://crates.io/crates/sodiumoxide)
Expand All @@ -13,25 +12,24 @@ sodiumoxide
This package aims to provide a type-safe and efficient Rust binding that's just
as easy to use.

Dependencies
------------
## Dependencies

[Clang](https://clang.llvm.org/) >= 3.9

[Libsodium](https://github.com/jedisct1/libsodium) 1.0.16 ([installation](https://download.libsodium.org/doc/installation))

pkg-config (OSX: `brew install pkg-config`)

Building
--------
## Building

cargo build

Testing
-------
## Testing

cargo test

Documentation
-------------
## Documentation

cargo doc

Documentation will be generated in target/doc/...
Expand All @@ -42,8 +40,7 @@ differs between the C and Rust versions.
Documentation for the latest build can be found at
[gh-pages](https://sodiumoxide.github.io/sodiumoxide).

Optional features
-----------------
## Optional features

Several [optional features](http://doc.crates.io/manifest.html#usage-in-end-products) are available:

Expand All @@ -58,40 +55,40 @@ Several [optional features](http://doc.crates.io/manifest.html#usage-in-end-prod
* `benchmarks` (default: **disabled**). Compile benchmark tests. Requires a
nightly build of Rust.

Examples
--------
## Examples

TBD

Platform Compatibiility
------------------------
Sodiumoxide has been tested on:
## Platform Compatibiility

Linux - Yes
Sodiumoxide has been tested on:

Windows - In Progress
- Linux: Yes
- Windows: Yes (MSVC)
- Mac OS:
- IOS:
- Android:

Mac OS - `
### Using vcpkg

IOS -
To build sodiumoxide using libsodium from vcpkg, you have to add either `VCPKGRS_DYNAMIC=1` (for dynamic linking) or `RUSTFLAGS=-Ctarget-feature=+crt-static` (for static linking) to environment variables.

Android -
Regardless of the OS, to link libsodium statically you also need to set `SODIUM_STATIC` environment variable.

# Join in

Join in
=======
File bugs in the issue tracker

Master git repository

git clone https://github.com/sodiumoxide/sodiumoxide.git

License
-------
## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

Expand Down
62 changes: 62 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
image: Visual Studio 2017

environment:
VCPKGRS_DYNAMIC: 1
LLVM_VERSION: 6.0.1
matrix:
- TARGET: x86_64-pc-windows-msvc
VCPKG_TARGET: x64-windows
RUST_CHANNEL: stable
LLVM_TARGET: win64

- TARGET: i686-pc-windows-msvc
VCPKG_TARGET: x86-windows
RUST_CHANNEL: stable
LLVM_TARGET: win32

- TARGET: x86_64-pc-windows-msvc
VCPKG_TARGET: x64-windows
RUST_CHANNEL: beta
LLVM_TARGET: win64

- TARGET: i686-pc-windows-msvc
VCPKG_TARGET: x86-windows
RUST_CHANNEL: beta
LLVM_TARGET: win32

- TARGET: x86_64-pc-windows-msvc
VCPKG_TARGET: x64-windows
RUST_CHANNEL: nightly
LLVM_TARGET: win64

- TARGET: i686-pc-windows-msvc
VCPKG_TARGET: x86-windows
RUST_CHANNEL: nightly
LLVM_TARGET: win32

install:
- ps: |
# Install Rust
appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
cmd.exe /c .\rustup-init.exe -y --default-host "$env:TARGET" --default-toolchain "$env:RUST_CHANNEL" 2`>`&1
$env:PATH += ";$env:USERPROFILE\.cargo\bin"
# Install LLVM
echo "LLVM-${env:LLVM_VERSION}-${env:LLVM_TARGET}"
appveyor-retry appveyor DownloadFile "http://releases.llvm.org/${env:LLVM_VERSION}/LLVM-${env:LLVM_VERSION}-${env:LLVM_TARGET}.exe" -FileName llvm-installer.exe
7z x llvm-installer.exe -oc:\llvm-binary
#$env:PATH += ";C:\llvm-binary\bin"
$env:LIBCLANG_PATH = "C:\llvm-binary\bin"
# Install libsodium
vcpkg install "libsodium:$env:VCPKG_TARGET"
cache:
- C:\Users\appveyor\.cargo\registry
- target

build_script:
- cmd: cargo build --target %TARGET%

test_script:
- cmd: cargo test --target %TARGET%
1 change: 1 addition & 0 deletions bors.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
status = [
"continuous-integration/travis-ci/push",
"continuous-integration/appveyor/branch"
]
3 changes: 3 additions & 0 deletions libsodium-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ travis-ci = { repository = "sodiumoxide/sodiumoxide" }
pkg-config = "^0.3.11"
bindgen = "0.38.0"

[target.'cfg(target_env = "msvc")'.build-dependencies]
vcpkg = "0.2"

[dependencies]
libc = { version = "^0.2.41" , default-features = false }

Expand Down
78 changes: 61 additions & 17 deletions libsodium-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@ extern crate pkg_config;
use std::env;
use std::path::PathBuf;

#[cfg(target_env = "msvc")]
extern crate vcpkg;

fn main() {
println!("cargo:rerun-if-env-changed=SODIUM_LIB_DIR");
println!("cargo:rerun-if-env-changed=SODIUM_INC_DIR");
println!("cargo:rerun-if-env-changed=SODIUM_STATIC");

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_env = "msvc") {
println!("cargo:rustc-link-lib={0}=libsodium", mode);
} else {
println!("cargo:rustc-link-lib={0}=sodium", mode);
}
let mode = match env::var_os("SODIUM_STATIC") {
Some(_) => "static",
None => "dylib",
};
if cfg!(target_env = "msvc") {
println!("cargo:rustc-link-lib={0}=libsodium", mode);
} else {
pkg_config::find_library("libsodium").unwrap();
println!("cargo:rustc-link-lib={0}=sodium", mode);
}

let include_dir = match env::var("SODIUM_INC_DIR") {
Ok(dir) => dir,
Err(_) => pkg_config::get_variable("libsodium", "includedir").unwrap()
};
let include_dir = find_libsodium();

let bindings = bindgen::Builder::default()
.header("sodium_wrapper.h")
Expand All @@ -45,3 +39,53 @@ fn main() {
.write_to_file(out_path.join("sodium_bindings.rs"))
.expect("Couldn't write bindings!");
}

#[cfg(target_env = "msvc")]
fn find_libsodium() -> String {
if let Ok(lib_dir) = env::var("SODIUM_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
} else {
if !try_vcpkg() {
panic!("Could not find libsodium on this system!")
}
}

let include_dir =
env::var("SODIUM_INC_DIR").ok()
.or_else(get_vcpkg_include_path).unwrap();

include_dir
}

#[cfg(not(target_env = "msvc"))]
fn find_libsodium() -> String {
if let Ok(lib_dir) = env::var("SODIUM_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
} else {
pkg_config::find_library("libsodium").unwrap();
}

let include_dir =
env::var("SODIUM_INC_DIR")
.or_else(|_| pkg_config::get_variable("libsodium", "includedir")).unwrap();

include_dir
}

#[cfg(target_env = "msvc")]
fn try_vcpkg() -> bool {
vcpkg::Config::new()
.lib_name("libsodium")
.probe("libsodium")
.is_ok() || vcpkg::probe_package("libsodium").is_ok()
}


#[cfg(target_env = "msvc")]
fn get_vcpkg_include_path() -> Option<String> {
let lib = vcpkg::probe_package("libsodium");
match lib {
Ok(lib) => lib.include_paths.get(0).and_then(|path| path.clone().into_os_string().into_string().ok()),
_ => None,
}
}

0 comments on commit 9661968

Please sign in to comment.