lol its in c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.9 KiB

  1. .PHONY: all clean
  2. MYDIR := $(dir $(lastword $(MAKEFILE_LIST)))
  3. ifndef PROJECT_NAME
  4. $(error PROJECT_NAME is not set to the name of your rom.)
  5. endif
  6. ifndef MBC
  7. $(error MBC is not set. Should be set to the MBC name or value you want. Example "MBC5+RAM+BATTERY")
  8. endif
  9. ifndef TARGETS
  10. $(error TARGETS is not set. Should be a combination of DMG SGB CGB)
  11. endif
  12. ifneq ($(filter-out DMG SGB CGB,$(TARGETS)),)
  13. $(error TARGETS should be a combination of DMG SGB CGB, unknown: $(filter-out DMG SGB CGB,$(TARGETS)))
  14. endif
  15. MAPS := $(shell find level/ -type f -name '*.png' -not -name '*_coll.png')
  16. SRC := $(shell find src/ -type f -name '*.c') $(shell find $(MYDIR)/src/ -type f -name '*.c') $(MAPS:level/%.png=_build/level/%.c)
  17. ASM := $(shell find src/ -type f -name '*.asm') $(shell find $(MYDIR)/src/ -type f -name '*.asm')
  18. # Which files do we use from the sdcc libc
  19. LIBC := _memset.c gbz80/memcpy.s
  20. LIBC += _divuchar.c _divschar.c _muluchar.c _mulschar.c _modschar.c _moduchar.c
  21. LIBC += _divuint.c _divsint.c _mulint.c _modsint.c _moduint.c
  22. # the 32 bit functions use 10% of ROM0, so do not include them
  23. #LIBC += _divulong.c _divslong.c _mullong.c _modslong.c _modulong.c
  24. LIBC_PATH := $(subst \,/,$(shell sdcc -mgbz80 --print-search-dirs | grep gbz80 | tail -n 1))/../src
  25. ifneq (1,$(words [$(LIBC_PATH)]))
  26. $(error "your environment or sdcc is installed in a path with spaces in it, this is not allowed, as it will break sdcc")
  27. endif
  28. Q ?= @
  29. BUILD := _build
  30. TOOLS := $(MYDIR)/tools
  31. OBJS := $(patsubst %, $(BUILD)/%.o, $(ASM) $(SRC)) $(patsubst %, $(BUILD)/libc/%.o, $(LIBC)) $(BUILD)/gsinit.end.o
  32. CFLAGS := -mgbz80 -Isrc/ -I$(MYDIR)/inc -I$(BUILD)/level
  33. ASFLAGS := -isrc/ -i$(MYDIR)/inc -i$(BUILD)/assets/ -Wall
  34. LDFLAGS := --pad 0xFF
  35. FIXFLAGS := --validate --pad-value 0xFF --title "$(PROJECT_NAME)" --mbc-type "$(MBC)" -l 0x33
  36. ROM_EXTENSION := gb
  37. # Replace spaces with underscores in the project name.
  38. PROJECT_NAME := $(subst $() $(),_,$(PROJECT_NAME))
  39. ifeq ($(filter CGB,$(TARGETS)),) # Not targeting CGB, so disable CGB features
  40. CFLAGS += -DCGB=0
  41. ASFLAGS += -DCGB=0
  42. LDFLAGS += --dmg
  43. else
  44. CFLAGS += -DCGB=1
  45. ASFLAGS += -DCGB=1
  46. ifeq ($(filter DMG,$(TARGETS))$(filter SGB,$(TARGETS)),) # Check if not targeting both CGB and DMG
  47. FIXFLAGS += --color-only
  48. else
  49. FIXFLAGS += --color-compatible
  50. endif
  51. ROM_EXTENSION := gbc
  52. endif
  53. ifeq ($(filter SGB,$(TARGETS)),) # Not targeting SGB, so disable SGB features
  54. CFLAGS += -DSGB=0
  55. ASFLAGS += -DSGB=0
  56. else # Targeting SGB as well
  57. CFLAGS += -DSGB=1
  58. ASFLAGS += -DSGB=1
  59. FIXFLAGS += --sgb-compatible
  60. endif
  61. ifneq ($(findstring RAM,$(MBC)),)
  62. FIXFLAGS += --ram-size 2
  63. endif
  64. all: $(PROJECT_NAME).$(ROM_EXTENSION)
  65. # Rules to make c files
  66. $(BUILD)/%.c.as: %.c
  67. @echo Compiling $<
  68. @mkdir -p $(dir $@)
  69. $(Q)sdcc $(CFLAGS) -S $< -o $@ -Wp "-MQ $@ -MD $(@:.as=.as.d)"
  70. -include $(patsubst %.c, $(BUILD)/%.c.as.d, $(SRC))
  71. $(BUILD)/libc/%.c.as: $(LIBC_PATH)/%.c
  72. @echo Compiling $<
  73. @mkdir -p $(dir $@)
  74. $(Q)sdcc $(CFLAGS) -S $< -o $@
  75. $(BUILD)/%.c.asm: $(BUILD)/%.c.as $(TOOLS)/asmconvert.py
  76. $(Q)python3 $(TOOLS)/asmconvert.py $(notdir $<) < $< > $@
  77. $(BUILD)/%.c.asm.d: $(BUILD)/%.c.asm
  78. @mkdir -p $(dir $@)
  79. $(Q)rgbasm $(ASFLAGS) $< -M $@ -MT $(@:.asm.d=.o) -MT $@ -MG
  80. # rgbds dependency generation is broken, as it stops after the first missing file. so list all incbins always
  81. $(Q)cat $< | grep -i '^ *INCBIN' | sed -E 's/ *incbin *"([^"]*)"/$(subst /,\/,$(@:.asm.d=.o)): \1/gI' >> $@
  82. -include $(patsubst %.c, $(BUILD)/%.c.asm.d, $(SRC))
  83. $(BUILD)/%.c.o: $(BUILD)/%.c.asm $(BUILD)/%.c.asm.d
  84. @echo "Assembling (c)" $<
  85. @mkdir -p $(dir $@)
  86. $(Q)rgbasm $(ASFLAGS) $< -o $@
  87. # Rules to build libc asm files
  88. $(BUILD)/libc/%.s.asm: $(LIBC_PATH)/%.s $(TOOLS)/asmconvert.py
  89. @mkdir -p $(dir $@)
  90. $(Q)python3 $(TOOLS)/asmconvert.py $(notdir $<) < $< > $@
  91. $(BUILD)/libc/%.s.o: $(BUILD)/libc/%.s.asm
  92. @echo Assembling $<
  93. @mkdir -p $(dir $@)
  94. $(Q)rgbasm $(ASFLAGS) $< -o $@
  95. # Rules to build project asm files
  96. $(BUILD)/%.asm.d: %.asm
  97. @mkdir -p $(dir $@)
  98. $(Q)rgbasm $(ASFLAGS) $< -M $@ -MT $(@:.d=.o) -MT $@ -MG
  99. # rgbds dependency generation is broken, as it stops after the first missing file. so list all incbins always
  100. $(Q)cat $< | grep -i '^ *INCBIN' | sed -E 's/incbin "([^"]*)"/$(subst /,\/,$(@:.d=.o)): \1/gI' >> $@
  101. $(BUILD)/%.asm.o: %.asm $(BUILD)/%.asm.d
  102. @echo Assembling $<
  103. @mkdir -p $(dir $@)
  104. $(Q)rgbasm $(ASFLAGS) $< -o $@
  105. -include $(patsubst %.asm, $(BUILD)/%.asm.d, $(ASM))
  106. # Rule to build the final rom
  107. $(PROJECT_NAME).$(ROM_EXTENSION) $(PROJECT_NAME).map $(PROJECT_NAME).sym: $(OBJS)
  108. @echo Linking $@
  109. $(Q)rgblink $(LDFLAGS) $^ -o $@ -m $(basename $@).map -n $(basename $@).sym
  110. $(Q)rgbfix $(FIXFLAGS) $@
  111. @python3 $(TOOLS)/romspace.py $(basename $@).map
  112. # Asset related rules
  113. $(BUILD)/assets/%.2bpp $(BUILD)/assets/%.map: assets/%.png
  114. @echo Converting $<
  115. @mkdir -p $(dir $@)
  116. $(Q)rgbgfx \
  117. -t "$(BUILD)/$(shell dirname $<)/$(shell basename -s .png $<).map" \
  118. -o "$(BUILD)/$(shell dirname $<)/$(shell basename -s .png $<).2bpp" \
  119. -u \
  120. $<
  121. $(BUILD)/level/%.h $(BUILD)/level/%.c: level/%.png level/%_coll.png
  122. @echo Generating level $<
  123. @mkdir -p $(BUILD)/level
  124. @cp $< $(BUILD)/level/
  125. @cp $(shell dirname $<)/$(shell basename -s .png $<)_coll.png $(BUILD)/level/
  126. @python3 scripts/generate_map.py --compress 1 "$(BUILD)/$<"
  127. $(BUILD)/assets/%.oam.2bpp: assets/%.oam.png
  128. @echo Converting $<
  129. @mkdir -p $(dir $@)
  130. $(Q)rgbgfx -h $< -o $@
  131. $(BUILD)/assets/%.1bpp: assets/%.png
  132. @echo Converting $<
  133. @mkdir -p $(dir $@)
  134. $(Q)rgbgfx -d 1 $< -o $@
  135. # Special hack to place a ret instruction at the end of the GSINIT section.
  136. # This has to be linked last, as that ensures that this fragment is at the end of the "GSINIT" section.
  137. $(BUILD)/gsinit.end.o:
  138. @printf 'SECTION FRAGMENT "GSINIT", ROMX, BANK[1]\n ret' | rgbasm - -o $@
  139. clean:
  140. @rm -rf $(BUILD) $(PROJECT_NAME).$(ROM_EXTENSION) $(PROJECT_NAME).map $(PROJECT_NAME).sym
  141. # Do not remove intermediate files (like assets, c->asm files, etc)
  142. .SECONDARY: