diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index be728ab..ffe9e11 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -37,6 +37,11 @@ jobs: run: cargo build - name: Run tests run: cargo test + - name: Run tests with static CRT + if: matrix.config.os == 'windows-latest' + env: + RUSTFLAGS: -C target-feature=+crt-static + run: cargo test - name: Build with system-installed HiGHS if: matrix.config.os != 'windows-latest' run: | diff --git a/Cargo.toml b/Cargo.toml index e66d16a..df7a50b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "highs-sys" -version = "1.14.0" +version = "1.14.1" authors = ["Ophir LOJKINE"] edition = "2018" description = "Rust binding for the HiGHS linear programming solver. See http://highs.dev." diff --git a/build.rs b/build.rs index ac8f558..e7fcb1a 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,13 @@ use std::env; use std::path::{Path, PathBuf}; +fn target_has_feature(feature: &str) -> bool { + env::var("CARGO_CFG_TARGET_FEATURE") + .unwrap_or_default() + .split(',') + .any(|enabled_feature| enabled_feature == feature) +} + /// Used to exclude autogenerated files in the output dir from `cargo:rerun-if-changed` directives. #[derive(Debug)] pub struct CustomCargoCallbacks { @@ -86,6 +93,7 @@ fn build() -> bool { let target = env::var("TARGET").unwrap(); let emscripten = target.contains("emscripten"); let mut dst = Config::new("HiGHS"); + let crt_static = target_has_feature("crt-static"); if cfg!(feature = "ninja") { dst.generator("Ninja"); @@ -109,7 +117,14 @@ fn build() -> bool { let dst = dst .define("FAST_BUILD", "ON") .define("BUILD_SHARED_LIBS", "OFF") - .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL") + .define( + "CMAKE_MSVC_RUNTIME_LIBRARY", + if crt_static { + "MultiThreaded" + } else { + "MultiThreadedDLL" + }, + ) .define("CMAKE_INTERPROCEDURAL_OPTIMIZATION", "FALSE") .define("ZLIB", if cfg!(feature = "libz") { "ON" } else { "OFF" }) .build(); @@ -164,6 +179,10 @@ fn discover() -> bool { } fn main() { + println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_FEATURE"); + + let crt_static = target_has_feature("crt-static"); + if cfg!(all( any( feature = "highs_release", @@ -173,11 +192,18 @@ fn main() { not(feature = "build") )) { panic!( - "You have enabled features that control how HiGHS is built, but have not enabled the 'build' feature.\n\ + "You have enabled features that control how HiGHS is built, but have not enabled the 'build' feature. +\ Thus, your features will never have any effect. Please enable the 'build' feature on highs-sys if you want to build HiGHS or disable the 'libz', 'ninja' and 'highs_release' features." ); } + if cfg!(feature = "discover") && crt_static { + println!( + "cargo::warning=You have enabled Rust's 'crt-static' target feature, but also enabled the 'discover' feature. Discovering a system-installed HiGHS bypasses the bundled build, so highs-sys cannot ensure that HiGHS uses the same MSVC runtime. Please disable 'discover' when using '-C target-feature=+crt-static'." + ); + } + if !discover() && !build() { panic!("Could neither discover nor build HiGHS"); }