From ea1d68ddd9fd64aeacc55cda0330dfbea4d7d23c Mon Sep 17 00:00:00 2001 From: CHMANG Date: Wed, 23 Oct 2024 17:01:12 -0700 Subject: [PATCH 01/17] Create resource_compiler.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compiler.rs | 204 ++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 crates/wdk-build/src/resource_compiler.rs diff --git a/crates/wdk-build/src/resource_compiler.rs b/crates/wdk-build/src/resource_compiler.rs new file mode 100644 index 00000000..1dd0c2c0 --- /dev/null +++ b/crates/wdk-build/src/resource_compiler.rs @@ -0,0 +1,204 @@ +use std::env; +use std::fs; +use std::path::PathBuf; +use std::process::Command; +use cargo_metadata::MetadataCommand; + +pub fn generate_and_compile_rcfile(include_paths: Vec) { + // Initialize an empty vector to store modified include arguments + let mut includeargs: Vec = Vec::new(); + + // Iterate over each include path + for include_path in include_paths { + // Convert the include path to a string + if let Some(include_str) = include_path.to_str() { + // Append "/I" and the include path to the modified vector + includeargs.push("/I".to_string()); + includeargs.push(include_str.to_string()); + } else { + println!("Non-Unicode path is not supported: {:?}", include_path); + } + } + + // Print the modified include arguments + println!("IncludeArgs: {:?}", includeargs); + + let (companyname, copyright, productname) = get_packagemetadatadetails(); + let (productversion, description, fileversion) = get_packagedetails(); + getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, &includeargs); +} +fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6:String, s7: &Vec) { + println!("Set and create rc file... "); + let rcfile_path = "resources.rc"; + if fs::metadata(&rcfile_path).is_ok() { + // File exists, so let's remove it + if let Err(err) = fs::remove_file(&rcfile_path) { + eprintln!("Error deleting file: {}", err); + } else { + println!("File deleted successfully!"); + } + } else { + println!("File does not exist."); + } + + let ver_filetype = "VFT_DRV"; + let ver_filesubtype = "VFT2_DRV_SYSTEM"; + let ver_internalname = "sample-kmdf-driver.sys"; + let ver_originalfilename = "VER_INTERNALNAME_STR"; + + // Create the RC file content + let rc_content = format!( + r#"#include +#include +#define VER_FILETYPE {file_type} +#define VER_FILESUBTYPE {file_subtype} +#define VER_INTERNALNAME_STR "{internal_name}" +#define VER_ORIGINALFILENAME_STR {original_filename} + +#undef VER_FILEDESCRIPTION_STR +#define VER_FILEDESCRIPTION_STR "{s5}" + +#undef VER_PRODUCTNAME_STR +#define VER_PRODUCTNAME_STR VER_FILEDESCRIPTION_STR + +#define VER_FILEVERSION {s6},0 +#define VER_FILEVERSION_STR "{s4}.0" + +#undef VER_PRODUCTVERSION +#define VER_PRODUCTVERSION VER_FILEVERSION + +#undef VER_PRODUCTVERSION_STR +#define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR + +#define VER_LEGALCOPYRIGHT_STR {s2} +#ifdef VER_COMPANYNAME_STR + +#undef VER_COMPANYNAME_STR +#define VER_COMPANYNAME_STR {s1} +#endif + +#undef VER_PRODUCTNAME_STR +#define VER_PRODUCTNAME_STR {s3} + +#include "common.ver""#, + file_type = ver_filetype, + file_subtype = ver_filesubtype, + internal_name = ver_internalname, + original_filename = ver_originalfilename + ); + + // Print the RC file content + //println!("{}", env!("CARGO_PKG_VERSION")); + //println!("{}", env!("CARGO_PKG_METADATA.WDK")); + //println!("cargopkgcrate:{}", env!("CARGO_PKG_CRATE_NAME")); + + + std::fs::write("resources.rc", rc_content).expect("Unable to write RC file"); + invoke_rc(&s7); +} + +fn invoke_rc(s7: &Vec) { + // Replace with the actual path to rc.exe + let rc_path = env::var("PATH_TO_RC").unwrap_or_else(|_| { + // Default path if environment variable is not set + r#"D:\EWDK\content\Program Files\Windows Kits\10\bin\10.0.22621.0\x86\rc.exe"#.to_string() + }); + + println!("Using rc.exe path: {}", rc_path); + + // Replace "resource.rc" with the name of your resource script file + let resource_script = "resources.rc"; + //for value in s7.into_iter() { + // println!("Got: {}", value.to_string()); + //} + //println!("include args: {:?}", s7); + let um_path = r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#.to_string(); + let include_string = "/I"; + //println!("Modified Path: {}", modified_path); + // let s8 = "/I r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#".to_string(); + let mut command = Command::new(&rc_path); + command.args(s7).arg(include_string).arg(um_path).arg(resource_script); + println!("Command executed: {:?}", command); + + //let status = Command::new(&rc_path).args(s7).arg(resource_script).status(); + let status = command.status(); + + //let mut command = Command::new(&rc_path); + //command.args(s7).arg(resource_script); + //println!("Command executed: {:?}", command); + + match status { + Ok(exit_status) => { + if exit_status.success() { + println!("Resource compilation successful!"); + } else { + println!("Resource compilation failed."); + std::process::exit(1); // Exit with a non-zero status code + } + } + Err(err) => { + eprintln!("Error running rc.exe: {}", err); + std::process::exit(1); // Exit with a non-zero status code + } + } +} +fn get_packagemetadatadetails() -> (String, String, String) { + // Run the 'cargo metadata' command and capture its output + let path = env::var("CARGO_MANIFEST_DIR").unwrap(); + let meta = MetadataCommand::new() + .manifest_path("./Cargo.toml") + .current_dir(&path) + .exec() + .unwrap(); + let root = meta.root_package().unwrap(); + let metadata = &root.metadata; + + // Extract metadata values with default fallbacks + + let companyname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("companyname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Company name not found in metadata".to_string()); + let copyrightname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("copyright")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); + let productname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("productname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Product name not found in metadata".to_string()); + + (companyname, copyrightname, productname) +} + +fn get_packagedetails() -> (String, String, String) { + let mut fileversion = String::new(); + let mut description = String::new(); + let mut productversion = String::new(); + + match fs::read_to_string("Cargo.toml") { + Ok(text1) => { + for line in text1.lines() { + if line.starts_with("version") { + let start = line.find('"').unwrap_or(0) + 1; + let end = line.rfind('"').unwrap_or(0); + productversion = line[start..end].to_string(); + let versionparts: Vec<&str> = productversion.split('.').collect(); + fileversion = versionparts.join(","); + } + if line.starts_with("description") { + let start = line.find('"').unwrap_or(0) + 1; + let end = line.rfind('"').unwrap_or(0); + description = line[start..end].to_string(); + } + } + } + Err(_) => { + eprintln!("Error reading Cargo.toml"); + } + } + (productversion, description, fileversion) +} From 8998bfe07143cdb6e10fb675fbb306243f85a14e Mon Sep 17 00:00:00 2001 From: CHMANG Date: Wed, 23 Oct 2024 17:07:05 -0700 Subject: [PATCH 02/17] Update lib.rs Signed-off-by: CHMANG --- crates/wdk-build/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index db2e8aa8..a9c058f4 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -689,7 +689,7 @@ impl Config { for path in library_paths { println!("cargo::rustc-link-search={}", path.display()); } - + resource_compile::generate_and_compile_rcfile(self.get_include_paths()?); match &self.driver_config { DriverConfig::Wdm => { // Emit WDM-specific libraries to link to From 00ed121d9fa66589b492aa2fad8eaf1c9c5fa067 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Wed, 23 Oct 2024 17:07:40 -0700 Subject: [PATCH 03/17] Update Cargo.toml Signed-off-by: CHMANG --- crates/wdk-build/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/wdk-build/Cargo.toml b/crates/wdk-build/Cargo.toml index ad92f32f..fe030bbc 100644 --- a/crates/wdk-build/Cargo.toml +++ b/crates/wdk-build/Cargo.toml @@ -11,6 +11,8 @@ categories = ["development-tools::build-utils", "development-tools::ffi"] [build-dependencies] rustversion.workspace = true +serde = "1.0" +serde_json = "1.0" [dependencies] anyhow.workspace = true From bddc2c5b572d8295326a6828cecf20bf55edd01b Mon Sep 17 00:00:00 2001 From: CHMANG Date: Wed, 23 Oct 2024 17:08:10 -0700 Subject: [PATCH 04/17] Update Cargo.toml Signed-off-by: CHMANG --- examples/sample-wdm-driver/Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/sample-wdm-driver/Cargo.toml b/examples/sample-wdm-driver/Cargo.toml index 529876d9..756e90c7 100644 --- a/examples/sample-wdm-driver/Cargo.toml +++ b/examples/sample-wdm-driver/Cargo.toml @@ -12,6 +12,9 @@ publish = false [package.metadata.wdk.driver-model] driver-type = "WDM" +companyname = "Microsoft Corporation" +copyright = "(C) 2024 Microsoft. All rights reserved." +productname = "Surface" [lib] crate-type = ["cdylib"] From f1b8451694eb619877045ac6729b8c93307b5101 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Thu, 24 Oct 2024 07:26:10 -0700 Subject: [PATCH 05/17] Update lib.rs Signed-off-by: CHMANG --- crates/wdk-build/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index a9c058f4..3140ecd3 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -22,6 +22,8 @@ mod utils; mod bindgen; +mod resource_compile; + use std::{env, path::PathBuf}; use cargo_metadata::MetadataCommand; From 651df252e5378fed9a4cdec408ec21079664a807 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Thu, 24 Oct 2024 07:37:50 -0700 Subject: [PATCH 06/17] Create resource_compile.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 204 +++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 crates/wdk-build/src/resource_compile.rs diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs new file mode 100644 index 00000000..1dd0c2c0 --- /dev/null +++ b/crates/wdk-build/src/resource_compile.rs @@ -0,0 +1,204 @@ +use std::env; +use std::fs; +use std::path::PathBuf; +use std::process::Command; +use cargo_metadata::MetadataCommand; + +pub fn generate_and_compile_rcfile(include_paths: Vec) { + // Initialize an empty vector to store modified include arguments + let mut includeargs: Vec = Vec::new(); + + // Iterate over each include path + for include_path in include_paths { + // Convert the include path to a string + if let Some(include_str) = include_path.to_str() { + // Append "/I" and the include path to the modified vector + includeargs.push("/I".to_string()); + includeargs.push(include_str.to_string()); + } else { + println!("Non-Unicode path is not supported: {:?}", include_path); + } + } + + // Print the modified include arguments + println!("IncludeArgs: {:?}", includeargs); + + let (companyname, copyright, productname) = get_packagemetadatadetails(); + let (productversion, description, fileversion) = get_packagedetails(); + getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, &includeargs); +} +fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6:String, s7: &Vec) { + println!("Set and create rc file... "); + let rcfile_path = "resources.rc"; + if fs::metadata(&rcfile_path).is_ok() { + // File exists, so let's remove it + if let Err(err) = fs::remove_file(&rcfile_path) { + eprintln!("Error deleting file: {}", err); + } else { + println!("File deleted successfully!"); + } + } else { + println!("File does not exist."); + } + + let ver_filetype = "VFT_DRV"; + let ver_filesubtype = "VFT2_DRV_SYSTEM"; + let ver_internalname = "sample-kmdf-driver.sys"; + let ver_originalfilename = "VER_INTERNALNAME_STR"; + + // Create the RC file content + let rc_content = format!( + r#"#include +#include +#define VER_FILETYPE {file_type} +#define VER_FILESUBTYPE {file_subtype} +#define VER_INTERNALNAME_STR "{internal_name}" +#define VER_ORIGINALFILENAME_STR {original_filename} + +#undef VER_FILEDESCRIPTION_STR +#define VER_FILEDESCRIPTION_STR "{s5}" + +#undef VER_PRODUCTNAME_STR +#define VER_PRODUCTNAME_STR VER_FILEDESCRIPTION_STR + +#define VER_FILEVERSION {s6},0 +#define VER_FILEVERSION_STR "{s4}.0" + +#undef VER_PRODUCTVERSION +#define VER_PRODUCTVERSION VER_FILEVERSION + +#undef VER_PRODUCTVERSION_STR +#define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR + +#define VER_LEGALCOPYRIGHT_STR {s2} +#ifdef VER_COMPANYNAME_STR + +#undef VER_COMPANYNAME_STR +#define VER_COMPANYNAME_STR {s1} +#endif + +#undef VER_PRODUCTNAME_STR +#define VER_PRODUCTNAME_STR {s3} + +#include "common.ver""#, + file_type = ver_filetype, + file_subtype = ver_filesubtype, + internal_name = ver_internalname, + original_filename = ver_originalfilename + ); + + // Print the RC file content + //println!("{}", env!("CARGO_PKG_VERSION")); + //println!("{}", env!("CARGO_PKG_METADATA.WDK")); + //println!("cargopkgcrate:{}", env!("CARGO_PKG_CRATE_NAME")); + + + std::fs::write("resources.rc", rc_content).expect("Unable to write RC file"); + invoke_rc(&s7); +} + +fn invoke_rc(s7: &Vec) { + // Replace with the actual path to rc.exe + let rc_path = env::var("PATH_TO_RC").unwrap_or_else(|_| { + // Default path if environment variable is not set + r#"D:\EWDK\content\Program Files\Windows Kits\10\bin\10.0.22621.0\x86\rc.exe"#.to_string() + }); + + println!("Using rc.exe path: {}", rc_path); + + // Replace "resource.rc" with the name of your resource script file + let resource_script = "resources.rc"; + //for value in s7.into_iter() { + // println!("Got: {}", value.to_string()); + //} + //println!("include args: {:?}", s7); + let um_path = r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#.to_string(); + let include_string = "/I"; + //println!("Modified Path: {}", modified_path); + // let s8 = "/I r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#".to_string(); + let mut command = Command::new(&rc_path); + command.args(s7).arg(include_string).arg(um_path).arg(resource_script); + println!("Command executed: {:?}", command); + + //let status = Command::new(&rc_path).args(s7).arg(resource_script).status(); + let status = command.status(); + + //let mut command = Command::new(&rc_path); + //command.args(s7).arg(resource_script); + //println!("Command executed: {:?}", command); + + match status { + Ok(exit_status) => { + if exit_status.success() { + println!("Resource compilation successful!"); + } else { + println!("Resource compilation failed."); + std::process::exit(1); // Exit with a non-zero status code + } + } + Err(err) => { + eprintln!("Error running rc.exe: {}", err); + std::process::exit(1); // Exit with a non-zero status code + } + } +} +fn get_packagemetadatadetails() -> (String, String, String) { + // Run the 'cargo metadata' command and capture its output + let path = env::var("CARGO_MANIFEST_DIR").unwrap(); + let meta = MetadataCommand::new() + .manifest_path("./Cargo.toml") + .current_dir(&path) + .exec() + .unwrap(); + let root = meta.root_package().unwrap(); + let metadata = &root.metadata; + + // Extract metadata values with default fallbacks + + let companyname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("companyname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Company name not found in metadata".to_string()); + let copyrightname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("copyright")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); + let productname = metadata.get("wdk") + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("productname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Product name not found in metadata".to_string()); + + (companyname, copyrightname, productname) +} + +fn get_packagedetails() -> (String, String, String) { + let mut fileversion = String::new(); + let mut description = String::new(); + let mut productversion = String::new(); + + match fs::read_to_string("Cargo.toml") { + Ok(text1) => { + for line in text1.lines() { + if line.starts_with("version") { + let start = line.find('"').unwrap_or(0) + 1; + let end = line.rfind('"').unwrap_or(0); + productversion = line[start..end].to_string(); + let versionparts: Vec<&str> = productversion.split('.').collect(); + fileversion = versionparts.join(","); + } + if line.starts_with("description") { + let start = line.find('"').unwrap_or(0) + 1; + let end = line.rfind('"').unwrap_or(0); + description = line[start..end].to_string(); + } + } + } + Err(_) => { + eprintln!("Error reading Cargo.toml"); + } + } + (productversion, description, fileversion) +} From 60b1f7662cf4f572e4646d861919f050c01bebc7 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Thu, 24 Oct 2024 07:38:20 -0700 Subject: [PATCH 07/17] Delete crates/wdk-build/src/resource_compiler.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compiler.rs | 204 ---------------------- 1 file changed, 204 deletions(-) delete mode 100644 crates/wdk-build/src/resource_compiler.rs diff --git a/crates/wdk-build/src/resource_compiler.rs b/crates/wdk-build/src/resource_compiler.rs deleted file mode 100644 index 1dd0c2c0..00000000 --- a/crates/wdk-build/src/resource_compiler.rs +++ /dev/null @@ -1,204 +0,0 @@ -use std::env; -use std::fs; -use std::path::PathBuf; -use std::process::Command; -use cargo_metadata::MetadataCommand; - -pub fn generate_and_compile_rcfile(include_paths: Vec) { - // Initialize an empty vector to store modified include arguments - let mut includeargs: Vec = Vec::new(); - - // Iterate over each include path - for include_path in include_paths { - // Convert the include path to a string - if let Some(include_str) = include_path.to_str() { - // Append "/I" and the include path to the modified vector - includeargs.push("/I".to_string()); - includeargs.push(include_str.to_string()); - } else { - println!("Non-Unicode path is not supported: {:?}", include_path); - } - } - - // Print the modified include arguments - println!("IncludeArgs: {:?}", includeargs); - - let (companyname, copyright, productname) = get_packagemetadatadetails(); - let (productversion, description, fileversion) = get_packagedetails(); - getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, &includeargs); -} -fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6:String, s7: &Vec) { - println!("Set and create rc file... "); - let rcfile_path = "resources.rc"; - if fs::metadata(&rcfile_path).is_ok() { - // File exists, so let's remove it - if let Err(err) = fs::remove_file(&rcfile_path) { - eprintln!("Error deleting file: {}", err); - } else { - println!("File deleted successfully!"); - } - } else { - println!("File does not exist."); - } - - let ver_filetype = "VFT_DRV"; - let ver_filesubtype = "VFT2_DRV_SYSTEM"; - let ver_internalname = "sample-kmdf-driver.sys"; - let ver_originalfilename = "VER_INTERNALNAME_STR"; - - // Create the RC file content - let rc_content = format!( - r#"#include -#include -#define VER_FILETYPE {file_type} -#define VER_FILESUBTYPE {file_subtype} -#define VER_INTERNALNAME_STR "{internal_name}" -#define VER_ORIGINALFILENAME_STR {original_filename} - -#undef VER_FILEDESCRIPTION_STR -#define VER_FILEDESCRIPTION_STR "{s5}" - -#undef VER_PRODUCTNAME_STR -#define VER_PRODUCTNAME_STR VER_FILEDESCRIPTION_STR - -#define VER_FILEVERSION {s6},0 -#define VER_FILEVERSION_STR "{s4}.0" - -#undef VER_PRODUCTVERSION -#define VER_PRODUCTVERSION VER_FILEVERSION - -#undef VER_PRODUCTVERSION_STR -#define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR - -#define VER_LEGALCOPYRIGHT_STR {s2} -#ifdef VER_COMPANYNAME_STR - -#undef VER_COMPANYNAME_STR -#define VER_COMPANYNAME_STR {s1} -#endif - -#undef VER_PRODUCTNAME_STR -#define VER_PRODUCTNAME_STR {s3} - -#include "common.ver""#, - file_type = ver_filetype, - file_subtype = ver_filesubtype, - internal_name = ver_internalname, - original_filename = ver_originalfilename - ); - - // Print the RC file content - //println!("{}", env!("CARGO_PKG_VERSION")); - //println!("{}", env!("CARGO_PKG_METADATA.WDK")); - //println!("cargopkgcrate:{}", env!("CARGO_PKG_CRATE_NAME")); - - - std::fs::write("resources.rc", rc_content).expect("Unable to write RC file"); - invoke_rc(&s7); -} - -fn invoke_rc(s7: &Vec) { - // Replace with the actual path to rc.exe - let rc_path = env::var("PATH_TO_RC").unwrap_or_else(|_| { - // Default path if environment variable is not set - r#"D:\EWDK\content\Program Files\Windows Kits\10\bin\10.0.22621.0\x86\rc.exe"#.to_string() - }); - - println!("Using rc.exe path: {}", rc_path); - - // Replace "resource.rc" with the name of your resource script file - let resource_script = "resources.rc"; - //for value in s7.into_iter() { - // println!("Got: {}", value.to_string()); - //} - //println!("include args: {:?}", s7); - let um_path = r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#.to_string(); - let include_string = "/I"; - //println!("Modified Path: {}", modified_path); - // let s8 = "/I r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#".to_string(); - let mut command = Command::new(&rc_path); - command.args(s7).arg(include_string).arg(um_path).arg(resource_script); - println!("Command executed: {:?}", command); - - //let status = Command::new(&rc_path).args(s7).arg(resource_script).status(); - let status = command.status(); - - //let mut command = Command::new(&rc_path); - //command.args(s7).arg(resource_script); - //println!("Command executed: {:?}", command); - - match status { - Ok(exit_status) => { - if exit_status.success() { - println!("Resource compilation successful!"); - } else { - println!("Resource compilation failed."); - std::process::exit(1); // Exit with a non-zero status code - } - } - Err(err) => { - eprintln!("Error running rc.exe: {}", err); - std::process::exit(1); // Exit with a non-zero status code - } - } -} -fn get_packagemetadatadetails() -> (String, String, String) { - // Run the 'cargo metadata' command and capture its output - let path = env::var("CARGO_MANIFEST_DIR").unwrap(); - let meta = MetadataCommand::new() - .manifest_path("./Cargo.toml") - .current_dir(&path) - .exec() - .unwrap(); - let root = meta.root_package().unwrap(); - let metadata = &root.metadata; - - // Extract metadata values with default fallbacks - - let companyname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("companyname")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Company name not found in metadata".to_string()); - let copyrightname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("copyright")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); - let productname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("productname")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Product name not found in metadata".to_string()); - - (companyname, copyrightname, productname) -} - -fn get_packagedetails() -> (String, String, String) { - let mut fileversion = String::new(); - let mut description = String::new(); - let mut productversion = String::new(); - - match fs::read_to_string("Cargo.toml") { - Ok(text1) => { - for line in text1.lines() { - if line.starts_with("version") { - let start = line.find('"').unwrap_or(0) + 1; - let end = line.rfind('"').unwrap_or(0); - productversion = line[start..end].to_string(); - let versionparts: Vec<&str> = productversion.split('.').collect(); - fileversion = versionparts.join(","); - } - if line.starts_with("description") { - let start = line.find('"').unwrap_or(0) + 1; - let end = line.rfind('"').unwrap_or(0); - description = line[start..end].to_string(); - } - } - } - Err(_) => { - eprintln!("Error reading Cargo.toml"); - } - } - (productversion, description, fileversion) -} From 13f371c2cc1d80ce8521303780bb6a7b5d291a8d Mon Sep 17 00:00:00 2001 From: CHMANG Date: Thu, 24 Oct 2024 16:52:10 -0700 Subject: [PATCH 08/17] Update resource_compile.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index 1dd0c2c0..35d9788a 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -131,6 +131,7 @@ fn invoke_rc(s7: &Vec) { Ok(exit_status) => { if exit_status.success() { println!("Resource compilation successful!"); + println!("cargo:rustc-link-arg=resources.res"); } else { println!("Resource compilation failed."); std::process::exit(1); // Exit with a non-zero status code From afcab5628a4b1a4193bbf861c1bd35ce96ac5507 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Thu, 24 Oct 2024 17:33:13 -0700 Subject: [PATCH 09/17] Update lib.rs Signed-off-by: CHMANG --- crates/wdk-build/src/lib.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index 3140ecd3..d3173f18 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -334,41 +334,38 @@ impl Config { let sdk_version = utils::get_latest_windows_sdk_version(include_directory.as_path())?; let windows_sdk_include_path = include_directory.join(sdk_version); - let crt_include_path = windows_sdk_include_path.join("km/crt"); - if !crt_include_path.is_dir() { + let km_include_path = windows_sdk_include_path.join("km"); + if !km_include_path.is_dir() { return Err(ConfigError::DirectoryNotFound { - directory: crt_include_path.to_string_lossy().into(), + directory: km_include_path.to_string_lossy().into(), }); } include_paths.push( - crt_include_path + km_include_path .canonicalize()? .strip_extended_length_path_prefix()?, ); - - let km_or_um_include_path = windows_sdk_include_path.join(match self.driver_config { - DriverConfig::Wdm | DriverConfig::Kmdf(_) => "km", - DriverConfig::Umdf(_) => "um", - }); - if !km_or_um_include_path.is_dir() { + + let kit_shared_include_path = windows_sdk_include_path.join("shared"); + if !kit_shared_include_path.is_dir() { return Err(ConfigError::DirectoryNotFound { - directory: km_or_um_include_path.to_string_lossy().into(), + directory: kit_shared_include_path.to_string_lossy().into(), }); } include_paths.push( - km_or_um_include_path + kit_shared_include_path .canonicalize()? .strip_extended_length_path_prefix()?, ); - - let kit_shared_include_path = windows_sdk_include_path.join("shared"); - if !kit_shared_include_path.is_dir() { + + let um_include_path = windows_sdk_include_path.join("um"); + if !um_include_path.is_dir() { return Err(ConfigError::DirectoryNotFound { - directory: kit_shared_include_path.to_string_lossy().into(), + directory: um_include_path.to_string_lossy().into(), }); } include_paths.push( - kit_shared_include_path + um_include_path .canonicalize()? .strip_extended_length_path_prefix()?, ); From 7041a3240e567f559d5dcdd8750200f8ab6dce53 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Fri, 25 Oct 2024 17:45:18 -0700 Subject: [PATCH 10/17] Update lib.rs Signed-off-by: CHMANG --- crates/wdk-build/src/lib.rs | 47 +++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index d3173f18..d5f6583e 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -315,6 +315,38 @@ impl Config { Ok(()) } + /// Returns Resource Compile path to build and link based off of + /// the architecture platform + /// + /// # Errors + /// + /// This function will return an error if any of the required paths do not + /// exist. + pub fn get_rc_root_path(&self) -> Result { + let bin_directory = self.wdk_content_root.join("bin"); + + // Add Windows SDK library paths based on logic from WindowsDriver.KernelMode.props & + // WindowsDriver.UserMode.props in NI(22H2) WDK + let sdk_version = utils::get_latest_windows_sdk_version(bin_directory.as_path())?; + let windows_sdk_rc_path = bin_directory + .join(sdk_version) + .join(self.cpu_architecture.as_windows_str()); + + if !windows_sdk_rc_path.is_dir() { + return Err(ConfigError::DirectoryNotFound { + directory: windows_sdk_rc_path.to_string_lossy().into(), + }); + } + + let rc_path = windows_sdk_rc_path + .canonicalize()? + .strip_extended_length_path_prefix()? + .to_string_lossy() + .into_owned(); + + Ok(rc_path) + } + /// Return header include paths required to build and link based off of the /// configuration of `Config` /// @@ -325,15 +357,15 @@ impl Config { pub fn get_include_paths(&self) -> Result, ConfigError> { // FIXME: consider deprecating in favor of iter let mut include_paths = vec![]; - + let include_directory = self.wdk_content_root.join("Include"); - + // Add windows sdk include paths // Based off of logic from WindowsDriver.KernelMode.props & // WindowsDriver.UserMode.props in NI(22H2) WDK let sdk_version = utils::get_latest_windows_sdk_version(include_directory.as_path())?; let windows_sdk_include_path = include_directory.join(sdk_version); - + let km_include_path = windows_sdk_include_path.join("km"); if !km_include_path.is_dir() { return Err(ConfigError::DirectoryNotFound { @@ -345,7 +377,7 @@ impl Config { .canonicalize()? .strip_extended_length_path_prefix()?, ); - + let kit_shared_include_path = windows_sdk_include_path.join("shared"); if !kit_shared_include_path.is_dir() { return Err(ConfigError::DirectoryNotFound { @@ -369,7 +401,7 @@ impl Config { .canonicalize()? .strip_extended_length_path_prefix()?, ); - + // Add other driver type-specific include paths match &self.driver_config { DriverConfig::Wdm => {} @@ -406,7 +438,7 @@ impl Config { ); } } - + Ok(include_paths) } @@ -688,7 +720,8 @@ impl Config { for path in library_paths { println!("cargo::rustc-link-search={}", path.display()); } - resource_compile::generate_and_compile_rcfile(self.get_include_paths()?); + resource_compile::generate_and_compile_rcfile(self.get_include_paths()?,self.get_rc_root_path()?); + match &self.driver_config { DriverConfig::Wdm => { // Emit WDM-specific libraries to link to From 3a7541182c69fe8dd12eb48467665c2e4b61b026 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Fri, 25 Oct 2024 17:46:57 -0700 Subject: [PATCH 11/17] Update resource_compile.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 82 +++++++++--------------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index 35d9788a..fe43e7b9 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -1,10 +1,11 @@ use std::env; use std::fs; +use std::path::Path; use std::path::PathBuf; use std::process::Command; use cargo_metadata::MetadataCommand; -pub fn generate_and_compile_rcfile(include_paths: Vec) { +pub fn generate_and_compile_rcfile(include_paths: Vec,rc_exe_rootpath: String) { // Initialize an empty vector to store modified include arguments let mut includeargs: Vec = Vec::new(); @@ -20,14 +21,11 @@ pub fn generate_and_compile_rcfile(include_paths: Vec) { } } - // Print the modified include arguments - println!("IncludeArgs: {:?}", includeargs); - let (companyname, copyright, productname) = get_packagemetadatadetails(); - let (productversion, description, fileversion) = get_packagedetails(); - getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, &includeargs); + let (productversion, description, fileversion, name) = get_packagedetails(); + getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, name, &includeargs, rc_exe_rootpath); } -fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6:String, s7: &Vec) { +fn getandset_rcfile(companyname: String, copyright: String, productname: String, productversion:String, description:String, fileversion:String, name:String, includeargs: &Vec, rc_exe_rootpath:String) { println!("Set and create rc file... "); let rcfile_path = "resources.rc"; if fs::metadata(&rcfile_path).is_ok() { @@ -43,7 +41,6 @@ fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6 let ver_filetype = "VFT_DRV"; let ver_filesubtype = "VFT2_DRV_SYSTEM"; - let ver_internalname = "sample-kmdf-driver.sys"; let ver_originalfilename = "VER_INTERNALNAME_STR"; // Create the RC file content @@ -52,17 +49,17 @@ fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6 #include #define VER_FILETYPE {file_type} #define VER_FILESUBTYPE {file_subtype} -#define VER_INTERNALNAME_STR "{internal_name}" +#define VER_INTERNALNAME_STR "{name}" #define VER_ORIGINALFILENAME_STR {original_filename} #undef VER_FILEDESCRIPTION_STR -#define VER_FILEDESCRIPTION_STR "{s5}" +#define VER_FILEDESCRIPTION_STR "{description}" #undef VER_PRODUCTNAME_STR #define VER_PRODUCTNAME_STR VER_FILEDESCRIPTION_STR -#define VER_FILEVERSION {s6},0 -#define VER_FILEVERSION_STR "{s4}.0" +#define VER_FILEVERSION {fileversion},0 +#define VER_FILEVERSION_STR "{productversion}.0" #undef VER_PRODUCTVERSION #define VER_PRODUCTVERSION VER_FILEVERSION @@ -70,63 +67,42 @@ fn getandset_rcfile(s1: String, s2: String, s3: String, s4:String, s5:String, s6 #undef VER_PRODUCTVERSION_STR #define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR -#define VER_LEGALCOPYRIGHT_STR {s2} +#define VER_LEGALCOPYRIGHT_STR {copyright} #ifdef VER_COMPANYNAME_STR #undef VER_COMPANYNAME_STR -#define VER_COMPANYNAME_STR {s1} +#define VER_COMPANYNAME_STR {companyname} #endif #undef VER_PRODUCTNAME_STR -#define VER_PRODUCTNAME_STR {s3} +#define VER_PRODUCTNAME_STR {productname} #include "common.ver""#, file_type = ver_filetype, file_subtype = ver_filesubtype, - internal_name = ver_internalname, original_filename = ver_originalfilename ); - - // Print the RC file content - //println!("{}", env!("CARGO_PKG_VERSION")); - //println!("{}", env!("CARGO_PKG_METADATA.WDK")); - //println!("cargopkgcrate:{}", env!("CARGO_PKG_CRATE_NAME")); - std::fs::write("resources.rc", rc_content).expect("Unable to write RC file"); - invoke_rc(&s7); + invoke_rc(&includeargs, rc_exe_rootpath); } -fn invoke_rc(s7: &Vec) { - // Replace with the actual path to rc.exe - let rc_path = env::var("PATH_TO_RC").unwrap_or_else(|_| { - // Default path if environment variable is not set - r#"D:\EWDK\content\Program Files\Windows Kits\10\bin\10.0.22621.0\x86\rc.exe"#.to_string() - }); +fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { - println!("Using rc.exe path: {}", rc_path); - - // Replace "resource.rc" with the name of your resource script file let resource_script = "resources.rc"; - //for value in s7.into_iter() { - // println!("Got: {}", value.to_string()); - //} - //println!("include args: {:?}", s7); - let um_path = r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#.to_string(); - let include_string = "/I"; - //println!("Modified Path: {}", modified_path); - // let s8 = "/I r#"D:\EWDK\content\Program Files\Windows Kits\10\Include\10.0.22621.0\um"#".to_string(); - let mut command = Command::new(&rc_path); - command.args(s7).arg(include_string).arg(um_path).arg(resource_script); - println!("Command executed: {:?}", command); + let rc_exe_path = format!("{}\\rc.exe", rc_exe_rootpath); + let rc_exe_path = Path::new(&rc_exe_path); + if !rc_exe_path.exists() { + eprintln!("Error: rc.exe path does not exist : {}", rc_exe_path.display()); + std::process::exit(1); // Exit with a non-zero status code + } + + let mut command = Command::new(rc_exe_path); + command.args(includeargs).arg(resource_script); + println!("Command executed: {:?}", command); - //let status = Command::new(&rc_path).args(s7).arg(resource_script).status(); let status = command.status(); - //let mut command = Command::new(&rc_path); - //command.args(s7).arg(resource_script); - //println!("Command executed: {:?}", command); - match status { Ok(exit_status) => { if exit_status.success() { @@ -175,10 +151,11 @@ fn get_packagemetadatadetails() -> (String, String, String) { (companyname, copyrightname, productname) } -fn get_packagedetails() -> (String, String, String) { +fn get_packagedetails() -> (String, String, String, String) { let mut fileversion = String::new(); let mut description = String::new(); let mut productversion = String::new(); + let mut name = String::new(); match fs::read_to_string("Cargo.toml") { Ok(text1) => { @@ -195,11 +172,16 @@ fn get_packagedetails() -> (String, String, String) { let end = line.rfind('"').unwrap_or(0); description = line[start..end].to_string(); } + if line.starts_with("name") { + let start = line.find('"').unwrap_or(0) + 1; + let end = line.rfind('"').unwrap_or(0); + name = line[start..end].to_string(); + } } } Err(_) => { eprintln!("Error reading Cargo.toml"); } } - (productversion, description, fileversion) + (productversion, description, fileversion, name) } From ce35c0aeee1ae1110f1c02c11c20eafcb1a8a518 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Tue, 12 Nov 2024 17:52:27 -0800 Subject: [PATCH 12/17] Update resource_compile.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index fe43e7b9..1595a159 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::process::Command; use cargo_metadata::MetadataCommand; +// function to generate and compile RC file pub fn generate_and_compile_rcfile(include_paths: Vec,rc_exe_rootpath: String) { // Initialize an empty vector to store modified include arguments let mut includeargs: Vec = Vec::new(); @@ -25,6 +26,7 @@ pub fn generate_and_compile_rcfile(include_paths: Vec,rc_exe_rootpath: let (productversion, description, fileversion, name) = get_packagedetails(); getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, name, &includeargs, rc_exe_rootpath); } +// function to get and set RC File with package metadata fn getandset_rcfile(companyname: String, copyright: String, productname: String, productversion:String, description:String, fileversion:String, name:String, includeargs: &Vec, rc_exe_rootpath:String) { println!("Set and create rc file... "); let rcfile_path = "resources.rc"; @@ -87,6 +89,7 @@ fn getandset_rcfile(companyname: String, copyright: String, productname: String, invoke_rc(&includeargs, rc_exe_rootpath); } +// function to invoke RC.exe fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { let resource_script = "resources.rc"; @@ -119,6 +122,7 @@ fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { } } } +// function to get package metadata details fn get_packagemetadatadetails() -> (String, String, String) { // Run the 'cargo metadata' command and capture its output let path = env::var("CARGO_MANIFEST_DIR").unwrap(); @@ -131,7 +135,6 @@ fn get_packagemetadatadetails() -> (String, String, String) { let metadata = &root.metadata; // Extract metadata values with default fallbacks - let companyname = metadata.get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("companyname")) @@ -150,7 +153,7 @@ fn get_packagemetadatadetails() -> (String, String, String) { (companyname, copyrightname, productname) } - +// function to get package details fn get_packagedetails() -> (String, String, String, String) { let mut fileversion = String::new(); let mut description = String::new(); From 52680752ee2a095aa2ef09bef961dc287c6e4d72 Mon Sep 17 00:00:00 2001 From: CHMANG Date: Tue, 12 Nov 2024 18:10:41 -0800 Subject: [PATCH 13/17] format fixes Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 59 ++++++++++++++++-------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index 1595a159..099dd8c3 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -6,7 +6,7 @@ use std::process::Command; use cargo_metadata::MetadataCommand; // function to generate and compile RC file -pub fn generate_and_compile_rcfile(include_paths: Vec,rc_exe_rootpath: String) { +pub fn generate_and_compile_rcfile(include_paths: Vec, rc_exe_rootpath: String) { // Initialize an empty vector to store modified include arguments let mut includeargs: Vec = Vec::new(); @@ -24,10 +24,30 @@ pub fn generate_and_compile_rcfile(include_paths: Vec,rc_exe_rootpath: let (companyname, copyright, productname) = get_packagemetadatadetails(); let (productversion, description, fileversion, name) = get_packagedetails(); - getandset_rcfile(companyname, copyright, productname, productversion ,description, fileversion, name, &includeargs, rc_exe_rootpath); + getandset_rcfile( + companyname, + copyright, + productname, + productversion, + description, + fileversion, + name, + &includeargs, + rc_exe_rootpath, + ); } // function to get and set RC File with package metadata -fn getandset_rcfile(companyname: String, copyright: String, productname: String, productversion:String, description:String, fileversion:String, name:String, includeargs: &Vec, rc_exe_rootpath:String) { +fn getandset_rcfile( + companyname: String, + copyright: String, + productname: String, + productversion:String, + description:String, + fileversion:String, + name:String, + includeargs: &Vec, + rc_exe_rootpath:String, +) { println!("Set and create rc file... "); let rcfile_path = "resources.rc"; if fs::metadata(&rcfile_path).is_ok() { @@ -89,14 +109,17 @@ fn getandset_rcfile(companyname: String, copyright: String, productname: String, invoke_rc(&includeargs, rc_exe_rootpath); } -// function to invoke RC.exe +// function to invoke RC.exe fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { let resource_script = "resources.rc"; let rc_exe_path = format!("{}\\rc.exe", rc_exe_rootpath); let rc_exe_path = Path::new(&rc_exe_path); if !rc_exe_path.exists() { - eprintln!("Error: rc.exe path does not exist : {}", rc_exe_path.display()); + eprintln!( + "Error: rc.exe path does not exist : {}", + rc_exe_path.display() + ); std::process::exit(1); // Exit with a non-zero status code } @@ -104,7 +127,7 @@ fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { command.args(includeargs).arg(resource_script); println!("Command executed: {:?}", command); - let status = command.status(); + let status = command.status(); match status { Ok(exit_status) => { @@ -136,20 +159,20 @@ fn get_packagemetadatadetails() -> (String, String, String) { // Extract metadata values with default fallbacks let companyname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("companyname")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Company name not found in metadata".to_string()); + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("companyname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Company name not found in metadata".to_string()); let copyrightname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("copyright")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("copyright")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); let productname = metadata.get("wdk") - .and_then(|wdk| wdk.get("driver-model")) - .and_then(|driver_model| driver_model.get("productname")) - .map(|s| s.to_string()) - .unwrap_or_else(|| "Product name not found in metadata".to_string()); + .and_then(|wdk| wdk.get("driver-model")) + .and_then(|driver_model| driver_model.get("productname")) + .map(|s| s.to_string()) + .unwrap_or_else(|| "Product name not found in metadata".to_string()); (companyname, copyrightname, productname) } From 49ef8ec05f7a9c61445c153d2338f9cd33e330fb Mon Sep 17 00:00:00 2001 From: CHMANG Date: Sat, 16 Nov 2024 15:49:26 -0800 Subject: [PATCH 14/17] Update lib.rs Signed-off-by: CHMANG --- crates/wdk-build/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index d5f6583e..2caaac25 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -326,7 +326,8 @@ impl Config { let bin_directory = self.wdk_content_root.join("bin"); // Add Windows SDK library paths based on logic from WindowsDriver.KernelMode.props & - // WindowsDriver.UserMode.props in NI(22H2) WDK + // WindowsDriver.UserMode.props in + // NI(22H2) WDK let sdk_version = utils::get_latest_windows_sdk_version(bin_directory.as_path())?; let windows_sdk_rc_path = bin_directory .join(sdk_version) @@ -720,7 +721,10 @@ impl Config { for path in library_paths { println!("cargo::rustc-link-search={}", path.display()); } - resource_compile::generate_and_compile_rcfile(self.get_include_paths()?,self.get_rc_root_path()?); + resource_compile::generate_and_compile_rcfile( + self.get_include_paths()?, + self.get_rc_root_path()?, + ); match &self.driver_config { DriverConfig::Wdm => { From 37f74cae7207571347706806121088917f5040ba Mon Sep 17 00:00:00 2001 From: CHMANG Date: Sat, 16 Nov 2024 15:54:47 -0800 Subject: [PATCH 15/17] Update resource_compile.rs Signed-off-by: CHMANG --- crates/wdk-build/src/resource_compile.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index 099dd8c3..aa8481a2 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -1,8 +1,9 @@ -use std::env; -use std::fs; -use std::path::Path; -use std::path::PathBuf; -use std::process::Command; +use std::{ + env, + fs, + path::{Path, PathBuf}, + process::Command, +}; use cargo_metadata::MetadataCommand; // function to generate and compile RC file @@ -32,7 +33,7 @@ pub fn generate_and_compile_rcfile(include_paths: Vec, rc_exe_rootpath: description, fileversion, name, - &includeargs, + &includeargs, rc_exe_rootpath, ); } @@ -158,17 +159,20 @@ fn get_packagemetadatadetails() -> (String, String, String) { let metadata = &root.metadata; // Extract metadata values with default fallbacks - let companyname = metadata.get("wdk") + let companyname = metadata + .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("companyname")) .map(|s| s.to_string()) .unwrap_or_else(|| "Company name not found in metadata".to_string()); - let copyrightname = metadata.get("wdk") + let copyrightname = metadata + .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("copyright")) .map(|s| s.to_string()) .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); - let productname = metadata.get("wdk") + let productname = metadata + .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("productname")) .map(|s| s.to_string()) From b36fba8ab4a8a44230659c4a89df28a330778ae4 Mon Sep 17 00:00:00 2001 From: Chaitanya Mangalagiri Date: Sat, 23 Nov 2024 17:16:59 -0800 Subject: [PATCH 16/17] updating to rust conventions --- crates/wdk-build/src/lib.rs | 3 - crates/wdk-build/src/resource_compile.rs | 130 ++++++++++++----------- 2 files changed, 68 insertions(+), 65 deletions(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index 2caaac25..a04603f1 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -325,9 +325,6 @@ impl Config { pub fn get_rc_root_path(&self) -> Result { let bin_directory = self.wdk_content_root.join("bin"); - // Add Windows SDK library paths based on logic from WindowsDriver.KernelMode.props & - // WindowsDriver.UserMode.props in - // NI(22H2) WDK let sdk_version = utils::get_latest_windows_sdk_version(bin_directory.as_path())?; let windows_sdk_rc_path = bin_directory .join(sdk_version) diff --git a/crates/wdk-build/src/resource_compile.rs b/crates/wdk-build/src/resource_compile.rs index aa8481a2..55da771b 100644 --- a/crates/wdk-build/src/resource_compile.rs +++ b/crates/wdk-build/src/resource_compile.rs @@ -6,54 +6,56 @@ use std::{ }; use cargo_metadata::MetadataCommand; -// function to generate and compile RC file -pub fn generate_and_compile_rcfile(include_paths: Vec, rc_exe_rootpath: String) { +// Function to generate and compile RC file +pub fn generate_and_compile_rc_file(include_paths: Vec, rc_exe_root_path: String) { // Initialize an empty vector to store modified include arguments - let mut includeargs: Vec = Vec::new(); + let mut include_args: Vec = Vec::new(); // Iterate over each include path for include_path in include_paths { // Convert the include path to a string if let Some(include_str) = include_path.to_str() { // Append "/I" and the include path to the modified vector - includeargs.push("/I".to_string()); - includeargs.push(include_str.to_string()); + include_args.push("/I".to_string()); + include_args.push(include_str.to_string()); } else { println!("Non-Unicode path is not supported: {:?}", include_path); } } - let (companyname, copyright, productname) = get_packagemetadatadetails(); - let (productversion, description, fileversion, name) = get_packagedetails(); - getandset_rcfile( - companyname, + let (company_name, copyright, product_name) = get_package_metadata_details(); + let (product_version, description, file_version, name) = get_package_details(); + + get_and_set_rc_file( + company_name, copyright, - productname, - productversion, + product_name, + product_version, description, - fileversion, + file_version, name, - &includeargs, - rc_exe_rootpath, + &include_args, + rc_exe_root_path, ); } -// function to get and set RC File with package metadata -fn getandset_rcfile( - companyname: String, + +// Function to get and set RC File with package metadata +fn get_and_set_rc_file( + company_name: String, copyright: String, - productname: String, - productversion:String, - description:String, - fileversion:String, - name:String, - includeargs: &Vec, - rc_exe_rootpath:String, + product_name: String, + product_version: String, + description: String, + file_version: String, + name: String, + include_args: &Vec, + rc_exe_root_path: String, ) { println!("Set and create rc file... "); - let rcfile_path = "resources.rc"; - if fs::metadata(&rcfile_path).is_ok() { + let rc_file_path = "resources.rc"; + if fs::metadata(&rc_file_path).is_ok() { // File exists, so let's remove it - if let Err(err) = fs::remove_file(&rcfile_path) { + if let Err(err) = fs::remove_file(&rc_file_path) { eprintln!("Error deleting file: {}", err); } else { println!("File deleted successfully!"); @@ -62,16 +64,16 @@ fn getandset_rcfile( println!("File does not exist."); } - let ver_filetype = "VFT_DRV"; - let ver_filesubtype = "VFT2_DRV_SYSTEM"; - let ver_originalfilename = "VER_INTERNALNAME_STR"; + let ver_file_type = "VFT_DRV"; + let ver_file_subtype = "VFT2_DRV_SYSTEM"; + let ver_original_filename = "VER_INTERNALNAME_STR"; // Create the RC file content let rc_content = format!( r#"#include #include -#define VER_FILETYPE {file_type} -#define VER_FILESUBTYPE {file_subtype} +#define VER_FILETYPE {file_type} +#define VER_FILESUBTYPE {file_subtype} #define VER_INTERNALNAME_STR "{name}" #define VER_ORIGINALFILENAME_STR {original_filename} @@ -81,8 +83,8 @@ fn getandset_rcfile( #undef VER_PRODUCTNAME_STR #define VER_PRODUCTNAME_STR VER_FILEDESCRIPTION_STR -#define VER_FILEVERSION {fileversion},0 -#define VER_FILEVERSION_STR "{productversion}.0" +#define VER_FILEVERSION {file_version},0 +#define VER_FILEVERSION_STR "{product_version}.0" #undef VER_PRODUCTVERSION #define VER_PRODUCTVERSION VER_FILEVERSION @@ -94,27 +96,26 @@ fn getandset_rcfile( #ifdef VER_COMPANYNAME_STR #undef VER_COMPANYNAME_STR -#define VER_COMPANYNAME_STR {companyname} +#define VER_COMPANYNAME_STR {company_name} #endif #undef VER_PRODUCTNAME_STR -#define VER_PRODUCTNAME_STR {productname} +#define VER_PRODUCTNAME_STR {product_name} #include "common.ver""#, - file_type = ver_filetype, - file_subtype = ver_filesubtype, - original_filename = ver_originalfilename + file_type = ver_file_type, + file_subtype = ver_file_subtype, + original_filename = ver_original_filename ); std::fs::write("resources.rc", rc_content).expect("Unable to write RC file"); - invoke_rc(&includeargs, rc_exe_rootpath); + invoke_rc(&include_args, rc_exe_root_path); } -// function to invoke RC.exe -fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { - +// Function to invoke RC.exe +fn invoke_rc(include_args: &Vec, rc_exe_root_path: String) { let resource_script = "resources.rc"; - let rc_exe_path = format!("{}\\rc.exe", rc_exe_rootpath); + let rc_exe_path = format!("{}\\rc.exe", rc_exe_root_path); let rc_exe_path = Path::new(&rc_exe_path); if !rc_exe_path.exists() { eprintln!( @@ -125,7 +126,7 @@ fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { } let mut command = Command::new(rc_exe_path); - command.args(includeargs).arg(resource_script); + command.args(include_args).arg(resource_script); println!("Command executed: {:?}", command); let status = command.status(); @@ -146,8 +147,9 @@ fn invoke_rc(includeargs: &Vec, rc_exe_rootpath: String) { } } } -// function to get package metadata details -fn get_packagemetadatadetails() -> (String, String, String) { + +// Function to get package metadata details +fn get_package_metadata_details() -> (String, String, String) { // Run the 'cargo metadata' command and capture its output let path = env::var("CARGO_MANIFEST_DIR").unwrap(); let meta = MetadataCommand::new() @@ -159,43 +161,46 @@ fn get_packagemetadatadetails() -> (String, String, String) { let metadata = &root.metadata; // Extract metadata values with default fallbacks - let companyname = metadata + let company_name = metadata .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("companyname")) .map(|s| s.to_string()) .unwrap_or_else(|| "Company name not found in metadata".to_string()); - let copyrightname = metadata + + let copyright_name = metadata .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("copyright")) .map(|s| s.to_string()) .unwrap_or_else(|| "Copyright name not found in metadata".to_string()); - let productname = metadata + + let product_name = metadata .get("wdk") .and_then(|wdk| wdk.get("driver-model")) .and_then(|driver_model| driver_model.get("productname")) .map(|s| s.to_string()) .unwrap_or_else(|| "Product name not found in metadata".to_string()); - (companyname, copyrightname, productname) + (company_name, copyright_name, product_name) } -// function to get package details -fn get_packagedetails() -> (String, String, String, String) { - let mut fileversion = String::new(); + +// Function to get package details +fn get_package_details() -> (String, String, String, String) { + let mut file_version = String::new(); let mut description = String::new(); - let mut productversion = String::new(); + let mut product_version = String::new(); let mut name = String::new(); match fs::read_to_string("Cargo.toml") { - Ok(text1) => { - for line in text1.lines() { + Ok(text) => { + for line in text.lines() { if line.starts_with("version") { let start = line.find('"').unwrap_or(0) + 1; let end = line.rfind('"').unwrap_or(0); - productversion = line[start..end].to_string(); - let versionparts: Vec<&str> = productversion.split('.').collect(); - fileversion = versionparts.join(","); + product_version = line[start..end].to_string(); + let version_parts: Vec<&str> = product_version.split('.').collect(); + file_version = version_parts.join(","); } if line.starts_with("description") { let start = line.find('"').unwrap_or(0) + 1; @@ -213,5 +218,6 @@ fn get_packagedetails() -> (String, String, String, String) { eprintln!("Error reading Cargo.toml"); } } - (productversion, description, fileversion, name) -} + + (product_version, description, file_version, name) +} \ No newline at end of file From edcd7c093cb949326b8eb88da471384513de8491 Mon Sep 17 00:00:00 2001 From: Chaitanya Mangalagiri Date: Sat, 23 Nov 2024 17:32:31 -0800 Subject: [PATCH 17/17] updating calling function name --- crates/wdk-build/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index a04603f1..4dc5aff7 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -718,7 +718,7 @@ impl Config { for path in library_paths { println!("cargo::rustc-link-search={}", path.display()); } - resource_compile::generate_and_compile_rcfile( + resource_compile::generate_and_compile_rc_file( self.get_include_paths()?, self.get_rc_root_path()?, );