From 683195b6b4d5427ad6b0b1f8f3a6e3317f2de687 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Tue, 24 Mar 2026 00:37:25 +0100 Subject: [PATCH 1/3] Add Windows static CRT build support --- .github/workflows/rust.yml | 5 +++++ Cargo.toml | 1 + build.rs | 9 ++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index be728ab..54f3e66 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 --features crt-static - name: Build with system-installed HiGHS if: matrix.config.os != 'windows-latest' run: | diff --git a/Cargo.toml b/Cargo.toml index fa36482..7a82cc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ build = ["dep:cmake"] highs_release = [] ninja = [] libz = [] +crt-static = [] diff --git a/build.rs b/build.rs index ac8f558..d4ea06e 100644 --- a/build.rs +++ b/build.rs @@ -109,7 +109,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 cfg!(feature = "crt-static") { + "MultiThreaded" + } else { + "MultiThreadedDLL" + }, + ) .define("CMAKE_INTERPROCEDURAL_OPTIMIZATION", "FALSE") .define("ZLIB", if cfg!(feature = "libz") { "ON" } else { "OFF" }) .build(); From 2ecbf911efafc64d9eac07e8ac38d19aa2341fb9 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Tue, 24 Mar 2026 01:15:20 +0100 Subject: [PATCH 2/3] Fix crt-static handling for Windows builds --- .github/workflows/rust.yml | 2 +- Cargo.toml | 1 - build.rs | 25 +++++++++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 54f3e66..ffe9e11 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -41,7 +41,7 @@ jobs: if: matrix.config.os == 'windows-latest' env: RUSTFLAGS: -C target-feature=+crt-static - run: cargo test --features 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 7a82cc1..fa36482 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,3 @@ build = ["dep:cmake"] highs_release = [] ninja = [] libz = [] -crt-static = [] diff --git a/build.rs b/build.rs index d4ea06e..02b1d27 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"); @@ -111,7 +119,7 @@ fn build() -> bool { .define("BUILD_SHARED_LIBS", "OFF") .define( "CMAKE_MSVC_RUNTIME_LIBRARY", - if cfg!(feature = "crt-static") { + if crt_static { "MultiThreaded" } else { "MultiThreadedDLL" @@ -171,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", @@ -180,11 +192,20 @@ 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 { + panic!( + "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"); } From 78335b90db99727f3b29c59c61e36da9d5b18fb2 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Fri, 10 Apr 2026 16:05:04 +0200 Subject: [PATCH 3/3] use a warning https://github.com/rust-or/highs-sys/pull/47/changes#r2979077144 --- Cargo.toml | 2 +- build.rs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fa36482..d17a532 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "highs-sys" -version = "1.13.1" +version = "1.13.2" 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 02b1d27..e7fcb1a 100644 --- a/build.rs +++ b/build.rs @@ -199,10 +199,8 @@ fn main() { } if cfg!(feature = "discover") && crt_static { - panic!( - "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'." + 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'." ); }