Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share my Makefile to everyone(macOS 13.5) #265

Open
1939323749 opened this issue Aug 4, 2023 · 1 comment
Open

Share my Makefile to everyone(macOS 13.5) #265

1939323749 opened this issue Aug 4, 2023 · 1 comment

Comments

@1939323749
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants