Skip to content

Commit

Permalink
updating to rust conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
CHMANG committed Nov 24, 2024
1 parent 37f74ca commit b36fba8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
3 changes: 0 additions & 3 deletions crates/wdk-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ impl Config {
pub fn get_rc_root_path(&self) -> Result<String, ConfigError> {
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)
Expand Down
130 changes: 68 additions & 62 deletions crates/wdk-build/src/resource_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>, rc_exe_rootpath: String) {
// Function to generate and compile RC file
pub fn generate_and_compile_rc_file(include_paths: Vec<PathBuf>, rc_exe_root_path: String) {
// Initialize an empty vector to store modified include arguments
let mut includeargs: Vec<String> = Vec::new();
let mut include_args: Vec<String> = 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<String>,
rc_exe_rootpath:String,
product_name: String,
product_version: String,
description: String,
file_version: String,
name: String,
include_args: &Vec<String>,
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!");
Expand All @@ -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 <windows.h>
#include <ntverp.h>
#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}
Expand All @@ -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
Expand All @@ -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<String>, rc_exe_rootpath: String) {

// Function to invoke RC.exe
fn invoke_rc(include_args: &Vec<String>, 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!(
Expand All @@ -125,7 +126,7 @@ fn invoke_rc(includeargs: &Vec<String>, 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();
Expand All @@ -146,8 +147,9 @@ fn invoke_rc(includeargs: &Vec<String>, 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()
Expand All @@ -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;
Expand All @@ -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)
}

0 comments on commit b36fba8

Please sign in to comment.