#!/bin/sh # SPDX-License-Identifier: GPL-3.0-or-later # Fetch Mesen2 at the pinned commit into upstream/Mesen2 (git-ignored), apply patches/*.patch in # order, and build its core library (MesenCore.so) without .NET, as famicom-python's # docs/validation.md "Mesen2 as a second detector" did. Writes upstream/build-info.json, which # the runner's manifest quotes. # # With --core mesence: the second pinned upstream, MesenCE (Mesen2's continuation), fetched into # upstream/MesenCE and built the same way, as published (the patches are Mesen2's and are not # applied to it); its record is upstream/build-info-mesence.json. run.py --core mesence runs it. # # ./build.sh fetch (if needed), patch, build Mesen2 # ./build.sh --no-patch build Mesen2's pinned commit as published (no patches) # ./build.sh --core mesence fetch (if needed) and build MesenCE's pinned commit # JOBS=8 ./build.sh parallel jobs for make (default 8) # # Needs git, a C++17 compiler (g++), make and SDL2's headers. Never deletes anything: a checkout # with changes other than our patches stops the script with a message. set -eu HERE=$(cd "$(dirname "$0")" && pwd) JOBS=${JOBS:-8} PATCH=yes CORE=mesen2 case "${1:-}" in "") ;; --no-patch) PATCH=no ;; --core) CORE=${2:-} [ $# -eq 2 ] || { echo "usage: build.sh [--no-patch | --core mesen2|mesence]" >&2; exit 2; } ;; *) echo "usage: build.sh [--no-patch | --core mesen2|mesence]" >&2; exit 2 ;; esac case "$CORE" in mesen2) PIN=b9fa69ddc6d0a331fb103fdb5eef6904305703c2 URL=https://github.com/SourMesen/Mesen2 SRC=$HERE/upstream/Mesen2 RECORD=$HERE/upstream/build-info.json ;; mesence) PIN=4b8a196e299b3de297b204fd1208d2981562c89b # 2026-10-07, as famicom-python built it URL=https://github.com/nesdev-org/MesenCE SRC=$HERE/upstream/MesenCE RECORD=$HERE/upstream/build-info-mesence.json PATCH=no ;; *) echo "build.sh: --core is mesen2 or mesence" >&2; exit 2 ;; esac mkdir -p "$HERE/upstream" if [ ! -d "$SRC/.git" ]; then git clone --quiet "$URL" "$SRC" fi if [ "$(git -C "$SRC" rev-parse HEAD)" != "$PIN" ]; then if [ -n "$(git -C "$SRC" status --porcelain)" ]; then echo "build.sh: $SRC has local changes and is not at $PIN; move it aside first" >&2 exit 1 fi git -C "$SRC" fetch --quiet origin "$PIN" 2>/dev/null || git -C "$SRC" fetch --quiet origin git -C "$SRC" -c advice.detachedHead=false checkout --quiet "$PIN" fi applied="" for patch in "$HERE"/patches/*.patch; do [ "$PATCH" = yes ] || break [ -e "$patch" ] || continue name=$(basename "$patch") if git -C "$SRC" apply --check "$patch" 2>/dev/null; then git -C "$SRC" apply "$patch" elif git -C "$SRC" apply --reverse --check "$patch" 2>/dev/null; then : # already applied else echo "build.sh: $name neither applies nor is applied at $PIN; move $SRC aside" >&2 exit 1 fi applied="$applied\"$name\"," done if [ "$PATCH" = no ] && [ -n "$(git -C "$SRC" status --porcelain --untracked-files=no)" ]; then echo "build.sh: --no-patch, but $SRC carries changes; move it aside first" >&2 exit 1 fi (cd "$SRC" && nice -n 10 make core USE_GCC=true LTO=false STATICLINK=false -j"$JOBS") LIB=$SRC/InteropDLL/obj.linux-x64/MesenCore.so [ -f "$LIB" ] || { echo "build.sh: no $LIB after make" >&2; exit 1; } applied=$(printf '%s' "$applied" | sed 's/,$//') printf '{"commit": "%s", "patches": [%s], "built": "%s"}\n' \ "$PIN" "$applied" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$RECORD" echo "built $LIB"