No way it works...

This commit is contained in:
2025-11-18 22:27:45 +01:00
commit 78177c0a07
17 changed files with 109146 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
#!/bin/bash
# Build script for kernel with picolibc
set -e
# Paths
PICOLIBC_INSTALL="$(pwd)/picolibc-install"
BUILD_DIR="build"
OUT_DIR="$BUILD_DIR/out"
# Check if picolibc is installed
if [ ! -d "$PICOLIBC_INSTALL" ]; then
echo "Error: Picolibc not found at $PICOLIBC_INSTALL"
echo "Please run ./build-picolibc.sh first"
exit 1
fi
# Create build directories
mkdir -p "$BUILD_DIR"
mkdir -p "$OUT_DIR"
# Compiler flags
CFLAGS="-m32 -march=i686 -ffreestanding -nostdlib -fno-builtin"
CFLAGS="$CFLAGS -I$PICOLIBC_INSTALL/include"
CFLAGS="$CFLAGS -I./src/lib"
CFLAGS="$CFLAGS -I./src"
CFLAGS="$CFLAGS -Wall -Wextra"
LDFLAGS="-m elf_i386 -nostdlib"
LDFLAGS="$LDFLAGS -L$PICOLIBC_INSTALL/lib"
echo "=== Building kernel with picolibc ==="
# Build boot assembly
echo "Assembling boot code..."
nasm -f elf32 src/boot/kernel.asm -o "$BUILD_DIR/kasm.o"
# Build duktape with picolibc
echo "Building Duktape..."
gcc $CFLAGS -c src/lib/duktape.c -o "$BUILD_DIR/duktape.o"
# Build kernel
echo "Building kernel..."
gcc $CFLAGS -c src/kernel/kernel.c -o "$BUILD_DIR/kc.o"
# Link everything together
echo "Linking kernel..."
ld $LDFLAGS -T src/link.ld -o "$OUT_DIR/kernel" \
"$BUILD_DIR/kasm.o" \
"$BUILD_DIR/kc.o" \
"$BUILD_DIR/duktape.o" \
-lc
echo ""
echo "=== Build complete! ==="
echo "Kernel binary: $OUT_DIR/kernel"
echo ""
echo "To run the kernel:"
echo " qemu-system-i386 -kernel $OUT_DIR/kernel"

58
scripts/embed_js.py Executable file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""
Embed JavaScript file as a C string constant
"""
import sys
def escape_c_string(s):
"""Escape a string for use in C source code"""
result = []
for char in s:
if char == "\n":
result.append("\\n")
elif char == "\r":
result.append("\\r")
elif char == "\t":
result.append("\\t")
elif char == "\\":
result.append("\\\\")
elif char == '"':
result.append('\\"')
elif ord(char) < 32 or ord(char) > 126:
result.append(f"\\x{ord(char):02x}")
else:
result.append(char)
return "".join(result)
def main():
if len(sys.argv) != 2:
print("Usage: embed_js.py <input.js>", file=sys.stderr)
sys.exit(1)
input_file = sys.argv[1]
try:
with open(input_file, "r") as f:
js_code = f.read()
except FileNotFoundError:
print(f"Error: File not found: {input_file}", file=sys.stderr)
sys.exit(1)
# Generate C header file
print("/* Auto-generated file - do not edit */")
print("#ifndef EMBEDDED_JS_H")
print("#define EMBEDDED_JS_H")
print()
escaped = escape_c_string(js_code)
print(f'const char *embedded_js_code = "{escaped}";')
print()
print("#endif /* EMBEDDED_JS_H */")
if __name__ == "__main__":
main()

77
scripts/run.sh Executable file
View File

@@ -0,0 +1,77 @@
#!/bin/bash
# Build and run kernel with picolibc
set -e
# Paths
PICOLIBC_INSTALL="$(pwd)/picolibc-install"
BUILD_DIR="build"
OUT_DIR="$BUILD_DIR/out"
# Check if picolibc is installed
if [ ! -d "$PICOLIBC_INSTALL" ]; then
echo "Error: Picolibc not found at $PICOLIBC_INSTALL"
echo "Please run: make picolibc"
exit 1
fi
# Create build directories
mkdir -p "$BUILD_DIR"
mkdir -p "$OUT_DIR"
# Copy JavaScript source to build directory
cp src/os/index.js build/
# Generate embedded JavaScript header
echo "Generating embedded JavaScript..."
python3 scripts/embed_js.py build/index.js > "$BUILD_DIR/embedded_js.h"
# Compiler flags
CFLAGS="-m32 -march=i686 -ffreestanding -nostdlib -fno-builtin"
CFLAGS="$CFLAGS -I$PICOLIBC_INSTALL/include"
CFLAGS="$CFLAGS -I./src/lib"
CFLAGS="$CFLAGS -I./src"
CFLAGS="$CFLAGS -I$BUILD_DIR"
CFLAGS="$CFLAGS -Wall -Wextra"
LDFLAGS="-m elf_i386 -nostdlib"
LDFLAGS="$LDFLAGS -L$PICOLIBC_INSTALL/lib"
# Get libgcc path
LIBGCC=$(gcc -m32 -print-libgcc-file-name)
echo "=== Building kernel with picolibc ==="
# Build boot assembly
echo "Assembling boot code..."
nasm -f elf32 src/boot/kernel.asm -o "$BUILD_DIR/kasm.o"
# Build duktape
echo "Building Duktape..."
gcc $CFLAGS -c src/lib/duktape.c -o "$BUILD_DIR/duktape.o"
# Build kernel
echo "Building kernel..."
gcc $CFLAGS -c src/kernel/kernel.c -o "$BUILD_DIR/kc.o"
# Build syscalls
echo "Building syscalls..."
gcc $CFLAGS -c src/lib/syscalls.c -o "$BUILD_DIR/syscalls.o"
# Link everything together
echo "Linking kernel..."
ld $LDFLAGS -T src/link.ld -o "$OUT_DIR/kernel" \
"$BUILD_DIR/kasm.o" \
"$BUILD_DIR/kc.o" \
"$BUILD_DIR/duktape.o" \
"$BUILD_DIR/syscalls.o" \
-lc "$LIBGCC"
echo ""
echo "=== Build complete! ==="
echo ""
echo "Running kernel in QEMU..."
echo "(Press Ctrl+A then X to exit)"
echo ""
qemu-system-i386 -kernel "$OUT_DIR/kernel"