summaryrefslogtreecommitdiff
path: root/Makefile
blob: 4d801430abe4013eb8aa0d4fe6e011b0e3ddb5bc (plain)
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
TOOLCHAIN := riscv64-linux-gnu
CC        := $(TOOLCHAIN)-gcc
CXX       := $(TOOLCHAIN)-g++
LD        := $(TOOLCHAIN)-ld
OBJCOPY   := $(TOOLCHAIN)-objcopy

TARGET_ELF := build/kernel.elf
TARGET_BIN := build/kernel.bin

ARCH_FLAGS := -march=rv64gc -mabi=lp64 -mcmodel=medany
CFLAGS     := -Wall -Wextra -O2 -ffreestanding -nostdlib -Iinclude $(ARCH_FLAGS)
CXXFLAGS   := $(CFLAGS) -fno-exceptions -fno-rtti -fno-use-cxa-atexit

SRC_DIRS  := src
SRCS_ASM  := $(shell find $(SRC_DIRS) -name "*.S")
SRCS_CXX  := $(shell find $(SRC_DIRS) -name "*.cc")

OBJS      := $(SRCS_ASM:%.S=build/%.o) $(SRCS_CXX:%.cc=build/%.o)

DEPS      := $(OBJS:.o=.d)


LINKER_SCRIPT := linker.ld

QEMU      := qemu-system-riscv64
QEMU_FLAGS := -machine virt -m 128M -nographic -bios none -kernel $(TARGET_ELF)


.PHONY: all clean run debug directories

all: directories $(TARGET_ELF)

$(TARGET_ELF): $(OBJS) $(LINKER_SCRIPT)
	@echo "Linking $@"
	@$(LD) -T $(LINKER_SCRIPT) -o $@ $(OBJS)

build/%.o: %.S
	@mkdir -p $(dir $@)
	@echo "Compiling ASM $<"
	@$(CC) $(CFLAGS) -MMD -c $< -o $@

build/%.o: %.cc
	@mkdir -p $(dir $@)
	@echo "Compiling C++ $<"
	@$(CXX) $(CXXFLAGS) -MMD -c $< -o $@

directories:
	@mkdir -p build

run: all
	@echo "Starting QEMU..."
	$(QEMU) $(QEMU_FLAGS)

debug: all
	@echo "Starting QEMU in debug mode (connect with gdb-multiarch)..."
	$(QEMU) $(QEMU_FLAGS) -S -s

clean:
	@rm -rf build

-include $(DEPS)