| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | repository=$(cygpath -u "$1") |
| 5 | runtime=$(cygpath -u "$2") |
| 6 | archive=$(cygpath -u "$3") |
| 7 | node_command=${CAGE_NODE:-node} |
| 8 | if [[ -n ${CAGE_NODE:-} ]]; then node_command=$(cygpath -u "$CAGE_NODE"); fi |
| 9 | for tool in "$node_command" git curl tar sha256sum find sort xargs pacman-key pacman-conf mkdir mktemp cp mv rm grep; do |
| 10 | command -v "$tool" >/dev/null 2>&1 || { echo "Missing source-collection tool: $tool" >&2; exit 1; } |
| 11 | done |
| 12 | commit=$("$node_command" -p "JSON.parse(require('fs').readFileSync(process.argv[1])).qemuSource.commit" "$repository/packaging/windows/dependencies.json") |
| 13 | source_repository=$("$node_command" -p "JSON.parse(require('fs').readFileSync(process.argv[1])).qemuSource.repository" "$repository/packaging/windows/dependencies.json") |
| 14 | [[ $commit =~ ^[a-f0-9]{40}$ && -s "$runtime/build-source-packages.txt" ]] || { echo 'QEMU source/package identity is missing.' >&2; exit 1; } |
| 15 | stage=$(mktemp -d) |
| 16 | trap 'rm -rf "$stage"' EXIT |
| 17 | mkdir -p "$stage/source/packages" "$stage/source/cage" |
| 18 | |
| 19 | |
| 20 | installed_keyring=$(pacman-conf gpgdir) |
| 21 | mkdir -p "$stage/keyring" |
| 22 | cp "$installed_keyring/pubring.gpg" "$installed_keyring/trustdb.gpg" "$stage/keyring/" |
| 23 | printf 'lock-never\n' > "$stage/keyring/gpg.conf" |
| 24 | git -c core.autocrlf=false init -q "$stage/source/qemu" |
| 25 | git -C "$stage/source/qemu" fetch --depth=1 "$source_repository" "$commit" |
| 26 | git -c core.autocrlf=false -C "$stage/source/qemu" checkout -q --detach FETCH_HEAD |
| 27 | [[ $(git -C "$stage/source/qemu" rev-parse HEAD) == "$commit" ]] |
| 28 | |
| 29 | |
| 30 | git_options=(-c core.autocrlf=false) |
| 31 | for module in QemuMacDrivers edk2 ipxe opensbi qboot qemu-palcode seabios seabios-hppa skiboot u-boot vbootrom; do |
| 32 | git_options+=(-c "url.https://github.com/qemu/$module.insteadOf=https://gitlab.com/qemu-project/$module") |
| 33 | done |
| 34 | git_options+=(-c url.https://github.com/openbios/openbios.insteadOf=https://gitlab.com/qemu-project/openbios) |
| 35 | git_options+=(-c url.https://github.com/zbalaton/u-boot-sam460ex.insteadOf=https://gitlab.com/qemu-project/u-boot-sam460ex) |
| 36 | git_options+=(-c url.https://github.com/tianocore/edk2-cmocka.git.insteadOf=https://gitlab.com/cmocka/cmocka.git) |
| 37 | |
| 38 | |
| 39 | git "${git_options[@]}" -C "$stage/source/qemu" submodule update --init --recursive --depth=1 -- roms |
| 40 | git -C "$stage/source/qemu" submodule status --recursive -- roms > "$stage/source/firmware-revisions.txt" |
| 41 | if grep -Eq '^[-+U]' "$stage/source/firmware-revisions.txt"; then echo 'Firmware source revisions are incomplete.' >&2; exit 1; fi |
| 42 | cp "$runtime/build-source-packages.txt" "$runtime/build-packages.txt" "$runtime/cage-build.json" "$stage/source/cage/" |
| 43 | cp "$repository/scripts/build-qemu-windows.sh" "$repository/scripts/build-qemu-windows.ps1" "$repository/scripts/bundle-qemu-sources.sh" "$stage/source/cage/" |
| 44 | cp -R "$repository/patches/qemu" "$stage/source/cage/patches" |
| 45 | keymap_url=$("$node_command" -p "JSON.parse(require('fs').readFileSync(process.argv[1])).qemuKeycodemap.url" "$repository/packaging/windows/dependencies.json") |
| 46 | keymap_hash=$("$node_command" -p "JSON.parse(require('fs').readFileSync(process.argv[1])).qemuKeycodemap.hash" "$repository/packaging/windows/dependencies.json") |
| 47 | curl --fail --location --retry 3 -o "$stage/source/keycodemapdb.tar.gz" "$keymap_url" |
| 48 | printf '%s %s\n' "$keymap_hash" "$stage/source/keycodemapdb.tar.gz" | sha256sum -c - |
| 49 | while read -r base version; do |
| 50 | [[ $base =~ ^[a-zA-Z0-9+_.-]+$ && $version =~ ^[a-zA-Z0-9+_.:-]+$ ]] || exit 1 |
| 51 | found=false |
| 52 | for extension in zst xz gz; do |
| 53 | name="$base-$version.src.tar.$extension" |
| 54 | url="https://repo.msys2.org/mingw/sources/$name" |
| 55 | if curl --fail --location --retry 2 -o "$stage/source/packages/$name" "$url"; then |
| 56 | curl --fail --location --retry 3 -o "$stage/source/packages/$name.sig" "$url.sig" |
| 57 | pacman-key --gpgdir "$stage/keyring" --verify "$stage/source/packages/$name.sig" "$stage/source/packages/$name" |
| 58 | found=true |
| 59 | break |
| 60 | fi |
| 61 | done |
| 62 | $found || { echo "No verified corresponding source for $base $version" >&2; exit 1; } |
| 63 | done < "$runtime/build-source-packages.txt" |
| 64 | |
| 65 | |
| 66 | find "$stage/source" -name .git -type d -prune -exec rm -rf {} + |
| 67 | find "$stage/source" -name .git -type f -delete |
| 68 | (cd "$stage/source" && find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum > SHA256SUMS) |
| 69 | mkdir -p "$(dirname "$archive")" |
| 70 | tar -czf "$archive.partial" -C "$stage" source |
| 71 | mv "$archive.partial" "$archive" |
| 72 | sha256sum "$archive" |