HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Makefile for a custom operating system kernel

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
makefilesystemcustomoperatingforkernel

Problem

I'm now posting the second semi-complete part of my operating system makefile. The kernel itself is currently incomplete, so don't be surprised by the small size of the makefile. When the kernel expands, it will follow suit.

OFLAGS = -Ofast
DEBUGFLAGS = -g -g3 -ggdb3
TARGET = i686-elf
CC = gcc
INCDIR = -I kernel/include/
KERNEL = opal101
CFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -funsigned-char
LDFLAGS = -T linker.ld -Ofast -nostdlib -lgcc
AS = as
VPATH = kernel/arch/$(TARGET) $(shell find . -type d ! -name "arch"*)
include kernel/arch/$(TARGET)/Makefile
include kernel/drivers/vga/Makefile
include kernel/lib/Makefile
OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)
.PHONY: help all clean iso run debug
help:
        @echo "make: Usage: make [target]";\
        echo "Targets [run | iso | all | cleani | debug]"
debug: CFLAGS:= $(CFLAGS) $(DEBUGFLAGS)
debug: all
        qemu-system-i386 --kernel $(KERNEL).bin -S -s& \
        gdb-multiarch -s $(KERNEL).bin
run:iso
        qemu-system-i386 -cdrom $(KERNEL).iso
iso: CFLAGS := $(CFLAGS) $(OFLAGS)
iso: all
        mkdir -p $(KERNEL)/boot/grub;\
        cp kernel/arch/i686-elf/stage2_eltorito $(KERNEL)/boot/grub;\
        cp $(KERNEL).bin $(KERNEL)/boot;\
        printf "default 0\n" >>menu.lst;\
        printf "title %s\n" $(KERNEL) >>menu.lst;\
        printf "kernel /boot/%s.bin" $(KERNEL) >>menu.lst;\
        mv menu.lst $(KERNEL)/boot/grub;\
        genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \
        -boot-load-size 4 -boot-info-table -o $(KERNEL).iso $(KERNEL)
all: $(KERNEL).bin

$(KERNEL).bin:$(OBJECTS)
        $(TARGET)-$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $(KERNEL).bin
$(C_OBJECTS):%.o:%.c
        $(TARGET)-$(CC) $(CFLAGS) -c %%CODEBLOCK_0%%lt; -o $@ $(INCDIR)
$(ASM_OBJECTS): %.o:%.s
        $(TARGET)-$(AS) %%CODEBLOCK_0%%lt; -o $@
clean:
        rm -rf *.o *.bin *.iso opal101/


I will show the kernel/lib/Makefile. All the rest are almost the same.

C_OBJECTS := $(C_OBJECTS) string.o
string.o:string.h


NOTE: I

Solution

Disclaimer: I'm not experienced with this field, but this question definitely needs some activity, so this is my attempt at an answer. If there's anything that you want me to cover that I don't mention here, just tell me about it and I'll try my best to see what I can do.

Style/nitpicks

I find that some of your rule indentation is a bit excessive, and makes your code a little harder to read. Right now you're indenting your rules by tabs with a width of eight spaces. I'd recommend that you indent by tabs with a width of four, like this:

some_rule : file.c
    do_something file.c -foo -bar


You also seem to have a lack of blank lines between your rules as well. I'd highly recommend including at least one blank line between each rule, and a comment, if necessary:

# ...
some_rule1 : file.c
    ...

some_rule2 : file.c
    ...


Finally, you're inconsistent in your colon (:) placement in some places. For example, sometimes you place your colons like this:

all: $(KERNEL).bin


Other times, you place them like this:

$(C_OBJECTS):%.o:%.c


I prefer the following style, but as long as you're, again, consistent, any of the above or below are fine:

some_rule : ...

Code Snippets

some_rule : file.c
    do_something file.c -foo -bar
# ...
some_rule1 : file.c
    ...

some_rule2 : file.c
    ...
all: $(KERNEL).bin
$(C_OBJECTS):%.o:%.c
some_rule : ...

Context

StackExchange Code Review Q#105716, answer score: 3

Revisions (0)

No revisions yet.