From fa73cf932b35fe670886f4441fc46f808428c315 Mon Sep 17 00:00:00 2001
From: Eric Huss <eric@huss.org>
Date: Wed, 22 Dec 2021 14:05:47 -0800
Subject: [PATCH 1/3] Error if submodule fetch fails.

---
 src/ci/init_repo.sh | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/ci/init_repo.sh b/src/ci/init_repo.sh
index 3c61dcc9d9c..217192af2fe 100755
--- a/src/ci/init_repo.sh
+++ b/src/ci/init_repo.sh
@@ -62,6 +62,7 @@ for i in ${!modules[@]}; do
         url=${urls[$i]}
         url=${url/\.git/}
         fetch_github_commit_archive $module "$url/archive/$commit.tar.gz" &
+        bg_pids[${i}]=$!
         continue
     else
         use_git="$use_git $module"
@@ -70,4 +71,9 @@ done
 retry sh -c "git submodule deinit -f $use_git && \
     git submodule sync && \
     git submodule update -j 16 --init --recursive $use_git"
-wait
+STATUS=0
+for pid in ${bg_pids[*]}
+do
+    wait $pid || STATUS=1
+done
+exit ${STATUS}

From 31267e80071d689230b7718fa0410a03af99798e Mon Sep 17 00:00:00 2001
From: Eric Huss <eric@huss.org>
Date: Mon, 28 Feb 2022 06:31:50 -0800
Subject: [PATCH 2/3] Fix extracting submodules on windows.

---
 src/ci/init_repo.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/ci/init_repo.sh b/src/ci/init_repo.sh
index 217192af2fe..93af8c26111 100755
--- a/src/ci/init_repo.sh
+++ b/src/ci/init_repo.sh
@@ -43,6 +43,11 @@ function fetch_github_commit_archive {
         curl -f -sSL -o $cached $2"
     mkdir $module
     touch "$module/.git"
+    # On Windows, the default behavior is to emulate symlinks by copying
+    # files. However, that ends up being order-dependent while extracting,
+    # which can cause a failure if the symlink comes first. This env var
+    # causes tar to use real symlinks instead, which are allowed to dangle.
+    export MSYS=winsymlinks:nativestrict
     tar -C $module --strip-components=1 -xf $cached
     rm $cached
 }

From 43f83bc013270c7113cd75c27cc781df229c4645 Mon Sep 17 00:00:00 2001
From: Eric Huss <eric@huss.org>
Date: Tue, 1 Mar 2022 10:16:47 -0800
Subject: [PATCH 3/3] Move submodule checkout before msys2 installation.

For some reason, `tar` behaves differently in such a way that it does
not create symlinks on Windows correctly, resulting in
`Cannot create symlink to 'ld.gold': No such file or directory`
errors.
---
 .github/workflows/ci.yml     | 18 +++++++++---------
 src/ci/github-actions/ci.yml |  8 ++++----
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 97a31d3c970..ff3a8326315 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -104,6 +104,9 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
+      - name: checkout submodules
+        run: src/ci/scripts/checkout-submodules.sh
+        if: success() && !env.SKIP_JOB
       - name: install MSYS2
         run: src/ci/scripts/install-msys2.sh
         if: success() && !env.SKIP_JOB
@@ -119,9 +122,6 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
-      - name: checkout submodules
-        run: src/ci/scripts/checkout-submodules.sh
-        if: success() && !env.SKIP_JOB
       - name: ensure line endings are correct
         run: src/ci/scripts/verify-line-endings.sh
         if: success() && !env.SKIP_JOB
@@ -502,6 +502,9 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
+      - name: checkout submodules
+        run: src/ci/scripts/checkout-submodules.sh
+        if: success() && !env.SKIP_JOB
       - name: install MSYS2
         run: src/ci/scripts/install-msys2.sh
         if: success() && !env.SKIP_JOB
@@ -517,9 +520,6 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
-      - name: checkout submodules
-        run: src/ci/scripts/checkout-submodules.sh
-        if: success() && !env.SKIP_JOB
       - name: ensure line endings are correct
         run: src/ci/scripts/verify-line-endings.sh
         if: success() && !env.SKIP_JOB
@@ -615,6 +615,9 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
+      - name: checkout submodules
+        run: src/ci/scripts/checkout-submodules.sh
+        if: success() && !env.SKIP_JOB
       - name: install MSYS2
         run: src/ci/scripts/install-msys2.sh
         if: success() && !env.SKIP_JOB
@@ -630,9 +633,6 @@ jobs:
       - name: disable git crlf conversion
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         if: success() && !env.SKIP_JOB
-      - name: checkout submodules
-        run: src/ci/scripts/checkout-submodules.sh
-        if: success() && !env.SKIP_JOB
       - name: ensure line endings are correct
         run: src/ci/scripts/verify-line-endings.sh
         if: success() && !env.SKIP_JOB
diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml
index 8abf4244a38..5622422d50f 100644
--- a/src/ci/github-actions/ci.yml
+++ b/src/ci/github-actions/ci.yml
@@ -169,6 +169,10 @@ x--expand-yaml-anchors--remove:
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         <<: *step
 
+      - name: checkout submodules
+        run: src/ci/scripts/checkout-submodules.sh
+        <<: *step
+
       - name: install MSYS2
         run: src/ci/scripts/install-msys2.sh
         <<: *step
@@ -194,10 +198,6 @@ x--expand-yaml-anchors--remove:
         run: src/ci/scripts/disable-git-crlf-conversion.sh
         <<: *step
 
-      - name: checkout submodules
-        run: src/ci/scripts/checkout-submodules.sh
-        <<: *step
-
       - name: ensure line endings are correct
         run: src/ci/scripts/verify-line-endings.sh
         <<: *step