From 5883d7177abf3a2324cfd5e7c2d28469787b1427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Fri, 28 Feb 2025 23:17:56 +0800 Subject: [PATCH] Add cygwin support Co-authored-by: Ookiineko --- build.rs | 14 ++++++++------ examples/intrinsics.rs | 6 +++--- src/macros.rs | 18 +++++++++--------- src/probestack.rs | 4 ++-- src/x86_64.rs | 6 +++++- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/build.rs b/build.rs index 39cee311..3c04423f 100644 --- a/build.rs +++ b/build.rs @@ -575,7 +575,7 @@ mod c { ("__fe_raise_inexact", "fp_mode.c"), ]); - if target.os != "windows" { + if target.os != "windows" && target.os != "cygwin" { sources.extend(&[("__multc3", "multc3.c")]); } } @@ -608,13 +608,15 @@ mod c { sources.remove(&["__aeabi_cdcmp", "__aeabi_cfcmp"]); } - // Android uses emulated TLS so we need a runtime support function. - if target.os == "android" { + // Android and Cygwin uses emulated TLS so we need a runtime support function. + if target.os == "android" || target.os == "cygwin" { sources.extend(&[("__emutls_get_address", "emutls.c")]); + } - // Work around a bug in the NDK headers (fixed in - // https://r.android.com/2038949 which will be released in a future - // NDK version) by providing a definition of LONG_BIT. + // Work around a bug in the NDK headers (fixed in + // https://r.android.com/2038949 which will be released in a future + // NDK version) by providing a definition of LONG_BIT. + if target.os == "android" { cfg.define("LONG_BIT", "(8 * sizeof(long))"); } diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index 59a70e20..e90cfb33 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -673,17 +673,17 @@ pub fn __aeabi_unwind_cpp_pr0() {} #[no_mangle] pub fn __aeabi_unwind_cpp_pr1() {} -#[cfg(not(windows))] +#[cfg(not(any(windows, target_os = "cygwin")))] #[allow(non_snake_case)] #[no_mangle] pub fn _Unwind_Resume() {} -#[cfg(not(windows))] +#[cfg(not(any(windows, target_os = "cygwin")))] #[lang = "eh_personality"] #[no_mangle] pub extern "C" fn eh_personality() {} -#[cfg(all(windows, target_env = "gnu"))] +#[cfg(any(all(windows, target_env = "gnu"), target_os = "cygwin"))] mod mingw_unwinding { #[no_mangle] pub fn rust_eh_personality() {} diff --git a/src/macros.rs b/src/macros.rs index 5d9f5819..b1b71379 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -193,7 +193,7 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( - #[cfg(all(any(windows, all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64"))] + #[cfg(all(any(windows, target_os = "cygwin", all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64"))] intrinsics! { $(#[$($attr)*])* pub extern "unadjusted" fn $name( $($argname: $ty),* ) $(-> $ret)? { @@ -201,7 +201,7 @@ macro_rules! intrinsics { } } - #[cfg(not(all(any(windows, all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64")))] + #[cfg(not(all(any(windows, target_os = "cygwin", all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64")))] intrinsics! { $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? { @@ -257,7 +257,7 @@ macro_rules! intrinsics { #[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"), not(feature = "mangled-names")))] mod $name { #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] $(#[$($attr)*])* extern $abi fn $name( $($argname: u16),* ) $(-> $ret)? { super::$name($(f16::from_bits($argname)),*) @@ -293,7 +293,7 @@ macro_rules! intrinsics { #[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"), not(feature = "mangled-names")))] mod $name { #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] $(#[$($attr)*])* extern $abi fn $name( $($argname: $ty),* ) -> u16 { super::$name($($argname),*).to_bits() @@ -334,7 +334,7 @@ macro_rules! intrinsics { #[cfg(all(target_arch = "arm", not(feature = "mangled-names")))] mod $name { #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] $(#[$($attr)*])* extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? { super::$name($($argname),*) @@ -344,7 +344,7 @@ macro_rules! intrinsics { #[cfg(all(target_arch = "arm", not(feature = "mangled-names")))] mod $alias { #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] $(#[$($attr)*])* extern "aapcs" fn $alias( $($argname: $ty),* ) $(-> $ret)? { super::$name($($argname),*) @@ -411,7 +411,7 @@ macro_rules! intrinsics { mod $name { $(#[$($attr)*])* #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? { super::$name($($argname),*) } @@ -436,7 +436,7 @@ macro_rules! intrinsics { #[naked] $(#[$($attr)*])* #[cfg_attr(not(feature = "mangled-names"), no_mangle)] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? { $($body)* } @@ -503,7 +503,7 @@ macro_rules! intrinsics { mod $name { $(#[$($attr)*])* #[no_mangle] - #[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")] + #[cfg_attr(not(any(all(windows, target_env = "gnu"), target_os = "cygwin")), linkage = "weak")] $(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? { super::$name($($argname),*) } diff --git a/src/probestack.rs b/src/probestack.rs index 0c30384d..5b6abd21 100644 --- a/src/probestack.rs +++ b/src/probestack.rs @@ -42,8 +42,8 @@ //! be more than welcome to accept such a change! #![cfg(not(feature = "mangled-names"))] -// Windows already has builtins to do this. -#![cfg(not(windows))] +// Windows and Cygwin already has builtins to do this. +#![cfg(not(any(windows, target_os = "cygwin")))] // All these builtins require assembly #![cfg(not(feature = "no-asm"))] // We only define stack probing for these architectures today. diff --git a/src/x86_64.rs b/src/x86_64.rs index 9c91a455..aae601f5 100644 --- a/src/x86_64.rs +++ b/src/x86_64.rs @@ -10,7 +10,11 @@ use core::intrinsics; intrinsics! { #[naked] #[cfg(all( - any(all(windows, target_env = "gnu"), target_os = "uefi"), + any( + all(windows, target_env = "gnu"), + target_os = "cygwin", + target_os = "uefi" + ), not(feature = "no-asm") ))] pub unsafe extern "C" fn ___chkstk_ms() {