User Tools

Site Tools


my_makefile

This is an old revision of the document!


My makefile

The makefile builds a complete project from scratch up to and including building a format ready for usage.

CC=i686-elf-gcc
AS=i686-elf-as
KOBJ=kernel.o
MBIN=mbr/mbr.bin mbr/64.bin
#SECTOR=2048		# CD
SECTOR=512		# HD

# linkable C objects
%.o: %.c $(DEPS)
	$(CC) -c $< -o $@ -std=gnu99 -ffreestanding -O2 -Wall -Wextra

# linkable AS objects
%.o: %.s
	$(AS) $< -o $@

# real mode NASM binaries
%.bin: %.nasm
	nasm $< -f bin -o $@

# build full kernel with boot
kernel: $(KOBJ) $(MBIN)
	$(CC) -T linker.ld -o $@.bin -ffreestanding -O2 -nostdlib $(KOBJ) -lgcc
	mkdir -p isodir/boot
	rm -f isodir/boot/hd.img
	dd if=/dev/zero of=isodir/boot/boot.catalog bs=1 count=2048
	dd if=mbr/mbr.bin of=isodir/boot/hd.img bs=$(SECTOR) seek=0 conv=notrunc
	dd if=mbr/64.bin of=isodir/boot/hd.img bs=$(SECTOR) seek=1 conv=notrunc
	dd if=kernel.bin of=isodir/boot/hd.img bs=$(SECTOR) seek=2 conv=notrunc
	rm -f kernel.iso
	genisoimage -r -b isodir/boot/hd.img -c isodir/boot/boot.catalog -hard-disk-boot -o kernel.iso .

You will notice i686-elf-gcc and i686-elf-as referenced at the top of the file. These are the cross compiler and the cross assembler respectively.
We do not need a cross assembler for NASM, as the output we generate is pure x86 code. The reason we use NASM and not our own cross assembler is because NASM have better output options for those kinds of files.

The generated hd.img is a byte-wise representation of our hard-drive containing all our code. I am using this file to create a bootable USB-drive.

Observe also the last line that invokes genisoimage. This entry will create a bootable ISO image from hd.img that can be mounted as a disk in an emulator or even burned to a CD.

my_makefile.1488549265.txt.gz · Last modified: 2017/03/03 13:54 by admin