ring/
prefixed.rs

1// Keep in sync with `core_name_and_version` in build.rs.
2macro_rules! core_name_and_version {
3    () => {
4        concat!(
5            env!("CARGO_PKG_NAME"),
6            "_core_",
7            env!("CARGO_PKG_VERSION_MAJOR"),
8            "_",
9            env!("CARGO_PKG_VERSION_MINOR"),
10            "_",
11            env!("CARGO_PKG_VERSION_PATCH"),
12            "_",
13            env!("CARGO_PKG_VERSION_PRE"), // Often empty
14        )
15    };
16}
17
18// Keep in sync with `prefix` in build.rs.
19macro_rules! prefix {
20    ( ) => {
21        concat!(core_name_and_version!(), "_")
22    };
23}
24
25macro_rules! prefixed_extern {
26    // Functions.
27    {
28        $(
29            $( #[$meta:meta] )*
30            $vis:vis fn $name:ident ( $( $arg_pat:ident : $arg_ty:ty ),* $(,)? )
31            $( -> $ret_ty:ty )?;
32        )+
33    } => {
34        extern "C" {
35            $(
36                prefixed_item! {
37                    link_name
38                    $name
39                    {
40                        $( #[$meta] )*
41                        $vis fn $name ( $( $arg_pat : $arg_ty ),* ) $( -> $ret_ty )?;
42                    }
43
44                }
45            )+
46        }
47    };
48
49    // A `static` global variable.
50    {
51        $( #[$meta:meta] )*
52        $vis:vis static $name:ident: $typ:ty;
53    } => {
54        extern "C" {
55            prefixed_item! {
56                link_name
57                $name
58                {
59                    $( #[$meta] )*
60                    $vis static $name: $typ;
61                }
62            }
63        }
64    };
65
66    // A `static mut` global variable.
67    {
68        $( #[$meta:meta] )*
69        $vis:vis static mut $name:ident: $typ:ty;
70    } => {
71        extern "C" {
72            prefixed_item! {
73                link_name
74                $name
75                {
76                    $( #[$meta] )*
77                    $vis static mut $name: $typ;
78                }
79            }
80        }
81    };
82}
83
84#[deprecated = "`#[export_name]` creates problems and we will stop doing it."]
85#[cfg(not(any(
86    all(target_arch = "aarch64", target_endian = "little"),
87    all(target_arch = "arm", target_endian = "little"),
88    target_arch = "x86",
89    target_arch = "x86_64"
90)))]
91macro_rules! prefixed_export {
92    // A function.
93    {
94        $( #[$meta:meta] )*
95        $vis:vis unsafe extern "C"
96        fn $name:ident ( $( $arg_pat:ident : $arg_ty:ty ),* $(,)? ) $body:block
97    } => {
98        prefixed_item! {
99            export_name
100            $name
101            {
102                $( #[$meta] )*
103                $vis unsafe extern "C" fn $name ( $( $arg_pat : $arg_ty ),* ) $body
104            }
105        }
106    };
107}
108
109macro_rules! prefixed_item {
110    {
111        $attr:ident
112        $name:ident
113        { $item:item }
114    } => {
115        #[$attr = concat!(prefix!(), stringify!($name))]
116        $item
117    };
118}