From c437ad1e8dfcbc1705cb7195ddac24a4874f1123 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= <Strawberry_Str@hotmail.com>
Date: Sat, 8 Mar 2025 23:56:57 +0800
Subject: [PATCH 1/3] Add cygwin support

---
 Cargo.toml       | 3 ++-
 src/backends.rs  | 1 +
 src/util_libc.rs | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index abd0d79b..eca00f4b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -47,7 +47,7 @@ r-efi = { version = "5.1", default-features = false }
 libc = { version = "0.2.154", default-features = false }
 
 # getrandom
-[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
+[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "cygwin", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
 libc = { version = "0.2.154", default-features = false }
 
 # netbsd
@@ -87,6 +87,7 @@ check-cfg = [
   'cfg(getrandom_test_linux_fallback)',
   'cfg(getrandom_test_linux_without_fallback)',
   'cfg(getrandom_test_netbsd_fallback)',
+  'cfg(target_os, values("cygwin"))'
 ]
 
 [package.metadata.docs.rs]
diff --git a/src/backends.rs b/src/backends.rs
index 82141a2b..adf8a0d0 100644
--- a/src/backends.rs
+++ b/src/backends.rs
@@ -104,6 +104,7 @@ cfg_if! {
         target_os = "freebsd",
         target_os = "hurd",
         target_os = "illumos",
+        target_os = "cygwin",
         // Check for target_arch = "arm" to only include the 3DS. Does not
         // include the Nintendo Switch (which is target_arch = "aarch64").
         all(target_os = "horizon", target_arch = "arm"),
diff --git a/src/util_libc.rs b/src/util_libc.rs
index 80003013..24c53c0c 100644
--- a/src/util_libc.rs
+++ b/src/util_libc.rs
@@ -2,7 +2,7 @@ use crate::Error;
 use core::mem::MaybeUninit;
 
 cfg_if! {
-    if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
+    if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android", target_os = "cygwin"))] {
         use libc::__errno as errno_location;
     } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
         use libc::__errno_location as errno_location;

From 5105695f51ac375060bf2bc1cbac5ab49d92bfaf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= <Strawberry_Str@hotmail.com>
Date: Sun, 9 Mar 2025 13:45:51 +0800
Subject: [PATCH 2/3] Apply suggestions on doc & comments

---
 .github/workflows/build.yml | 3 +++
 CHANGELOG.md                | 2 ++
 Cargo.toml                  | 2 +-
 README.md                   | 2 ++
 4 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index ed9788d8..d63f9dbd 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -94,6 +94,9 @@ jobs:
           x86_64-wrs-vxworks,
           x86_64-unknown-dragonfly,
           x86_64-unknown-haiku,
+          # TODO: once libstd support for cygwin is added
+          # https://github.com/rust-lang/rust/pull/137621
+          # x86_64-pc-cygwin,
         ]
     steps:
       - uses: actions/checkout@v4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index acfa3719..b53ccd03 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - `.cargo/config.toml` example in the crate-level docs [#591]
 - `getrandom_test_linux_without_fallback` configuration flag to test that file fallback
   is not triggered in the `linux_android_with_fallback` backend [#605]
+- Cygwin support [#626]
 
 ### Changed
 - Remove `windows-targets` dependency and use [`raw-dylib`] directly [#627]
@@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 [#605]: https://github.com/rust-random/getrandom/pull/605
 [#610]: https://github.com/rust-random/getrandom/pull/610
 [#614]: https://github.com/rust-random/getrandom/pull/614
+[#626]: https://github.com/rust-random/getrandom/pull/626
 [#627]: https://github.com/rust-random/getrandom/pull/627
 [`raw-dylib`]: https://doc.rust-lang.org/reference/items/external-blocks.html?highlight=link#dylib-versus-raw-dylib
 
diff --git a/Cargo.toml b/Cargo.toml
index eca00f4b..e55cbb89 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -87,7 +87,7 @@ check-cfg = [
   'cfg(getrandom_test_linux_fallback)',
   'cfg(getrandom_test_linux_without_fallback)',
   'cfg(getrandom_test_netbsd_fallback)',
-  'cfg(target_os, values("cygwin"))'
+  'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this.
 ]
 
 [package.metadata.docs.rs]
diff --git a/README.md b/README.md
index 1c6cb3e8..125daa08 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,7 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
 | PS Vita            | `*-vita-*`         | [`getentropy`][19]
 | QNX Neutrino       | `*‑nto-qnx*`       | [`/dev/urandom`][14] (identical to `/dev/random`)
 | AIX                | `*-ibm-aix`        | [`/dev/urandom`][15]
+| Cygwin             | `*-cygwin`         | [`getrandom`][20] (based on [`RtlGenRandom`])
 
 Pull Requests that add support for new targets to `getrandom` are always welcome.
 
@@ -351,6 +352,7 @@ dual licensed as above, without any additional terms or conditions.
 [17]: https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-getrandom
 [18]: https://github.com/rust3ds/shim-3ds/commit/b01d2568836dea2a65d05d662f8e5f805c64389d
 [19]: https://github.com/vitasdk/newlib/blob/2d869fe47aaf02b8e52d04e9a2b79d5b210fd016/newlib/libc/sys/vita/getentropy.c
+[20]: https://github.com/cygwin/cygwin/blob/main/winsup/cygwin/libc/getentropy.cc
 
 [`ProcessPrng`]: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
 [`RtlGenRandom`]: https://learn.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom

From 4d737a008b9a9ec44c7e7a08ab140bbcf26f7667 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= <Strawberry_Str@hotmail.com>
Date: Sun, 9 Mar 2025 15:22:39 +0800
Subject: [PATCH 3/3] Update .github/workflows/build.yml

Co-authored-by: Joe Richey <dev@richey.us>
---
 .github/workflows/build.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d63f9dbd..d0c2feb7 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -94,8 +94,8 @@ jobs:
           x86_64-wrs-vxworks,
           x86_64-unknown-dragonfly,
           x86_64-unknown-haiku,
-          # TODO: once libstd support for cygwin is added
-          # https://github.com/rust-lang/rust/pull/137621
+          # TODO: once libc support for cygwin is released
+          # https://github.com/rust-lang/libc/pull/4308
           # x86_64-pc-cygwin,
         ]
     steps: