Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
108 lines (87 loc) · 3.13 KB
/
Copy pathMakefile
File metadata and controls
108 lines (87 loc) · 3.13 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
# SPDX-License-Identifier: MIT
# Copyright (C) 2025 Laurentiu Cristian Preda <laurentiu.cristian.preda@gmail.com>
#
# Author: Laurentiu Cristian Preda (criseda)
# GitHub: https://github.com/criseda
#
# Makefile for CPOS
#
# This Makefile builds the CPOS operating system, including both C and Rust components.
# It handles compilation, linking, and provides targets for running the system in QEMU.
# Target binary name
TARGET = cpos.elf
# Toolchain
CC = arm-none-eabi-gcc
AS = arm-none-eabi-as
LD = arm-none-eabi-ld
OBJCOPY = arm-none-eabi-objcopy
# Directories
BOOTLOADER_DIR = bootloader
KERNEL_DIR = kernel
INCLUDE_DIR = include
LIB_DIR = lib
INIT_DIR = init
USER_DIR = user
RUST_DIR = rust_kernel
# Rust settings
RUST_TARGET = thumbv7m-none-eabi
RUST_LIB = $(RUST_DIR)/target/$(RUST_TARGET)/release/librust_kernel.a
# Source files
BOOTLOADER_SRC = $(BOOTLOADER_DIR)/bootloader.s
KERNEL_SRC = $(wildcard $(KERNEL_DIR)/*.c)
USER_SRC = $(wildcard $(USER_DIR)/*.c)
INIT_SRC = $(INIT_DIR)/init.c
LIB_SRC = $(wildcard $(LIB_DIR)/*.c)
# Object files
BOOTLOADER_OBJ = $(BOOTLOADER_SRC:.s=.o)
KERNEL_OBJ = $(KERNEL_SRC:.c=.o)
INIT_OBJ = $(INIT_SRC:.c=.o)
USER_OBJ = $(USER_SRC:.c=.o)
LIB_OBJ = $(LIB_SRC:.c=.o)
# Linker script
LD_SCRIPT = linker.ld
# Assembler flags
ASFLAGS = -mcpu=cortex-m3 -mthumb
# Compiler flags
# No loop-to-memcpy/memset rewriting: there is no libc to provide them
CFLAGS = $(ASFLAGS) -nostdlib -ffreestanding -fno-tree-loop-distribute-patterns -Wall -Wextra -I$(INCLUDE_DIR) -Os
LDFLAGS = -T $(LD_SCRIPT) -nostdlib
# QEMU settings
QEMU = qemu-system-arm
QEMU_MCU = cortex-m3
QEMU_MACHINE = lm3s6965evb
QEMU_ARGS = -machine $(QEMU_MACHINE) -cpu $(QEMU_MCU) -nographic -monitor null -serial stdio
QEMU_DEBUG = -d in_asm,cpu_reset > qemu_debug.log 2>&1
QEMU_GDB_DEBUG = -nographic -s -S
# Build Rules
all: $(TARGET)
# Pattern rules for building object files
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
%.o: %.s
$(AS) $(ASFLAGS) -o $@ $<
# Rule to build the Rust library; always ask cargo, it knows what changed
$(RUST_LIB): FORCE
@echo "Building Rust components..."
@cd $(RUST_DIR) && rustup target add $(RUST_TARGET) 2>/dev/null || true
@# staticlib only: building the rlib too (for cargo-fuzz) would disable LTO
@cd $(RUST_DIR) && cargo rustc --release --target $(RUST_TARGET) --crate-type staticlib
$(TARGET): $(BOOTLOADER_OBJ) $(KERNEL_OBJ) $(INIT_OBJ) $(USER_OBJ) $(LIB_OBJ) $(RUST_LIB)
$(CC) $(ASFLAGS) -nostdlib -ffreestanding -T $(LD_SCRIPT) -Wl,--no-warn-mismatch -o $@ $^
$(OBJCOPY) -O binary $@ cpos.bin
# QEMU target
qemu:
@echo "Starting QEMU. To exit, press Ctrl+C"
$(QEMU) $(QEMU_ARGS) -kernel $(TARGET)
qemu-gdb:
@echo "Starting QEMU with GDB server. Connect with 'arm-none-eabi-gdb -ex \"target remote localhost:1234\"'"
$(QEMU) $(QEMU_ARGS) -kernel $(TARGET) $(QEMU_GDB_DEBUG)
qemu-debug:
@echo "Starting QEMU with debug logging. Check qemu_debug.log for details."
$(QEMU) $(QEMU_ARGS) -kernel $(TARGET) $(QEMU_DEBUG)
clean:
rm -f $(BOOTLOADER_OBJ) $(KERNEL_OBJ) $(INIT_OBJ) $(USER_OBJ) $(LIB_OBJ) $(TARGET) cpos.bin
@echo "Cleaning Rust artifacts..."
@cd $(RUST_DIR) && cargo clean
FORCE:
.PHONY: all clean qemu qemu-gdb qemu-debug FORCE