#!/bin/sh # SPDX-License-Identifier: GPL-3.0-or-later # Fetch blargg's snes_spc 0.9.0 (LGPL-2.1-or-later) into upstream/ (git-ignored), check the # archive's SHA-256, unpack it, and build the S-DSP referee driver into build/spc_dump # (git-ignored): spc_dump.cpp linked with every source file in the archive's snes_spc/ folder, # unmodified, as the library's readme says, with the consumers' flags (g++ -O2 -DNDEBUG -w). # Writes build/build-info.json. # # ./build.sh fetch what is missing, build build/spc_dump # ./build.sh --version print the tool, its version and the pinned archive # CXX=clang++ ./build.sh another compiler (CXXFLAGS overrides the flags) # SNES_SPC_ZIP=FILE ./build.sh use a copy of the archive you already have (checked by SHA-256) # # Exit status as the family's tool contract says: 0 built, 2 a usage error, 3 the archive could # not be fetched or does not match the pin, anything else a failed build. Never deletes anything: # an unpacked tree that is not the pinned archive's stops the script until it is moved aside. set -eu VERSION=0.1.0 NAME=snes_spc-0.9.0 SHA256=dbad8273973a26f1b0c2a07b61b68855b2d96526fbc2cdd237f9d4fb91083aed # blargg's own site is gone; the Internet Archive's copy of the file it served (2011-08-12). URL=https://web.archive.org/web/20110812132836id_/http://blargg.parodius.com/libs/$NAME.zip case "${1:-}" in --version) echo "snes-spc $VERSION (snes_spc 0.9.0, LGPL-2.1-or-later, archive SHA-256 ${SHA256%"${SHA256#????????????}"})"; exit 0 ;; "") ;; *) echo "usage: build.sh [--version]" >&2; exit 2 ;; esac HERE=$(cd "$(dirname "$0")" && pwd) UP=$HERE/upstream ZIP=$UP/$NAME.zip LIB=$UP/$NAME/snes_spc OUT=$HERE/build CXX=${CXX:-g++} CXXFLAGS=${CXXFLAGS:--O2 -DNDEBUG -w} sha() { sha256sum "$1" | cut -d' ' -f1; } mkdir -p "$UP" "$OUT" if [ ! -f "$ZIP" ]; then if [ -n "${SNES_SPC_ZIP:-}" ]; then [ -f "$SNES_SPC_ZIP" ] || { echo "build.sh: SNES_SPC_ZIP names no file: $SNES_SPC_ZIP" >&2; exit 3; } cp "$SNES_SPC_ZIP" "$ZIP.part" else curl -fsSL --retry 3 -o "$ZIP.part" "$URL" || { echo "build.sh: could not fetch $URL" >&2; exit 3; } fi if [ "$(sha "$ZIP.part")" != "$SHA256" ]; then echo "build.sh: the archive's SHA-256 is $(sha "$ZIP.part"), not the pin $SHA256 (left as $ZIP.part)" >&2 exit 3 fi mv "$ZIP.part" "$ZIP" fi if [ "$(sha "$ZIP")" != "$SHA256" ]; then echo "build.sh: $ZIP does not match the pin $SHA256; move it aside" >&2 exit 3 fi if [ ! -d "$UP/$NAME" ]; then mkdir "$UP/$NAME.part" (cd "$UP/$NAME.part" && unzip -q "$ZIP") # the archive's files are under $NAME/ mv "$UP/$NAME.part/$NAME" "$UP/$NAME" rmdir "$UP/$NAME.part" fi # The unpacked tree must hold exactly the archive's files, as the archive has them. if ! (cd "$UP/$NAME/.." && unzip -tq "$ZIP" >/dev/null && unzip -Z1 "$ZIP" | while read -r f; do case "$f" in */) continue ;; esac [ -f "$f" ] && unzip -p "$ZIP" "$f" | cmp -s - "$f" || exit 1 done); then echo "build.sh: $UP/$NAME differs from the pinned archive; move it aside" >&2 exit 1 fi SOURCES=$(ls "$LIB"/*.cpp) $CXX --version | head -n 1 # shellcheck disable=SC2086 $CXX $CXXFLAGS -I "$LIB" -o "$OUT/spc_dump" "$HERE/spc_dump.cpp" $SOURCES echo "built $OUT/spc_dump (snes_spc 0.9.0 + spc_dump.cpp: an LGPL-2.1-or-later combined work)" printf '{"tool": "snes-spc", "tool_version": "%s", "upstream": "snes_spc 0.9.0", "archive_sha256": "%s", "compiler": "%s", "cxxflags": "%s", "built": "%s", "patches": []}\n' \ "$VERSION" "$SHA256" "$($CXX --version | head -n 1)" "$CXXFLAGS" \ "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$OUT/build-info.json"