From a902deece63594269c0c84186df694e4d1909269 Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Thu, 16 May 2024 17:21:27 -0700 Subject: [PATCH 1/7] Naive refactor to run bindgen in parallel. --- crates/wdk-build/src/bindgen.rs | 4 ++-- crates/wdk-sys/build.rs | 38 +++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/crates/wdk-build/src/bindgen.rs b/crates/wdk-build/src/bindgen.rs index 51aae94b..48a5d615 100644 --- a/crates/wdk-build/src/bindgen.rs +++ b/crates/wdk-build/src/bindgen.rs @@ -15,7 +15,7 @@ pub trait BuilderExt { /// /// Implementation may return `wdk_build::ConfigError` if it fails to create /// a builder - fn wdk_default(c_header_files: Vec<&str>, config: Config) -> Result; + fn wdk_default(c_header_files: Vec<&str>, config: &Config) -> Result; } impl BuilderExt for Builder { @@ -26,7 +26,7 @@ impl BuilderExt for Builder { /// /// Will return `wdk_build::ConfigError` if any of the resolved include or /// library paths do not exist - fn wdk_default(c_header_files: Vec<&str>, config: Config) -> Result { + fn wdk_default(c_header_files: Vec<&str>, config: &Config) -> Result { let mut builder = Self::default(); for c_header in c_header_files { diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index efb569ab..4c619075 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -6,6 +6,7 @@ use std::{ env, path::{Path, PathBuf}, + thread::{self, JoinHandle}, }; use bindgen::CodegenConfig; @@ -21,7 +22,7 @@ use wdk_build::{BuilderExt, Config, ConfigError, DriverConfig, KMDFConfig}; // "2.0", "2.15", "2.17", "2.19", "2.21", "2.23", "2.25", "2.27", "2.31", // "2.33", ]; -fn generate_constants(out_path: &Path, config: Config) -> Result<(), ConfigError> { +fn generate_constants(out_path: &Path, config: &Config) -> Result<(), ConfigError> { Ok( bindgen::Builder::wdk_default(vec!["src/ntddk-input.h", "src/wdf-input.h"], config)? .with_codegen_config(CodegenConfig::VARS) @@ -31,7 +32,7 @@ fn generate_constants(out_path: &Path, config: Config) -> Result<(), ConfigError ) } -fn generate_types(out_path: &Path, config: Config) -> Result<(), ConfigError> { +fn generate_types(out_path: &Path, config: &Config) -> Result<(), ConfigError> { Ok( bindgen::Builder::wdk_default(vec!["src/ntddk-input.h", "src/wdf-input.h"], config)? .with_codegen_config(CodegenConfig::TYPES) @@ -41,7 +42,7 @@ fn generate_types(out_path: &Path, config: Config) -> Result<(), ConfigError> { ) } -fn generate_ntddk(out_path: &Path, config: Config) -> Result<(), ConfigError> { +fn generate_ntddk(out_path: &Path, config: &Config) -> Result<(), ConfigError> { Ok( bindgen::Builder::wdk_default(vec!["src/ntddk-input.h"], config)? .with_codegen_config((CodegenConfig::TYPES | CodegenConfig::VARS).complement()) @@ -51,7 +52,7 @@ fn generate_ntddk(out_path: &Path, config: Config) -> Result<(), ConfigError> { ) } -fn generate_wdf(out_path: &Path, config: Config) -> Result<(), ConfigError> { +fn generate_wdf(out_path: &Path, config: &Config) -> Result<(), ConfigError> { // As of NI WDK, this may generate an empty file due to no non-type and non-var // items in the wdf headers(i.e. functions are all inlined). This step is // intentionally left here in case older WDKs have non-inlined functions or new @@ -135,12 +136,31 @@ fn main() -> anyhow::Result<()> { ), ]; + let export_config = config.clone(); + let mut handles = Vec::>::new(); for out_path in out_paths { - generate_constants(&out_path, config.clone())?; - generate_types(&out_path, config.clone())?; - generate_ntddk(&out_path, config.clone())?; - generate_wdf(&out_path, config.clone())?; + let temp_config = config.clone(); + let handle = thread::spawn(move || { + if let Err(err) = generate_constants(&out_path, &temp_config) { + return Err(err); + } + if let Err(err) = generate_types(&out_path, &temp_config) { + return Err(err); + } + if let Err(err) = generate_ntddk(&out_path, &temp_config) { + return Err(err); + } + if let Err(err) = generate_wdf(&out_path, &temp_config) { + return Err(err); + } + Ok(()) + }); + handles.push(handle); } - Ok(config.export_config()?) + for handle in handles { + handle.join().unwrap(); + } + + Ok(export_config.export_config()?) } From 82d1e417e549172b30f5c1772d4ba388ea1a4546 Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Thu, 16 May 2024 17:32:57 -0700 Subject: [PATCH 2/7] Additional cleanup --- crates/wdk-sys/build.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 4c619075..1b25bd0d 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -136,11 +136,10 @@ fn main() -> anyhow::Result<()> { ), ]; - let export_config = config.clone(); - let mut handles = Vec::>::new(); + let mut handles = Vec::>>::new(); for out_path in out_paths { let temp_config = config.clone(); - let handle = thread::spawn(move || { + let handle : JoinHandle> = thread::spawn(move || { if let Err(err) = generate_constants(&out_path, &temp_config) { return Err(err); } @@ -159,8 +158,10 @@ fn main() -> anyhow::Result<()> { } for handle in handles { - handle.join().unwrap(); + if let Err(e) = handle.join().unwrap() { + return Err(e.into()); + } } - Ok(export_config.export_config()?) + Ok(config.export_config()?) } From a0c770bb82f18aa9f6039e3dc464381534e446ce Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Thu, 16 May 2024 17:45:55 -0700 Subject: [PATCH 3/7] Clippy+fmt fixes --- crates/wdk-sys/build.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 1b25bd0d..2952a43b 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -139,19 +139,11 @@ fn main() -> anyhow::Result<()> { let mut handles = Vec::>>::new(); for out_path in out_paths { let temp_config = config.clone(); - let handle : JoinHandle> = thread::spawn(move || { - if let Err(err) = generate_constants(&out_path, &temp_config) { - return Err(err); - } - if let Err(err) = generate_types(&out_path, &temp_config) { - return Err(err); - } - if let Err(err) = generate_ntddk(&out_path, &temp_config) { - return Err(err); - } - if let Err(err) = generate_wdf(&out_path, &temp_config) { - return Err(err); - } + let handle: JoinHandle> = thread::spawn(move || { + generate_constants(&out_path, &temp_config)?; + generate_types(&out_path, &temp_config)?; + generate_ntddk(&out_path, &temp_config)?; + generate_wdf(&out_path, &temp_config)?; Ok(()) }); handles.push(handle); @@ -159,8 +151,8 @@ fn main() -> anyhow::Result<()> { for handle in handles { if let Err(e) = handle.join().unwrap() { - return Err(e.into()); - } + return Err(e.into()); + } } Ok(config.export_config()?) From 7fb4a832e794829de136010c058464f691303d5e Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Thu, 16 May 2024 23:04:00 -0700 Subject: [PATCH 4/7] More parallelization This is a little silly with all the clones, but it works. --- crates/wdk-sys/build.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 2952a43b..731b2744 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -139,11 +139,30 @@ fn main() -> anyhow::Result<()> { let mut handles = Vec::>>::new(); for out_path in out_paths { let temp_config = config.clone(); + let temp_path = out_path.clone(); let handle: JoinHandle> = thread::spawn(move || { - generate_constants(&out_path, &temp_config)?; - generate_types(&out_path, &temp_config)?; - generate_ntddk(&out_path, &temp_config)?; - generate_wdf(&out_path, &temp_config)?; + generate_constants(&temp_path, &temp_config)?; + Ok(()) + }); + handles.push(handle); + let temp_config = config.clone(); + let temp_path = out_path.clone(); + let handle: JoinHandle> = thread::spawn(move || { + generate_types(&temp_path, &temp_config)?; + Ok(()) + }); + handles.push(handle); + let temp_config = config.clone(); + let temp_path = out_path.clone(); + let handle: JoinHandle> = thread::spawn(move || { + generate_ntddk(&temp_path, &temp_config)?; + Ok(()) + }); + handles.push(handle); + let temp_config = config.clone(); + let temp_path = out_path.clone(); + let handle: JoinHandle> = thread::spawn(move || { + generate_wdf(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); From 43cccb41a6f29067c9b3d7d3fc505cafcfb765bc Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Mon, 20 May 2024 00:12:41 -0500 Subject: [PATCH 5/7] Update to use Arc --- crates/wdk-sys/build.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 731b2744..10e08bc0 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -7,6 +7,7 @@ use std::{ env, path::{Path, PathBuf}, thread::{self, JoinHandle}, + sync::Arc, }; use bindgen::CodegenConfig; @@ -137,30 +138,32 @@ fn main() -> anyhow::Result<()> { ]; let mut handles = Vec::>>::new(); + let config_arc = Arc::new(config); for out_path in out_paths { - let temp_config = config.clone(); - let temp_path = out_path.clone(); + let path_arc = Arc::new(out_path); + let temp_path = path_arc.clone(); + let temp_config = config_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_constants(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); - let temp_config = config.clone(); - let temp_path = out_path.clone(); + let temp_config = config_arc.clone(); + let temp_path = path_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_types(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); - let temp_config = config.clone(); - let temp_path = out_path.clone(); + let temp_config = config_arc.clone(); + let temp_path = path_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_ntddk(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); - let temp_config = config.clone(); - let temp_path = out_path.clone(); + let temp_config = config_arc.clone(); + let temp_path = path_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_wdf(&temp_path, &temp_config)?; Ok(()) @@ -174,5 +177,5 @@ fn main() -> anyhow::Result<()> { } } - Ok(config.export_config()?) + Ok(config_arc.export_config()?) } From 345fec1ca7edcc10ef8cdeffab75e1c58c398c5b Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Mon, 20 May 2024 00:23:05 -0500 Subject: [PATCH 6/7] Formatting fixes --- crates/wdk-sys/build.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 10e08bc0..2967f598 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -6,8 +6,8 @@ use std::{ env, path::{Path, PathBuf}, - thread::{self, JoinHandle}, sync::Arc, + thread::{self, JoinHandle}, }; use bindgen::CodegenConfig; @@ -148,22 +148,22 @@ fn main() -> anyhow::Result<()> { Ok(()) }); handles.push(handle); - let temp_config = config_arc.clone(); let temp_path = path_arc.clone(); + let temp_config = config_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_types(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); - let temp_config = config_arc.clone(); let temp_path = path_arc.clone(); + let temp_config = config_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_ntddk(&temp_path, &temp_config)?; Ok(()) }); handles.push(handle); - let temp_config = config_arc.clone(); let temp_path = path_arc.clone(); + let temp_config = config_arc.clone(); let handle: JoinHandle> = thread::spawn(move || { generate_wdf(&temp_path, &temp_config)?; Ok(()) From 49016b04c07a8d4fd05522ba277555306ae94b7c Mon Sep 17 00:00:00 2001 From: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com> Date: Mon, 20 May 2024 14:40:04 -0500 Subject: [PATCH 7/7] Reduce code duplication --- crates/wdk-sys/build.rs | 47 +++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/crates/wdk-sys/build.rs b/crates/wdk-sys/build.rs index 2967f598..3721bf27 100644 --- a/crates/wdk-sys/build.rs +++ b/crates/wdk-sys/build.rs @@ -69,6 +69,15 @@ fn generate_wdf(out_path: &Path, config: &Config) -> Result<(), ConfigError> { ) } +type GenerateFn = fn(&Path, &Config) -> Result<(), ConfigError>; + +const GENERATE_FUNCTIONS: [GenerateFn; 4] = [ + generate_constants, + generate_types, + generate_ntddk, + generate_wdf, +]; + fn main() -> anyhow::Result<()> { let tracing_filter = EnvFilter::default() // Show errors and warnings by default @@ -139,36 +148,18 @@ fn main() -> anyhow::Result<()> { let mut handles = Vec::>>::new(); let config_arc = Arc::new(config); + for out_path in out_paths { let path_arc = Arc::new(out_path); - let temp_path = path_arc.clone(); - let temp_config = config_arc.clone(); - let handle: JoinHandle> = thread::spawn(move || { - generate_constants(&temp_path, &temp_config)?; - Ok(()) - }); - handles.push(handle); - let temp_path = path_arc.clone(); - let temp_config = config_arc.clone(); - let handle: JoinHandle> = thread::spawn(move || { - generate_types(&temp_path, &temp_config)?; - Ok(()) - }); - handles.push(handle); - let temp_path = path_arc.clone(); - let temp_config = config_arc.clone(); - let handle: JoinHandle> = thread::spawn(move || { - generate_ntddk(&temp_path, &temp_config)?; - Ok(()) - }); - handles.push(handle); - let temp_path = path_arc.clone(); - let temp_config = config_arc.clone(); - let handle: JoinHandle> = thread::spawn(move || { - generate_wdf(&temp_path, &temp_config)?; - Ok(()) - }); - handles.push(handle); + for generate_function in GENERATE_FUNCTIONS { + let temp_path = path_arc.clone(); + let temp_config = config_arc.clone(); + let handle: JoinHandle> = thread::spawn(move || { + generate_function(&temp_path, &temp_config)?; + Ok(()) + }); + handles.push(handle); + } } for handle in handles {