Files
MontaukOS/programs/GNUmakefile
T

484 lines
17 KiB
Makefile

# Nuke built-in rules and variables.
MAKEFLAGS += -rR
.SUFFIXES:
# Target architecture.
ARCH := x86_64
# Auto-detect cross compiler from toolchain/local/.
TOOLCHAIN_PREFIX := $(shell cd .. && pwd)/toolchain/local/bin/x86_64-montauk-
ifeq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CXX = $(error x86_64-montauk toolchain not found. Run ./toolchain/build-montauk-toolchain.sh first)
CC = $(error x86_64-montauk toolchain not found. Run ./toolchain/build-montauk-toolchain.sh first)
else
CXX := $(TOOLCHAIN_PREFIX)g++
CC := $(TOOLCHAIN_PREFIX)gcc
endif
# Compiler flags. The x86_64-montauk toolchain supplies the target ABI
# (static ET_EXEC, 4 KiB pages, no red zone) and header paths; system
# programs additionally avoid FP/SIMD and their own runtime (-nostdlib).
override CXXFLAGS := \
-std=gnu++20 \
-g -O2 -pipe \
-Wall \
-Wextra \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-rtti \
-fno-exceptions \
-ffunction-sections \
-fdata-sections \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-I include
# Linker flags: freestanding static ELF.
override LDFLAGS := \
-nostdlib \
-Wl,--gc-sections \
-T link.ld
# Output directory.
BINDIR := bin
# Discover all programs (each subdirectory under src/ is a program).
PROGRAMS := $(notdir $(wildcard src/*))
# Programs with custom Makefiles (built separately).
CUSTOM_BUILDS := 2048 doom fetch wiki wikipedia weather imageviewer fontpreview spreadsheet wordprocessor pdfviewer disks devexplorer installer audio music video bluetooth network terminal klog procmgr powermgr calculator charmap desktop login shell rpgdemo paint tcc lua screenshot texteditor mandelbrot printers timezone printd printctl dialogs libloader crashpad
SYSTEM_PROGRAMS := $(filter-out $(CUSTOM_BUILDS),$(PROGRAMS))
# Build targets: system programs go to bin/os/, apps go to bin/apps/<name>/.
TARGETS := $(addprefix $(BINDIR)/os/,$(addsuffix .elf,$(SYSTEM_PROGRAMS)))
# Man pages source directory.
MANDIR := man
MANSRC := $(wildcard $(MANDIR)/*.*)
MANDST := $(patsubst $(MANDIR)/%,$(BINDIR)/man/%,$(MANSRC))
# Web content source directory.
WWWDIR := www
WWWSRC := $(wildcard $(WWWDIR)/*.*)
WWWDST := $(patsubst $(WWWDIR)/%,$(BINDIR)/www/%,$(WWWSRC))
# CA certificate bundle.
CA_CERTS := $(BINDIR)/os/certs/ca-certificates.crt
# Device firmware blobs (e.g. Intel Bluetooth ibt-*.sfi/.ddc) bundled into
# bin/os/firmware/ so the kernel can load them from the ramdisk at runtime.
FWDIR := data/firmware
FWSRC := $(shell find $(FWDIR) -type f 2>/dev/null)
FWDST := $(patsubst $(FWDIR)/%,$(BINDIR)/os/firmware/%,$(FWSRC))
# System config TOML files bundled into bin/config/.
CONFIGDIR := data/config
CONFIGSRC := $(wildcard $(CONFIGDIR)/*.toml)
CONFIGDST := $(patsubst $(CONFIGDIR)/%,$(BINDIR)/config/%,$(CONFIGSRC))
# Third-party license texts and notices bundled into bin/os/licenses/.
LICDIR := data/licenses
LICSRC := $(wildcard $(LICDIR)/*.txt)
LICDST := $(patsubst $(LICDIR)/%,$(BINDIR)/os/licenses/%,$(LICSRC)) $(BINDIR)/os/licenses/NOTICES.txt $(BINDIR)/os/licenses/LICENSE.txt
# Wallpapers bundled into bin/os/wallpapers/.
WPDIR := data/wallpapers
WPSRC := $(wildcard $(WPDIR)/*.jpg)
WPDST := $(patsubst $(WPDIR)/%,$(BINDIR)/os/wallpapers/%,$(WPSRC))
.PHONY: all clean 2048 doom fetch wiki wikipedia weather imageviewer fontpreview spreadsheet wordprocessor pdfviewer disks devexplorer installer audio music video bluetooth network terminal klog procmgr powermgr calculator charmap login desktop shell rpgdemo paint tcc lua screenshot texteditor mandelbrot printers timezone printd printctl dialogs icons fonts configs bearssl libc tls libjpeg libjpegwrite install-apps libloader crashpad check-syscalls gen-syscalls
all: bearssl libc libjpeg libjpegwrite tls libloader devkit $(TARGETS) fetch wiki wikipedia weather imageviewer fontpreview spreadsheet wordprocessor pdfviewer disks devexplorer installer audio music video bluetooth network terminal klog procmgr powermgr calculator charmap 2048 doom rpgdemo paint tcc lua screenshot texteditor mandelbrot printers timezone printd printctl dialogs login desktop shell icons fonts install-apps crashpad $(MANDST) $(WWWDST) $(CA_CERTS) $(CONFIGDST) $(FWDST) $(LICDST) $(WPDST)
# Build BearSSL static library (cross-compiled for freestanding x86_64).
BEARSSL_INCLUDES := -isystem $(shell cd .. && pwd)/kernel/freestnd-c-hdrs/x86_64/include -isystem $(abspath include/libc)
lib/bearssl/build/libbearssl.a:
$(MAKE) -C lib/bearssl CONF=MontaukOS \
BEARSSL_CC="$(CC)" \
BEARSSL_AR="$(TOOLCHAIN_PREFIX)ar" \
BEARSSL_INCLUDES="$(BEARSSL_INCLUDES)"
bearssl: lib/bearssl/build/libbearssl.a
# Build shared libc static library.
libc:
$(MAKE) -C lib/libc
# Build shared TLS helper library.
tls: bearssl libc
$(MAKE) -C lib/tls
# Build the shared-library loader used by runtime GUI components.
libloader: libc
$(MAKE) -C src/libloader
# Build fetch via its own Makefile (depends on bearssl, libc, and tls).
fetch: bearssl libc tls
$(MAKE) -C src/fetch
# Build wiki via its own Makefile (depends on bearssl, libc, and tls).
wiki: bearssl libc tls
$(MAKE) -C src/wiki
# Build wikipedia standalone GUI client (depends on bearssl, libc, and tls).
wikipedia: bearssl libc tls
$(MAKE) -C src/wikipedia
# Build weather standalone GUI client (depends on bearssl, libc, and tls).
weather: bearssl libc tls
$(MAKE) -C src/weather
# Build libjpeg static library (JPEG decoding via stb_image).
libjpeg: libc
$(MAKE) -C lib/libjpeg
# Build libjpegwrite static library (JPEG encoding via stb_image_write).
libjpegwrite: libc
$(MAKE) -C lib/libjpegwrite
# Build imageviewer standalone GUI client (depends on libc and libjpeg).
imageviewer: libc libjpeg
$(MAKE) -C src/imageviewer
# Build fontpreview standalone GUI client (no library deps).
fontpreview:
$(MAKE) -C src/fontpreview
# Build spreadsheet standalone GUI client (depends on libc and libdialogs).
spreadsheet: libc libloader dialogs
$(MAKE) -C src/spreadsheet
# Build word processor standalone GUI client (depends on libc and libdialogs).
wordprocessor: libc libloader dialogs
$(MAKE) -C src/wordprocessor
# Build PDF viewer standalone GUI client (depends on libc and libdialogs).
pdfviewer: libc libloader dialogs
$(MAKE) -C src/pdfviewer
# Build disks standalone GUI tool (depends on libc).
disks: libc
$(MAKE) -C src/disks
# Build devexplorer standalone GUI tool (depends on libc).
devexplorer: libc
$(MAKE) -C src/devexplorer
# Build installer standalone GUI tool (depends on libc).
installer: libc
$(MAKE) -C src/installer
# Build audio control standalone GUI tool (depends on libc).
audio: libc
$(MAKE) -C src/audio
# Build music player standalone GUI tool (depends on libc).
music: libc
$(MAKE) -C src/music
# Build videos standalone GUI tool (depends on libc).
video: libc
$(MAKE) -C src/video
# Build bluetooth manager standalone GUI tool (depends on libc).
bluetooth: libc
$(MAKE) -C src/bluetooth
# Build network manager standalone GUI tool (depends on libc).
network: libc
$(MAKE) -C src/network
# Build terminal standalone GUI tool (depends on libc).
terminal: libc
$(MAKE) -C src/terminal
# Build kernel log standalone GUI tool (depends on libc).
klog: libc
$(MAKE) -C src/klog
# Build process manager standalone GUI tool (depends on libc).
procmgr: libc
$(MAKE) -C src/procmgr
# Build power manager standalone GUI tool (depends on libc).
powermgr: libc
$(MAKE) -C src/powermgr
# Build calculator standalone GUI tool (depends on libc).
calculator: libc
$(MAKE) -C src/calculator
# Build character map standalone GUI tool (depends on libc).
charmap: libc
$(MAKE) -C src/charmap
# Build printers standalone GUI tool (depends on libc, bearssl, and tls).
printers: bearssl libc tls
$(MAKE) -C src/printers
# Build time zone standalone GUI tool (depends on libc).
timezone: libc
$(MAKE) -C src/timezone
# Build login screen via its own Makefile (depends on bearssl and libc).
login: bearssl libc
$(MAKE) -C src/login
# Build shell via its own Makefile (multi-file build).
shell:
$(MAKE) -C src/shell
# Build desktop via its own Makefile (depends on libc, libjpeg, bearssl for user.h, dialogs, and registry config files).
desktop: libc libjpeg bearssl libloader dialogs configs
$(MAKE) -C src/desktop
# Copy SVG icons for the desktop into bin/icons/.
icons:
../scripts/copy_icons.sh
# Copy TTF fonts for the desktop into bin/fonts/.
fonts:
../scripts/copy_fonts.sh
# Copy default system config files into bin/config/.
configs: $(CONFIGDST)
# Build paint standalone GUI tool (depends on libc).
paint: libc
$(MAKE) -C src/paint
# Build text editor standalone GUI app (depends on libc and libdialogs).
texteditor: libc libloader dialogs
$(MAKE) -C src/texteditor
# Build Mandelbrot standalone GUI app (depends on libc).
mandelbrot: libc
$(MAKE) -C src/mandelbrot
# Build 2048 standalone MTK game (depends on libc, libloader, and libdialogs).
2048: libc libloader dialogs
$(MAKE) -C src/2048
# Build RPG demo game via its own Makefile (depends on libc).
rpgdemo: libc
$(MAKE) -C src/rpgdemo
# Build doom via its own Makefile (depends on libc).
doom: libc
$(MAKE) -C src/doom
# Build screenshot tool (depends on libc, libjpegwrite, libloader, and libdialogs).
screenshot: libc libjpegwrite libloader dialogs
$(MAKE) -C src/screenshot
# Verify the TCC C API header (libc/montauk.h) exposes every kernel syscall with
# matching numbers. Fails the build on drift; 'make gen-syscalls' refreshes them.
check-syscalls:
python3 ../scripts/montauk-syscalls.py check
# Regenerate the MTK_SYS_* number block in libc/montauk.h from Api/Syscall.hpp.
# Wrapper functions stay hand-written; this only updates the numbers.
gen-syscalls:
python3 ../scripts/montauk-syscalls.py gen
# Build TCC (Tiny C Compiler) via its own Makefile (depends on libc).
# check-syscalls guards against shipping a stale C API header into the ramdisk.
tcc: libc check-syscalls
$(MAKE) -C src/tcc
mkdir -p $(BINDIR)/sdk/tcc/include
cp -r src/tcc/tcc_include/* $(BINDIR)/sdk/tcc/include/
cp include/libc/*.h $(BINDIR)/sdk/tcc/include/
mkdir -p $(BINDIR)/sdk/tcc/include/sys
cp include/libc/sys/*.h $(BINDIR)/sdk/tcc/include/sys/
# Build Lua interpreter and static library (depends on libc).
lua: libc
$(MAKE) -C src/lua
# Build print spooler daemon (depends on libc, tls, and bearssl).
printd: bearssl libc tls libjpegwrite
$(MAKE) -C src/printd
# Build print control utility (depends on libc, tls, and bearssl).
printctl: bearssl libc tls
$(MAKE) -C src/printctl
# Build libdialogs shared library.
dialogs:
$(MAKE) -C src/dialogs
# Build crashpad (crash reporter popup, depends on libc).
crashpad: libc
$(MAKE) -C src/crashpad
# Install app bundles (manifests, icons, data files) into bin/apps/<name>/.
install-apps: 2048 doom rpgdemo paint spreadsheet wordprocessor weather wikipedia imageviewer fontpreview pdfviewer disks devexplorer installer audio music video bluetooth network terminal klog procmgr powermgr calculator charmap screenshot texteditor mandelbrot printers timezone crashpad
../scripts/install_apps.sh
# Copy man pages into bin/man/ so mkramdisk.sh picks them up.
$(BINDIR)/man/%: $(MANDIR)/%
mkdir -p $(BINDIR)/man
cp $< $@
# Copy web content into bin/www/ so mkramdisk.sh picks them up.
$(BINDIR)/www/%: $(WWWDIR)/%
mkdir -p $(BINDIR)/www
cp $< $@
# Copy CA certificate bundle into bin/os/certs/.
$(CA_CERTS): data/ca-certificates.crt
mkdir -p $(BINDIR)/os/certs
cp $< $@
# Copy device firmware blobs into bin/os/firmware/ (preserving subdirs).
$(BINDIR)/os/firmware/%: $(FWDIR)/%
mkdir -p $(dir $@)
cp $< $@
# Copy system config files into bin/config/.
$(BINDIR)/config/%: $(CONFIGDIR)/%
mkdir -p $(BINDIR)/config
cp $< $@
# Copy third-party license texts into bin/os/licenses/. The notices and
# MontaukOS license files are sourced from the website copies so there is a
# single file of each to keep updated.
$(BINDIR)/os/licenses/NOTICES.txt: ../montaukos.org/THIRD-PARTY-NOTICES.txt
mkdir -p $(BINDIR)/os/licenses
cp $< $@
$(BINDIR)/os/licenses/LICENSE.txt: ../montaukos.org/license.txt
mkdir -p $(BINDIR)/os/licenses
cp $< $@
$(BINDIR)/os/licenses/%: $(LICDIR)/%
mkdir -p $(BINDIR)/os/licenses
cp $< $@
# Copy wallpapers into bin/os/wallpapers/.
$(BINDIR)/os/wallpapers/%: $(WPDIR)/%
mkdir -p $(BINDIR)/os/wallpapers
cp $< $@
# Build each system program from its source files.
$(BINDIR)/os/%.elf: src/%/main.cpp link.ld GNUmakefile
mkdir -p $(BINDIR)/os obj/$*
$(CXX) $(CXXFLAGS) -c src/$*/main.cpp -o obj/$*/main.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) obj/$*/main.o lib/libc/liblibc.a -o $@
# ============================================================================
# Native development kit (Montauk SDK): binutils built for MontaukOS
# plus a target sysroot (headers, libc.a, crt objects) under 0:/sdk.
# Skipped when the native tools have not been built; see
# toolchain/README.md.
# ============================================================================
NATIVE_BIN := ../toolchain/native/sdk/bin
DEVKIT_TOOLS := as ld ar nm objcopy objdump readelf ranlib strip size strings addr2line c++filt elfedit
.PHONY: devkit
devkit: libc
ifneq ($(wildcard $(NATIVE_BIN)/as),)
# Clean only the devkit-owned subtrees; sdk/tcc and sdk/lua are
# installed into bin/sdk/ by their own targets.
rm -rf $(BINDIR)/sdk/bin $(BINDIR)/sdk/lib $(BINDIR)/sdk/include \
$(BINDIR)/sdk/libexec $(BINDIR)/sdk/x86_64-montauk
mkdir -p $(BINDIR)/sdk/bin $(BINDIR)/sdk/lib $(BINDIR)/sdk/include
for t in $(DEVKIT_TOOLS); do \
cp $(NATIVE_BIN)/$$t $(BINDIR)/sdk/bin/$$t.elf || exit 1; \
done
cp -r ../toolchain/native/sdk/x86_64-montauk $(BINDIR)/sdk/x86_64-montauk
cp -r include/libc/. $(BINDIR)/sdk/include/
cp -r include/montauk $(BINDIR)/sdk/include/montauk
cp -r include/Api $(BINDIR)/sdk/include/Api
cp -r ../kernel/freestnd-cxx-hdrs/x86_64/include/. $(BINDIR)/sdk/include/
cp lib/libc/liblibc.a $(BINDIR)/sdk/lib/libc.a
cp lib/libc/crt1.o lib/libc/crti.o lib/libc/crtn.o $(BINDIR)/sdk/lib/
ar rcs $(BINDIR)/sdk/lib/libm.a
cp ../toolchain/local/x86_64-montauk/lib/libstdc++.a $(BINDIR)/sdk/lib/libstdc++.a
cp -r ../toolchain/local/x86_64-montauk/include/c++ $(BINDIR)/sdk/include/c++
../toolchain/local/bin/x86_64-montauk-gcc -O2 ../toolchain/files/tls-test.c -o $(BINDIR)/sdk/bin/tls-test.elf
../toolchain/local/bin/x86_64-montauk-g++ -O2 ../toolchain/files/cxx-test.cpp -o $(BINDIR)/sdk/bin/cxx-test.elf
../toolchain/local/bin/x86_64-montauk-gcc -O2 ../toolchain/files/sdk-diag.c -o $(BINDIR)/sdk/bin/sdk-diag.elf
ifneq ($(wildcard ../toolchain/native-gcc/sdk/bin/gcc),)
cp ../toolchain/native-gcc/sdk/bin/gcc $(BINDIR)/sdk/bin/gcc.elf
cp ../toolchain/native-gcc/sdk/bin/g++ $(BINDIR)/sdk/bin/g++.elf
cp ../toolchain/native-gcc/sdk/bin/cpp $(BINDIR)/sdk/bin/cpp.elf
mkdir -p $(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0
cp ../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/cc1 \
../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/cc1plus \
../toolchain/native-gcc/sdk/libexec/gcc/x86_64-montauk/14.2.0/collect2 \
$(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0/
cp -r ../toolchain/native-gcc/sdk/lib/gcc $(BINDIR)/sdk/lib/gcc
cp ../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/libgcc.a \
../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/crtbegin.o \
../toolchain/local/lib/gcc/x86_64-montauk/14.2.0/crtend.o \
$(BINDIR)/sdk/lib/gcc/x86_64-montauk/14.2.0/
cp ../toolchain/native/sdk/bin/ld $(BINDIR)/sdk/libexec/gcc/x86_64-montauk/14.2.0/ld
mkdir -p $(BINDIR)/tmp
printf 'scratch space for compiler intermediates\n' > $(BINDIR)/tmp/readme.txt
else
@echo "======================================================================"
@echo "devkit: WARNING: toolchain/native-gcc/ not built."
@echo " The image will ship WITHOUT the native GCC (g++, cc1plus, ...)."
@echo " Run 'make sdk' from the repo root to build the full Montauk SDK."
@echo "======================================================================"
endif
else
@echo "======================================================================"
@echo "devkit: WARNING: toolchain/native/ not built."
@echo " The image will ship WITHOUT the Montauk SDK (0:/sdk dev tools)."
@echo " Run 'make sdk' from the repo root to build it (one-time, long)."
@echo "======================================================================"
endif
clean:
rm -rf $(BINDIR) obj
$(MAKE) -C lib/bearssl clean
$(MAKE) -C lib/libc clean
$(MAKE) -C lib/libjpeg clean
$(MAKE) -C lib/libjpegwrite clean
$(MAKE) -C lib/tls clean
$(MAKE) -C src/fetch clean
$(MAKE) -C src/doom clean
$(MAKE) -C src/login clean
$(MAKE) -C src/shell clean
$(MAKE) -C src/desktop clean
$(MAKE) -C src/wikipedia clean
$(MAKE) -C src/weather clean
$(MAKE) -C src/imageviewer clean
$(MAKE) -C src/fontpreview clean
$(MAKE) -C src/spreadsheet clean
$(MAKE) -C src/wordprocessor clean
$(MAKE) -C src/pdfviewer clean
$(MAKE) -C src/disks clean
$(MAKE) -C src/devexplorer clean
$(MAKE) -C src/installer clean
$(MAKE) -C src/audio clean
$(MAKE) -C src/music clean
$(MAKE) -C src/video clean
$(MAKE) -C src/bluetooth clean
$(MAKE) -C src/network clean
$(MAKE) -C src/terminal clean
$(MAKE) -C src/klog clean
$(MAKE) -C src/procmgr clean
$(MAKE) -C src/powermgr clean
$(MAKE) -C src/calculator clean
$(MAKE) -C src/charmap clean
$(MAKE) -C src/printers clean
$(MAKE) -C src/timezone clean
$(MAKE) -C src/paint clean
$(MAKE) -C src/rpgdemo clean
$(MAKE) -C src/screenshot clean
$(MAKE) -C src/mandelbrot clean
$(MAKE) -C src/2048 clean
$(MAKE) -C src/tcc clean
$(MAKE) -C src/printd clean
$(MAKE) -C src/printctl clean
$(MAKE) -C src/dialogs clean
$(MAKE) -C src/crashpad clean
$(MAKE) -C src/libloader clean