about summary refs log tree commit diff
path: root/library/windows_targets/src/lib.rs
blob: 3446e2113dda99b0abc5b44a61b0fae88e420200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! 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)]

pub macro link_raw_dylib {
    ($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"))]
        unsafe extern $abi {
            $(#[link_name=$link_name])?
            pub fn $($function)*;
        }
    )
}

pub macro link_dylib {
    ($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")]
        unsafe extern $abi {
            $(#[link_name=$link_name])?
            pub fn $($function)*;
        }
    )
}

#[cfg(feature = "windows_raw_dylib")]
pub macro link($($tt:tt)*) {
    $crate::link_raw_dylib!($($tt)*);
}

#[cfg(not(feature = "windows_raw_dylib"))]
pub macro link($($tt:tt)*) {
    $crate::link_dylib!($($tt)*);
}

#[cfg(not(feature = "windows_raw_dylib"))]
#[cfg(not(target_os = "cygwin"))] // Cygwin doesn't need these libs
#[cfg_attr(target_vendor = "win7", link(name = "advapi32"))]
#[link(name = "ntdll")]
#[link(name = "userenv")]
#[link(name = "ws2_32")]
#[link(name = "dbghelp")] // required for backtrace-rs symbolization
unsafe extern "C" {}