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