diff --git a/.cargo/config.toml b/.cargo/config.toml index fac678a9..63947653 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,11 @@ [build] -rustflags = ["-C", "target-feature=+crt-static"] +rustflags = [ + "-C", + "target-feature=+crt-static", + + # Enable unstable cfg options: + # "--cfg", "wdk_build_unstable", + + # Unstable cfg options: + # "--cfg", "skip_umdf_static_crt_check", +] diff --git a/Cargo.lock b/Cargo.lock index 8ade2491..8f299e4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -730,6 +730,7 @@ dependencies = [ "bindgen", "camino", "cargo_metadata", + "cfg-if", "clap", "clap-cargo", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index b136f24f..0618b878 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ bindgen = "0.69.4" camino = "1.1.7" cargo_metadata = "0.18.1" cc = "1.1.0" +cfg-if = "1.0.0" clap = "4.5.9" clap-cargo = "0.14.0" itertools = "0.13.0" diff --git a/crates/wdk-build/Cargo.toml b/crates/wdk-build/Cargo.toml index c50d0867..b23ef44c 100644 --- a/crates/wdk-build/Cargo.toml +++ b/crates/wdk-build/Cargo.toml @@ -17,6 +17,7 @@ anyhow.workspace = true bindgen.workspace = true camino.workspace = true cargo_metadata.workspace = true +cfg-if.workspace = true clap = { workspace = true, features = ["derive"] } clap-cargo.workspace = true lazy_static.workspace = true @@ -33,6 +34,10 @@ windows = { workspace = true, features = [ [dev-dependencies] windows = { workspace = true, features = ["Win32_UI_Shell"] } +[lints.rust.unexpected_cfgs] +level = "warn" +check-cfg = ["cfg(wdk_build_unstable)", "cfg(skip_umdf_static_crt_check)"] + # Cannot inherit workspace lints since overriding them is not supported yet: https://github.com/rust-lang/cargo/issues/13157 # [lints] # workspace = true diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index fd16041d..17565d64 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -149,7 +149,7 @@ pub enum ConfigError { /// Error returned when the WDK version string does not match the expected /// format - #[error("The WDK version string provided ({version}) was not in a valid format.")] + #[error("the WDK version string provided ({version}) was not in a valid format")] WdkVersionStringFormatError { /// The incorrect WDK version string. version: String, @@ -173,8 +173,14 @@ pub enum ConfigError { /// Error returned when the c runtime is not configured to be statically /// linked #[error( - "the C runtime is not properly configured to be statically linked. This is required for building \ - WDK drivers. The recommended solution is to add the following snippiet to a `.config.toml` file: See https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes for more ways to enable static crt linkage." + "the C runtime is not properly configured to be statically linked. This is required for building WDK drivers. The recommended solution is to add the following snippet to a \ + `.config.toml` file: +[build] +rustflags = [\"-C\", \"target-feature=+crt-static\"] + +\ + See https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes for more ways \ + to enable static crt linkage" )] StaticCrtNotEnabled, @@ -658,14 +664,23 @@ impl Config { /// /// This function will return an error if: /// * any of the required WDK paths do not exist - /// * the C runtime is not configured to be statically linked + /// * the C runtime is not configured to be statically linked for a + /// kernel-mode driver /// /// # Panics /// /// Panics if the invoked from outside a Cargo build environment pub fn configure_binary_build(&self) -> Result<(), ConfigError> { if !Self::is_crt_static_linked() { - return Err(ConfigError::StaticCrtNotEnabled); + cfg_if::cfg_if! { + if #[cfg(all(wdk_build_unstable, skip_umdf_static_crt_check))] { + if !matches!(self.driver_config, DriverConfig::Umdf(_)) { + return Err(ConfigError::StaticCrtNotEnabled); + } + } else { + return Err(ConfigError::StaticCrtNotEnabled); + } + }; } let library_paths: Vec = self.get_library_paths()?; diff --git a/crates/wdk-build/src/utils.rs b/crates/wdk-build/src/utils.rs index 79c6c2be..5fbc0b0e 100644 --- a/crates/wdk-build/src/utils.rs +++ b/crates/wdk-build/src/utils.rs @@ -467,7 +467,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -475,7 +475,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -483,7 +483,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -491,7 +491,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -499,7 +499,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -507,7 +507,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); @@ -515,7 +515,7 @@ mod tests { assert_eq!( format!("{}", get_wdk_version_number(test_string).err().unwrap()), format!( - "The WDK version string provided ({}) was not in a valid format.", + "the WDK version string provided ({}) was not in a valid format", test_string ) ); diff --git a/examples/sample-kmdf-driver/Cargo.lock b/examples/sample-kmdf-driver/Cargo.lock index 94dc7081..5db7ac5a 100644 --- a/examples/sample-kmdf-driver/Cargo.lock +++ b/examples/sample-kmdf-driver/Cargo.lock @@ -725,6 +725,7 @@ dependencies = [ "bindgen", "camino", "cargo_metadata", + "cfg-if", "clap", "clap-cargo", "lazy_static", diff --git a/examples/sample-umdf-driver/Cargo.lock b/examples/sample-umdf-driver/Cargo.lock index 70b7f029..660bcbb0 100644 --- a/examples/sample-umdf-driver/Cargo.lock +++ b/examples/sample-umdf-driver/Cargo.lock @@ -713,6 +713,7 @@ dependencies = [ "bindgen", "camino", "cargo_metadata", + "cfg-if", "clap", "clap-cargo", "lazy_static", diff --git a/examples/sample-wdm-driver/Cargo.lock b/examples/sample-wdm-driver/Cargo.lock index 1725178b..95c7cd12 100644 --- a/examples/sample-wdm-driver/Cargo.lock +++ b/examples/sample-wdm-driver/Cargo.lock @@ -725,6 +725,7 @@ dependencies = [ "bindgen", "camino", "cargo_metadata", + "cfg-if", "clap", "clap-cargo", "lazy_static",