Files
x86-bare-metal-examples/Makefile

81 lines
2.2 KiB
Makefile
Raw Normal View History

2015-09-06 16:17:39 +02:00
.POSIX:
COMMON ?= common.h
2018-12-03 00:08:47 +00:00
DOC_OUT = README.html
2015-09-06 16:17:39 +02:00
LD ?= ld
2015-09-11 12:12:31 +02:00
LINKER_SCRIPT ?= linker.ld
# Use gcc so that the preprocessor will run first.
2015-10-06 12:27:24 +02:00
GAS ?= gcc
GAS_EXT ?= .S
NASM_EXT ?= .asm
2015-09-06 16:17:39 +02:00
OBJ_EXT ?= .o
OUT_EXT ?= .img
2015-10-04 19:59:15 +02:00
QEMU ?= qemu-system-i386
2015-09-06 16:17:39 +02:00
RUN ?= bios_hello_world
2015-09-18 11:35:08 +02:00
TMP_EXT ?= .tmp
2015-09-06 16:17:39 +02:00
2015-11-11 15:10:36 +01:00
OUTS := $(sort $(foreach IN_EXT,$(NASM_EXT) $(GAS_EXT),$(patsubst %$(IN_EXT),%$(OUT_EXT),$(wildcard *$(IN_EXT)))))
2015-10-04 19:59:15 +02:00
RUN_FILE := $(RUN)$(OUT_EXT)
2015-09-06 16:17:39 +02:00
.PRECIOUS: %$(OBJ_EXT)
2018-12-03 00:08:47 +00:00
.PHONY: all clean doc run
2015-09-06 16:17:39 +02:00
all: $(OUTS)
2015-09-11 12:12:31 +02:00
%$(OUT_EXT): %$(OBJ_EXT) $(LINKER_SCRIPT)
@# Failed attempt at getting debug symbols.
@#$(LD) -melf_i386 -o '$(@:$(OUT_EXT)=.elf)' -T '$(LINKER_SCRIPT)' '$<'
$(LD) --oformat binary -o '$@' -T '$(LINKER_SCRIPT)' '$<'
2015-09-06 16:17:39 +02:00
%$(OBJ_EXT): %$(GAS_EXT) $(COMMON)
$(GAS) -c -g -o '$@' '$<'
2015-10-06 12:27:24 +02:00
%$(OUT_EXT): %$(NASM_EXT)
2015-10-06 12:27:24 +02:00
nasm -f bin -o '$@' '$<'
2015-09-06 16:17:39 +02:00
# So that directories without a common.h can reuse this.
$(COMMON):
2015-09-06 16:17:39 +02:00
clean:
2018-12-03 00:08:47 +00:00
rm -fr '$(DOC_OUT)' *$(OBJ_EXT) *$(OUT_EXT) *$(TMP_EXT)
2015-09-06 16:17:39 +02:00
2015-11-11 15:10:36 +01:00
run: $(RUN_FILE)
$(QEMU) -drive 'file=$(RUN_FILE),format=raw' -smp 2 -soundhw pcspk
2015-10-04 19:59:15 +02:00
2015-11-11 15:10:36 +01:00
debug: $(RUN_FILE)
2015-10-04 19:59:15 +02:00
$(QEMU) -hda '$(RUN_FILE)' -S -s &
gdb -x gdb.gdb
2015-09-18 11:35:08 +02:00
2015-11-11 15:10:36 +01:00
bochs: $(RUN_FILE)
2015-10-04 19:59:15 +02:00
# Supposes size is already multiples of 512.
2015-10-06 12:27:24 +02:00
# We force that with our linker script,
# and `grub-mkrescue` also seems to respect it as well.
2015-10-04 19:59:15 +02:00
CYLINDERS="$$(($$(stat -c '%s' '$(RUN_FILE)') / 512))" && \
bochs -qf /dev/null \
2015-10-04 19:59:15 +02:00
'ata0-master: type=disk, path="$(RUN_FILE)", mode=flat, cylinders='"$$CYLINDERS"', heads=1, spt=1' \
'boot: disk' \
'display_library: sdl' \
'megs: 128'
2015-09-18 11:35:08 +02:00
BIG_IMG_DIR := big_img$(TMP_EXT)
BOOT_DIR := $(BIG_IMG_DIR)/boot
GRUB_DIR := $(BOOT_DIR)/grub
2015-11-11 15:10:36 +01:00
big$(OUT_EXT): all
2015-09-18 11:35:08 +02:00
rm -rf '$(BIG_IMG_DIR)'
mkdir -p '$(GRUB_DIR)'
for out in $(OUTS); do\
printf "menuentry \"$${out%.*}\" {\n chainloader /boot/$$out\n}\n" >> '$(GRUB_DIR)/grub.cfg';\
cp "$$out" '$(BOOT_DIR)';\
done
# TODO why does this fail to boot properly?
#make -C multiboot/hello-world
#mkdir -p '$(BOOT_DIR)/multiboot'
#printf "menuentry \"multiboot/hello-world\" {\n chainloader /boot/multiboot/hello-world.img\n}\n" >> '$(GRUB_DIR)/grub.cfg';\
#cp multiboot/hello-world/main.img '$(BOOT_DIR)/multiboot/hello-world.img'
2015-11-11 15:10:36 +01:00
grub-mkrescue -o '$@' '$(BIG_IMG_DIR)'
2018-12-03 00:08:47 +00:00
doc: $(DOC_OUT)
$(DOC_OUT): README.adoc
asciidoctor -o $@ -v $<