-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmakefile
225 lines (167 loc) · 5.57 KB
/
makefile
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
###################################################
# This is the SGDK PROJECT makefile
# Put this file in the root of your project
# directory and build with the 'make' command
###################################################
# specify SGDK root dir
SGDK?=/opt/sgdk
# specify your M68k GNU toolchain prefix
M68K_PREFIX?=m68k-elf-
# project source code & resources directories
# C source code
SRC_DIR:=src
# C headers
INC_DIR:=inc
# Resources (images, music, etc)
RES_DIR:=res
# Output directory (build files, final ROM, etc)
OUT_DIR:=out
# The name of your final ROM binary
BIN=rom.bin
###################################################
# You shouldn't need to configure anything further
# for your project below this line
###################################################
# fancy colors cause we're fancy
CLEAR=\033[0m
RED=\033[1;31m
YELLOW=\033[1;33m
GREEN=\033[1;32m
# references to SGDK library souce
LIB_SRC:=$(SGDK)/src
LIB_RES:=$(SGDK)/res
LIB_INC:=$(SGDK)/inc
# NOTE: we assume all commands appear somewhere in PATH.
# If you've manually configured/built some of these
# tools and their directories are not listed in PATH
# you will need to specify the full path of each command
# m68k toolset
CC:=$(M68K_PREFIX)gcc
OBJCPY:=$(M68K_PREFIX)objcopy
NM:=$(M68K_PREFIX)nm
LD:=$(M68K_PREFIX)ld
# z80 toolset
ASM_Z80=sjasmplus
# SGDK toolset
RESCOMP:=java -jar $(SGDK)/bin/rescomp.jar
BINTOS:=$(SGDK)/bin/bintos
MACCER:=$(SGDK)/bin/maccer
# gather code & resources
SRC_C:=$(wildcard $(SRC_DIR)/*.c)
SRC_C+=$(wildcard *.c)
SRC_S:=$(wildcard $(SRC_DIR)/*.s)
SRC_S+=$(wildcard *.s)
SRC_S80:=$(wildcard $(SRC_DIR)/*.s80)
SRC_S80+=$(wildcard *.s80)
SRC_ASM:=$(wildcard $(SRC_DIR)/*.asm)
SRC_ASM+=$(wildcard *.asm)
RES:=$(wildcard $(RES_DIR)/*.res)
RES+=$(wildcard *.res)
RES_C:=$(wildcard $(RES_DIR)/*.c)
RES_S:=$(wildcard $(RES_DIR)/*.s)
RES_RS:=$(RES:.res=.rs)
RES_H:=$(RES:.res=.h)
RES_DEP:=$(RES:.red=.d)
RES_DEPS:=$(addprefix $(OUT_DIR)/,$(RES_DEP))
# setup output objects
OBJ:=$(RES:.res=.o)
OBJ+=$(RES_C:.c=.o)
OBJ+=$(RES_S:.s=.o)
OBJ+=$(SRC_S80:.s80=.o)
OBJ+=$(SRC_ASM:.asm=.o)
OBJ+=$(SRC_S:.s=.o)
OBJ+=$(SRC_C:.c=.o)
OBJS:=$(addprefix $(OUT_DIR)/, $(OBJ))
DEPS:=$(OBJS:.o=.d)
LST:=$(SRC_C:.c=.lst)
LSTS:=$(addprefix $(OUT_DIR)/, $(LST))
# setup includes
INC:=-I$(INC_DIR) -I$(SRC_DIR) -I$(RES_DIR) -I$(LIB_INC) -I$(LIB_RES)
# default flags
ARCH_FLAG:=-m68000
DEF_FLAGS_M68K:=$(ARCH_FLAG) -Wall -Wextra -Wno-shift-negative-value -fno-builtin $(INC)
DEF_FLAGS_Z80:=-i$(SRC_DIR) -i$(INC_DIR) -i$(RES_DIR) -i$(LIB_SRC) -i$(LIB_INC)
release: FLAGS:=$(DEF_FLAGS_M68K) -O3 -fuse-linker-plugin -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer -flto
release: LIB_MD:=$(SGDK)/lib/libmd.a
release: BUILDTYPE=release
release: envcheck prebuild $(OUT_DIR)/$(BIN) postbuild
debug: FLAGS:=$(DEF_FLAGS_M68K) -O1 -ggdb -DDEBUG=1
debug: LIB_MD:=$(SGDK)/lib/libmd_debug.a
debug: BUILDTYPE=debug
debug: envcheck prebuild $(OUT_DIR)/$(BIN) $(OUT_DIR)/rom.out $(OUT_DIR)/symbols.txt postbuild
asm: FLAGS:=$(DEF_FLAGS_M68K) -O3 -fuse-linker-plugin -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer -S
asm: BUILDTYPE:=asm
asm: envcheck prebuild $(LSTS) postbuild
all: release
Release: release
Asm: asm
.PHONY: clean
-include $(DEPS)
envcheck:
@if [ ! -d $(SGDK) ]; then echo -e "${RED}*** SGDK directory not found!${CLEAR}"; exit -1; fi
cleantmp:
@rm -f $(RES_RS)
cleandep:
@rm -f $(DEPS)
cleanlst:
@rm -f $(LSTS)
cleanres: cleantmp
@rm -f $(RES_H) $(RES_DEP) $(RES_DEPS)
cleanobj:
@rm -f $(OBJS) $(OUT_DIR)/sega.o $(OUT_DIR)/rom_head.bin $(OUT_DIR)/rom_head.o $(OUT_DIR)/rom.out
clean: cleanobj cleanlst
@rm -f out.lst $(OUT_DIR)/rom.nm $(OUT_DIR)/rom.wch $(OUT_DIR)/$(BIN)
cleandebug: clean
@rm -f $(OUT_DIR)/symbols.txt
cleanasm: cleanlst
cleandefault: clean
cleanDefault: clean
cleanRelease: cleanrelease
cleanDebug: cleandebug
cleanAsm: cleanasm
prebuild:
@mkdir -p $(SRC_DIR)/boot
@mkdir -p $(OUT_DIR)
@mkdir -p $(OUT_DIR)/$(SRC_DIR)
@mkdir -p $(OUT_DIR)/$(RES_DIR)
@echo -e "${YELLOW}Beginning $(BUILDTYPE) project build...${CLEAR}"
postbuild:
@echo -e "${GREEN}Build complete!${CLEAR}"
$(OUT_DIR)/$(BIN): $(OUT_DIR)/rom.out
@$(OBJCPY) -O binary $(OUT_DIR)/rom.out $(OUT_DIR)/temp
@dd if=out/temp of=$@ bs=128K conv=sync status=none
@rm -f out/temp
$(OUT_DIR)/symbols.txt: $(OUT_DIR)/rom.out
$(NM) -n $(OUT_DIR)/rom.out > $(OUT_DIR)/symbols.txt
# Please see readme file about linking libgcc in this section
$(OUT_DIR)/rom.out: $(OUT_DIR)/sega.o $(OBJS) $(LIB_MD)
$(CC) $(ARCH_FLAG) -n -T $(SGDK)/md.ld -nostdlib $(OUT_DIR)/sega.o $(OBJS) $(LIB_MD) $(SGDK)/lib/libgcc.a -o $(OUT_DIR)/rom.out -Wl,--gc-sections
# $(CC) $(ARCH_FLAG) -n -T $(SGDK)/md.ld -nostdlib $(OUT_DIR)/sega.o $(OBJS) $(LIB_MD) -lgcc -o $(OUT_DIR)/rom.out
$(OUT_DIR)/sega.o: $(SRC_DIR)/boot/sega.s $(OUT_DIR)/rom_head.bin
$(CC) $(DEF_FLAGS_M68K) -c $(SRC_DIR)/boot/sega.s -o $@
$(OUT_DIR)/rom_head.bin: $(OUT_DIR)/rom_head.o
$(LD) -T $(SGDK)/md.ld -nostdlib --oformat binary -o $@ $<
$(OUT_DIR)/rom_head.o: $(SRC_DIR)/boot/rom_head.c
$(CC) $(DEF_FLAGS_M68K) -c $< -o $@
$(SRC_DIR)/boot/sega.s: $(LIB_SRC)/boot/sega.s
@cp $< $@
$(SRC_DIR)/boot/rom_head.c: $(LIB_SRC)/boot/rom_head.c
@cp $< $@
$(OUT_DIR)/%.lst: %.c
$(CC) $(FLAGS) -c $< -o $@
$(OUT_DIR)/%.o: %.c
$(CC) $(FLAGS) -MMD -c $< -o $@
$(OUT_DIR)/%.o: %.s
$(CC) -x assembler-with-cpp $(FLAGS) -MMD -c $< -o $@
$(OUT_DIR)/%.o: %.rs
$(CC) -x assembler-with-cpp $(FLAGS) -c $< -o $@
@cp $*.d $(OUT_DIR)/$*.d
@rm $*.d
%.rs: %.res
@$(RESCOMP) $< $@ -dep $(OUT_DIR)/$*.o
%.s: %.asm
$(MACCER) -o $@ $<
%.o80: %.s80
@$(ASM_Z80) $(DEF_FLAGS_Z80) --raw=$@ $<
%.s: %.o80
@$(BINTOS) $<