summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDani Rodríguez2020-05-11 22:07:00 +0200
committerDani Rodríguez2020-05-12 20:05:06 +0200
commit0a8dfc9a547c503c48d75147afd0ecb98cea2e1f (patch)
tree42e71faf7eb943f398cecd31b196168118a2bcef
parenta4afc5cc9fefcf66239a6289d0367f43b8016f84 (diff)
build: extract Makefile rules related to kernel
This commit will add an architecture-specific Makefile at arch/i386/Makefile, with rules specific for compiling the kernel bootable image for the i386 platform, moving them out of the root Makefile file. The new build process for NativeOS implies the following steps: * Compile libc.a: using libc/Makefile, build an archive file (.a) with the object code containing the C standard library used by the kernel. * Compile libi386.a: using arch/i386/Makefile, build an archive file (.a) with the object code containing the kernel code for the i386 platform. * Link the kernel image: the global Makefile still has rules for building the kernel image (an executable file using the ELF file format and metadata required by the bootloader), using all the .a files compiled in previous steps. * Pack the CD-ROM: the global Makefile is able to bootstrap an ISO image using the contents of the tools/cdrom directory, adding the kernel image into the ISO distribution.
-rw-r--r--.gitignore4
-rw-r--r--Makefile93
-rw-r--r--arch/i386/Makefile47
3 files changed, 73 insertions, 71 deletions
diff --git a/.gitignore b/.gitignore
index 3768ef5..827d945 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,8 +4,6 @@ nativeos.elf
# CD-ROM
*.iso
-./cdrom/
-cdrom/*
.codelite/
*.project
@@ -19,5 +17,5 @@ run.sh
###########
-out/
+build/
docs/
diff --git a/Makefile b/Makefile
index 10e5de3..061c906 100644
--- a/Makefile
+++ b/Makefile
@@ -14,17 +14,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Build flags
-ARCH = i386
-GIT_VERSION = $(shell git describe --always)
-DEBUG ?= 0
+ARCH ?= i386
+
+# QEMU flags
+GRUB_MKRESCUE = i386-elf-grub-mkrescue
+QEMU = qemu-system-i386
# Tools.
CC = i386-elf-gcc
LD = i386-elf-gcc
AS = i386-elf-gcc
-GRUB_MKRESCUE = i386-elf-grub-mkrescue
-QEMU = qemu-system-i386
# Check that we have the required software.
ifeq (, $(shell which $(CC)))
@@ -37,86 +36,44 @@ ifeq (, $(shell which $(AS)))
$(error "$(AS) not found. Is the toolchain compiler enabled?")
endif
-# Directories
-KERNEL_PATH = kernel
-INCLUDE_PATH = include
-BUILD_PATH = out
-
-# Tool flags
-CFLAGS = -nostdlib --freestanding -fno-builtin -g -I$(INCLUDE_PATH)/
-CFLAGS += -Iarch/$(ARCH)/$(INCLUDE_PATH)/
-CFLAGS += -Ilibc/include/
-CFLAGS += -D_NTOS_VERSION_="\"$(GIT_VERSION)\""
-ASFLAGS = -g
-LDFLAGS = -nostdlib
QEMUARGS = -serial stdio
QEMU_DEBUGARGS = -s -S
-ifeq ($(DEBUG), 1)
- CFLAGS += -g -O0 -DHAVE_DEBUG
-endif
-
-# Compilation units that don't depend on system architecture.
-S_BASE_SOURCES = $(shell find $(KERNEL_PATH) -name '*.S')
-C_BASE_SOURCES = $(shell find $(KERNEL_PATH) -name '*.c')
-
-# Compilation units that depend on the current system architecture.
-S_ARCH_SOURCES = $(shell find 'arch/$(ARCH)/$(KERNEL_PATH)' -name '*.S')
-C_ARCH_SOURCES = $(shell find 'arch/$(ARCH)/$(KERNEL_PATH)' -name '*.c')
-
-# The klibc is also required to compile the kernel.
+# Global assets
KLIBC_LIBRARY = libc/libc.a
-KLIBC_SOURCES = $(shell find 'libc/src/' -name '*.c')
-
-# All compilation units. Note S_BASE_SOURCES has priority. This is because
-# we need the bootloader to be early in the compilation list in order to
-# properly link the binary.
-S_SOURCES = $(S_BASE_SOURCES) $(S_ARCH_SOURCES)
-C_SOURCES = $(C_BASE_SOURCES) $(C_ARCH_SOURCES)
-S_OBJECTS = $(patsubst %.s, $(BUILD_PATH)/%.o ,$(S_SOURCES))
-C_OBJECTS = $(patsubst %.c, $(BUILD_PATH)/%.o, $(C_SOURCES))
-KERNEL_OBJS = $(S_OBJECTS) $(C_OBJECTS) $(KLIBC_LIBRARY)
-
-KERNEL_LD = arch/$(ARCH)/kernel/linker.ld
-KERNEL_IMAGE = $(BUILD_PATH)/nativeos.elf
+KARCH_LIBRARY = arch/$(ARCH)/lib$(ARCH).a
-# These variables are used when building the distribution disk.
-NATIVE_DISK = $(BUILD_PATH)/nativeos.iso
-NATIVE_DISK_KERNEL = nativeos.exe
+# Kernel compilation
+KERNEL_IMAGE = nativeos.elf
+LDSCRIPT = arch/$(ARCH)/kernel/linker.ld
+LDFLAGS = -nostdlib
# It might not work on some platforms unless this is done.
GRUB_ROOT = $(shell dirname `which $(GRUB_MKRESCUE)`)/..
+# CD-ROM
+NATIVE_DISK = nativeos.iso
+
# Mark some targets as phony. Otherwise they might not always work.
.PHONY: qemu qemu-gdb clean
# Main build targets.
-kernel: $(KERNEL_IMAGE)
cdrom: $(NATIVE_DISK)
-# Common build targets.
-$(BUILD_PATH)/%.o: %.c
- @mkdir -p $(dir $@)
- $(CC) -c $(CFLAGS) -o $@ $<
-
-$(BUILD_PATH)/%.o: %.s
- @mkdir -p $(dir $@)
- $(AS) $(ASFLAGS) -o $@ $<
-
-# Kernel ELF binary image.
-$(KERNEL_IMAGE): $(KERNEL_OBJS)
- $(LD) $(LDFLAGS) -T $(KERNEL_LD) -o $@ $^
+$(KERNEL_IMAGE): $(KARCH_LIBRARY) $(KLIBC_LIBRARY)
+ $(LD) $(LDFLAGS) -T $(LDSCRIPT) -o $@ $^
-# Build the static library.
-$(KLIBC_LIBRARY): $(KLIBC_SOURCES)
+$(KLIBC_LIBRARY):
make -C libc
+$(KARCH_LIBRARY):
+ make -C arch/$(ARCH)
# Builds CD-ROM.
$(NATIVE_DISK): $(KERNEL_IMAGE)
- rm -rf $(BUILD_PATH)/cdrom
- cp -R tools/cdrom $(BUILD_PATH)
- cp $(KERNEL_IMAGE) $(BUILD_PATH)/cdrom/boot/$(NATIVE_DISK_KERNEL)
- $(GRUB_MKRESCUE) -d $(GRUB_ROOT)/lib/grub/i386-pc -o $@ $(BUILD_PATH)/cdrom
+ rm -rf build
+ cp -R tools/cdrom build
+ cp $(KERNEL_IMAGE) build/boot/nativeos.exe
+ $(GRUB_MKRESCUE) -d $(GRUB_ROOT)/lib/grub/i386-pc -o $@ build
# Create an ISO file and run it through QEMU.
qemu: $(NATIVE_DISK)
@@ -131,6 +88,6 @@ qemu-gdb: $(NATIVE_DISK)
################################################################################
# Clean the distribution and remove any generated file.
clean:
- rm -rvf $(BUILD_PATH)
- rm -vf $(NATIVE_DISK) $(KERNEL_IMAGE)
+ rm -rf build $(KERNEL_IMAGE) $(NATIVE_DISK)
+ make -C arch/$(ARCH) clean
make -C libc clean
diff --git a/arch/i386/Makefile b/arch/i386/Makefile
new file mode 100644
index 0000000..bad1a8b
--- /dev/null
+++ b/arch/i386/Makefile
@@ -0,0 +1,47 @@
+# This file is part of NativeOS
+# Copyright (C) 2015-2020 The NativeOS contributors
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+DEBUG ?= 0
+
+# Tools
+AR = i386-elf-ar
+AS = i386-elf-as
+CC = i386-elf-gcc
+
+# Tool flags
+CFLAGS = -nostdlib --freestanding -fno-builtin
+CFLAGS += -Iinclude
+CFLAGS += -I../../libc/include
+CFLAGS += -D_NTOS_VERSION_="\"$(GIT_VERSION)\""
+
+ifeq ($(DEBUG), 1)
+ CFLAGS += -g -O0 -DHAVE_DEBUG
+ ASFLAGS = -g
+endif
+
+ARCH_C_SOURCES = $(shell find kernel -name '*.c')
+ARCH_S_SOURCES = $(shell find kernel -name '*.S')
+ARCH_C_OBJECTS = $(patsubst %.c,%.o,$(ARCH_C_SOURCES))
+ARCH_S_OBJECTS = $(patsubst %.S,%.o,$(ARCH_S_SOURCES))
+ARCH_OBJECTS = $(ARCH_C_OBJECTS) $(ARCH_S_OBJECTS)
+
+.PHONY = clean
+
+libi386.a: $(ARCH_OBJECTS)
+ $(AR) r $@ $^
+
+clean:
+ rm -f $(ARCH_OBJECTS) libi386.a