diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 1b9f5400c..d09226570 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,16 +10,16 @@ env: CARGO_INCREMENTAL: 0 jobs: - test: + test-nix: strategy: matrix: - platform: [ubuntu-latest, macos-latest, windows-latest] + platform: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.platform }} steps: - name: Setup Rust uses: actions-rs/toolchain@v1 with: - toolchain: 1.43.0 + toolchain: 1.53.0 override: true components: rustfmt - name: Checkout @@ -32,3 +32,35 @@ jobs: run: cargo build --release - name: unit tests run: cargo test -- --nocapture + test-windows: + runs-on: windows-latest + steps: + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.53.0 + target: i686-pc-windows-msvc + override: true + components: rustfmt + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: true + - name: Check Rust formatting + run: cargo fmt -- --check + - name: build 64-bit + run: cargo build --release + - name: build 32-bit + run: cargo build --release --target i686-pc-windows-msvc + - name: build 64-bit static + env: + CRT_STATIC: "true" + RUSTFLAGS: "-C target-feature=+crt-static" + run: cargo build --release + - name: build 32-bit static + env: + CRT_STATIC: "true" + RUSTFLAGS: "-C target-feature=+crt-static" + run: cargo build --release --target i686-pc-windows-msvc + - name: unit tests + run: cargo test -- --nocapture diff --git a/lz4-sys/build.rs b/lz4-sys/build.rs index bd591adaa..8e8e01935 100644 --- a/lz4-sys/build.rs +++ b/lz4-sys/build.rs @@ -1,8 +1,8 @@ extern crate cc; -use std::{env, fs, process}; use std::error::Error; use std::path::PathBuf; +use std::{env, fs, process}; fn main() { match run() { @@ -23,15 +23,22 @@ fn run() -> Result<(), Box> { .file("liblz4/lib/xxhash.c") // We always compile the C with optimization, because otherwise it is 20x slower. .opt_level(3); - match env::var("TARGET") - .map_err(|err| format!("reading TARGET environment variable: {}", err))? - .as_str() - { - "i686-pc-windows-gnu" => { - compiler - .flag("-fno-tree-vectorize"); - }, - _ => {} + + let target = get_from_env("TARGET")?; + if target.contains("windows") { + if target == "i686-pc-windows-gnu" { + // Disable auto-vectorization for 32-bit MinGW target. + compiler.flag("-fno-tree-vectorize"); + } + if let Ok(value) = get_from_env("CRT_STATIC") { + if value.to_uppercase() == "TRUE" { + // Must supply the /MT compiler flag to use the multi-threaded, static VCRUNTIME library + // when building on Windows. Cargo does not pass RUSTFLAGS to build scripts + // (see: https://github.com/rust-lang/cargo/issues/4423) so we must use a custom env + // variable "CRT_STATIC." + compiler.static_crt(true); + } + } } compiler.compile("liblz4.a"); @@ -42,16 +49,24 @@ fn run() -> Result<(), Box> { .map_err(|err| format!("creating directory {}: {}", include.display(), err))?; for e in fs::read_dir(&src)? { let e = e?; - let utf8_file_name = e.file_name().into_string() + let utf8_file_name = e + .file_name() + .into_string() .map_err(|_| format!("unable to convert file name {:?} to UTF-8", e.file_name()))?; if utf8_file_name.ends_with(".h") { let from = e.path(); let to = include.join(e.file_name()); - fs::copy(&from, &to) - .map_err(|err| format!("copying {} to {}: {}", from.display(), to.display(), err))?; + fs::copy(&from, &to).map_err(|err| { + format!("copying {} to {}: {}", from.display(), to.display(), err) + })?; } } println!("cargo:root={}", dst.display()); Ok(()) } + +/// Try to read environment variable as `String` +fn get_from_env(variable: &str) -> Result { + env::var(variable).map_err(|err| format!("reading {} environment variable: {}", variable, err)) +}