SchOS/ports/ncurses/Makefile
Nexoniarz 14f0e4d0e2 kernel: processes have an environment now, and zsh is the shell
Porting zsh found four defects in the kernel and one in the filesystem. None
of them were about zsh; all of them had been there the whole time.

**exec threw the environment away.** kernel/fs/exec.c wrote a single zero for
envp with the comment "(no environment)" -- not yet, but ever, for every
process. musl has been passing a perfectly good envp to execve (Linux 59, which
SchOS answers) and it was never read. So $TERM was unset, $PATH was whatever
each program invented, and a variable exported in a shell did not survive the
exec meant to carry it. exec() takes an envp, sys_exec reads argument three,
and schinit manifests grew an Environment key. The userland two-argument
exec(path, argv) stays as a wrapper that passes null explicitly -- the raw stub
is execve now, so a short caller cannot leave %rdx holding a stray pointer.

**Which overflowed the kernel stack.** Three pointer arrays, ~1.8 KB, on a
4096-byte stack, under exec -> loaduvm -> readi -> SchFS -> btree. Pid 1 went
down on the first exec. The canary caught it and named the process, which is
what it is for. They live in one kalloc'd page now.

**waitpid returned a bare -1** for both "no children" and "interrupted". musl
reads [-4095,-1] as -errno, so userspace saw EPERM; zsh printed "wait failed:
operation not permitted" and abandoned the rest of /etc/zsh/zshrc. -ECHILD and
-EINTR now. Third time a bare -1 has cost real time -- on a system with no
permission model, EPERM should be read as "a syscall returned -1".

**NVMA was 64.** musl's allocator takes a mapping per size class and does not
release them eagerly, so compinit ran out of slots and got -1, which is EPERM,
which malloc turns into NULL, which zsh reports as "fatal error: out of memory"
-- on a machine with 1000 MB free. 256 slots, and -ENOMEM.

**And a small write to SchFS costs 58 ms.** 200 writes to one open file take
11.75 s; the same to /dev/null take 0.14 s. That is the design stated at the
top of kmount.c -- one transaction group per operation, and a commit is two
flushes and an uberblock -- not a bug. But compdump writes its digest with
thousands of separate prints, so it takes minutes and looks like a hang.
compinit -D, which skips the dump, finishes in 3.6 s. This one is a decision
rather than a fix, so it is written up and left: /etc/zsh/zshrc keeps compinit
switched off and explains exactly why, with the measurements.

zsh runs as /bin/zsh and is the console shell. ZLE works -- arrow keys recall
history through terminfo, $COLUMNS reads the real 160 because TIOCGWINSZ was
fixed, colour and prompt expansion work, startup is 0.14 s. mksh keeps /bin/sh,
so schinit's no-jobs fallback is a different program from the one that could
have failed.

Also fixed while here: AT_CLKTCK was the literal 100 and is USER_HZ; the
extract rule in four ports could move a source tree inside itself when make
re-ran it, which is how zsh's configure came to write modfile=zsh-5.9/... into
config.modules and fail three steps later.

schfs-check passes. Full writeup in docs/history/kernel.md section 14.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-12 23:26:50 +02:00

128 lines
5 KiB
Makefile

# SPDX-License-Identifier: Apache-2.0
# ncurses -- terminal capability database and screen library, MIT:
# https://invisible-island.net/ncurses/
#
# Here because zsh's line editor is the reason to want zsh, and the line editor
# drives the terminal through tigetstr/tputs. zsh's configure will happily build
# without a terminal library -- AC_SEARCH_LIBS is soft -- and produce a shell
# whose cursor cannot move and whose arrow keys do nothing. So this comes first.
#
# Unlike every other port, nothing here reaches sysroot/. ncurses is built
# static and consumed at link time by zsh; `install` means "into $(PREFIX)".
#
# The awkward part of ncurses is that it is a library whose data is a database.
# Normally that database is ~4000 compiled terminal descriptions under
# /usr/share/terminfo, which SchOS has no business shipping. --disable-database
# plus --with-fallbacks compiles a chosen handful directly into the library, so
# the resulting .a is self-contained and the filesystem stays clean.
include ../mk/port.mk
NC_VERSION := 6.4
NC_URL := https://ftp.gnu.org/gnu/ncurses/ncurses-$(NC_VERSION).tar.gz
NC_SHA256 := 6931283d9ac87c5073f30b6290c4c75f21632bb4fc3603ac8100812bed248159
TARBALL := $(DISTDIR)/ncurses-$(NC_VERSION).tar.gz
# The terminal types compiled into the library. `linux` is what SchOS's own
# console is closest to and what TERM is set to; the rest are here so a serial
# console or an ssh client from a real machine still gets a working editor.
# Every name added is bytes in the binary, so this list is deliberately short.
NC_FALLBACKS := linux,vt100,vt102,xterm,xterm-256color,screen,dumb
# Cross-compiling ncurses needs tic and infocmp *on the host* to turn those
# names into C. Both exist here; the paths are looked up rather than written
# down because on NixOS they live in the store and move on every update.
HOST_TIC ?= $(shell command -v tic)
HOST_INFOCMP ?= $(shell command -v infocmp)
# gcc, for the same reason fastfetch uses it: -print-libgcc-file-name.
HOSTCC_NC ?= gcc
GCC_INC := $(shell $(HOSTCC_NC) -print-file-name=include)
LIBGCC := $(shell $(HOSTCC_NC) -print-libgcc-file-name)
# Split exactly as autoconf consumes them: it links a test as
# $CC $CFLAGS $LDFLAGS conftest.c $LIBS
# so crt1.o/crti.o must ride in LDFLAGS (before the object) and libc in LIBS
# (after it). libc twice -- libgcc reaches back into it, and a static link
# resolves left to right.
NC_CPPFLAGS := -nostdinc -isystem $(MUSL)/include -isystem $(GCC_INC) -D__SCHOS__
NC_CFLAGS := -O2 $(CFLAGS_BT)
NC_LDFLAGS := -static -nostdlib -L$(MUSL)/lib $(MUSL)/lib/crt1.o $(MUSL)/lib/crti.o
NC_LIBS := $(MUSL)/lib/libc.a $(LIBGCC) $(MUSL)/lib/libc.a $(MUSL)/lib/crtn.o
# ncurses builds make_hash and make_keys to run during the build, not on the
# target. In a cross build those must be compiled for the host, with the host's
# own libc -- hence a second, completely separate set of flags.
NC_BUILD_CC := $(HOSTCC_NC)
NC_CONFIGURE := \
--host=x86_64-linux-musl \
--build=x86_64-pc-linux-gnu \
--prefix=$(PREFIX) \
--disable-database \
--with-fallbacks=$(NC_FALLBACKS) \
--with-tic-path=$(HOST_TIC) \
--with-infocmp-path=$(HOST_INFOCMP) \
--without-shared --with-normal \
--enable-widec \
--enable-overwrite \
--without-cxx --without-cxx-binding \
--without-ada --without-tests --without-progs --without-manpages \
--without-debug --without-profile \
--disable-stripping \
--disable-home-terminfo \
--disable-rpath-hack
$(TARBALL): | $(DISTDIR)
curl -fsSL -o $@ $(NC_URL)
sh $(SHA256CHECK) "$(NC_SHA256)" "$@"
download: $(TARBALL)
$(SRCDIR): $(TARBALL)
mkdir -p $(PORTDIR)
rm -rf $@ $(PORTDIR)/ncurses-$(NC_VERSION)
tar -xzf $< -C $(PORTDIR)
mv $(PORTDIR)/ncurses-$(NC_VERSION) $@
touch $@
extract: $(SRCDIR)
$(BUILDDIR)/config.status: | $(SRCDIR)
@test -n "$(HOST_TIC)" -a -n "$(HOST_INFOCMP)" || { \
echo "ncurses: host tic/infocmp not found -- install ncurses on the build host" >&2; \
exit 1; }
mkdir -p $(BUILDDIR)
cd $(BUILDDIR) && \
CC="$(HOSTCC_NC)" \
CPPFLAGS="$(NC_CPPFLAGS)" \
CFLAGS="$(NC_CFLAGS)" \
LDFLAGS="$(NC_LDFLAGS)" \
LIBS="$(NC_LIBS)" \
BUILD_CC="$(NC_BUILD_CC)" \
BUILD_CPPFLAGS= BUILD_CFLAGS= BUILD_LDFLAGS= BUILD_LIBS= \
$(SRCDIR)/configure $(NC_CONFIGURE)
$(PREFIX)/lib/libncursesw.a: $(BUILDDIR)/config.status
$(MAKE) -C $(BUILDDIR)
$(MAKE) -C $(BUILDDIR) install
build: $(PREFIX)/lib/libncursesw.a
# The acceptance test. It is the one thing this port puts in sysroot/, and it
# earns the space: the build proving the library compiles says nothing about
# whether terminfo works on a SchOS console, which is a different question
# with a different answer. sysroot/bin already carries socktest, nettest and
# shmtest for the same reason.
$(SYSROOT)/bin/tinfotest: tinfotest.c $(PREFIX)/lib/libncursesw.a | $(SYSROOT)/bin
$(HOSTCC_NC) $(NC_CPPFLAGS) -isystem $(PREFIX)/include $(NC_CFLAGS) $(NC_LDFLAGS) -L$(PREFIX)/lib -o $@ $< -lncursesw $(NC_LIBS)
chmod 755 $@
install: $(PREFIX)/lib/libncursesw.a $(SYSROOT)/bin/tinfotest
clean:
rm -rf $(PORTDIR)
distclean: clean
rm -f $(TARBALL)