universalisos/kernel/Makefile

91 lines
3.1 KiB
Makefile

# Universalisos Kernel Makefile (Bao-Style)
# Default to riscv / polarfire if not set
ARCH ?= riscv
PLATFORM ?= polarfire
SRC_DIR := src
BUILD_DIR := build
OBJ_DIR := $(BUILD_DIR)/obj
# Object-file lists (initialized before arch/platform includes so they can
# override or append; appended in objects.mk)
cpu-objs-y :=
plat-objs-y :=
core-objs-y :=
lib-objs-y :=
# Architecture and platform Makefiles
include $(SRC_DIR)/arch/$(ARCH)/arch.mk
include $(SRC_DIR)/platform/$(PLATFORM)/platform.mk
# Linker script: defaults to the arch linker.ld; a platform (platform.mk above)
# may override UOS_LDSCRIPT to ship a board-specific script (load address etc.).
UOS_LDSCRIPT ?= $(SRC_DIR)/arch/$(ARCH)/linker.ld
# Global flags
CPPFLAGS += -Iinclude -Iinclude/universalisos -I$(SRC_DIR)/arch/$(ARCH)/inc -I$(SRC_DIR)/platform/$(PLATFORM)/inc -I$(SRC_DIR)/core -I$(SRC_DIR)/platform
CPPFLAGS += $(arch-cppflags) $(plat-cppflags)
CFLAGS += -Wall -Wextra -ffreestanding -O2 -g $(arch-cflags) $(plat-cflags)
CXXFLAGS += $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS += $(arch-asflags) $(plat-asflags)
LDFLAGS += -nostdlib $(arch-ldflags) $(plat-ldflags)
include $(SRC_DIR)/arch/$(ARCH)/objects.mk
include $(SRC_DIR)/platform/$(PLATFORM)/objects.mk
# Core objects: RISC-V is currently self-contained under src/arch/riscv/ and
# does not pull in the ARM-centric core files. ARMv7 compiles all core sources.
ifeq ($(ARCH),riscv)
CORE_SRCS := $(addprefix $(SRC_DIR)/core/, $(core-objs-y))
else
CORE_SRCS := $(wildcard $(SRC_DIR)/core/*.cpp) $(wildcard $(SRC_DIR)/core/*/*.cpp) $(wildcard $(SRC_DIR)/core/*.S) $(wildcard $(SRC_DIR)/core/*/*.S)
endif
CORE_OBJS := $(patsubst $(SRC_DIR)/%,$(OBJ_DIR)/%,$(CORE_SRCS:.cpp=.o))
CORE_OBJS := $(CORE_OBJS:.S=.o)
# Include lib objects (only if lib-objs-y is defined; otherwise skip the lib dir)
ifdef lib-objs-y
LIB_SRCS := $(addprefix $(SRC_DIR)/lib/, $(lib-objs-y))
LIB_OBJS := $(patsubst $(SRC_DIR)/%,$(OBJ_DIR)/%,$(LIB_SRCS:.cpp=.o))
else
LIB_OBJS :=
endif
# Map arch and plat objects to obj directory
CPU_OBJS := $(addprefix $(OBJ_DIR)/arch/$(ARCH)/, $(cpu-objs-y))
PLAT_OBJS := $(addprefix $(OBJ_DIR)/platform/$(PLATFORM)/, $(plat-objs-y))
# Shared platform drivers (common to all platforms that use them). Compiled
# from src/platform/drivers/; the platform objects.mk opts in via drv-objs-y
# (a bare filename list). When empty/unset, no drivers are built; set to a
# wildcard explicitly (e.g. drv-objs-y := $(wildcard *.cpp)) to build all of
# them (ARMv7/qemu-arm-virt path).
ifneq ($(strip $(drv-objs-y)),)
DRV_SRCS := $(addprefix $(SRC_DIR)/platform/drivers/, $(drv-objs-y))
else
DRV_SRCS :=
endif
DRV_OBJS := $(patsubst $(SRC_DIR)/%,$(OBJ_DIR)/%,$(DRV_SRCS:.cpp=.o))
OBJS := $(CORE_OBJS) $(LIB_OBJS) $(CPU_OBJS) $(PLAT_OBJS) $(DRV_OBJS)
TARGET := $(BUILD_DIR)/universalisos.elf
all: $(TARGET)
$(TARGET): $(OBJS)
$(CROSS_COMPILE)ld $(LDFLAGS) -T $(UOS_LDSCRIPT) -o $@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(dir $@)
$(CROSS_COMPILE)g++ $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.S
@mkdir -p $(dir $@)
$(CROSS_COMPILE)gcc $(CPPFLAGS) $(ASFLAGS) -c $< -o $@
clean:
rm -rf $(BUILD_DIR)
.PHONY: all clean