Makefile 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. .POSIX:
  2. COMMON ?= common
  3. LD ?= ld
  4. LINKER_SCRIPT ?= linker.ld
  5. # Use gcc so that the preprocessor will run first.
  6. GAS ?= gcc
  7. GAS_EXT ?= .S
  8. GAS_FLAGS ?= -g
  9. NASM_EXT ?= .asm
  10. OBJ_EXT ?= .o
  11. OUT_EXT ?= .img
  12. QEMU ?= qemu-system-i386
  13. RUN ?= bios_hello_world
  14. TMP_EXT ?= .tmp
  15. COMMON_O := $(COMMON).o
  16. COMMON_S := $(COMMON).S
  17. COMMON_H := $(COMMON).h
  18. OUTS := $(foreach IN_EXT,$(NASM_EXT) $(GAS_EXT),$(patsubst %$(IN_EXT),%$(OUT_EXT),$(wildcard *$(IN_EXT))))
  19. RUN_FILE := $(RUN)$(OUT_EXT)
  20. .PRECIOUS: %$(OBJ_EXT)
  21. .PHONY: all clean run
  22. all: $(OUTS)
  23. %$(OUT_EXT): %$(OBJ_EXT) $(COMMON_O) $(LINKER_SCRIPT)
  24. @# Failed attempt at getting debug symbols.
  25. @#$(LD) -melf_i386 -o '$(@:$(OUT_EXT)=.elf)' -T '$(LINKER_SCRIPT)' '$<'
  26. $(LD) --oformat binary -o '$@' -T '$(LINKER_SCRIPT)' '$<' '$(COMMON_O)'
  27. $(COMMON_O): $(COMMON_S)
  28. $(GAS) $(GAS_FLAGS) -c -o '$@' '$<'
  29. %$(OBJ_EXT): %$(GAS_EXT) $(COMMON_H)
  30. $(GAS) $(GAS_FLAGS) -c -o '$@' '$<'
  31. %$(OUT_EXT): %$(NASM_EXT)
  32. nasm -f bin -o '$@' '$<'
  33. # So that directories without a common.X don't have to define it.
  34. $(COMMON_H):
  35. $(COMMON_S):
  36. clean:
  37. rm -fr *$(OBJ_EXT) *$(OUT_EXT) *$(TMP_EXT)
  38. run: $(RUN_FILE)
  39. $(QEMU) -drive 'file=$(RUN_FILE),format=raw' -smp 2
  40. debug: $(RUN_FILE)
  41. $(QEMU) -hda '$(RUN_FILE)' -S -s &
  42. gdb -x gdb.gdb
  43. bochs: $(RUN_FILE)
  44. # Supposes size is already multiples of 512.
  45. # We force that with our linker script,
  46. # and `grub-mkrescue` also seems to respect it as well.
  47. CYLINDERS="$$(($$(stat -c '%s' '$(RUN_FILE)') / 512))" && \
  48. bochs -qf /dev/null \
  49. 'ata0-master: type=disk, path="$(RUN_FILE)", mode=flat, cylinders='"$$CYLINDERS"', heads=1, spt=1' \
  50. 'boot: disk' \
  51. 'display_library: sdl' \
  52. 'megs: 128'
  53. BIG_IMG_DIR := big_img$(TMP_EXT)
  54. BOOT_DIR := $(BIG_IMG_DIR)/boot
  55. GRUB_DIR := $(BOOT_DIR)/grub
  56. big-img: all
  57. rm -rf '$(BIG_IMG_DIR)'
  58. mkdir -p '$(GRUB_DIR)'
  59. for out in $(OUTS); do\
  60. printf "menuentry \"$${out%.*}\" {\n chainloader /boot/$$out\n}\n" >> '$(GRUB_DIR)/grub.cfg';\
  61. cp "$$out" '$(BOOT_DIR)';\
  62. done
  63. # TODO why does this fail to boot properly?
  64. #make -C multiboot/hello-world
  65. #mkdir -p '$(BOOT_DIR)/multiboot'
  66. #printf "menuentry \"multiboot/hello-world\" {\n chainloader /boot/multiboot/hello-world.img\n}\n" >> '$(GRUB_DIR)/grub.cfg';\
  67. #cp multiboot/hello-world/main.img '$(BOOT_DIR)/multiboot/hello-world.img'
  68. grub-mkrescue -o 'big.img' '$(BIG_IMG_DIR)'