Added global constructors for kernel-space.

Updated Makefile to generate crtbegin.o and crtend.o.
Separated crti.s and crtn.s from the normal directory because the objects need to be linked in a specific order along with crtbegin.o and crtend.o.
Added comments to describe the makefile better.
This commit is contained in:
Zoarial94 2020-12-14 14:03:12 -05:00
parent a6d4a0b9d9
commit d3e6109656
3 changed files with 39 additions and 12 deletions

View File

@ -1,10 +1,18 @@
#Standard Compiling Options
#Something weird with i386 vs i686
ARCH ?= i386
C := i686-elf-gcc
CXX := i686-elf-g++
AS := i686-elf-as
SRCDIR := src
SRCDIRS := src/arch/$(ARCH) src/kernel src/libc
#Separate out source directories for better compartmentalization
SRCDIR := src
ARCHDIR := $(SRCDIR)/arch/$(ARCH)
LIBCDIR := $(SRCDIR)/libc
KERNELDIR := $(SRCDIR)/kernel
SRCDIRS := $(ARCHDIR) $(LIBCDIR) $(KERNELDIR)
BUILDDIR := build
INCDIR := include
TARGET := bin/ZoarialBareOS.iso
@ -17,39 +25,53 @@ CPPSRCEXT := cpp
CPPINCEXT := hpp
ASSRCEXT := s
#Locate C and C++ files
CSOURCES := $(shell find $(SRCDIRS) -type f -name "*.$(CSRCEXT)")
CPPSOURCES := $(shell find $(SRCDIRS) -type f -name "*.$(CPPSRCEXT)")
#Get object files from C and C++ source files
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(CSOURCES:.$(CSRCEXT)=.o)) $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(CPPSOURCES:.$(CPPSRCEXT)=.o))
#$(info OBJECTS is $(OBJECTS))
#Get assembly source files and create objects from them
ASSOURCES := $(shell find $(SRCDIRS) -type f -name "*.$(ASSRCEXT)")
ASOBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(ASSOURCES:.$(ASSRCEXT)=.o))
#Find dependencies from C and C++ source files
DEPENDENCIES := $(patsubst $(SRCDIR)/%,$(DEPDIR)/%,$(CSOURCES:.$(CSRCEXT)=.d))
DEPENDENCIES := $(DEPENDENCIES) $(patsubst $(SRCDIR)/%,$(DEPDIR)/%,$(CPPSOURCES:.$(CPPSRCEXT)=.d))
#Directories needed by gcc
LIB :=
LIBDIR :=
INC := -I include/ -I include/libc/
#Combined flags (Both C and C++)
FLAGS := -O2 -Wall -Wextra -g -D__is_kernel -ffreestanding
#Specific flags
CFLAGS := $(FLAGS) -std=gnu99
CXXFLAGS := $(FLAGS) -std=c++17
ASFLAGS :=
INC := -I include/ -I include/libc/
#Testing Compiling Files and Arguments
#TESTSOURCES := $(shell find $(TESTSRCDIR) -type f -name "*.$(SRCEXT)")
#TESTOBJECTS := $(patsubst $(TESTSRCDIR)/%,$(TESTBUILDDIR)/%,$(TESTSOURCES:.$(SRCEXT)=.o))
MAINOBJS := $(OBJECTS) $(ASOBJECTS)
#Make sure certain directories are made
$(shell mkdir -p $(BUILDDIR) $(DEPDIR) bin/)
#Copy the tree structure of src/ to the build/ and dep/ directories
TREE := $(shell find $(SRCDIR) -type d)
$(shell mkdir -p $(patsubst $(SRCDIR)/%, $(BUILDDIR)/%, $(TREE)))
$(shell mkdir -p $(patsubst $(SRCDIR)/%, $(DEPDIR)/%, $(TREE)))
GLOBALARCHDIR := $(BUILDDIR)/arch/global-$(ARCH)
#Define/create the global constructor objects
CRTBEGINOBJ := $($(C) $(CFLAGS) -print-file-name=crtbegin.o)
CRTENDOBJ := $($(C) $(CFLAGS) -print-file-name=crtend.o)
CRTBEGIN := $(GLOBALARCHDIR)/crti.o $(GLOBALARCHDIR)/crtbegin.o
CRTEND := $(GLOBALARCHDIR)/crtend.o $(GLOBALARCHDIR)/crtn.o
#Order the objects to prevent weird gcc bugs with global constructors
MAINOBJS := $(CRTBEGIN) $(OBJECTS) $(ASOBJECTS) $(CRTEND)
#Compile Target
bin/ZoarialBareOS.iso: bin/ZoarialBareOS.bin bin/grub.cfg
cp bin/ZoarialBareOS.bin $(BUILDDIR)/isodir/boot/
@ -122,6 +144,11 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(ASSRCEXT)
#Compile object
$(AS) $(ASFLAGS) -o $@ $<
$(GLOBALARCHDIR)/crtbegin.o $(GLOBALARCHDIR)/crtend.o:
OBJ=`$(C) $(CFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@
#Clean
clean:
@echo " Cleaning...";