# Used to generated .d file that deal with header dependencies CPPFLAGS = -MMD AS=nasm ASFLAGS += -f elf32 LDFLAGS += -m elf_i386 CFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-pie -fno-stack-protector -fno-tree-vectorize CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti -fno-pie DEBUG_FLAGS += -g -Og -DDEBUG -fno-omit-frame-pointer -fno-inline LIBGCC = $(shell $(CC) -print-libgcc-file-name $(CFLAGS)) QEMU_OPT += -hda disk.img ARCH?=x86 SUBDIRS := core drivers tests arch/$(ARCH) INCDIRS += $(foreach dir, $(SUBDIRS), -I$(dir)) CPPFLAGS += $(INCDIRS) asmsrc= asmobj=$(asmsrc:%.asm=%.o) gasmsrc=$(wildcard arch/$(ARCH)/*.S arch/$(ARCH)/boot/*.S) gasmobj=$(gasmsrc:%.S=%.o) csrc=$(shell find $(SUBDIRS) -type f -name "*.c")# $(wildcard *.c) cobj=$(csrc:%.c=%.o) deps=$(csrc:%.c=%.d) $(gasmsrc:%.S=%.d) kernel kernel.sym &: $(asmobj) $(gasmobj) $(cobj) linker.ld $(LD) $(LDFLAGS) $(asmobj) $(gasmobj) $(cobj) -o kernel -T linker.ld $(LIBGCC) objcopy --only-keep-debug kernel kernel.sym objcopy --strip-debug kernel fd.iso: kernel mkdir -p isodir/boot/grub cp $< isodir/boot/ @printf "menuentry \"myos\" {\n\tmultiboot /boot/kernel\n}" > isodir/boot/grub/grub.cfg grub-mkrescue -o $@ isodir disk.img: disk.sfdisk qemu-img create -f raw $@ 32M sfdisk $@ < disk.sfdisk # NASM without preprocessing %.o:%.asm $(AS) $(ASFLAGS) -o $@ $< #GNU GAS ASM with C Preprocessing %.o: %.S $(CC) $(CFLAGS) $(CPPFLAGS) -c "$<" -o "$@" test: CFLAGS += -DRUN_TEST test: clean kernel disk.img qemu-system-x86_64 -kernel kernel -serial stdio -m 32M $(QEMU_OPT) run:kernel disk.img qemu-system-x86_64 -kernel $< $(QEMU_OPT) debug: CFLAGS += $(DEBUG_FLAGS) -DRUN_TEST debug: CXXFLAGS += $(DEBUG_FLAGS) debug:kernel kernel.sym gdb -q -x debug.gdb debug_test: CFLAGS += $(DEBUG_FLAGS) -DRUN_TEST debug_test: debug clean: $(RM) kernel $(asmobj) $(gasmobj) $(cobj) $(deps) fd.iso kernel.sym $(RM) -r isodir ifneq ($(MAKECMDGOALS),clean) -include $(deps) endif