I recently had submitted my bashscript for building an OS for review and got valuable feedback, so I rewrote the entire script for a makefile:
Makefile
# Make File for Bulding Ultron
# Sets up the Assembler
AS := nasm
ASFLAGS := -f elf
ASFILES := $(wildcard kernel/boot/*.s kernel/arch/*.s)
ASOBJECTS := $(ASFILES:.s=.s.o)
# Compile C++ Files
CC := g++
CCFLAGS := -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding -m32 -I./sdk/include
CCFILES := $(wildcard *.cc sdk/lib/*.cc)
CCOBJECTS := $(CCFILES:.cc=.o)
# Linker
LINKER := ld
LINKERFLAGS := -m elf_i386 -T link.ld
# The Final Product will be ultron.img in the build directory
all: ultron.img
# Ultron.img : It is the Final Product formed by linking all the object files together
ultron.img : $(ASOBJECTS) $(CCOBJECTS)
$(LINKER) $(LINKERFLAGS) -o build/$@ $^
# Run the Kernel : First Make the Kernel and then run it
run: ultron.img
qemu-system-i386 -kernel 'build/ultron.img'
# Assemble the Assembly Files into object Files
# This Assembles all the Files into thier respective object files
# $< : Names the first prerequiste
# $@ : Names the current Target
%.s.o: %.s
$(AS) $(ASFLAGS) $< -o $@
# Compile the C++ Files into corresponding object Files and place it in same directory
%.o: %.cc
$(CC) $(CCFLAGS) -o $@ $<
# Clean this Mess
clean: $(ASOBJECTS) $(CCOBJECTS)
$(RM) $^
This is my first makefile and I have a couple of questions:
- Should I make the makefile as verbose as it is now?
- Is my coding style Ok?
- What are the things that I should keep in mind while writing makefiles?