-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
264 lines (220 loc) · 8.55 KB
/
Makefile
File metadata and controls
264 lines (220 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# PromptOS Main Makefile
# ========================
.PHONY: all clean run debug toolchain crosscompiler bootloader kernel userland image \
check-toolchain check-image-tools iso usb test test-host tests tools
# Configuration
ARCH := x86_64
TARGET := $(ARCH)-elf
BUILD_DIR := build
ISO_DIR := $(BUILD_DIR)/iso
IMG_FILE := $(BUILD_DIR)/promptos.img
TOOLCHAIN_DIR ?= toolchain
TOOLCHAIN_BIN := $(TOOLCHAIN_DIR)/bin
# Toolchain (prefer local toolchain, then cross-compiler, then system gcc)
ifeq ($(wildcard $(TOOLCHAIN_BIN)/$(TARGET)-gcc),)
# Check if cross-compiler is in PATH
ifneq ($(shell command -v $(TARGET)-gcc 2>/dev/null),)
CROSS_PREFIX := $(TARGET)-
else
# Fallback to system gcc (works for same-arch builds, e.g., x86_64 host → x86_64 target)
CROSS_PREFIX :=
endif
else
CROSS_PREFIX := $(TOOLCHAIN_BIN)/$(TARGET)-
endif
CC ?= $(CROSS_PREFIX)gcc
LD ?= $(CROSS_PREFIX)ld
AS := nasm
OBJCOPY ?= $(CROSS_PREFIX)objcopy
# Compiler Flags
CFLAGS_COMMON := -std=c17 -ffreestanding -fno-stack-protector \
-fno-pic -mno-red-zone -mno-mmx -mno-sse -mno-sse2 \
-mcmodel=kernel \
-Wall -Wextra -Werror -Wno-unused-parameter
ifdef RELEASE
CFLAGS_COMMON += -O2 -DNDEBUG
else
CFLAGS_COMMON += -O0 -g -DDEBUG
endif
# Include paths
INCLUDES := -I$(CURDIR)/kernel/include -I$(CURDIR)/libc/include
# QEMU Configuration
QEMU := qemu-system-x86_64
OVMF_CODE := /usr/share/OVMF/OVMF_CODE_4M.fd
OVMF_VARS := /usr/share/OVMF/OVMF_VARS_4M.fd
# Windows QEMU paths (if on Windows)
ifeq ($(OS),Windows_NT)
OVMF_CODE := C:/Program Files/qemu/share/OVMF_CODE-pure-efi.fd
OVMF_VARS := C:/Program Files/qemu/share/OVMF_VARS-pure-efi.fd
define MKDIR_P
powershell -Command "New-Item -ItemType Directory -Force -Path '$(1)' | Out-Null"
endef
define COPY_FILE
powershell -Command "if (Test-Path '$(1)') { Copy-Item -Force '$(1)' '$(2)' }"
endef
else
MKDIR_P := mkdir -p
define COPY_FILE
cp $(1) $(2)
endef
endif
QEMU_FLAGS := -m 512M -smp 4 \
-drive if=pflash,format=raw,readonly=on,file="$(OVMF_CODE)" \
-drive format=raw,file=$(IMG_FILE) \
-serial stdio \
-no-reboot -no-shutdown
ifdef DEBUG
QEMU_FLAGS += -s -S
endif
# =============================================================================
# Main Targets
# =============================================================================
all: image
# Create bootable disk image
image: check-toolchain check-image-tools bootloader kernel userland
@echo "[IMG] Creating GPT disk image..."
@bash ./scripts/make-image.sh
@echo "[IMG] Done: $(IMG_FILE)"
# Build bootloader
bootloader: check-toolchain
@echo "[BOOT] Building UEFI bootloader..."
@$(MAKE) -C bootloader BUILD_DIR=$(CURDIR)/$(BUILD_DIR)/bootloader
# Build kernel
kernel: check-toolchain
@echo "[KERN] Building kernel..."
@$(MAKE) -C kernel BUILD_DIR=$(CURDIR)/$(BUILD_DIR)/kernel \
CC="$(CC)" LD="$(LD)" AS="$(AS)" \
CFLAGS="$(CFLAGS_COMMON) $(INCLUDES)"
# Build userland
userland: check-toolchain
@echo "[USER] Building userland..."
@$(MAKE) -C userland BUILD_DIR=$(CURDIR)/$(BUILD_DIR)/userland \
CC="$(CC)" LD="$(LD)" AS="$(AS)"
# Build stress tests
tests: check-toolchain
@echo "[TEST] Building stress tests..."
@$(MAKE) -C tests BUILD_DIR=$(CURDIR)/$(BUILD_DIR)/tests \
CC="$(CC)" LD="$(LD)"
# Build host development tools
tools:
@echo "[TOOLS] Building host tools..."
@$(MAKE) -C tools
@chmod +x tools/prompt-pkg-config tools/prompt-config 2>/dev/null || true
# Run host unit tests (no QEMU required)
test-host:
@$(MAKE) -C tests host-tests
# Run all tests
test: test-host tests
# =============================================================================
# Run Targets
# =============================================================================
run: image
@echo "[QEMU] Starting PromptOS..."
$(QEMU) $(QEMU_FLAGS)
# Debug with GDB
debug: QEMU_FLAGS += -s -S
debug: image
@echo "[QEMU] Starting in debug mode (waiting for GDB on :1234)..."
@echo "Connect with: gdb -ex 'target remote :1234' $(BUILD_DIR)/kernel/kernel.elf"
$(QEMU) $(QEMU_FLAGS)
# Run with more verbose output
run-verbose: QEMU_FLAGS += -d int,cpu_reset -D qemu.log
run-verbose: run
# =============================================================================
# Toolchain
# =============================================================================
TOOLCHAIN_DIR ?= toolchain
BINUTILS_VER := 2.41
GCC_VER := 13.2.0
toolchain:
@echo "[TOOL] Building cross-compiler toolchain..."
@mkdir -p $(TOOLCHAIN_DIR)
@./scripts/build-toolchain.sh $(TOOLCHAIN_DIR) $(TARGET) $(BINUTILS_VER) $(GCC_VER)
crosscompiler: toolchain
# =============================================================================
# Cleanup
# =============================================================================
clean:
@echo "[CLEAN] Removing build directory..."
@rm -rf $(BUILD_DIR)
distclean: clean
@echo "[CLEAN] Removing toolchain..."
@rm -rf $(TOOLCHAIN_DIR)
# =============================================================================
# Toolchain & Image Checks
# =============================================================================
check-toolchain:
@command -v $(CC) >/dev/null 2>&1 || command -v gcc >/dev/null 2>&1 || \
(echo "[ERR] No C compiler found: $(CC) or gcc. Run 'make toolchain' or install gcc."; exit 1)
@command -v $(LD) >/dev/null 2>&1 || command -v ld >/dev/null 2>&1 || \
(echo "[ERR] No linker found: $(LD) or ld. Run 'make toolchain' or install binutils."; exit 1)
@command -v $(AS) >/dev/null 2>&1 || (echo "[ERR] Assembler not found: $(AS). Install NASM."; exit 1)
@# If cross-compiler not found, use system gcc as fallback
@if ! command -v $(CC) >/dev/null 2>&1; then \
echo "[WARN] Cross-compiler $(CC) not found, falling back to system gcc"; \
fi
check-image-tools:
@command -v mkfs.fat >/dev/null 2>&1 || (echo "[ERR] mkfs.fat not found. Install dosfstools."; exit 1)
@command -v mcopy >/dev/null 2>&1 || (echo "[ERR] mcopy not found. Install mtools."; exit 1)
@command -v mmd >/dev/null 2>&1 || (echo "[ERR] mmd not found. Install mtools."; exit 1)
@command -v sgdisk >/dev/null 2>&1 || (echo "[ERR] sgdisk not found. Install gdisk."; exit 1)
# =============================================================================
# Release Image Pipeline
# =============================================================================
# Create ISO9660 + UEFI El Torito bootable image
iso: image
@echo "[ISO] Creating bootable PromptOS ISO..."
@bash ./scripts/make-iso.sh
@echo "[ISO] Done: $(BUILD_DIR)/promptos.iso"
# Create bootable USB image
usb: image
@echo "[USB] Creating bootable USB image..."
bash scripts/make-usb.sh $(BUILD_DIR)/promptos-usb.img
@echo "[USB] Done: $(BUILD_DIR)/promptos-usb.img"
@echo "[USB] Write with: dd if=$(BUILD_DIR)/promptos-usb.img of=/dev/sdX bs=4M status=progress"
# Run clang-format on all source files
format:
@find . -name '*.c' -o -name '*.h' | xargs clang-format -i
# Check code style
check-style:
@find . -name '*.c' -o -name '*.h' | xargs clang-format --dry-run -Werror
# Generate documentation
docs:
@doxygen Doxyfile
# Create tags file
tags:
@ctags -R kernel libc userland
# Release gate (run ./scripts/release.sh)
release:
@chmod +x scripts/release.sh 2>/dev/null || true
@./scripts/release.sh $(VERSION)
# =============================================================================
# Help
# =============================================================================
help:
@echo "PromptOS Build System"
@echo "====================="
@echo ""
@echo "Usage: make [target] [options]"
@echo ""
@echo "Targets:"
@echo " all - Build everything (default)"
@echo " bootloader - Build UEFI bootloader"
@echo " kernel - Build kernel"
@echo " userland - Build userland applications"
@echo " image - Create bootable disk image"
@echo " run - Run in QEMU"
@echo " debug - Run in QEMU with GDB"
@echo " toolchain - Build cross-compiler"
@echo " test-host - Run host unit tests (no QEMU required)"
@echo " test - Run all tests"
@echo " clean - Remove build artifacts"
@echo " distclean - Remove everything including toolchain"
@echo " iso - Create ISO9660 image (requires xorriso)"
@echo " usb - Show instructions for USB boot"
@echo " release - Run release gate, create v1.0.0 archive"
@echo ""
@echo "Options:"
@echo " RELEASE=1 - Build release version"
@echo " VERBOSE=1 - Verbose output"
@echo ""