Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support static compilation on Windows in lz4-sys crate #18

Merged
merged 3 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}