From 66dc55c292d22767ca9d487ef70886f8e4ddab0b Mon Sep 17 00:00:00 2001
From: Lars Kanis <lars@greiz-reinsdorf.de>
Date: Wed, 22 Jan 2025 06:59:41 +0100
Subject: [PATCH 1/3] Add new platform aarch64-mingw-ucrt aka Windows on ARM

It fits to the latest RubyInstaller release:
  https://rubyinstaller.org/2025/01/19/rubyinstaller-3.4.1-2-released.html

The llvm binaries are taken from https://github.com/mstorsjo/llvm-mingw which looks pretty well maintained.
There's also a "Known issues" section, which lists some helpful instructions for porting.

Fixes #148
---
 .github/workflows/ci.yml             |  1 +
 .github/workflows/publish-images.yml |  1 +
 .github/workflows/release-images.yml |  1 +
 Dockerfile.mri.erb                   | 15 +++++++++++++--
 README.md                            |  2 ++
 Rakefile                             |  1 +
 test/rcd_test/Rakefile               |  1 +
 7 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6eb1eca..1373162 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -48,6 +48,7 @@ jobs:
           - platform: aarch64-linux-gnu
             alias:    aarch64-linux
           - platform: aarch64-linux-musl
+          - platform: aarch64-mingw-ucrt
           - platform: arm-linux-gnu
             alias:    arm-linux
           - platform: arm-linux-musl
diff --git a/.github/workflows/publish-images.yml b/.github/workflows/publish-images.yml
index 85cd73c..68db908 100644
--- a/.github/workflows/publish-images.yml
+++ b/.github/workflows/publish-images.yml
@@ -15,6 +15,7 @@ jobs:
         platform:
           - aarch64-linux-gnu
           - aarch64-linux-musl
+          - aarch64-mingw-ucrt
           - arm-linux-gnu
           - arm-linux-musl
           - arm64-darwin
diff --git a/.github/workflows/release-images.yml b/.github/workflows/release-images.yml
index 4787736..53047a8 100644
--- a/.github/workflows/release-images.yml
+++ b/.github/workflows/release-images.yml
@@ -22,6 +22,7 @@ jobs:
         platform:
           - aarch64-linux-gnu
           - aarch64-linux-musl
+          - aarch64-mingw-ucrt
           - arm-linux-gnu
           - arm-linux-musl
           - arm64-darwin
diff --git a/Dockerfile.mri.erb b/Dockerfile.mri.erb
index 8c4d981..beca069 100644
--- a/Dockerfile.mri.erb
+++ b/Dockerfile.mri.erb
@@ -24,6 +24,13 @@ COPY --from=larskanis/mingw64-ucrt:20.04 \
     /debs/
 RUN dpkg -i /debs/*.deb
 
+<% elsif platform =~ /aarch64-mingw-ucrt/ %>
+RUN wget https://github.com/mstorsjo/llvm-mingw/releases/download/20250114/llvm-mingw-20250114-ucrt-ubuntu-20.04-<%= RUBY_PLATFORM[/^\w+/] %>.tar.xz && \
+    tar xf llvm-mingw*.tar.xz && \
+    export MINGW_PATH=`ls -d llvm-mingw-*/` && \
+    echo "export PATH=`pwd`/$MINGW_PATH/bin:\$PATH" >> /etc/rubybashrc && \
+    rm -r $MINGW_PATH/bin/i686-w64* $MINGW_PATH/bin/armv7-w64* $MINGW_PATH/bin/x86_64-w64* $MINGW_PATH/i686-w64* $MINGW_PATH/armv7-w64* $MINGW_PATH/x86_64-w64*
+
 <% elsif platform =~ /linux-musl/ %>
 COPY build/mk_musl_cross.sh /tmp
 RUN /tmp/mk_musl_cross.sh <%= target %>
@@ -117,6 +124,10 @@ xrubies_build_plan = if platform =~ /x64-mingw-ucrt/
     # Rubyinstaller-3.1+ is platform x64-mingw-ucrt
     ["3.4.1:3.3.7:3.2.6:3.1.6", "3.4.1"],
   ]
+elsif platform =~ /aarch64-mingw-ucrt/
+  [
+    ["3.4.1", "3.1.6"],
+  ]
 elsif platform =~ /x64-mingw32/
   [
     # Rubyinstaller prior to 3.1 is platform x64-mingw32
@@ -131,7 +142,7 @@ else
   ]
 end
 
-strip = '-s' if platform !~ /darwin/
+strip = '-s' if platform !~ /darwin|aarch64-mingw/
 
 xrubies_build_plan.each do |xrubies, bootstrap_ruby_version| %>
 RUN bash -c " \
@@ -187,7 +198,7 @@ RUN find /usr/local/rake-compiler/ruby/*/*/lib/ruby -name rbconfig.rb | while re
 ##
 USER root
 
-<% if platform =~ /mingw/ %>
+<% if platform =~ /(x64|x86)-mingw/ %>
 # Install wrappers for strip commands as a workaround for "Protocol error" in boot2docker.
 COPY build/strip_wrapper_vbox /root/
 RUN mv /usr/bin/<%= target %>-strip /usr/bin/<%= target %>-strip.bin && \
diff --git a/README.md b/README.md
index cd36cd7..a957da1 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,7 @@ The following platforms are supported for cross-compilation by rake-compiler-doc
 
 - `aarch64-linux` and `aarch64-linux-gnu`
 - `aarch64-linux-musl`
+- `aarch64-mingw-ucrt`
 - `arm-linux` and `arm-linux-gnu`
 - `arm-linux-musl`
 - `arm64-darwin`
@@ -193,6 +194,7 @@ This can be done like this:
 PLATFORMS = %w[
   aarch64-linux-gnu
   aarch64-linux-musl
+  aarch64-mingw-ucrt
   arm-linux-gnu
   arm-linux-musl
   arm64-darwin
diff --git a/Rakefile b/Rakefile
index 65f3bb8..e8f42cb 100644
--- a/Rakefile
+++ b/Rakefile
@@ -12,6 +12,7 @@ platforms = [
   # tuple is [platform, target]
   ["aarch64-linux-gnu", "aarch64-linux-gnu"],
   ["aarch64-linux-musl", "aarch64-linux-musl"],
+  ["aarch64-mingw-ucrt", "aarch64-w64-mingw32"],
   ["arm-linux-gnu", "arm-linux-gnueabihf"],
   ["arm-linux-musl", "arm-linux-musleabihf"],
   ["arm64-darwin", "aarch64-apple-darwin"],
diff --git a/test/rcd_test/Rakefile b/test/rcd_test/Rakefile
index 252f94c..2467191 100644
--- a/test/rcd_test/Rakefile
+++ b/test/rcd_test/Rakefile
@@ -22,6 +22,7 @@ else
       aarch64-linux
       aarch64-linux-gnu
       aarch64-linux-musl
+      aarch64-mingw-ucrt
       arm-linux
       arm-linux-gnu
       arm-linux-musl

From 2ac348c7d0ad46e9b81bed86bddccddaa0d714e1 Mon Sep 17 00:00:00 2001
From: Lars Kanis <lars@greiz-reinsdorf.de>
Date: Sat, 1 Feb 2025 20:48:05 +0100
Subject: [PATCH 2/3] Add -Wall and -Werror to rcd_test compiler options

This is done by some extensions and it shouldn't fail.
---
 test/rcd_test/ext/mri/extconf.rb | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/test/rcd_test/ext/mri/extconf.rb b/test/rcd_test/ext/mri/extconf.rb
index d075f79..174408b 100644
--- a/test/rcd_test/ext/mri/extconf.rb
+++ b/test/rcd_test/ext/mri/extconf.rb
@@ -18,6 +18,8 @@
   puts "cc --version: #{ %x[#{CONFIG['CC']} --version].lines.first}"
   puts "-"*70
 
+  $CFLAGS << " -Wall -Werror"
+
   have_func('rb_thread_call_without_gvl', 'ruby/thread.h') ||
       raise("rb_thread_call_without_gvl() not found")
 

From e722508da7377a2e9188b067f54f4c99a2dad4f2 Mon Sep 17 00:00:00 2001
From: Lars Kanis <lars@greiz-reinsdorf.de>
Date: Sat, 1 Feb 2025 21:00:53 +0100
Subject: [PATCH 3/3] Avoid the warning raised by clang on aarch64-mingw-ucrt

The warning is raised when compiling a C extension within the ruby header files:
  warning: implicit conversion loses integer precision: 'unsigned long long' to 'long' [-Wshorten-64-to-32]

When the extconf.rb enforces "-Werror" then this warning breaks mkmf tests and the subsequent build.
---
 .../0004-Enable-build-of-static-libruby.patch                  | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/build/patches/rake-compiler-1.2.9/0004-Enable-build-of-static-libruby.patch b/build/patches/rake-compiler-1.2.9/0004-Enable-build-of-static-libruby.patch
index 72a34da..f93a7e7 100644
--- a/build/patches/rake-compiler-1.2.9/0004-Enable-build-of-static-libruby.patch
+++ b/build/patches/rake-compiler-1.2.9/0004-Enable-build-of-static-libruby.patch
@@ -2,7 +2,7 @@ diff --git a/tasks/bin/cross-ruby.rake b/tasks/bin/cross-ruby.rake
 index 8317a2a3..8ed21718 100644
 --- a/tasks/bin/cross-ruby.rake
 +++ b/tasks/bin/cross-ruby.rake
-@@ -116,11 +116,30 @@
+@@ -116,11 +116,31 @@
          "--host=#{mingw_host}",
          "--target=#{mingw_target}",
          "--build=#{RUBY_BUILD}",
@@ -12,6 +12,7 @@ index 8317a2a3..8ed21718 100644
 +        'ac_cv_lib_z_uncompress=no',
 +        'ac_cv_lib_crypt_crypt=no',
 +        'ac_cv_func_crypt_r=no',
++        'extra_warnflags=-Wno-shorten-64-to-32 -Wno-dll-attribute-on-redeclaration',
          '--disable-install-doc',
          '--with-ext=',
        ]