Skip to content

Commit

Permalink
Merge pull request #18 from analyzeDFIR/ms-build-patch
Browse files Browse the repository at this point in the history
Support static compilation on Windows in lz4-sys crate
  • Loading branch information
pmarks authored Mar 5, 2022
2 parents 59cc025 + c3f063e commit 807a82b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
38 changes: 35 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
41 changes: 28 additions & 13 deletions lz4-sys/build.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -23,15 +23,22 @@ fn run() -> Result<(), Box<dyn Error>> {
.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");

Expand All @@ -42,16 +49,24 @@ fn run() -> Result<(), Box<dyn Error>> {
.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<String, String> {
env::var(variable).map_err(|err| format!("reading {} environment variable: {}", variable, err))
}

0 comments on commit 807a82b

Please sign in to comment.