From e477458c139423e8fae5ade4e02950dcdadb49cb Mon Sep 17 00:00:00 2001 From: Be Wilson Date: Sun, 28 Jan 2024 19:11:37 -0600 Subject: [PATCH] move Windows code into a module --- gen-windows-sys-binding/src/main.rs | 2 +- src/lib.rs | 27 +++++-------------- src/os_pipe/windows.rs | 2 +- src/parallel/job_token/windows.rs | 2 +- src/{ => windows}/com.rs | 2 +- .../find_tools.rs} | 8 +++--- src/windows/mod.rs | 20 ++++++++++++++ src/{ => windows}/registry.rs | 2 +- src/{ => windows}/setup_config.rs | 2 +- src/{ => windows}/vs_instances.rs | 2 +- src/{ => windows}/winapi.rs | 10 +++---- src/{ => windows}/windows_sys.rs | 0 12 files changed, 43 insertions(+), 36 deletions(-) rename src/{ => windows}/com.rs (99%) rename src/{windows_registry.rs => windows/find_tools.rs} (99%) create mode 100644 src/windows/mod.rs rename src/{ => windows}/registry.rs (99%) rename src/{ => windows}/setup_config.rs (99%) rename src/{ => windows}/vs_instances.rs (98%) rename src/{ => windows}/winapi.rs (92%) rename src/{ => windows}/windows_sys.rs (100%) diff --git a/gen-windows-sys-binding/src/main.rs b/gen-windows-sys-binding/src/main.rs index 5dd96cdd6..3b5a856ba 100644 --- a/gen-windows-sys-binding/src/main.rs +++ b/gen-windows-sys-binding/src/main.rs @@ -53,7 +53,7 @@ fn main() -> io::Result<()> { let bindings = windows_bindgen::standalone_std(&names).replace("::core::ptr::invalid_mut", "invalid_mut"); - let mut f = fs::File::create("../src/windows_sys.rs")?; + let mut f = fs::File::create("../src/windows/windows_sys.rs")?; f.write_all(PRELUDE.as_bytes())?; f.write_all(bindings.as_bytes())?; f.write_all(POSTLUDE.as_bytes())?; diff --git a/src/lib.rs b/src/lib.rs index 98af4ff1f..0f9e93395 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,23 +69,10 @@ use std::thread::{self, JoinHandle}; mod os_pipe; #[cfg(feature = "parallel")] mod parallel; -// These modules are all glue to support reading the MSVC version from -// the registry and from COM interfaces -#[cfg(windows)] -mod registry; -#[cfg(windows)] -#[macro_use] -mod winapi; -#[cfg(windows)] -mod com; -#[cfg(windows)] -mod setup_config; -#[cfg(windows)] -mod vs_instances; -#[cfg(windows)] -mod windows_sys; - -pub mod windows_registry; +mod windows; +// Regardless of whether this should be in this crate's public API, +// it has been since 2015, so don't break it. +pub use windows::find_tools as windows_registry; /// A builder for compilation of a native library. /// @@ -2195,7 +2182,7 @@ impl Build { } else { "ml.exe" }; - let mut cmd = windows_registry::find(&target, tool).unwrap_or_else(|| self.cmd(tool)); + let mut cmd = windows::find_tools::find(&target, tool).unwrap_or_else(|| self.cmd(tool)); cmd.arg("-nologo"); // undocumented, yet working with armasm[64] for directory in self.include_directories.iter() { cmd.arg("-I").arg(&**directory); @@ -2551,7 +2538,7 @@ impl Build { traditional }; - let cl_exe = windows_registry::find_tool(target, "cl.exe"); + let cl_exe = windows::find_tools::find_tool(target, "cl.exe"); let tool_opt: Option = self .env_tool(env) @@ -3022,7 +3009,7 @@ impl Build { if lib.is_empty() { name = String::from("lib.exe"); - let mut cmd = match windows_registry::find(&target, "lib.exe") { + let mut cmd = match windows::find_tools::find(&target, "lib.exe") { Some(t) => t, None => self.cmd("lib.exe"), }; diff --git a/src/os_pipe/windows.rs b/src/os_pipe/windows.rs index 8a6a3fbec..de0106a2a 100644 --- a/src/os_pipe/windows.rs +++ b/src/os_pipe/windows.rs @@ -1,4 +1,4 @@ -use crate::windows_sys::{CreatePipe, INVALID_HANDLE_VALUE}; +use crate::windows::windows_sys::{CreatePipe, INVALID_HANDLE_VALUE}; use std::{fs::File, io, os::windows::prelude::*, ptr}; /// NOTE: These pipes do not support IOCP. diff --git a/src/parallel/job_token/windows.rs b/src/parallel/job_token/windows.rs index 03ec8869b..b85b1a27d 100644 --- a/src/parallel/job_token/windows.rs +++ b/src/parallel/job_token/windows.rs @@ -3,7 +3,7 @@ use std::{ io, ptr, }; -use crate::windows_sys::{ +use crate::windows::windows_sys::{ OpenSemaphoreA, ReleaseSemaphore, WaitForSingleObject, FALSE, HANDLE, SEMAPHORE_MODIFY_STATE, THREAD_SYNCHRONIZE, WAIT_ABANDONED, WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT, }; diff --git a/src/com.rs b/src/windows/com.rs similarity index 99% rename from src/com.rs rename to src/windows/com.rs index 6eea58ee2..e85348d7a 100644 --- a/src/com.rs +++ b/src/windows/com.rs @@ -7,7 +7,7 @@ #![allow(unused)] -use crate::{ +use crate::windows::{ winapi::{IUnknown, Interface}, windows_sys::{ CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE, diff --git a/src/windows_registry.rs b/src/windows/find_tools.rs similarity index 99% rename from src/windows_registry.rs rename to src/windows/find_tools.rs index cf1c2edb7..23348cacf 100644 --- a/src/windows_registry.rs +++ b/src/windows/find_tools.rs @@ -159,10 +159,10 @@ pub fn find_vs_version() -> Result { #[cfg(windows)] mod impl_ { - use crate::com; - use crate::registry::{RegistryKey, LOCAL_MACHINE}; - use crate::setup_config::SetupConfiguration; - use crate::vs_instances::{VsInstances, VswhereInstance}; + use crate::windows::com; + use crate::windows::registry::{RegistryKey, LOCAL_MACHINE}; + use crate::windows::setup_config::SetupConfiguration; + use crate::windows::vs_instances::{VsInstances, VswhereInstance}; use std::convert::TryFrom; use std::env; use std::ffi::OsString; diff --git a/src/windows/mod.rs b/src/windows/mod.rs new file mode 100644 index 000000000..9b6f297e1 --- /dev/null +++ b/src/windows/mod.rs @@ -0,0 +1,20 @@ +//! These modules are all glue to support reading the MSVC version from +//! the registry and from COM interfaces. + +// This is used in the crate's public API, so don't use #[cfg(windows)] +pub mod find_tools; + +#[cfg(windows)] +pub(crate) mod windows_sys; + +#[cfg(windows)] +mod registry; +#[cfg(windows)] +#[macro_use] +mod winapi; +#[cfg(windows)] +mod com; +#[cfg(windows)] +mod setup_config; +#[cfg(windows)] +mod vs_instances; diff --git a/src/registry.rs b/src/windows/registry.rs similarity index 99% rename from src/registry.rs rename to src/windows/registry.rs index dbfb2c39d..83983032d 100644 --- a/src/registry.rs +++ b/src/windows/registry.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::windows_sys::{ +use crate::windows::windows_sys::{ RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, ERROR_NO_MORE_ITEMS, ERROR_SUCCESS, HKEY, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY, REG_SZ, }; diff --git a/src/setup_config.rs b/src/windows/setup_config.rs similarity index 99% rename from src/setup_config.rs rename to src/windows/setup_config.rs index fe2c03096..5739ecf7d 100644 --- a/src/setup_config.rs +++ b/src/windows/setup_config.rs @@ -8,7 +8,7 @@ #![allow(bad_style)] #![allow(unused)] -use crate::{ +use crate::windows::{ com::{BStr, ComPtr}, winapi::{ IUnknown, IUnknownVtbl, Interface, LCID, LPCOLESTR, LPCWSTR, LPFILETIME, LPSAFEARRAY, diff --git a/src/vs_instances.rs b/src/windows/vs_instances.rs similarity index 98% rename from src/vs_instances.rs rename to src/windows/vs_instances.rs index 31d3dd147..e863dadab 100644 --- a/src/vs_instances.rs +++ b/src/windows/vs_instances.rs @@ -4,7 +4,7 @@ use std::convert::TryFrom; use std::io::BufRead; use std::path::PathBuf; -use crate::setup_config::{EnumSetupInstances, SetupInstance}; +use crate::windows::setup_config::{EnumSetupInstances, SetupInstance}; pub enum VsInstance { Com(SetupInstance), diff --git a/src/winapi.rs b/src/windows/winapi.rs similarity index 92% rename from src/winapi.rs rename to src/windows/winapi.rs index 52790a585..09965daa8 100644 --- a/src/winapi.rs +++ b/src/windows/winapi.rs @@ -11,7 +11,7 @@ use std::os::raw; pub type wchar_t = u16; -pub use crate::windows_sys::{FILETIME, GUID, HRESULT, SAFEARRAY}; +pub use crate::windows::windows_sys::{FILETIME, GUID, HRESULT, SAFEARRAY}; pub type REFIID = *const IID; pub type IID = GUID; @@ -37,7 +37,7 @@ macro_rules! DEFINE_GUID { $name:ident, $l:expr, $w1:expr, $w2:expr, $b1:expr, $b2:expr, $b3:expr, $b4:expr, $b5:expr, $b6:expr, $b7:expr, $b8:expr ) => { - pub const $name: $crate::winapi::GUID = $crate::winapi::GUID { + pub const $name: $crate::windows::winapi::GUID = $crate::windows::winapi::GUID { data1: $l, data2: $w1, data3: $w2, @@ -121,10 +121,10 @@ macro_rules! RIDL { $l:expr, $w1:expr, $w2:expr, $b1:expr, $b2:expr, $b3:expr, $b4:expr, $b5:expr, $b6:expr, $b7:expr, $b8:expr ) => ( - impl $crate::winapi::Interface for $interface { + impl $crate::windows::winapi::Interface for $interface { #[inline] - fn uuidof() -> $crate::winapi::GUID { - $crate::winapi::GUID { + fn uuidof() -> $crate::windows::winapi::GUID { + $crate::windows::winapi::GUID { data1: $l, data2: $w1, data3: $w2, diff --git a/src/windows_sys.rs b/src/windows/windows_sys.rs similarity index 100% rename from src/windows_sys.rs rename to src/windows/windows_sys.rs