cfenollosa / os-tutorial

How to create an OS from scratch
BSD 3-Clause "New" or "Revised" License
26.62k stars 3.24k forks source link

Share my Makefile to everyone(macOS 13.5) #265

Open 1939323749 opened 11 months ago

1939323749 commented 11 months ago

Cross-compiling gcc and gdb is a bit difficult for me, so I used brew to complete this part. It runs well on macOS 13.5! Someone will need it.

# $@ = target file
# $< = first dependency
# $^ = all dependencies

# First rule is the one executed when no parameters are fed to the Makefile
C_SOURCE_FILES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h)
OBJ = ${C_SOURCE_FILES:.c=.o cpu/interrupt.o}

CC = x86_64-elf-gcc
# Get x86_64-elf-gcc by `brew install x86_64-elf-gcc`
GDB = i386-elf-gdb
# Get i386-elf-gdb by `brew install i386-elf-gdb`
LD = x86_64-elf-ld
# Get x86_64-elf-ld by `brew install x86_64-elf-binutils`

CFLAGS = -g

all: run

kernel.bin: boot/kernel_entry.o ${OBJ}
    ${LD} -m elf_i386 -o $@ -Ttext 0x1000 --entry _start $^ --oformat binary

kernel.elf: boot/kernel_entry.o ${OBJ}
    ${LD} -m elf_i386 -o $@ -Ttext 0x1000 --entry _start $^

kernel_entry.o: kernel_entry.asm
    nasm $< -f elf -o $@

kernel.o: kernel.c
    ${CC} -m32 -march=i386 -ffreestanding -c $< -o $@

kernel.dis: kernel.bin
    ndisasm -b 32 $< > $@

bootsect.bin: bootsect.asm
    nasm $< -f bin -o $@

os-image.bin: boot/bootsect.bin kernel.bin
    cat $^ > $@

run: os-image.bin
    qemu-system-i386 -fda $<

debug: os-image.bin kernel.elf
    qemu-system-i386 -s -fda os-image.bin &
    ${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"

%.o : %.c ${HEADERS}
    ${CC} ${CFLAGS} -m32 -march=i386 -ffreestanding -c $< -o $@

%.o : %.asm
    nasm $< -f elf -o $@

%.bin : %.asm
    nasm $< -f bin -o $@

clean:
    rm -fr *.bin *.dis *.o os-image.bin *.elf
    rm -fr kernel/*.o boot/*.bin boot/*.o drivers/*.o cpu/*.o libc/*.o
firestar4204 commented 7 months ago

You can also use i686-elf-gcc and ld and skip the flag requiring the output to be i386