matos/Makefile

112 lines
4.0 KiB
Makefile
Raw Normal View History

2018-07-20 15:41:58 +02:00
# Used to generated .d file that deal with header dependencies
CPPFLAGS = -MMD
AS=nasm
ASFLAGS += -f elf32
2021-11-04 20:26:31 +01:00
LDFLAGS += -m elf_i386
2021-11-05 23:02:23 +01:00
CFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-pie -fno-stack-protector -fno-tree-vectorize -D__KERNEL__
2023-11-08 21:00:31 +01:00
#keep .i and .s
#CFLAGS += -save-temps
#CFLAGS += -fanalyzer -Wno-analyzer-malloc-leak -Wno-analyzer-out-of-bounds
CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti -fno-pie
DEBUG_FLAGS += -g -Og -DDEBUG -fno-omit-frame-pointer -fno-inline
2021-11-04 20:26:31 +01:00
LIBGCC = $(shell $(CC) -print-libgcc-file-name $(CFLAGS))
2018-07-20 15:41:58 +02:00
2021-10-05 22:01:13 +02:00
QEMU_OPT += -hda disk.img
2020-04-27 23:45:38 +02:00
ARCH?=x86
SUBDIRS := core drivers tests arch/$(ARCH)
2018-07-20 15:41:58 +02:00
INCDIRS += $(foreach dir, $(SUBDIRS), -I$(dir))
CPPFLAGS += $(INCDIRS)
2018-07-20 15:41:58 +02:00
2021-10-31 00:04:01 +02:00
asmsrc=
2018-07-20 15:41:58 +02:00
asmobj=$(asmsrc:%.asm=%.o)
2021-10-31 00:04:01 +02:00
gasmsrc=$(wildcard arch/$(ARCH)/*.S arch/$(ARCH)/boot/*.S)
gasmobj=$(gasmsrc:%.S=%.o)
2018-07-20 15:41:58 +02:00
csrc=$(shell find $(SUBDIRS) -type f -name "*.c")# $(wildcard *.c)
2021-10-31 00:04:01 +02:00
cobj=$(csrc:%.c=%.o)
2023-11-08 21:00:31 +01:00
cinc=$(csrc:%.c=%.i)
2021-10-31 00:04:01 +02:00
deps=$(csrc:%.c=%.d) $(gasmsrc:%.S=%.d)
2022-09-03 23:04:31 +02:00
docsrc=$(wildcard docs/*.md)
docobj=$(docsrc:%.md=%.html)
2018-07-20 15:41:58 +02:00
kernel kernel.debug &: $(asmobj) $(gasmobj) $(cobj) linker.ld
2023-11-08 21:00:31 +01:00
$(LD) $(LDFLAGS) $(asmobj) $(gasmobj) $(cobj) -o kernel -T linker.ld $(LIBGCC) -Map kernel.map
objcopy --only-keep-debug kernel kernel.debug
objcopy --strip-debug kernel
objcopy --add-gnu-debuglink=kernel.debug kernel
2018-07-20 17:10:58 +02:00
2018-07-20 16:55:41 +02:00
fd.iso: kernel
mkdir -p isodir/boot/grub
cp $< isodir/boot/
2023-11-19 23:52:41 +01:00
@printf "set timeout=0\nset default=0\nmenuentry \"matos\" {\n\tmultiboot /boot/kernel\n}" > isodir/boot/grub/grub.cfg
2018-07-20 16:55:41 +02:00
grub-mkrescue -o $@ isodir
2021-11-05 23:02:23 +01:00
userspace: FORCE
$(MAKE) -C userspace
FORCE:
@
2023-11-09 23:46:26 +01:00
doc: $(docobj) ## generate documentation
2022-09-03 23:04:31 +02:00
2023-11-08 21:00:31 +01:00
help: ## show this help
# regex for general help
@sed -ne "s/^##\(.*\)/\1/p" $(MAKEFILE_LIST)
# regex for makefile commands (targets)
@printf "────────────────────────`tput bold``tput setaf 2` Make Commands `tput sgr0`────────────────────────────────\n"
@sed -ne "/@sed/!s/\(^[^#?=]*:\).*##\(.*\)/`tput setaf 2``tput bold`\1`tput sgr0`\2/p" $(MAKEFILE_LIST)
# regex for makefile variables
@printf "────────────────────────`tput bold``tput setaf 4` Make Variables `tput sgr0`───────────────────────────────\n"
@sed -ne "/@sed/!s/\(.*\)?=\(.*\)##\(.*\)/`tput setaf 4``tput bold`\1:`tput setaf 5`\2`tput sgr0`\3/p" $(MAKEFILE_LIST)
2021-11-05 23:02:23 +01:00
disk.img: disk.sfdisk userspace
2021-11-04 20:14:48 +01:00
qemu-img create -f raw $@ 32M
2021-11-05 23:02:23 +01:00
sfdisk $@ < $<
# Before having filesystem support, just dump the user prog into the first partition
strip userspace/user -o userspace/user.strip
$(eval file_size:=$(shell du -b userspace/user.strip|awk '{print $$1}' | xargs printf "%016d" )) \
sed '1s/^/$(file_size)/' userspace/user.strip | dd of=disk.img seek=2048 bs=512
2018-07-20 15:41:58 +02:00
2021-11-04 20:26:31 +01:00
# NASM without preprocessing
2018-07-20 15:41:58 +02:00
%.o:%.asm
$(AS) $(ASFLAGS) -o $@ $<
2021-11-04 20:26:31 +01:00
#GNU GAS ASM with C Preprocessing
%.o: %.S
2021-10-31 00:04:01 +02:00
$(CC) $(CFLAGS) $(CPPFLAGS) -c "$<" -o "$@"
2022-09-03 23:04:31 +02:00
%.html: %.md
markdown -o $@ $<
2023-11-08 21:00:31 +01:00
%.i: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -E -c "$<" -o "$@"
test: CFLAGS += -DRUN_TEST ## Run the OS on qemu with the inner test activated
2021-10-05 22:07:17 +02:00
test: clean kernel disk.img
2021-10-05 22:01:13 +02:00
qemu-system-x86_64 -kernel kernel -serial stdio -m 32M $(QEMU_OPT)
2018-11-08 22:09:12 +01:00
2023-11-08 21:00:31 +01:00
run:kernel disk.img ## Run the OS on qemu
qemu-system-x86_64 -kernel $< -serial stdio $(QEMU_OPT)
2018-07-20 15:41:58 +02:00
2023-11-08 21:00:31 +01:00
debug: CFLAGS += $(DEBUG_FLAGS) ## Run the OS on qemu and attach a debugger to it (may need a clean befor to have the debug symbols)
2018-11-16 14:47:21 +01:00
debug: CXXFLAGS += $(DEBUG_FLAGS)
debug:kernel kernel.debug disk.img
2021-10-25 21:29:02 +02:00
gdb -q -x debug.gdb
2021-01-26 17:58:33 +01:00
debug_test: CFLAGS += $(DEBUG_FLAGS) -DRUN_TEST
debug_test: debug
2023-11-09 23:46:26 +01:00
screenshot: ## Take a screenshot of the qemu window
2021-11-14 14:54:20 +01:00
shutter --window=qemu -o screenshot_1.png -e && zopflipng screenshot_1.png screenshot_1.png
2018-07-20 15:41:58 +02:00
clean:
$(RM) kernel $(asmobj) $(gasmobj) $(cobj) $(deps) $(cinc) fd.iso kernel.debug kernel.map $(docobj)
2019-01-09 17:21:51 +01:00
$(RM) -r isodir
2018-07-20 15:41:58 +02:00
2021-11-05 23:02:23 +01:00
.PHONY:
2021-11-14 14:54:20 +01:00
userspace screenshot
2021-11-05 23:02:23 +01:00
2018-07-20 15:41:58 +02:00
ifneq ($(MAKECMDGOALS),clean)
-include $(deps)
endif