SchOS/ports/toybox/Makefile
Nexoniarz 4a032c9c62 build: fix four things a clean build found, three of them mine
`make` had been failing since the e1000 work and I did not see it, because I
was checking build output with `grep error` -- which matches neither "Błąd" nor
mkfs's "image full". Checking the exit status instead found all of this in a
minute.

**usbstick.img was full.** `make ldls-stage` puts a module and its BTF in
sysroot/lib/modules, and a vmlinux BTF is 5 MB on its own; the 16 MB test image
could not hold it. The size cannot move -- tools/windows/bootcheck.ps1 asserts
READ CAPACITY returns exactly 32768 sectors -- so the exclusions grew instead:
lib and usr as well as bin. That image exists to prove the USB driver can read
one block, so its contents were never the point. It also stopped a GPL module
riding into a test artifact, which RAMDISK_EXCLUDE does for the ramdisk and
nothing was doing for this one.

**toybox's httpd was shadowing ours.** /bin/httpd answered `-p` with toybox's
"Unknown option 'p'", because whichever install rule ran second won the name.
TOYBOX_SKIP existed for exactly this and held one entry, `ping`, found the same
hard way. It is now generated from cmd/ -- a list you have to remember is a
list that is wrong the next time cmd/ grows, and it already was: `chvt` had
been silently toybox's, whose VT ioctls are Linux's and not ours.

**ldlsd would not link with clang.** `*out = in;` on a 96-byte struct is a call
to memcpy as far as the C standard is concerned; gcc unrolled it and clang
emitted the call, so a freestanding program linked for months and then did not
on a machine where nothing about it had changed but the compiler. lib/glue.c
now provides memcpy, memmove, memset and memcmp -- the four an implementation
may call without being asked.

**OVMF was never found on NixOS.** `$(dir $(QEMU))share` is the *bin*
directory plus "share", so it looked in /usr/bin/share. The absolute paths
after it covered that up on distributions that keep firmware in /usr; on one
that keeps nothing there it meant the firmware was undiscoverable on a machine
that had it, and the only way to build was to copy the file in by hand. It
resolves the prefix properly now and looks in <prefix>/share/qemu too.

Verified by exit status, from a clean tree and a no-op: both 0. ldls-check
passes 27. The guest boots, serves /srv to the host and answers 403 to
%2e%2e/etc/passwd.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-14 19:45:22 +02:00

155 lines
7.2 KiB
Makefile

# SPDX-License-Identifier: Apache-2.0
# toybox — 0BSD-licensed: https://landley.net/toybox/
# Provides SchOS's core utilities (ls, cat, cp, mv, rm, mkdir, ...) as a
# single multicall binary, installed under sysroot/bin/ once per applet name.
#
# mkfs (tools/mkfs.c) doesn't understand symlinks or hardlink dedup — each
# sysroot/bin/<name> below is a real, independent copy of the same toybox
# binary. That costs disk space per applet instead of one shared inode, but
# needs no changes to mkfs and keeps this Makefile self-contained.
include ../mk/port.mk
# Applets SchOS provides itself, which toybox must not link over.
#
# The two collide by name, and the collision is worse than a duplicate: the
# applets are hard links to one multicall binary, so whichever install runs
# second wins *for all 236 of them*. Installing first-party ping over the
# applet replaced the shared inode and every applet became ping; installing
# the applet over first-party ping put it back and lost the real one.
#
# SchOS's ping talks to its own lwIP stack. toybox's would be the wrong answer
# even if it worked.
#
# The list is generated rather than remembered, because remembering it failed:
# `ping` was found the hard way, and then `httpd` was found the same hard way
# months later -- /bin/httpd ran, printed toybox's "Unknown option 'p'" and
# nobody could see why the web server had no -p flag. Anything in cmd/ that
# toybox also provides belongs here, and asking the directory is the only
# version of that which stays true when cmd/ grows.
#
# `chvt` is in the list for the same reason and was never noticed: toybox's
# wants Linux's VT ioctls, ours talks to SchOS's own terminal layer, and which
# one you got depended on the order two install rules happened to run in.
TOYBOX_SKIP = $(notdir $(wildcard $(TOPDIR)/cmd/*))
TOYBOX_VERSION := 0.8.12
TOYBOX_URL := http://landley.net/toybox/downloads/toybox-$(TOYBOX_VERSION).tar.gz
TOYBOX_SHA256 := ad88a921133ae2231d9f2df875ec0bd42af4429145caea7d7db9e02208a6fd2e
TARBALL := $(DISTDIR)/toybox-$(TOYBOX_VERSION).tar.gz
CFLAGS := -static -D__SCHOS__ -I$(MUSL)/include -fno-stack-protector
LDFLAGS := -static -L$(MUSL)/lib -lc
# The applet list is not hard-coded any more: `toybox` with no arguments prints
# exactly the applets the binary was built with, so the install below asks the
# binary itself. A hand-maintained list installed 57 of the ~250 available and
# silently drifted every time the toybox config changed.
$(TARBALL): | $(DISTDIR)
curl -fsSL -o $@ $(TOYBOX_URL)
sh $(SHA256CHECK) "$(TOYBOX_SHA256)" "$@"
download: $(TARBALL)
$(SRCDIR): $(TARBALL)
mkdir -p $(PORTDIR)
tar -xzf $< -C $(PORTDIR)
mv $(PORTDIR)/toybox-$(TOYBOX_VERSION) $@
# Applets toybox has written but leaves out of defconfig.
#
# `defconfig` builds the 236 applets upstream considers finished. The other
# ~50 live in toys/pending/ -- complete enough to use, not yet held to toybox's
# standard for a release -- and among them are **awk**, **bc** and **diff**,
# which is a large part of why the shipped userland felt thin. They are the
# same 0BSD code from the same tarball; the only thing standing between them
# and the image was a config default.
#
# This list is curated rather than "enable everything", because everything does
# not build: `strace` wants xfork and ptrace, `syslogd` wants BSD's
# facilitynames[], and `git` wants OpenSSL. Those three break the link, so they
# are not here. If one of them ever matters, it goes in with the patch it needs
# rather than by widening a wildcard.
#
# `vi` is in pending too and is deliberately **not** on this list. SchOS ships
# an editor you do not have to be taught -- see ports/kilo.
TOYBOX_PENDING = \
AWK BC DIFF MORE EXPR TR HEXDUMP STTY XZCAT CSPLIT \
LSOF MAN LAST FDISK CHSH GETTY \
TELNET TRACEROUTE ARP ARPING ROUTE \
IPCS IPCRM USERADD USERDEL GROUPADD GROUPDEL
# toybox has no out-of-tree build mode, so its source is copied into $(BUILDDIR)
# and built in place. $(SRCDIR) is an order-only prerequisite (|): the extracted
# tarball never changes, and depending on it normally made a directory mtime
# trigger a rebuild. The rebuild then failed anyway, because `cp -r` over a
# populated $(BUILDDIR) hits "File exists" on kconfig's symlinks — so wipe it
# first and always build from a clean copy.
#
# Two things happen to the copy before it is built.
#
# **The shebangs are rewritten.** toybox's build scripts start `#!/bin/bash`,
# and on a distribution that does not keep bash at that path -- NixOS, and any
# other that takes the FHS as a suggestion -- the build fails at the first
# script with ENOENT. bash is installed and on PATH; it is only the absolute
# path that is wrong, so `/usr/bin/env bash` fixes it everywhere without
# needing bash to move. This is not a nicety: without it toybox could not be
# rebuilt on this machine at all, and the binary in the tree was one nobody
# could reproduce.
#
# **Then the pending applets are switched on**, by editing .config directly.
# Not via `make oldconfig`, which is the obvious way and does not work: every
# pending symbol is `default n`, so oldconfig dutifully turns all of them back
# off again. generated/ is removed so the edit is actually noticed -- toybox
# caches the expanded config there and will otherwise link the previous set and
# report success.
$(BUILDDIR)/toybox: | $(SRCDIR)
rm -rf $(BUILDDIR)
mkdir -p $(BUILDDIR)
cp -r $(SRCDIR)/. $(BUILDDIR)/
sed -i '1s|^\#!/bin/bash$$|\#!/usr/bin/env bash|' $(BUILDDIR)/scripts/*.sh
$(MAKE) -C $(BUILDDIR) defconfig
for c in $(TOYBOX_PENDING); do \
sed -i "s/^\# CONFIG_$$c is not set$$/CONFIG_$$c=y/" $(BUILDDIR)/.config; \
done
rm -rf $(BUILDDIR)/generated
$(MAKE) -C $(BUILDDIR) CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
# Install every applet the binary provides, one name per applet.
#
# mkfs deduplicates by content, so all of these collapse to a single inode with
# many hard links in the image -- the cost is one copy, not one per name. On the
# host we try a hard link first for the same reason and fall back to a copy if
# the filesystem will not take one (NTFS via WSL usually will).
#
# The work hangs off a stamp file rather than off the phony `install` target.
# port.mk declares install .PHONY for every port, so this recipe used to run on
# every single build -- re-creating 236 links whether or not toybox had been
# rebuilt. That is slow on its own, and worse, it bumped the mtime of 236 files
# in sysroot/ every time, which made the top-level build think the staged tree
# had changed and rebuild the whole filesystem image from scratch.
install: $(BUILDDIR)/toybox.installed
$(BUILDDIR)/toybox.installed: $(BUILDDIR)/toybox | $(SYSROOT)/bin
cp $< $(SYSROOT)/bin/toybox
chmod 755 $(SYSROOT)/bin/toybox
@for a in $$($(BUILDDIR)/toybox); do \
case " $(TOYBOX_SKIP) " in *" $$a "*) continue ;; esac; \
rm -f $(SYSROOT)/bin/"$$a"; \
ln $(SYSROOT)/bin/toybox $(SYSROOT)/bin/"$$a" 2>/dev/null \
|| cp $< $(SYSROOT)/bin/"$$a"; \
done
@echo "toybox: installed $$($(BUILDDIR)/toybox | wc -w) applets, minus" \
"$$(for a in $$($(BUILDDIR)/toybox); do \
case " $(TOYBOX_SKIP) " in *" $$a "*) echo $$a ;; esac; \
done | tr '\n' ' ')(first-party)"
@touch $@
clean:
rm -rf $(PORTDIR)
distclean: clean
rm -f $(TARBALL)