From 65566e57cda918b85b39bfa906768a5844fc8f64 Mon Sep 17 00:00:00 2001 From: Andrea Della Corte Date: Mon, 6 Apr 2026 00:28:04 +0100 Subject: [PATCH 1/2] Add native macOS build scripts for 2.7.2-psx, 2.7.2-cdk, 2.8.1-psx Produces native Mach-O arm64/x86_64 cc1 binaries on macOS without Docker. Approach: - Replace config.guess/config.sub with modern versions + psx* OS entry - Add xm-darwin.h (LP64 host descriptor) and patch configure for *-apple-darwin* - Pass --host/--build explicitly to avoid multi-arg config.sub calls - Export CFLAGS=-std=gnu89 so configure's compiler test (main(){}) passes clang - 2.7.2-cdk: requires byacc, single-threaded build (yacc race), __eprintf stub - 2.8.1-psx: touch insn-config.h, unset CC/CXX, prepend system PATH Also adds Makefile PLATFORM=macos support and a build-macos.yml GitHub Actions workflow (macos-latest runner, matrix: 2.7.2-psx, 2.7.2-cdk, 2.8.1-psx). Closes #33 --- .github/workflows/build-macos.yml | 45 +++++++++++++++ Makefile | 12 ++++ gcc-2.7.2-cdk-macos.sh | 80 ++++++++++++++++++++++++++ gcc-2.7.2-psx-macos.sh | 76 +++++++++++++++++++++++++ gcc-2.8.1-psx-macos.sh | 94 +++++++++++++++++++++++++++++++ patches/eprintf-darwin.c | 11 ++++ patches/xm-darwin.h | 25 ++++++++ 7 files changed, 343 insertions(+) create mode 100644 .github/workflows/build-macos.yml create mode 100755 gcc-2.7.2-cdk-macos.sh create mode 100755 gcc-2.7.2-psx-macos.sh create mode 100755 gcc-2.8.1-psx-macos.sh create mode 100644 patches/eprintf-darwin.c create mode 100644 patches/xm-darwin.h diff --git a/.github/workflows/build-macos.yml b/.github/workflows/build-macos.yml new file mode 100644 index 0000000..0a88aaa --- /dev/null +++ b/.github/workflows/build-macos.yml @@ -0,0 +1,45 @@ +name: Compile GCC (macOS) +on: + push: + tags: '*' + branches: [master] + paths-ignore: + - README.md + pull_request: + branches: [master] +jobs: + build: + runs-on: macos-latest + strategy: + fail-fast: false + matrix: + version: + - 2.7.2-psx + - 2.7.2-cdk + - 2.8.1-psx + name: Build GCC ${{ matrix.version }} (macOS) + steps: + - name: Clone repository + uses: actions/checkout@v4 + - name: Install dependencies + run: | + # byacc is required for gcc-2.7.2-cdk + brew install byacc + - name: Build GCC + run: VERSION=${{ matrix.version }} PLATFORM=macos make + - name: Create release archive + shell: bash + run: | + cd build-gcc-${{ matrix.version }} + tar -czvf ../gcc-${{ matrix.version }}-macos.tar.gz * + - name: Create artifact + uses: actions/upload-artifact@v4 + with: + name: gcc-${{ matrix.version }}-macos + path: gcc-${{ matrix.version }}-macos.tar.gz + - name: Publish release + uses: softprops/action-gh-release@v2 + if: startsWith(github.ref, 'refs/tags/') + with: + files: | + gcc-${{ matrix.version }}-macos.tar.gz diff --git a/Makefile b/Makefile index 10936df..b45b67e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,11 @@ +PLATFORM ?= linux + all: check-version +ifeq ($(PLATFORM),macos) + bash gcc-$(VERSION)-macos.sh +else docker build -f gcc-$(VERSION).Dockerfile --target export --output build-gcc-$(VERSION) . +endif clean: rm -rf build-gcc-*/ @@ -8,8 +14,14 @@ check-version: ifndef VERSION $(error You must specify a VERSION e.g. `make VERSION=2.8.1`) endif +ifeq ($(PLATFORM),macos) +ifeq ($(wildcard gcc-$(VERSION)-macos.sh),) + $(error Building GCC $(VERSION) for macOS is not currently supported) +endif +else ifeq ($(wildcard gcc-$(VERSION).Dockerfile),) $(error Building GCC $(VERSION) is not currently supported) endif +endif .PHONY: all check-version diff --git a/gcc-2.7.2-cdk-macos.sh b/gcc-2.7.2-cdk-macos.sh new file mode 100755 index 0000000..bf2681b --- /dev/null +++ b/gcc-2.7.2-cdk-macos.sh @@ -0,0 +1,80 @@ +#!/bin/bash +# Build gcc-2.7.2-cdk natively on macOS (x86_64 and aarch64). +# Produces a Mach-O cc1 binary targeting mips-sony-psx. +# +# Requires: byacc (install via Homebrew: brew install byacc) +set -e + +if ! command -v byacc >/dev/null 2>&1; then + echo "Error: byacc is required. Install it with: brew install byacc" >&2 + exit 1 +fi + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PATCHES="$SCRIPT_DIR/patches" +OUTDIR="$SCRIPT_DIR/build-gcc-2.7.2-cdk" +WORKDIR="$(mktemp -d)" +trap 'rm -rf "$WORKDIR"' EXIT + +echo "Building gcc-2.7.2-cdk for macOS in $WORKDIR" + +cd "$WORKDIR" +curl -fL "https://github.com/decompals/old-gcc/releases/download/0.14/b18.tar.gz" | tar xz +cd cdk-gcc-b18 + +# Apply the same patches as the Linux Dockerfile +patch -u -p1 Makefile.in -i "$PATCHES/Makefile-2.7.2-cdk.in.patch" +patch -u -p1 obstack.h -i "$PATCHES/obstack-2.7.2-cdk.h.patch" +patch -u -p1 config/mips/mips.h -i "$PATCHES/mipsel-2.7-cdk.patch" +patch -su -p1 < "$PATCHES/psx-2.7.2-cdk.patch" + +# macOS: replace config.guess/config.sub with modern versions that know +# about aarch64-apple-darwin, then add psx* to the OS list. +chmod +w config.guess config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +chmod +x config.guess config.sub +python3 -c " +s = open('config.sub').read() +s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) +open('config.sub', 'w').write(s) +" + +# Add xm-darwin.h and teach configure about *-apple-darwin* hosts. +cp "$PATCHES/xm-darwin.h" config/ +chmod +w configure +python3 -c " +s = open('configure').read() +insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' +s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', + insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) +open('configure', 'w').write(s) +" + +./configure \ + --target=mips-sony-psx \ + --prefix=/opt/cross \ + --with-endian-little \ + --with-gnu-as \ + --disable-gprof \ + --disable-gdb \ + --disable-werror + +# Compile the __eprintf stub — this symbol was removed from macOS SDKs +# after 10.14 but is referenced by the exception-handling code. +cc -std=gnu89 -c "$PATCHES/eprintf-darwin.c" -o eprintf-darwin.o + +# Build single-threaded: parallel yacc invocations race on y.tab.h, +# causing bi-parser.h to be generated with the wrong grammar. +make cc1 \ + CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration" \ + LDFLAGS="eprintf-darwin.o" + +# Run the same tests as the Dockerfile +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/little_endian.c" -o little_endian.s +grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/section_attribute.c" -o /dev/null + +mkdir -p "$OUTDIR" +cp cc1 "$OUTDIR/cc1" +echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/gcc-2.7.2-psx-macos.sh b/gcc-2.7.2-psx-macos.sh new file mode 100755 index 0000000..ff8f75c --- /dev/null +++ b/gcc-2.7.2-psx-macos.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# Build gcc-2.7.2-psx natively on macOS (x86_64 and aarch64). +# Produces a Mach-O cc1 binary targeting mips-sony-psx. +set -e + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PATCHES="$SCRIPT_DIR/patches" +OUTDIR="$SCRIPT_DIR/build-gcc-2.7.2-psx" +WORKDIR="$(mktemp -d)" +trap 'rm -rf "$WORKDIR"' EXIT + +echo "Building gcc-2.7.2-psx for macOS in $WORKDIR" + +cd "$WORKDIR" +curl -fL "https://ftp.gnu.org/old-gnu/gcc/gcc-2.7.2.tar.gz" | tar xz +cd gcc-2.7.2 + +# Apply the same patches as the Linux Dockerfile +python3 -c " +import glob, os, stat +for f in glob.glob('*.c'): + s = open(f).read() + s2 = s.replace('include ', 'include ') + if s2 != s: + os.chmod(f, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) + open(f, 'w').write(s2) +" +patch -u -p1 obstack.h -i "$PATCHES/obstack-2.7.2.h.patch" +patch -u -p1 configure -i "$PATCHES/configure.patch" +patch -u -p1 config/mips/mips.h -i "$PATCHES/mipsel-2.7.patch" +patch -su -p1 < "$PATCHES/psx-2.5.7.patch" + +# macOS: replace config.guess/config.sub with modern versions that know +# about aarch64-apple-darwin, then add psx* to the OS list. +chmod +w config.guess config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +chmod +x config.guess config.sub +python3 -c " +s = open('config.sub').read() +s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) +open('config.sub', 'w').write(s) +" + +# Add xm-darwin.h and teach configure about *-apple-darwin* hosts. +cp "$PATCHES/xm-darwin.h" config/ +chmod +w configure +python3 -c " +s = open('configure').read() +insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' +s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', + insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) +open('configure', 'w').write(s) +" + +./configure \ + --target=mips-sony-psx \ + --prefix=/opt/cross \ + --with-endian-little \ + --with-gnu-as \ + --disable-gprof \ + --disable-gdb \ + --disable-werror + +make --jobs "$(sysctl -n hw.ncpu)" cc1 \ + CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration" + +# Run the same tests as the Dockerfile +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/little_endian.c" -o little_endian.s +grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/section_attribute.c" -o /dev/null +./cc1 -quiet -help &1 | grep -- -msoft-float + +mkdir -p "$OUTDIR" +cp cc1 "$OUTDIR/cc1" +echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/gcc-2.8.1-psx-macos.sh b/gcc-2.8.1-psx-macos.sh new file mode 100755 index 0000000..3fca097 --- /dev/null +++ b/gcc-2.8.1-psx-macos.sh @@ -0,0 +1,94 @@ +#!/bin/bash +# Build gcc-2.8.1-psx natively on macOS (x86_64 and aarch64). +# Produces a Mach-O cc1 binary targeting mips-sony-psx. +set -e + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PATCHES="$SCRIPT_DIR/patches" +OUTDIR="$SCRIPT_DIR/build-gcc-2.8.1-psx" +WORKDIR="$(mktemp -d)" +trap 'rm -rf "$WORKDIR"' EXIT + +echo "Building gcc-2.8.1-psx for macOS in $WORKDIR" + +# Ensure we use the system compiler, not any cross-compiler wrappers that +# may be in PATH (e.g. from a nix shell). +export PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH" +unset CC CXX + +cd "$WORKDIR" +curl -fL "https://mirrors.kernel.org/gnu/gcc/gcc-2.8.1.tar.gz" | tar xz +cd gcc-2.8.1 + +# Apply the same patches as the Linux Dockerfile +python3 -c " +import glob, os, stat +for f in glob.glob('*.c'): + s = open(f).read() + s2 = s.replace('include ', 'include ') + if s2 != s: + os.chmod(f, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) + open(f, 'w').write(s2) +" +patch -u -p1 obstack.h -i "$PATCHES/obstack-2.8.1.h.patch" +patch -u -p1 config/mips/mips.h -i "$PATCHES/mips.patch" +patch -su -p1 < "$PATCHES/psx.patch" + +# macOS: replace config.guess/config.sub with modern versions that know +# about aarch64-apple-darwin, then add psx* to the OS list. +chmod +w config.guess config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +chmod +x config.guess config.sub +python3 -c " +s = open('config.sub').read() +s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) +open('config.sub', 'w').write(s) +" + +# Add xm-darwin.h and teach configure about *-apple-darwin* hosts. +cp "$PATCHES/xm-darwin.h" config/ +chmod +w configure +python3 -c " +s = open('configure').read() +insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' +s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', + insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) +open('configure', 'w').write(s) +" + +# Explicitly pass --host/--build so configure uses a single-arg config.sub +# call (which modern config.sub accepts), rather than its multi-arg form. +# Export CFLAGS so configure's compiler test (main(){return(0);}) passes under +# modern clang, which rejects implicit int without -std=gnu89. +DARWIN_HOST="$(uname -m)-apple-darwin" +export CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration -Wno-return-mismatch" +./configure \ + --target=mips-sony-psx \ + --host="$DARWIN_HOST" \ + --build="$DARWIN_HOST" \ + --prefix=/opt/cross \ + --with-endian-little \ + --with-gnu-as \ + --disable-gprof \ + --disable-gdb \ + --disable-werror + +# insn-config.h is generated during the build but referenced early; touch +# it to prevent spurious missing-file errors on some make versions. +touch insn-config.h + +make --jobs "$(sysctl -n hw.ncpu)" cc1 \ + CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration -Wno-return-mismatch" + +# Run the same tests as the Dockerfile +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/little_endian.c" -o little_endian.s +grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s +./cc1 -quiet -O2 "$SCRIPT_DIR/tests/section_attribute.c" -o /dev/null +./cc1 -version &1 | grep -- -msoft-float +./cc1 -version &1 | grep -- -msplit-addresses +./cc1 -version &1 | grep -- -mgpopt + +mkdir -p "$OUTDIR" +cp cc1 "$OUTDIR/cc1" +echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/patches/eprintf-darwin.c b/patches/eprintf-darwin.c new file mode 100644 index 0000000..8096626 --- /dev/null +++ b/patches/eprintf-darwin.c @@ -0,0 +1,11 @@ +/* Stub for __eprintf, removed from macOS SDKs after 10.14. + Required by gcc-2.7.2-cdk's exception-handling code. */ +#include +#include + +void +__eprintf (const char *fmt, const char *file, unsigned line, const char *expr) +{ + fprintf (stderr, fmt, file, line, expr); + abort (); +} diff --git a/patches/xm-darwin.h b/patches/xm-darwin.h new file mode 100644 index 0000000..8197624 --- /dev/null +++ b/patches/xm-darwin.h @@ -0,0 +1,25 @@ +/* Host machine description for GCC running on macOS (Darwin). + Works for both x86_64 and aarch64 (Apple Silicon). + + macOS uses the LP64 model: int is 32-bit, long and pointers are 64-bit. */ + +#define HOST_BITS_PER_CHAR 8 +#define HOST_BITS_PER_SHORT 16 +#define HOST_BITS_PER_INT 32 +#define HOST_BITS_PER_LONG 64 +#define HOST_BITS_PER_LONGLONG 64 + +#define FALSE 0 +#define TRUE 1 + +#define SUCCESS_EXIT_CODE 0 +#define FATAL_EXIT_CODE 33 + +#define HAVE_VPRINTF +#define HAVE_STRERROR +#define POSIX + +/* macOS provides bcopy/bcmp/bzero via . */ +#define BSTRING + +#include "tm.h" From 1a73e914b383f339c900594207fe3e50ab8aa82b Mon Sep 17 00:00:00 2001 From: Andrea Della Corte Date: Mon, 6 Apr 2026 20:37:38 +0100 Subject: [PATCH 2/2] Address review feedback: pin URLs, use sed/awk, build all binaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Pin config.guess/config.sub fetches to specific commits (74af13c, 6fad101) instead of tracking master, for reproducibility - Replace Python text substitutions with grep+sed (varargs) and awk (configure patch); add PATH=/usr/bin:... to all scripts so BSD sed is used - Build cpp/cc1/xgcc/cc1plus/g++ to match the Linux Dockerfiles; fixes needed: - sys_nerr declared 'const int' in macOS SDKs — patch gcc.c and cp/g++.c - __eprintf in eprintf-darwin.c marked weak so tree.o's strong definition wins in cdk (avoids duplicate symbol error when linking cc1plus) - awk configure patch made context-aware: buffer each \t*) line and only insert *-apple-darwin* before the one followed by the "not supported" echo (2.8.1 configure has an unrelated \t*) earlier that fooled the first pass) --- gcc-2.7.2-cdk-macos.sh | 51 ++++++++++++++++++++-------------- gcc-2.7.2-psx-macos.sh | 59 +++++++++++++++++++++------------------- gcc-2.8.1-psx-macos.sh | 52 +++++++++++++++++------------------ patches/eprintf-darwin.c | 4 ++- 4 files changed, 89 insertions(+), 77 deletions(-) diff --git a/gcc-2.7.2-cdk-macos.sh b/gcc-2.7.2-cdk-macos.sh index bf2681b..0709a80 100755 --- a/gcc-2.7.2-cdk-macos.sh +++ b/gcc-2.7.2-cdk-macos.sh @@ -1,6 +1,6 @@ #!/bin/bash # Build gcc-2.7.2-cdk natively on macOS (x86_64 and aarch64). -# Produces a Mach-O cc1 binary targeting mips-sony-psx. +# Produces Mach-O cc1 and companion binaries targeting mips-sony-psx. # # Requires: byacc (install via Homebrew: brew install byacc) set -e @@ -18,11 +18,20 @@ trap 'rm -rf "$WORKDIR"' EXIT echo "Building gcc-2.7.2-cdk for macOS in $WORKDIR" +# Ensure we use the system compiler and tools, not any cross-compiler wrappers +# that may be in PATH (e.g. from a nix shell). +export PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH" +unset CC CXX + cd "$WORKDIR" curl -fL "https://github.com/decompals/old-gcc/releases/download/0.14/b18.tar.gz" | tar xz cd cdk-gcc-b18 # Apply the same patches as the Linux Dockerfile +# macOS SDKs declare sys_nerr as 'const int'; fix the conflicting declarations. +for f in gcc.c cp/g++.c; do + [ -f "$f" ] && sed -i '' 's/^extern int sys_nerr;/extern const int sys_nerr;/' "$f" +done patch -u -p1 Makefile.in -i "$PATCHES/Makefile-2.7.2-cdk.in.patch" patch -u -p1 obstack.h -i "$PATCHES/obstack-2.7.2-cdk.h.patch" patch -u -p1 config/mips/mips.h -i "$PATCHES/mipsel-2.7-cdk.patch" @@ -30,26 +39,24 @@ patch -su -p1 < "$PATCHES/psx-2.7.2-cdk.patch" # macOS: replace config.guess/config.sub with modern versions that know # about aarch64-apple-darwin, then add psx* to the OS list. +# Pinned to specific commits for reproducibility. chmod +w config.guess config.sub -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/74af13c174714dd3b9f1ded4b39955f003c16361/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/6fad101f3063d722e3348d07dc93cf737f8709e4/config.sub" -o config.sub chmod +x config.guess config.sub -python3 -c " -s = open('config.sub').read() -s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) -open('config.sub', 'w').write(s) -" +sed -i '' 's/| hiux\* | abug | nacl\*/| psx* \\\ +'"$(printf '\t')"' | hiux* | abug | nacl*/' config.sub # Add xm-darwin.h and teach configure about *-apple-darwin* hosts. cp "$PATCHES/xm-darwin.h" config/ chmod +w configure -python3 -c " -s = open('configure').read() -insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' -s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', - insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) -open('configure', 'w').write(s) -" +awk '/^\t\*\)$/ { buf=$0; next } + buf != "" { if (!done && /echo.*Configuration.*not supported/) { + print "\t*-apple-darwin*)"; print "\t\txm_file=xm-darwin.h" + print "\t\tfixincludes=Makefile.in"; print "\t\t;;"; done=1 } + print buf; buf="" } { print }' configure > configure.tmp +mv configure.tmp configure +chmod +x configure ./configure \ --target=mips-sony-psx \ @@ -62,13 +69,14 @@ open('configure', 'w').write(s) # Compile the __eprintf stub — this symbol was removed from macOS SDKs # after 10.14 but is referenced by the exception-handling code. -cc -std=gnu89 -c "$PATCHES/eprintf-darwin.c" -o eprintf-darwin.o +EPRINTF_OBJ="$PWD/eprintf-darwin.o" +cc -std=gnu89 -c "$PATCHES/eprintf-darwin.c" -o "$EPRINTF_OBJ" # Build single-threaded: parallel yacc invocations race on y.tab.h, # causing bi-parser.h to be generated with the wrong grammar. -make cc1 \ - CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration" \ - LDFLAGS="eprintf-darwin.o" +make cpp cc1 xgcc cc1plus g++ \ + CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration -Wno-return-mismatch" \ + LDFLAGS="$EPRINTF_OBJ" # Run the same tests as the Dockerfile ./cc1 -quiet -O2 "$SCRIPT_DIR/tests/little_endian.c" -o little_endian.s @@ -76,5 +84,6 @@ grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s ./cc1 -quiet -O2 "$SCRIPT_DIR/tests/section_attribute.c" -o /dev/null mkdir -p "$OUTDIR" -cp cc1 "$OUTDIR/cc1" -echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" +cp cpp cc1 xgcc cc1plus g++ "$OUTDIR/" +mv "$OUTDIR/xgcc" "$OUTDIR/gcc" +echo "Done — $OUTDIR/ ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/gcc-2.7.2-psx-macos.sh b/gcc-2.7.2-psx-macos.sh index ff8f75c..31d20be 100755 --- a/gcc-2.7.2-psx-macos.sh +++ b/gcc-2.7.2-psx-macos.sh @@ -1,6 +1,6 @@ #!/bin/bash # Build gcc-2.7.2-psx natively on macOS (x86_64 and aarch64). -# Produces a Mach-O cc1 binary targeting mips-sony-psx. +# Produces Mach-O cc1 and companion binaries targeting mips-sony-psx. set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" @@ -11,20 +11,24 @@ trap 'rm -rf "$WORKDIR"' EXIT echo "Building gcc-2.7.2-psx for macOS in $WORKDIR" +# Ensure we use the system compiler and tools, not any cross-compiler wrappers +# that may be in PATH (e.g. from a nix shell). +export PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH" +unset CC CXX + cd "$WORKDIR" curl -fL "https://ftp.gnu.org/old-gnu/gcc/gcc-2.7.2.tar.gz" | tar xz cd gcc-2.7.2 # Apply the same patches as the Linux Dockerfile -python3 -c " -import glob, os, stat -for f in glob.glob('*.c'): - s = open(f).read() - s2 = s.replace('include ', 'include ') - if s2 != s: - os.chmod(f, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) - open(f, 'w').write(s2) -" +# Replace varargs.h with stdarg.h; some files are read-only from the tarball. +grep -rl 'include ' *.c | xargs chmod u+w +sed -i '' 's/include /include /g' *.c +# macOS SDKs declare sys_nerr as 'const int'; fix the conflicting declarations. +for f in gcc.c cp/g++.c; do + [ -f "$f" ] && sed -i '' 's/^extern int sys_nerr;/extern const int sys_nerr;/' "$f" +done + patch -u -p1 obstack.h -i "$PATCHES/obstack-2.7.2.h.patch" patch -u -p1 configure -i "$PATCHES/configure.patch" patch -u -p1 config/mips/mips.h -i "$PATCHES/mipsel-2.7.patch" @@ -32,26 +36,24 @@ patch -su -p1 < "$PATCHES/psx-2.5.7.patch" # macOS: replace config.guess/config.sub with modern versions that know # about aarch64-apple-darwin, then add psx* to the OS list. +# Pinned to specific commits for reproducibility. chmod +w config.guess config.sub -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/74af13c174714dd3b9f1ded4b39955f003c16361/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/6fad101f3063d722e3348d07dc93cf737f8709e4/config.sub" -o config.sub chmod +x config.guess config.sub -python3 -c " -s = open('config.sub').read() -s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) -open('config.sub', 'w').write(s) -" +sed -i '' 's/| hiux\* | abug | nacl\*/| psx* \\\ +'"$(printf '\t')"' | hiux* | abug | nacl*/' config.sub # Add xm-darwin.h and teach configure about *-apple-darwin* hosts. cp "$PATCHES/xm-darwin.h" config/ chmod +w configure -python3 -c " -s = open('configure').read() -insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' -s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', - insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) -open('configure', 'w').write(s) -" +awk '/^\t\*\)$/ { buf=$0; next } + buf != "" { if (!done && /echo.*Configuration.*not supported/) { + print "\t*-apple-darwin*)"; print "\t\txm_file=xm-darwin.h" + print "\t\tfixincludes=Makefile.in"; print "\t\t;;"; done=1 } + print buf; buf="" } { print }' configure > configure.tmp +mv configure.tmp configure +chmod +x configure ./configure \ --target=mips-sony-psx \ @@ -62,8 +64,8 @@ open('configure', 'w').write(s) --disable-gdb \ --disable-werror -make --jobs "$(sysctl -n hw.ncpu)" cc1 \ - CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration" +make --jobs "$(sysctl -n hw.ncpu)" cpp cc1 xgcc cc1plus g++ \ + CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration -Wno-return-mismatch" # Run the same tests as the Dockerfile ./cc1 -quiet -O2 "$SCRIPT_DIR/tests/little_endian.c" -o little_endian.s @@ -72,5 +74,6 @@ grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s ./cc1 -quiet -help &1 | grep -- -msoft-float mkdir -p "$OUTDIR" -cp cc1 "$OUTDIR/cc1" -echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" +cp cpp cc1 xgcc cc1plus g++ "$OUTDIR/" +mv "$OUTDIR/xgcc" "$OUTDIR/gcc" +echo "Done — $OUTDIR/ ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/gcc-2.8.1-psx-macos.sh b/gcc-2.8.1-psx-macos.sh index 3fca097..961eca5 100755 --- a/gcc-2.8.1-psx-macos.sh +++ b/gcc-2.8.1-psx-macos.sh @@ -1,6 +1,6 @@ #!/bin/bash # Build gcc-2.8.1-psx natively on macOS (x86_64 and aarch64). -# Produces a Mach-O cc1 binary targeting mips-sony-psx. +# Produces Mach-O cc1 and companion binaries targeting mips-sony-psx. set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" @@ -21,41 +21,38 @@ curl -fL "https://mirrors.kernel.org/gnu/gcc/gcc-2.8.1.tar.gz" | tar xz cd gcc-2.8.1 # Apply the same patches as the Linux Dockerfile -python3 -c " -import glob, os, stat -for f in glob.glob('*.c'): - s = open(f).read() - s2 = s.replace('include ', 'include ') - if s2 != s: - os.chmod(f, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) - open(f, 'w').write(s2) -" +# Replace varargs.h with stdarg.h; some files are read-only from the tarball. +grep -rl 'include ' *.c | xargs chmod u+w +sed -i '' 's/include /include /g' *.c +# macOS SDKs declare sys_nerr as 'const int'; fix the conflicting declarations. +for f in gcc.c cp/g++.c; do + [ -f "$f" ] && sed -i '' 's/^extern int sys_nerr;/extern const int sys_nerr;/' "$f" +done + patch -u -p1 obstack.h -i "$PATCHES/obstack-2.8.1.h.patch" patch -u -p1 config/mips/mips.h -i "$PATCHES/mips.patch" patch -su -p1 < "$PATCHES/psx.patch" # macOS: replace config.guess/config.sub with modern versions that know # about aarch64-apple-darwin, then add psx* to the OS list. +# Pinned to specific commits for reproducibility. chmod +w config.guess config.sub -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess" -o config.guess -curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub" -o config.sub +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/74af13c174714dd3b9f1ded4b39955f003c16361/config.guess" -o config.guess +curl -fsL "https://raw.githubusercontent.com/gcc-mirror/gcc/6fad101f3063d722e3348d07dc93cf737f8709e4/config.sub" -o config.sub chmod +x config.guess config.sub -python3 -c " -s = open('config.sub').read() -s = s.replace('| hiux* | abug | nacl*', '| psx* \\\\\n\t | hiux* | abug | nacl*', 1) -open('config.sub', 'w').write(s) -" +sed -i '' 's/| hiux\* | abug | nacl\*/| psx* \\\ +'"$(printf '\t')"' | hiux* | abug | nacl*/' config.sub # Add xm-darwin.h and teach configure about *-apple-darwin* hosts. cp "$PATCHES/xm-darwin.h" config/ chmod +w configure -python3 -c " -s = open('configure').read() -insert = '\t*-apple-darwin*)\n\t\txm_file=xm-darwin.h\n\t\tfixincludes=Makefile.in\n\t\t;;\n' -s = s.replace('\t*)\n\t\techo \"Configuration \$machine not supported\"', - insert + '\t*)\n\t\techo \"Configuration \$machine not supported\"', 1) -open('configure', 'w').write(s) -" +awk '/^\t\*\)$/ { buf=$0; next } + buf != "" { if (!done && /echo.*Configuration.*not supported/) { + print "\t*-apple-darwin*)"; print "\t\txm_file=xm-darwin.h" + print "\t\tfixincludes=Makefile.in"; print "\t\t;;"; done=1 } + print buf; buf="" } { print }' configure > configure.tmp +mv configure.tmp configure +chmod +x configure # Explicitly pass --host/--build so configure uses a single-arg config.sub # call (which modern config.sub accepts), rather than its multi-arg form. @@ -78,7 +75,7 @@ export CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declarat # it to prevent spurious missing-file errors on some make versions. touch insn-config.h -make --jobs "$(sysctl -n hw.ncpu)" cc1 \ +make --jobs "$(sysctl -n hw.ncpu)" cpp cc1 xgcc cc1plus g++ \ CFLAGS="-std=gnu89 -w -Wno-int-conversion -Wno-implicit-function-declaration -Wno-return-mismatch" # Run the same tests as the Dockerfile @@ -90,5 +87,6 @@ grep -E 'lbu\s\$2,0\(\$4\)' little_endian.s ./cc1 -version &1 | grep -- -mgpopt mkdir -p "$OUTDIR" -cp cc1 "$OUTDIR/cc1" -echo "Done — $OUTDIR/cc1 ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" +cp cpp cc1 xgcc cc1plus g++ "$OUTDIR/" +mv "$OUTDIR/xgcc" "$OUTDIR/gcc" +echo "Done — $OUTDIR/ ($(file "$OUTDIR/cc1" | cut -d: -f2 | xargs))" diff --git a/patches/eprintf-darwin.c b/patches/eprintf-darwin.c index 8096626..35dd16c 100644 --- a/patches/eprintf-darwin.c +++ b/patches/eprintf-darwin.c @@ -3,7 +3,9 @@ #include #include -void +/* weak: if another object (e.g. tree.o in cdk) also defines __eprintf, + that strong definition takes precedence and no duplicate-symbol error occurs. */ +__attribute__((weak)) void __eprintf (const char *fmt, const char *file, unsigned line, const char *expr) { fprintf (stderr, fmt, file, line, expr);