Skip to content

Commit

Permalink
Introducing the windows-link crate (#3450)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 16, 2025
1 parent 021257c commit 134e63f
Show file tree
Hide file tree
Showing 326 changed files with 21,421 additions and 21,050 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ jobs:
run: cargo clippy -p windows-implement
- name: Clippy windows-interface
run: cargo clippy -p windows-interface
- name: Clippy windows-link
run: cargo clippy -p windows-link
- name: Clippy windows-registry
run: cargo clippy -p windows-registry
- name: Clippy windows-result
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/msrv-windows-link.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: windows-link

on:
pull_request:
paths-ignore:
- '.github/ISSUE_TEMPLATE/**'
- 'web/**'
push:
paths-ignore:
- '.github/ISSUE_TEMPLATE/**'
- 'web/**'
branches:
- master

jobs:
check:
strategy:
matrix:
rust: [1.71.0, stable, nightly]
runs-on: windows-2022
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Prepare
run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- name: Check
run: cargo check -p windows-link
2 changes: 2 additions & 0 deletions .github/workflows/no-default-features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
run: cargo check -p windows-implement --no-default-features
- name: Check windows-interface
run: cargo check -p windows-interface --no-default-features
- name: Check windows-link
run: cargo check -p windows-link --no-default-features
- name: Check windows-registry
run: cargo check -p windows-registry --no-default-features
- name: Check windows-result
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/raw-dylib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ jobs:
run: cargo test -p windows-implement --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-interface
run: cargo test -p windows-interface --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-link
run: cargo test -p windows-link --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-registry
run: cargo test -p windows-registry --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-result
Expand All @@ -362,6 +364,8 @@ jobs:
run: cargo test -p windows_x86_64_gnu --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows_x86_64_gnullvm
run: cargo test -p windows_x86_64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test windows_x86_64_msvc
run: cargo test -p windows_x86_64_msvc --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Check diff
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ jobs:
run: cargo test -p windows-implement --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-interface
run: cargo test -p windows-interface --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-link
run: cargo test -p windows-link --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-registry
run: cargo test -p windows-registry --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows-result
Expand All @@ -359,6 +361,8 @@ jobs:
run: cargo test -p windows_x86_64_gnu --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test windows_x86_64_gnullvm
run: cargo test -p windows_x86_64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test windows_x86_64_msvc
run: cargo test -p windows_x86_64_msvc --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Check diff
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ windows-result = { path = "crates/libs/result" }
windows-strings = { path = "crates/libs/strings" }
windows-sys = { path = "crates/libs/sys" }
windows-targets = { path = "crates/libs/targets" }
windows-link = { path = "crates/libs/link" }
windows-version = { path = "crates/libs/version" }
helpers = { path = "crates/libs/helpers" }
6 changes: 6 additions & 0 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct Config {
pub sys: bool,
pub implement: bool,
pub derive: Derive,
pub link: String,
}

/// The Windows code generator.
Expand Down Expand Up @@ -88,6 +89,7 @@ where
let mut rustfmt = String::new();
let mut output = String::new();
let mut sys = false;
let mut link = "windows_link".to_string();

for arg in &args {
if arg.starts_with('-') {
Expand All @@ -110,6 +112,7 @@ where
"--package" => package = true,
"--sys" => sys = true,
"--implement" => implement = true,
"--link" => kind = ArgKind::Link,
_ => panic!("invalid option `{arg}`"),
},
ArgKind::Output => {
Expand All @@ -134,6 +137,7 @@ where
derive.push(arg.as_str());
}
ArgKind::Rustfmt => rustfmt = arg.to_string(),
ArgKind::Link => link = arg.to_string(),
}
}

Expand Down Expand Up @@ -179,6 +183,7 @@ where
output,
sys,
implement,
link,
}));

let tree = TypeTree::new(&config.types);
Expand All @@ -199,6 +204,7 @@ enum ArgKind {
Rustfmt,
Reference,
Derive,
Link,
}

#[track_caller]
Expand Down
4 changes: 3 additions & 1 deletion crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ impl CppFn {
quote! {}
};

let link = to_ident(&writer.config.link);

link_fmt(quote! {
windows_targets::link!(#library #abi #symbol fn #name(#(#params),* #vararg) #return_sig);
#link::link!(#library #abi #symbol fn #name(#(#params),* #vararg) #return_sig);
})
}

Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ workspace = true
default-target = "x86_64-pc-windows-msvc"
targets = []

[dependencies.windows-targets]
version = "0.53.0"
path = "../targets"
[dependencies.windows-link]
version = "0.1.0"
path = "../link"

[dependencies.windows-result]
version = "0.3.0"
Expand Down
24 changes: 12 additions & 12 deletions crates/libs/core/src/imp/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
clippy::all
)]

windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL);
windows_targets::link!("ole32.dll" "system" fn CoIncrementMTAUsage(pcookie : *mut CO_MTA_USAGE_COOKIE) -> HRESULT);
windows_targets::link!("ole32.dll" "system" fn CoTaskMemAlloc(cb : usize) -> *mut core::ffi::c_void);
windows_targets::link!("ole32.dll" "system" fn CoTaskMemFree(pv : *const core::ffi::c_void));
windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE);
windows_targets::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const core::ffi::c_void) -> *mut core::ffi::c_void);
windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC);
windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
windows_targets::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoGetActivationFactory(activatableclassid : HSTRING, iid : *const GUID, factory : *mut *mut core::ffi::c_void) -> HRESULT);
windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
windows_link::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL);
windows_link::link!("ole32.dll" "system" fn CoIncrementMTAUsage(pcookie : *mut CO_MTA_USAGE_COOKIE) -> HRESULT);
windows_link::link!("ole32.dll" "system" fn CoTaskMemAlloc(cb : usize) -> *mut core::ffi::c_void);
windows_link::link!("ole32.dll" "system" fn CoTaskMemFree(pv : *const core::ffi::c_void));
windows_link::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE);
windows_link::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const core::ffi::c_void) -> *mut core::ffi::c_void);
windows_link::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL);
windows_link::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC);
windows_link::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
windows_link::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoGetActivationFactory(activatableclassid : HSTRING, iid : *const GUID, factory : *mut *mut core::ffi::c_void) -> HRESULT);
windows_link::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
windows_link::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
pub type BOOL = i32;
pub type CO_MTA_USAGE_COOKIE = *mut core::ffi::c_void;
pub type FARPROC = Option<unsafe extern "system" fn() -> isize>;
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/core/src/imp/com_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#[inline]
pub unsafe fn CoCreateGuid() -> windows_core::Result<windows_core::GUID> {
windows_targets::link!("ole32.dll" "system" fn CoCreateGuid(pguid : *mut windows_core::GUID) -> windows_core::HRESULT);
windows_link::link!("ole32.dll" "system" fn CoCreateGuid(pguid : *mut windows_core::GUID) -> windows_core::HRESULT);
unsafe {
let mut result__ = core::mem::zeroed();
CoCreateGuid(&mut result__).map(|| core::mem::transmute(result__))
Expand All @@ -23,7 +23,7 @@ pub unsafe fn RoGetAgileReference<P2>(
where
P2: windows_core::Param<windows_core::IUnknown>,
{
windows_targets::link!("ole32.dll" "system" fn RoGetAgileReference(options : AgileReferenceOptions, riid : *const windows_core::GUID, punk : * mut core::ffi::c_void, ppagilereference : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
windows_link::link!("ole32.dll" "system" fn RoGetAgileReference(options : AgileReferenceOptions, riid : *const windows_core::GUID, punk : * mut core::ffi::c_void, ppagilereference : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
unsafe {
let mut result__ = core::mem::zeroed();
RoGetAgileReference(options, riid, punk.param().abi(), &mut result__)
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false
[dependencies]
regex = "1.7"
windows-bindgen = { workspace = true }
windows-targets = { workspace = true }
windows-link = { workspace = true }
2 changes: 1 addition & 1 deletion crates/libs/helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn find<P: AsRef<Path>>(path: P, regex: &Regex) -> Vec<(String, String)> {

pub fn set_thread_ui_language() {
// Enables testing without pulling in a dependency on the `windows` crate.
windows_targets::link!("kernel32.dll" "system" fn SetThreadPreferredUILanguages(flags : u32, language : *const u16, _ : *mut u32) -> i32);
windows_link::link!("kernel32.dll" "system" fn SetThreadPreferredUILanguages(flags : u32, language : *const u16, _ : *mut u32) -> i32);
pub const MUI_LANGUAGE_NAME: u32 = 8u32;

let language: Vec<_> = "en-US".encode_utf16().chain(std::iter::once(0)).collect();
Expand Down
14 changes: 14 additions & 0 deletions crates/libs/link/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

[package]
name = "windows-link"
version = "0.1.0"
authors = ["Microsoft"]
edition = "2021"
rust-version = "1.71"
license = "MIT OR Apache-2.0"
description = "Linking for Windows"
repository = "https://github.com/microsoft/windows-rs"
readme = "readme.md"

[lints]
workspace = true
Loading

0 comments on commit 134e63f

Please sign in to comment.