Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 58


# Automatically expand to a list of existing files that
# match the patterns
C_SOURCES = $(wildcard kernel /*.c drivers /*.c)

Then we can convert the source filenames into object filenames using anothermake
declaration, as follows:


# Create a list of object files to build , simple by replacing
# the ’.c’ extension of filenames in C_SOURCES with ’.o’
OBJ = ${C_SOURCES :.c=.o}

Now we can link the kernel object files together, to build the kernel binary, as follows:


# Link kernel object files into one binary , making sure the
# entry code is right at the start of the binary.
kernel.bin: kernel/kernel_entry.o ${OBJ}
ld -o $@ -Ttext 0x1000 $^ --oformat binary

A feature ofmakethat will go hand-in-hand with our dynamic inclusion of object files is
pattern rules, which tellmakehow to build one file type from another based on simple
pattern machine of the filename, as follows:


# Generic rule for building ’somefile.o’ from ’somefile.c’
%.o : %.c
gcc -ffreestanding -c $< -o $@

The alternative to this would be much repetion, as follows:


kernel/kernel.o : kernel/kernel.c
gcc -ffreestanding -c $< -o $@

drivers/screen.o : drivers/screen.c
gcc -ffreestanding -c $< -o $@

drivers/keyboard.o : drivers/keyboard.c
gcc -ffreestanding -c $< -o $@

...

Great, now that we understandmakesufficiently, we can progress to develop our kernel,
without having to re-type lots of commands, over and over, to check if something is
working correctly. Figure XXX shows a complete makefile that will be suitable for
progressing with our kernel.


# Automatically generate lists of sources using wildcards.
C_SOURCES = $(wildcard kernel /*.c drivers /*.c)
HEADERS = $(wildcard kernel /*.h drivers /*.h)

# TODO: Make sources dep on all header files.

# Convert the *.c filenames to *.o to give a list of object files to build
OBJ = ${C_SOURCES :.c=.o}

# Defaul build target
all: os-image
Free download pdf