1#!/bin/sh
2# Fetches llvm-mingw into target/windows/llvm-mingw: clang, lld and the MinGW-w64 sysroots
3# for x86_64 and aarch64, built for msvcrt.dll, which Windows 7 has without updates, where
4# the UCRT needs KB2999226. llvm-mingw publishes msvcrt builds only for Linux and Windows
5# hosts, so on macOS its UCRT toolchain takes the msvcrt build's target files.
6set -eu
7here=$(cd "$(dirname "$0")" && pwd)
8root=$(cd "$here/../.." && pwd)
9release=20260922
10out=$root/target/windows/llvm-mingw
11stage=$root/target/windows/llvm-mingw.partial
12base=https://github.com/mstorsjo/llvm-mingw/releases/download/$release
13msvcrt=llvm-mingw-$release-msvcrt-ubuntu-22.04-x86_64
14[ ! -e "$out" ] || { echo "$out exists" >&2; exit 1; }
15rm -rf "${stage:?}"
16mkdir -p "$stage"
17curl -fsSL "$base/$msvcrt.tar.xz" | tar -xJ -C "$stage"
18if [ "$(uname -s)" = Darwin ]; then
19 host=llvm-mingw-$release-ucrt-macos-universal
20 curl -fsSL "$base/$host.tar.xz" | tar -xJ -C "$stage"
21 clang=$(ls "$stage/$host/lib/clang")
22 for part in x86_64-w64-mingw32 aarch64-w64-mingw32 generic-w64-mingw32 \
23 "lib/clang/$clang/lib/windows"; do
24 rm -rf "${stage:?}/$host/$part"
25 mv "$stage/$msvcrt/$part" "$stage/$host/$part"
26 done
27else
28 host=$msvcrt
29fi
30# Only x86_64 and aarch64 are built.
31rm -rf "${stage:?}/$host/i686-w64-mingw32" "${stage:?}/$host/armv7-w64-mingw32"
32find "$stage/$host/bin" \( -name 'i686-*' -o -name 'armv7-*' -o -name '*uwp*' \) -delete
33mv "$stage/$host" "$out"
34rm -rf "${stage:?}"
35echo "$out"