Merge pull request #42 from clang-retro/claude/project-thread-k44b6o

Add build-sysroot.sh: a ready-made classic Mac OS sysroot
This commit is contained in:
Neo authored and GitHub committed 2026-11-28 13:17:37 -08:00
commit a970e3af17
6 files changed
+267

No files matched your search

+7
View File
@@ -102,3 +102,10 @@ jobs:
persist-credentials: false
- name: Tests
run: LLVM_SRC=$PWD/llvm PICOLIBC_SRC=$PWD/picolibc QUESA_SRC=$PWD/quesa tests/run.sh
# The sysroot that dos-sdk's nightly release publishes: build it, and
# build and run programs with it alone.
- name: Sysroot
run: |
LLVM_SRC=$PWD/llvm PICOLIBC_SRC=$PWD/picolibc sh build-sysroot.sh "$RUNNER_TEMP/macos-classic" >"$RUNNER_TEMP/sysroot.log" 2>&1 ||
{ tail -50 "$RUNNER_TEMP/sysroot.log"; exit 1; }
sh tests/sysroot.sh "$RUNNER_TEMP/macos-classic"
+8
View File
@@ -258,6 +258,14 @@ with 32-bit addresses, so it works in programs of any size.
calls that never ran on 68k (Navigation Services, Driver Services, the
Name Registry).
## The sysroot
A ready-made sysroot for 68k and PowerPC is in the nightly release of
clang-retro/dos-sdk (`get-toolchain.sh -s macos-classic`). It has the
headers, the C and C++ libraries, the builtins and `macapp.py`, so you
do not need to build them. `build-sysroot.sh` builds it from source.
See [docs/sysroot.md](docs/sysroot.md).
## Building applications
`tools/macapp.py` compiles C sources with this SDK's headers and links
+92
View File
@@ -0,0 +1,92 @@
#!/bin/sh
# Build the classic Mac OS sysroot: one directory that has all that is
# necessary to compile and link applications for 68k and PowerPC, from
# System 6 to Mac OS 9, without this source tree.
#
# CLANG=... LLVM_SRC=... PICOLIBC_SRC=... [GCC_SRC=...] build-sysroot.sh OUT
#
# CLANG clang-retro's clang (default: clang on the PATH); llvm-ar
# and the other LLVM tools are taken from the same directory
# LLVM_SRC a clang-retro/llvm checkout (compiler-rt's builtins and
# libunwind are read)
# PICOLIBC_SRC a clang-retro/picolibc checkout
# GCC_SRC an unpacked GCC 15.2.0 release (optional). Without it,
# the sysroot has no C++ library (libstdc++ and libunwind).
# OUT the sysroot directory. The script removes it first.
#
# OUT has:
# include/ the Toolbox headers
# lib/include/ picolibc's headers, and libstdc++'s in c++/
# lib/m68k/ lib/ppc/ libmacrt.a (the builtins), libc.a, libmacos.a,
# libmacconsole.a, and libstdc++.a, libsupc++.a and
# libunwind.a
# tools/macapp.py the compiler and linker driver
# tools/link/ the linkers (codelink.py, peflink.py, fatlink.py),
# start.S and the import index
# tools/sim68k.py tools/simppc.py
# the CPU emulators that run small programs
# examples/ the example programs
# share/licenses/ the license of each part
#
# Every file in OUT is free software: the SDK is AGPL-3.0-only, the
# headers made from Multiversal Interfaces are MIT, picolibc is BSD,
# compiler-rt and libunwind are Apache-2.0 with the LLVM exception, and
# libstdc++ is GPL-3.0 with the GCC Runtime Library Exception. No file
# from Apple is in OUT. docs/sysroot.md tells more.
set -e
here=$(cd "$(dirname "$0")" && pwd)
out=${1:?usage: build-sysroot.sh OUT}
: "${LLVM_SRC:?set LLVM_SRC to a clang-retro/llvm checkout}"
: "${PICOLIBC_SRC:?set PICOLIBC_SRC to a clang-retro/picolibc checkout}"
CLANG=${CLANG:-clang}
export CLANG LLVM_SRC PICOLIBC_SRC
rm -rf "$out"
mkdir -p "$out/lib"
out=$(cd "$out" && pwd)
# The libraries: the builtins first, because the C library calls them,
# then the C library, then the C++ library, which is built over it.
sh "$here/runtime/build.sh" "$out/lib"
sh "$here/runtime/build-libc.sh" "$out/lib"
if [ -n "${GCC_SRC:-}" ]; then
export GCC_SRC
sh "$here/runtime/build-libstdcxx.sh" "$out/lib"
else
echo "GCC_SRC is not set: the sysroot has no C++ library" >&2
fi
# The headers, the driver, the linkers and the emulators. macapp.py finds
# include/ and lib/ in the directory above its own, so it works in OUT as
# it does in this source tree.
cp -R "$here/include" "$out/include"
mkdir -p "$out/tools/link" "$out/examples"
cp "$here/tools/macapp.py" "$here/tools/sim68k.py" "$here/tools/simppc.py" \
"$here/tools/fakegl.py" "$out/tools/"
for f in "$here"/tools/link/*.py "$here"/tools/link/*.S "$here/tools/link/imports.json"; do
cp "$f" "$out/tools/link/"
done
cp -R "$here"/examples/* "$out/examples/"
# The licenses go with the sysroot.
lic=$out/share/licenses
mkdir -p "$lic/mac-sdk" "$lic/picolibc" "$lic/llvm"
cp "$here/LICENSE" "$lic/mac-sdk/LICENSE"
cp "$here/defs/COPYING.multiversal" "$lic/mac-sdk/COPYING.multiversal"
cp "$PICOLIBC_SRC/COPYING.picolibc" "$lic/picolibc/"
# A sparse checkout of LLVM can lack some of these files; each part of
# LLVM has the same license, so one of them is enough.
for p in compiler-rt libunwind llvm; do
if [ -f "$LLVM_SRC/$p/LICENSE.TXT" ]; then
cp "$LLVM_SRC/$p/LICENSE.TXT" "$lic/llvm/$p-LICENSE.TXT"
fi
done
ls "$lic/llvm/"*-LICENSE.TXT >/dev/null 2>&1 ||
{ echo "no LICENSE.TXT of LLVM in $LLVM_SRC" >&2; exit 1; }
# build-libstdcxx.sh puts the licenses of libstdc++ in lib/share/licenses.
if [ -d "$out/lib/share/licenses" ]; then
cp -R "$out/lib/share/licenses/"* "$lic/"
rm -rf "$out/lib/share"
fi
cp "$here/docs/sysroot.md" "$out/README.md"
echo "built $out"
+87
View File
@@ -0,0 +1,87 @@
# The classic Mac OS sysroot
The sysroot is one directory that has all the parts that are necessary
to compile and link classic Mac OS applications. It is for 68k and
PowerPC, from System 6 to Mac OS 9. You do not need this source tree to
use it. You do not need to build the libraries.
## Get the sysroot
The nightly release of clang-retro/dos-sdk has the sysroot as
`clang-dos-sysroot-macos-classic.tar.xz`. To get it, use
`get-toolchain.sh` from dos-sdk:
```sh
packaging/get-toolchain.sh -s macos-classic ~/retro # gives ~/retro/macos-classic
```
You also need clang from clang-retro/llvm, for example the clang of the
same release (`packaging/get-toolchain.sh ~/retro/toolchain`), and
Python 3.
## Build an application
`tools/macapp.py` in the sysroot compiles and links. It finds the
headers and the libraries in the sysroot:
```sh
S=~/retro/macos-classic
CLANG=path/to/clang python3 $S/tools/macapp.py -o Hello.bin $S/examples/hello.c
CLANG=path/to/clang python3 $S/tools/macapp.py --cpu 68k -o Hello68.bin hello.c
CLANG=path/to/clang python3 $S/tools/macapp.py --cpu ppc -o HelloPPC.bin hello.cpp
```
The default output is a fat application (68k and PowerPC) in MacBinary
II. To put it on a Mac disk, use hfsutils (`hcopy -m`). The options of
`macapp.py` are in [Building applications](building.md).
To compile without `macapp.py`, give clang the two include directories:
```sh
clang --target=m68k-apple-macos -isystem $S/lib/include -I$S/include -c a.c
clang --target=powerpc-apple-macos9 -isystem $S/lib/include -I$S/include -c a.c
```
Then link the objects with `$S/tools/macapp.py --cpu 68k` or
`--cpu ppc`.
## Contents
| Path | Contents |
| --- | --- |
| `include/` | The Toolbox headers (`Multiverse.h`, `Quickdraw.h`, ...), with MacTCP, Open Transport, the Shared Library Manager, QuickTime, OpenGL and Carbon accessors |
| `lib/include/` | The headers of picolibc (the C library) and of libstdc++ (`c++/`) |
| `lib/m68k/`, `lib/ppc/` | `libmacrt.a` (the compiler-rt builtins and soft float), `libc.a` (picolibc), `libmacos.a` (the Mac layer: files, sockets, threads, Open Transport, the Shared Library Manager client), `libmacconsole.a` (the console window), `libstdc++.a`, `libsupc++.a`, `libunwind.a` |
| `tools/macapp.py` | The compiler and linker driver |
| `tools/link/` | The linkers (`codelink.py` for 68k, `peflink.py` for PowerPC, `fatlink.py`), the start code, and the index of the shared library imports |
| `tools/sim68k.py`, `tools/simppc.py` | CPU emulators that run small programs (they need the `unicorn` Python package) |
| `examples/` | The example programs |
| `share/licenses/` | The license of each part |
## Build the sysroot
`build-sysroot.sh` in mac-sdk builds the sysroot. It takes about two
minutes.
```sh
CLANG=path/to/clang LLVM_SRC=path/to/llvm PICOLIBC_SRC=path/to/picolibc \
GCC_SRC=path/to/gcc-15.2.0 sh build-sysroot.sh out/macos-classic
CLANG=path/to/clang sh tests/sysroot.sh out/macos-classic
```
`GCC_SRC` is optional. If you do not set it, the sysroot has no C++
library. It needs meson, ninja and make. `tests/sysroot.sh` builds
programs with the sysroot only, and runs them in the CPU emulators.
## Licenses
All the files in the sysroot are free software. No file from Apple is
in the sysroot.
- The SDK (headers, Mac layer, linkers, tools): AGPL-3.0-only. The
headers made from Multiversal Interfaces are also MIT.
- picolibc: BSD-style licenses.
- compiler-rt builtins and libunwind: Apache-2.0 with the LLVM
exception.
- libstdc++ and libsupc++: GPL-3.0 with the GCC Runtime Library
Exception.
+1
View File
@@ -25,6 +25,7 @@ nav:
- "Mac OS 8, 9 and QuickTime": headers-newer.md
- "Which System has which call": availability.md
- "Systems and availability": systems.md
- "The sysroot": sysroot.md
- "Building applications": building.md
- "C++": cxx.md
- "Threads": threads.md
+72
View File
@@ -0,0 +1,72 @@
#!/bin/sh
# Test a sysroot that build-sysroot.sh made: compile and link programs
# with the macapp.py of the sysroot only (not with this source tree), for
# 68k, PowerPC and fat, and run the 68k builds as a 68000 and a 68020 and
# the PowerPC builds in the CPU emulators of the sysroot.
#
# CLANG=... tests/sysroot.sh SYSROOT
#
# CLANG clang-retro's clang (default: clang on the PATH)
# SYSROOT the sysroot to test
#
# The emulators need the unicorn Python package. Without it, the script
# only compiles and links. The C++ tests are done only when the sysroot
# has the C++ library.
set -e
tests=$(cd "$(dirname "$0")" && pwd)
root=$(cd "${1:?usage: tests/sysroot.sh SYSROOT}" && pwd)
CLANG=${CLANG:-clang}
export CLANG
work=$(mktemp -d "${TMPDIR:-/tmp}/sysroot-test.XXXXXX")
trap 'rm -rf "$work"' EXIT
# The sources go to a directory of their own, so that nothing in this
# source tree can be found by mistake.
cp "$tests/libc.c" "$tests/thread.c" "$tests/net.c" "$tests/ot.c" \
"$tests/cxx.cpp" "$tests/cxxthread.cpp" "$work/"
cd "$work"
macapp="python3 $root/tools/macapp.py"
# check NAME OUTPUT: stop when the output of a program shows a failure.
check() {
if grep -q FAILED "$2" || ! grep -q "^all ok" "$2"; then
echo "$1:"; cat "$2"; exit 1
fi
}
# The examples, fat.
for ex in "$root"/examples/*.c; do
$macapp -o "$(basename "$ex" .c).bin" -- -Wall -Werror "$ex" >/dev/null
done
echo "examples: linked"
progs="libc thread net ot"
[ -f "$root/lib/m68k/libstdc++.a" ] && progs="$progs cxx cxxthread"
for t in $progs; do
src=$t.c
[ -f "$src" ] || src=$t.cpp
for cpu in 68k ppc; do
$macapp --cpu $cpu -o "$t-$cpu.bin" -- -Wall -Werror "$src" >/dev/null
done
done
echo "tests: linked ($progs)"
if ! python3 -c "import unicorn" 2>/dev/null; then
echo "install unicorn to run the tests in the emulators"
exit 0
fi
sim68k="python3 $root/tools/sim68k.py --os --files --exit-anywhere --steps 10000000"
simppc="python3 $root/tools/simppc.py --files"
for t in $progs; do
input=
[ $t = libc ] && input="typed\nline\n"
for c in 68000 68020; do
$sim68k --cpu $c --input "$input" "$t-68k.bin" >sim.txt || { cat sim.txt; exit 1; }
check "$t on a $c" sim.txt
done
# Open Transport on PowerPC calls Mac OS's Open Transport, which the
# emulator does not have (tools/qemu/nettest.py runs that build).
[ $t = ot ] && continue
$simppc --input "$input" "$t-ppc.bin" >sim.txt || { cat sim.txt; exit 1; }
check "$t on PowerPC" sim.txt
done
echo "sysroot: ok"