From 4c80b12df09d0d7c10efbb82442d3968d6875201 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Thu, 12 Sep 2024 14:04:44 -0700 Subject: [PATCH 1/5] feat: allow hybrid crt linking for UMDF drivers --- crates/wdk-build/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index 64b1a136..5853ca16 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -150,7 +150,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, @@ -174,8 +174,7 @@ 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 kernel mode WDK drivers. The recommended solution is to add the following snippiet to a `.config.toml` file: [build]\n rustflags = [\"-C\", \"target-feature=+crt-static\"]\n\nSee https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes for more ways to enable static crt linkage." )] StaticCrtNotEnabled, @@ -659,13 +658,14 @@ 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() { + if !Self::is_crt_static_linked() && matches!(self.driver_config, DriverConfig::Umdf(_)) { return Err(ConfigError::StaticCrtNotEnabled); } From 131dfd452631d10e33bc4279c28a34fee68518ab Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Fri, 13 Sep 2024 10:38:04 -0700 Subject: [PATCH 2/5] improve implementation --- .cargo/config.toml | 11 ++++++++++- Cargo.lock | 1 + Cargo.toml | 1 + crates/wdk-build/Cargo.toml | 5 +++++ crates/wdk-build/src/lib.rs | 21 ++++++++++++++++++--- examples/sample-umdf-driver/Cargo.lock | 1 + 6 files changed, 36 insertions(+), 4 deletions(-) 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 5853ca16..c5d500b7 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -174,7 +174,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 kernel mode WDK drivers. The recommended solution is to add the following snippiet to a `.config.toml` file: [build]\n rustflags = [\"-C\", \"target-feature=+crt-static\"]\n\nSee 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, @@ -665,8 +672,16 @@ impl Config { /// /// Panics if the invoked from outside a Cargo build environment pub fn configure_binary_build(&self) -> Result<(), ConfigError> { - if !Self::is_crt_static_linked() && matches!(self.driver_config, DriverConfig::Umdf(_)) { - return Err(ConfigError::StaticCrtNotEnabled); + if !Self::is_crt_static_linked() { + 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/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", From a600231276ffa70b8073cbcc5042805752d6b8b5 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Fri, 13 Sep 2024 10:46:53 -0700 Subject: [PATCH 3/5] update cargo locks --- examples/sample-kmdf-driver/Cargo.lock | 1 + examples/sample-wdm-driver/Cargo.lock | 1 + 2 files changed, 2 insertions(+) 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-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", From 760b9742ad26cd17c74e8ae0bd633df8098fc538 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Fri, 13 Sep 2024 12:31:31 -0700 Subject: [PATCH 4/5] fix error normalization --- crates/wdk-build/src/utils.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/wdk-build/src/utils.rs b/crates/wdk-build/src/utils.rs index c75cd7a0..44ad4c1f 100644 --- a/crates/wdk-build/src/utils.rs +++ b/crates/wdk-build/src/utils.rs @@ -464,7 +464,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 ) ); @@ -472,7 +472,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 ) ); @@ -480,7 +480,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 ) ); @@ -488,7 +488,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 ) ); @@ -496,7 +496,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 ) ); @@ -504,7 +504,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 ) ); @@ -512,7 +512,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 ) ); From bc4c0b38a0b069df361379875a600b7c4147d029 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Fri, 13 Sep 2024 12:33:38 -0700 Subject: [PATCH 5/5] normalize errors per C-GOOD-ERR --- crates/wdk-build/src/lib.rs | 4 ++-- crates/wdk-build/src/utils.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index c5d500b7..efcbdd06 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -150,7 +150,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, @@ -181,7 +181,7 @@ 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." + to enable static crt linkage" )] StaticCrtNotEnabled, diff --git a/crates/wdk-build/src/utils.rs b/crates/wdk-build/src/utils.rs index 44ad4c1f..194f0793 100644 --- a/crates/wdk-build/src/utils.rs +++ b/crates/wdk-build/src/utils.rs @@ -464,7 +464,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 ) ); @@ -472,7 +472,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 ) ); @@ -480,7 +480,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 ) ); @@ -488,7 +488,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 ) ); @@ -496,7 +496,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 ) ); @@ -504,7 +504,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 ) ); @@ -512,7 +512,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 ) );