summaryrefslogtreecommitdiff
path: root/kernel/Makefile
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/Makefile')
-rw-r--r--kernel/Makefile115
1 files changed, 115 insertions, 0 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
new file mode 100644
index 0000000..a5f6741
--- /dev/null
+++ b/kernel/Makefile
@@ -0,0 +1,115 @@
+# ============================================================================
+# kernel/Makefile - Kernel Build
+# Produces: $(RESULT_DIR)/(CONFIG_KERNEL_FILE_NAME)).elf
+#
+# ATM: uses config_generated.mk for compilation of drivers/filesystems/entyties.
+# I hard-code it in this Makefile, but in future (TODO) i should remove it
+# ============================================================================
+
+include ../config.mk
+-include ../config_generated.mk
+
+INCLUDES := \
+ -Iinclude \
+ -I../include
+
+# ── Always-compiled sources ─────────────────────────────────────────────────
+
+ARCH_CPP_SRC := $(wildcard arch/$(ARCH)/*.cpp)
+ARCH_ASM_SRC := $(wildcard arch/$(ARCH)/*.S)
+
+#TODO: move it to special Makefiles
+CORE_SRC := $(wildcard core/*.cpp)
+MM_SRC := $(wildcard mm/*.cpp)
+LDR_SRC := $(wildcard loader/*.cpp)
+LIB_SRC := $(wildcard lib/*.cpp)
+
+# ── Conditionally-compiled sources (from .config) ───────────────────────────
+# TODO: change it to Driver Makefiles
+DRV_SRC :=
+FS_SRC :=
+
+# Drivers — add src based on CONFIG_ variables
+ifeq ($(CONFIG_DRIVER_FBCON),y)
+ DRV_SRC += $(wildcard drivers/console/*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_UART_16550),y)
+ DRV_SRC += $(wildcard drivers/char/uart_16550*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_PL011_UART),y)
+ DRV_SRC += $(wildcard drivers/char/pl011*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_PS2KBD),y)
+ DRV_SRC += $(wildcard drivers/input/ps2kbd*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_VIRTIO_BLK),y)
+ DRV_SRC += $(wildcard drivers/block/virtio_blk*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_AHCI),y)
+ DRV_SRC += $(wildcard drivers/block/ahci*.cpp)
+endif
+ifeq ($(CONFIG_DRIVER_VIRTIO_NET),y)
+ DRV_SRC += $(wildcard drivers/net/virtio_net*.cpp)
+endif
+
+# Filesystems(just for example)
+ifeq ($(CONFIG_FS_INITRAMFS),y)
+ FS_SRC += $(wildcard fs/initramfs*.cpp)
+endif
+ifeq ($(CONFIG_FS_TMPFS),y)
+ FS_SRC += $(wildcard fs/tmpfs*.cpp)
+endif
+ifeq ($(CONFIG_FS_EXT2),y)
+ FS_SRC += $(wildcard fs/ext2*.cpp)
+endif
+ifeq ($(CONFIG_FS_FAT32),y)
+ FS_SRC += $(wildcard fs/fat32*.cpp)
+endif
+
+# ── Collect all sources ─────────────────────────────────────────────────────
+
+ALL_CPP_SRC := $(ARCH_CPP_SRC) $(CORE_SRC) $(MM_SRC) $(DRV_SRC) $(FS_SRC) $(LDR_SRC) $(LIB_SRC)
+ALL_ASM_SRC := $(ARCH_ASM_SRC)
+
+ALL_CPP_OBJ := $(patsubst %.cpp,%.o,$(ALL_CPP_SRC))
+ALL_ASM_OBJ := $(patsubst %.S,%.o,$(ALL_ASM_SRC))
+ALL_OBJ := $(ALL_ASM_OBJ) $(ALL_CPP_OBJ)
+
+# ── Flags ───────────────────────────────────────────────────────────────────
+
+KERNEL_CXXFLAGS := \
+ $(COMMON_CXXFLAGS) \
+ $(ARCH_CFLAGS) \
+ $(INCLUDES) \
+ -DARCH_$(shell echo $(ARCH) | tr a-z A-Z)
+
+KERNEL_ASFLAGS := \
+ $(COMMON_ASFLAGS) \
+ $(ARCH_CFLAGS)
+
+KERNEL_LDFLAGS := \
+ $(COMMON_LDFLAGS) \
+ -T arch/$(ARCH)/linker.ld
+
+# ── Targets ─────────────────────────────────────────────────────────────────
+
+.PHONY: all clean
+
+all: $(RESULT_DIR)/$(CONFIG_KERNEL_FILE_NAME)
+
+$(RESULT_DIR)/$(CONFIG_KERNEL_FILE_NAME): $(ALL_OBJ) arch/$(ARCH)/linker.ld
+ @mkdir -p $(dir $@)
+ @echo " LD $@"
+ @$(LD) $(KERNEL_LDFLAGS) -o $@ $(ALL_OBJ)
+
+%.o: %.cpp
+ @echo " CXX $<"
+ @$(CXX) $(KERNEL_CXXFLAGS) -c $< -o $@
+
+%.o: %.S
+ @echo " AS $<"
+ @$(CXX) $(KERNEL_ASFLAGS) -c $< -o $@
+
+clean:
+ rm -rf $(RESULT_DIR)
+ find . -name '*.o' -delete