Updated module and lib Makefiles, deleted root Makefile

This commit is contained in:
lucic71 2022-06-28 19:40:51 +03:00
parent a60184e5b2
commit 28f9618ec2
3 changed files with 12 additions and 144 deletions

131
Makefile
View File

@ -1,131 +0,0 @@
# loader and kmain must be the only object files placed in the root directory.
OBJ = loader.o kmain.o
# Configuration directory that contains files for building the project and
# testing it.
CONFIG_DIR := config/
# Compiler settings
CC = i686-elf-gcc
CCFLAGS = -ffreestanding -mno-red-zone -c
CCFLAGS += -Wall -Wextra -Werror -pedantic
# Linker settings
LD = i686-elf-gcc
LDFLAGS = -T $(CONFIG_DIR)/link.ld -nostdlib -ffreestanding -lgcc
# Assembler settings
AS = i686-elf-as
ASFLAGS =
# Libraries directory. Normally I don't want to include the library headers
# in kmain, but given the fact that in the module interfaces there are includes
# from lib, they must be resolved when linking kmain.
LIB_DIR = lib/
# Module directories are located in modules/
# The interfaces are placed in module/include/ and the module objects are
# located in each module under mobj/.
MODULES_ROOT_DIR := modules/
MODULE_DIR = $(wildcard $(MODULES_ROOT_DIR)/*)
MODULE_INC_DIR = $(MODULES_ROOT_DIR)/include $(LIB_DIR)/include
# Locate the mobj/ directories and their contents.
MODULE_OBJ_DIR = mobj/
MODULE_OBJ = $(foreach TMP, $(MODULE_DIR), $(wildcard $(TMP)/$(MODULE_OBJ_DIR)/*.o))
# Exclude modules/include from module directories list.
MODULE_DIR := $(filter-out $(MODULE_INC_DIR), $(MODULE_DIR))
# Include flags
INCFLAGS = $(foreach TMP, $(MODULE_INC_DIR), -I$(TMP))
# Library flags
LIB_SO_DIR := lib/so
LIBFLAGS = $(foreach TMP, $(LIB_SO_DIR), -L$(TMP))
LIBS = -lklib
.PHONY: all clean module_all
all: lib_all | module_all kernel.elf
# module_all - Recursively build all modules.
module_all:
@$(call print_banner, "Building modules")
@$(MAKE) -C $(MODULES_ROOT_DIR)
@echo
# lib_all - Recursively build the library
lib_all:
@$(call print_banner, "Building the library")
@$(MAKE) -C $(LIB_DIR)
@echo
# kernel.elf - Link all root objects and module objects to create the
# kernel.elf executable
kernel.elf: $(OBJ)
@$(call print_banner, "Building the kernel")
$(LD) $(OBJ) $(MODULE_OBJ) -o kernel.elf $(LIBFLAGS) $(LIBS) $(LDFLAGS)
# os.iso - Create the OS image and place the kernel fiel in GRUB directory.
os.iso: kernel.elf
cp kernel.elf iso/boot/kernel.elf
genisoimage -R \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso
# run - Run the emulator using the config file.
BOCHS_CONFIG := $(CONFIG_DIR)/bochsrc.txt
run: os.iso
bochs -f $(BOCHS_CONFIG) -q
%.o: %.c
$(CC) $< -o $@ $(CCFLAGS) $(INCFLAGS)
%.o: %.s
$(AS) $< -o $@ $(ASFLAGS)
clean: mclean lclean
@$(call print_banner, "Cleaning root directory")
@$(RM) *.o kernel.elf os.iso bochslog.txt iso/boot/kernel.elf
mclean:
@$(call print_banner, "Cleaning the modules")
$(MAKE) -C $(MODULES_ROOT_DIR) clean
lclean:
@$(call print_banner, "Cleaning the library")
$(MAKE) -C $(LIB_DIR) clean
# Functions
define print_banner
@echo
@echo "*************** $(1) ***************"
@echo
endef

View File

@ -2,7 +2,8 @@
SRC_DIR := src
OBJ_DIR := obj
INCLUDE_DIRS := ../include ../include/kernel ./include
ASM_DIR := asm
INCLUDE_DIRS := ../include ./include
# MOD_OBJ - Module Object. Its name will be the same as the module so it is
# a good idea to automate that.
@ -17,7 +18,10 @@ LIB_INCLUDE_DIRS := ../lib/include
# Module sources and their object files
SRC = $(wildcard $(SRC_DIR)/*.c)
ASM := $(wildcard $(ASM_DIR)/*.s)
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
OBJ += $(ASM:$(ASM_DIR)/%.s=$(OBJ_DIR)/%.o)
# Compiler settings
@ -39,6 +43,9 @@ $(MOD_OBJ): $(OBJ) | $(MOBJ_DIR)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $< -o $@ $(CFLAGS) $(HFLAGS) $(INCFLAGS)
$(OBJ_DIR)/%.o: $(ASM_DIR)/%.s | $(OBJ_DIR)
$(CC) $< -o $@ $(CFLAGS) $(HFLAGS) $(INCFLAGS)
$(OBJ_DIR) $(MOBJ_DIR):
mkdir -p $@

View File

@ -28,25 +28,17 @@ OBJ += $(ASM:$(ASM_DIR)/%.s=$(OBJ_DIR)/%.o)
# Compiler settings.
CC = i686-elf-gcc
CCFLAGS = -ffreestanding -mno-red-zone -c
CCFLAGS += -Wall -Wextra -Werror -pedantic
CFLAGS+=-c
HFLAGS = -MMD
# Linker settings.
LD = i686-elf-gcc
LDFLAGS = -r -nostdlib -ffreestanding -lgcc
# Include flags.
INCFLAGS := $(foreach TMP, $(INC_DIR), -I$(TMP))
# Assembler and flags for assembler.
AS = i686-elf-as
ASFLAGS =
.PHONY: all clean
# Start of rules.
@ -54,13 +46,13 @@ ASFLAGS =
all: $(LIB_OBJ)
$(LIB_OBJ): $(OBJ) | $(LOBJ_DIR)
$(LD) $(OBJ) -o $@ $(LDFLAGS)
$(CC) $(OBJ) -o $@ $(LDFLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $< -o $@ $(CCFLAGS) $(HFLAGS) $(INCFLAGS)
$(CC) $< -o $@ $(CFLAGS) $(HFLAGS) $(INCFLAGS)
$(OBJ_DIR)/%.o: $(ASM_DIR)/%.s | $(OBJ_DIR)
$(AS) $< -o $@ $(ASFLAGS)
$(CC) $< -o $@ $(CFLAGS) $(HFLAGS) $(INCFLAGS)
$(LOBJ_DIR) $(OBJ_DIR):
mkdir -p $@