about summary refs log tree commit diff
path: root/library/windows_targets
diff options
context:
space:
mode:
authorChris Denton <chris@chrisdenton.dev>2024-08-09 01:32:13 +0000
committerChris Denton <chris@chrisdenton.dev>2024-08-09 10:43:43 +0000
commitacb024110f337e77c515ebcafffd6c4c09c29ee7 (patch)
tree49af03529375374576e439a818c3eaf70bbc5bd8 /library/windows_targets
parentc7b0d4e81f56da02d41f73fb5a85673a29f6cbc9 (diff)
downloadrust-acb024110f337e77c515ebcafffd6c4c09c29ee7.tar.gz
rust-acb024110f337e77c515ebcafffd6c4c09c29ee7.zip
Add windows-targets crate to std's sysroot
Diffstat (limited to 'library/windows_targets')
-rw-r--r--library/windows_targets/Cargo.toml10
-rw-r--r--library/windows_targets/src/lib.rs41
2 files changed, 51 insertions, 0 deletions
diff --git a/library/windows_targets/Cargo.toml b/library/windows_targets/Cargo.toml
new file mode 100644
index 00000000000..94d7c821064
--- /dev/null
+++ b/library/windows_targets/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "windows-targets"
+description = "A drop-in replacement for the real windows-targets crate for use in std only."
+version = "0.0.0"
+edition = "2021"
+
+[features]
+# Enable using raw-dylib for Windows imports.
+# This will eventually be the default.
+windows_raw_dylib = []
diff --git a/library/windows_targets/src/lib.rs b/library/windows_targets/src/lib.rs
new file mode 100644
index 00000000000..1965b6cf4ce
--- /dev/null
+++ b/library/windows_targets/src/lib.rs
@@ -0,0 +1,41 @@
+//! Provides the `link!` macro used by the generated windows bindings.
+//!
+//! This is a simple wrapper around an `extern` block with a `#[link]` attribute.
+//! It's very roughly equivalent to the windows-targets crate.
+#![no_std]
+#![no_core]
+#![feature(decl_macro)]
+#![feature(no_core)]
+
+#[cfg(feature = "windows_raw_dylib")]
+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"))]
+        #[cfg_attr(target_arch = "x86", link(name = $library, kind = "raw-dylib", modifiers = "+verbatim", import_name_type = "undecorated"))]
+        extern $abi {
+            $(#[link_name=$link_name])?
+            pub fn $($function)*;
+        }
+    )
+}
+#[cfg(not(feature = "windows_raw_dylib"))]
+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
+        // have in this repo. So instead we always link kernel32.lib and add the rest of the import
+        // libraries below by using an empty extern block. This works because extern blocks are not
+        // connected to the library given in the #[link] attribute.
+        #[link(name = "kernel32")]
+        extern $abi {
+            $(#[link_name=$link_name])?
+            pub fn $($function)*;
+        }
+    )
+}
+
+#[cfg(not(feature = "windows_raw_dylib"))]
+#[link(name = "advapi32")]
+#[link(name = "ntdll")]
+#[link(name = "userenv")]
+#[link(name = "ws2_32")]
+extern "C" {}