From a4e08b3e6eb9e4e404f3b8c59b373fced84b4e42 Mon Sep 17 00:00:00 2001 From: analyzeDFIR Date: Sat, 3 Jul 2021 17:08:38 -0500 Subject: [PATCH 1/3] Add support for static compilation on Windows in build script for lz4-sys crate --- lz4-sys/build.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lz4-sys/build.rs b/lz4-sys/build.rs index bd591adaa..86c15511c 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,19 @@ 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"); + } else if get_from_env("CRT_STATIC")?.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 +46,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)) +} From 358e6c760d36aa425d0bf8b021dca3e653dbe316 Mon Sep 17 00:00:00 2001 From: analyzeDFIR Date: Sat, 3 Jul 2021 17:10:00 -0500 Subject: [PATCH 2/3] Update CI workflow to build static and dynamic 32 and 64-bit on Windows with latest version of Rust (1.53.0) --- .github/workflows/test.yaml | 38 ++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) 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 From c3f063ef3ef326879df02a65183c1a18f3e3ff9a Mon Sep 17 00:00:00 2001 From: analyzeDFIR Date: Mon, 5 Jul 2021 23:34:43 -0500 Subject: [PATCH 3/3] CRT_STATIC should be an optional env variable --- lz4-sys/build.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lz4-sys/build.rs b/lz4-sys/build.rs index 86c15511c..8e8e01935 100644 --- a/lz4-sys/build.rs +++ b/lz4-sys/build.rs @@ -29,12 +29,15 @@ fn run() -> Result<(), Box> { if target == "i686-pc-windows-gnu" { // Disable auto-vectorization for 32-bit MinGW target. compiler.flag("-fno-tree-vectorize"); - } else if get_from_env("CRT_STATIC")?.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); + } + 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");