Files
claude-os/scripts/build_apps.sh
AI c25ba1fccd feat: shell (sh) with keyboard driver, SYS_READ, SYS_EXEC
- PS/2 keyboard driver: IRQ1 handler, scancode set 1 translation,
  ring buffer, shift key support
- SYS_READ (fd=0): non-blocking keyboard read for stdin
- SYS_YIELD: calls schedule_tick directly (no nested INT 0x80)
- SYS_EXEC: loads binary from CPIO initrd, replaces process image
- User-space libc: crt0.S (C runtime startup), syscalls.h (inline
  syscall wrappers, basic string functions)
- Shell app (sh): readline with echo/backspace, builtins (cd, env,
  help, exit), fork+exec for external commands
- Updated build_apps.sh: C app support with crt0 linking, libc
  include path, .bss section in objcopy

Tested: shell boots, keyboard input works, hello-world runs via
fork+exec, env shows CWD, exit cleanly terminates.
2026-02-23 13:08:06 +00:00

69 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
# Build all user-mode applications as flat binaries.
# Usage: build_apps.sh <apps_dir> <output_dir>
# Each app directory in <apps_dir>/ gets compiled and its flat binary
# is placed in <output_dir>/.
set -e
APPS_DIR="$1"
OUTPUT_DIR="$2"
LINKER_SCRIPT="$APPS_DIR/user.ld"
CC="${CC:-clang}"
OBJCOPY="${OBJCOPY:-objcopy}"
CFLAGS="-ffreestanding -m32 -fno-pie -fno-pic -fno-builtin -fno-stack-protector -mno-sse -mno-mmx -O2 -Wall -I$APPS_DIR/libc"
LDFLAGS="-m32 -nostdlib -no-pie -Wl,--no-dynamic-linker"
mkdir -p "$OUTPUT_DIR"
# Build crt0 if it exists
CRT0_OBJ=""
if [ -f "$APPS_DIR/libc/crt0.S" ]; then
CRT0_OBJ="$OUTPUT_DIR/_crt0.o"
$CC $CFLAGS -c "$APPS_DIR/libc/crt0.S" -o "$CRT0_OBJ"
fi
for app_dir in "$APPS_DIR"/*/; do
[ -d "$app_dir" ] || continue
app_name=$(basename "$app_dir")
# Skip the libc directory (shared library, not an app)
[ "$app_name" = "libc" ] && continue
echo "Building app: $app_name"
# Collect source files
OBJ_FILES=""
for src in "$app_dir"*.S "$app_dir"*.c; do
[ -f "$src" ] || continue
obj="$OUTPUT_DIR/${app_name}_$(basename "${src%.*}").o"
$CC $CFLAGS -c "$src" -o "$obj"
OBJ_FILES="$OBJ_FILES $obj"
done
if [ -z "$OBJ_FILES" ]; then
echo " No sources found, skipping"
continue
fi
# Link into ELF (include crt0 if app has .c files and doesn't have its own _start)
elf="$OUTPUT_DIR/$app_name.elf"
HAS_C_FILES=""
for src in "$app_dir"*.c; do
[ -f "$src" ] && HAS_C_FILES="yes"
done
if [ -n "$HAS_C_FILES" ] && [ -n "$CRT0_OBJ" ]; then
$CC $LDFLAGS -T "$LINKER_SCRIPT" "$CRT0_OBJ" $OBJ_FILES -o "$elf"
else
$CC $LDFLAGS -T "$LINKER_SCRIPT" $OBJ_FILES -o "$elf"
fi
# Convert to flat binary (include .bss for zero-initialized data)
bin="$OUTPUT_DIR/$app_name"
$OBJCOPY -O binary --only-section=.text --only-section=.rodata --only-section=.data --only-section=.bss "$elf" "$bin"
size=$(wc -c < "$bin")
echo " Built: $bin ($size bytes)"
done