Skip to content

Commit

Permalink
Auto merge of #129821 - ChrisDenton:raw-dylib-std, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Switch std to using raw-dylib by default on Windows

The `raw-dylib` feature allows rust to compile code even without have the Windows SDK installed (or mingw equivalent). We were already using it to work around a couple of import library issues, this just makes it the default for everything. The `windows_use_import_libs` std feature can be used to disable this.

try-job: dist-aarch64-msvc
  • Loading branch information
bors committed Sep 16, 2024
2 parents 13b5a4e + e528d5d commit 3a238f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 44 deletions.
5 changes: 2 additions & 3 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ std_detect_file_io = ["std_detect/std_detect_file_io"]
std_detect_dlsym_getauxval = ["std_detect/std_detect_dlsym_getauxval"]
std_detect_env_override = ["std_detect/std_detect_env_override"]

# Enable using raw-dylib for Windows imports.
# This will eventually be the default.
windows_raw_dylib = ["windows-targets/windows_raw_dylib"]
# Enable using Windows import libraries instead of raw-dylib.
windows_use_import_libs = ["windows-targets/windows_use_import_libs"]

[package.metadata.fortanix-sgx]
# Maximum possible number of threads when testing
Expand Down
42 changes: 8 additions & 34 deletions library/std/src/sys/pal/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ if #[cfg(not(target_vendor = "uwp"))] {
}
}

// Use raw-dylib to import ProcessPrng as we can't rely on there being an import library.
#[cfg(not(target_vendor = "win7"))]
#[cfg_attr(
target_arch = "x86",
link(name = "bcryptprimitives", kind = "raw-dylib", import_name_type = "undecorated")
)]
#[cfg_attr(not(target_arch = "x86"), link(name = "bcryptprimitives", kind = "raw-dylib"))]
extern "system" {
pub fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL;
// When not using raw-dylib, there isn't a reliable import library for ProcessPrng
// so we lazily load it instead.
#[cfg(all(feature = "windows_use_import_libs", not(target_vendor = "win7")))]
compat_fn_with_fallback! {
pub static BCRYPTPRIMITIVES: &CStr = c"bcryptprimitives";
pub fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL {
panic!("ProcessPrng is not available");
}
}

// Functions that aren't available on every version of Windows that we support,
Expand Down Expand Up @@ -150,31 +149,6 @@ compat_fn_with_fallback! {
}
}

#[cfg(not(target_vendor = "win7"))]
// Use raw-dylib to import synchronization functions to workaround issues with the older mingw import library.
#[cfg_attr(
target_arch = "x86",
link(
name = "api-ms-win-core-synch-l1-2-0",
kind = "raw-dylib",
import_name_type = "undecorated"
)
)]
#[cfg_attr(
not(target_arch = "x86"),
link(name = "api-ms-win-core-synch-l1-2-0", kind = "raw-dylib")
)]
extern "system" {
pub fn WaitOnAddress(
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: u32,
) -> BOOL;
pub fn WakeByAddressSingle(address: *const c_void);
pub fn WakeByAddressAll(address: *const c_void);
}

#[cfg(target_vendor = "win7")]
compat_fn_optional! {
crate::sys::compat::load_synch_functions();
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/windows/c/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,7 @@ Windows.Win32.Networking.WinSock.WSATRY_AGAIN
Windows.Win32.Networking.WinSock.WSATYPE_NOT_FOUND
Windows.Win32.Networking.WinSock.WSAVERNOTSUPPORTED
Windows.Win32.Security.Authentication.Identity.RtlGenRandom
Windows.Win32.Security.Cryptography.ProcessPrng
Windows.Win32.Security.SECURITY_ATTRIBUTES
Windows.Win32.Security.TOKEN_ACCESS_MASK
Windows.Win32.Security.TOKEN_ACCESS_PSEUDO_HANDLE
Expand Down Expand Up @@ -2595,7 +2596,10 @@ Windows.Win32.System.Threading.TryAcquireSRWLockShared
Windows.Win32.System.Threading.UpdateProcThreadAttribute
Windows.Win32.System.Threading.WaitForMultipleObjects
Windows.Win32.System.Threading.WaitForSingleObject
Windows.Win32.System.Threading.WaitOnAddress
Windows.Win32.System.Threading.WakeAllConditionVariable
Windows.Win32.System.Threading.WakeByAddressAll
Windows.Win32.System.Threading.WakeByAddressSingle
Windows.Win32.System.Threading.WakeConditionVariable
Windows.Win32.System.WindowsProgramming.PROGRESS_CONTINUE
Windows.Win32.UI.Shell.GetUserProfileDirectoryW
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/windows/c/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
windows_targets::link!("advapi32.dll" "system" fn OpenProcessToken(processhandle : HANDLE, desiredaccess : TOKEN_ACCESS_MASK, tokenhandle : *mut HANDLE) -> BOOL);
windows_targets::link!("advapi32.dll" "system" "SystemFunction036" fn RtlGenRandom(randombuffer : *mut core::ffi::c_void, randombufferlength : u32) -> BOOLEAN);
windows_targets::link!("api-ms-win-core-synch-l1-2-0.dll" "system" fn WaitOnAddress(address : *const core::ffi::c_void, compareaddress : *const core::ffi::c_void, addresssize : usize, dwmilliseconds : u32) -> BOOL);
windows_targets::link!("api-ms-win-core-synch-l1-2-0.dll" "system" fn WakeByAddressAll(address : *const core::ffi::c_void));
windows_targets::link!("api-ms-win-core-synch-l1-2-0.dll" "system" fn WakeByAddressSingle(address : *const core::ffi::c_void));
windows_targets::link!("bcryptprimitives.dll" "system" fn ProcessPrng(pbdata : *mut u8, cbdata : usize) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn AcquireSRWLockExclusive(srwlock : *mut SRWLOCK));
windows_targets::link!("kernel32.dll" "system" fn AcquireSRWLockShared(srwlock : *mut SRWLOCK));
windows_targets::link!("kernel32.dll" "system" fn AddVectoredExceptionHandler(first : u32, handler : PVECTORED_EXCEPTION_HANDLER) -> *mut core::ffi::c_void);
Expand Down
2 changes: 1 addition & 1 deletion library/sysroot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ profiler = ["std/profiler"]
std_detect_file_io = ["std/std_detect_file_io"]
std_detect_dlsym_getauxval = ["std/std_detect_dlsym_getauxval"]
std_detect_env_override = ["std/std_detect_env_override"]
windows_raw_dylib = ["std/windows_raw_dylib"]
windows_use_import_libs = ["std/windows_use_import_libs"]
5 changes: 2 additions & 3 deletions library/windows_targets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ version = "0.0.0"
edition = "2021"

[features]
# Enable using raw-dylib for Windows imports.
# This will eventually be the default.
windows_raw_dylib = []
# Enable using Windows import libraries instead of raw-dylib.
windows_use_import_libs = []
7 changes: 4 additions & 3 deletions library/windows_targets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![feature(decl_macro)]
#![feature(no_core)]

#[cfg(feature = "windows_raw_dylib")]
#[cfg(not(feature = "windows_use_import_libs"))]
pub macro link {
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
#[cfg_attr(not(target_arch = "x86"), link(name = $library, kind = "raw-dylib", modifiers = "+verbatim"))]
Expand All @@ -18,7 +18,7 @@ pub macro link {
}
)
}
#[cfg(not(feature = "windows_raw_dylib"))]
#[cfg(feature = "windows_use_import_libs")]
pub macro link {
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
// Note: the windows-targets crate uses a pre-built Windows.lib import library which we don't
Expand All @@ -33,10 +33,11 @@ pub macro link {
)
}

#[cfg(not(feature = "windows_raw_dylib"))]
#[cfg(feature = "windows_use_import_libs")]
#[link(name = "advapi32")]
#[link(name = "ntdll")]
#[link(name = "userenv")]
#[link(name = "ws2_32")]
#[link(name = "dbghelp")] // required for backtrace-rs symbolization
#[link(name = "synchronization")]
extern "C" {}

0 comments on commit 3a238f0

Please sign in to comment.