-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlibSFX.make
227 lines (184 loc) · 6.95 KB
/
libSFX.make
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
226
227
# Validate variables
ifndef name
name := out
endif
ifeq ($(name),)
name := out
endif
ifndef debug
debug := 0
endif
# Output
rom := $(name).sfc
# Default rule
.SUFFIXES:
.PHONY: clean
default: $(rom)
# Tools
libsfx_bin := $(libsfx_dir)/tools
as := $(libsfx_bin)/cc65/bin/ca65
ld := $(libsfx_bin)/cc65/bin/ld65
sfcheck := $(libsfx_bin)/superfamicheck/bin/superfamicheck
superfamiconv := $(libsfx_bin)/superfamiconv/bin/superfamiconv
brr_enc := $(libsfx_bin)/brrtools/bin/brr_encoder
lz4_compress := $(libsfx_bin)/lz4/programs/lz4
make_bp := $(libsfx_bin)/make_breakpoints
rwildcard = $(strip $(filter $(if $2,$2,%),$(foreach f,$(wildcard $1*),$(eval t = $(call rwildcard,$f/)) $(if $t,$t,$f))))
# File extensions
debug_sym_ext := cpu.sym
debug_map_ext := dmap
debug_nfo_ext := dnfo
# Set defaults
ifndef obj_dir
obj_dir := .build
endif
ifndef stack_size
stack_size := 100
endif
ifndef zpad_size
zpad_size := 10
endif
ifndef znmi_size
znmi_size := 10
endif
ifndef rpad_size
rpad_size := 100
endif
obj_dir_sfx := $(obj_dir)_libsfx
# Flags
libsfx_inc := $(libsfx_dir)/include
asflags := -D __STACKSIZE__=\$$$(stack_size) -D __ZPADSIZE__=\$$$(zpad_size) -D __ZNMISIZE__=\$$$(znmi_size) -D __RPADSIZE__=\$$$(rpad_size)
ldflags :=
ifeq ($(debug),1)
asflags += -D __DEBUG__=1
ldflags += -Ln $(name).$(debug_sym_ext)
endif
ifeq ($(debug),2)
asflags += -D __DEBUG__=1
ldflags += -Ln $(name).$(debug_sym_ext) -m $(name).$(debug_map_ext) -vm --dbgfile $(name).$(debug_nfo_ext)
endif
asflags += -g -U -I ./ -I $(libsfx_inc) -I $(libsfx_inc)/Configurations
ldflags += --cfg-path ./ --cfg-path $(libsfx_inc)/Configurations/
brr_flags := -rn1.0 -g
lz4_flags := -f -9
palette_flags := -v
tiles_flags := -v
map_flags := -v
# Include all source files under working directory if $(src) isn't set
ifndef src
src := $(call rwildcard, ,%.s)
endif
ifndef src_smp
src_smp := $(call rwildcard, ,%.s700)
endif
ifndef src_gsu
src_gsu := $(call rwildcard, ,%.sgs)
endif
ifndef headers
headers := $(call rwildcard, ,%.i) $(call rwildcard, ,%.i700)
endif
# libSFX
libsfx_src := $(wildcard $(libsfx_inc)/CPU/*.s)
libsfx_src_smp := $(wildcard $(libsfx_inc)/SMP/*.s700)
libsfx_headers := $(call rwildcard,$(libsfx_inc)/,%.i) $(call rwildcard,$(libsfx_inc)/,%.i700)
# Include libSFX package configs
sfx_incs := $(foreach inc,$(addprefix $(libsfx_inc)/Packages/,$(libsfx_packages)),$(wildcard $(inc)/config))
include $(sfx_incs)
# Configure SMP sub-projects
smp_overlays_src :=
smp_overlays_obj :=
define smp_overlay_add_src
smp_overlays_obj += $(patsubst %,$(obj_dir)/%,$(patsubst %.s700,%.o700,$(1)))
$(patsubst %,$(obj_dir)/%,$(patsubst %.s700,%.o700,$(1))): asflags = -g -U -I $(dir $(1)) -I ./ -I $(libsfx_inc) -I $(libsfx_inc)/Configurations
endef
define smp_overlay_add_product
smp_overlays_products := $(smp_overlays_products) $(1).bin
$(1).bin : $(filter $(obj_dir)/$(1)/%,$(smp_overlays_obj))
ifeq ($(debug),1)
$(ld) --cfg-path ./$(1) --cfg-path $(libsfx_inc)/Configurations -C SMP-Map.cfg -Ln $(1).$(debug_sym_ext) -o $(1).bin $(filter $(obj_dir)/$(1)/%,$(smp_overlays_obj))
else ifeq ($(debug),2)
$(ld) --cfg-path ./$(1) --cfg-path $(libsfx_inc)/Configurations -C SMP-Map.cfg -Ln $(1).$(debug_sym_ext) -m $(1).$(debug_map_ext) -vm --dbgfile $(1).$(debug_nfo_ext) -o $(1).bin $(filter $(obj_dir)/$(1)/%,$(smp_overlays_obj))
else
$(ld) --cfg-path ./$(1) --cfg-path $(libsfx_inc)/Configurations -C SMP-Map.cfg -o $(1).bin $(filter $(obj_dir)/$(1)/%,$(smp_overlays_obj))
endif
endef
ifdef smp_overlays
src_smp := $(filter-out $(foreach sub,$(smp_overlays),$(call rwildcard,$(sub)/,%.s700)),$(src_smp))
smp_overlays_src += $(foreach sub,$(smp_overlays),$(call rwildcard,$(sub)/,%.s700))
$(foreach src,$(smp_overlays_src),$(eval $(call smp_overlay_add_src,$(src))))
$(foreach sub,$(smp_overlays),$(eval $(call smp_overlay_add_product,$(sub))))
endif
# Configuration file dependencies
cfg_files := Makefile $(libsfx_dir)/libSFX.make
ifneq ("$(wildcard libSFX.cfg)","")
cfg_files += libSFX.cfg
endif
ifneq ("$(wildcard Map.cfg)","")
cfg_files += Map.cfg
endif
# Source -> obj targets
obj_sfx := $(patsubst $(libsfx_inc)%,$(obj_dir_sfx)%,$(patsubst %.s,%.o,$(libsfx_src)))
obj_smp_sfx := $(patsubst $(libsfx_inc)%,$(obj_dir_sfx)%,$(patsubst %.s700,%.o700,$(libsfx_src_smp)))
obj := $(patsubst %,$(obj_dir)/%,$(patsubst %.s,%.o,$(src)))
obj_smp := $(patsubst %,$(obj_dir)/%,$(patsubst %.s700,%.o700,$(src_smp)))
obj_gsu := $(patsubst %,$(obj_dir)/%,$(patsubst %.sgs,%.ogs,$(src_gsu)))
# Rules
all: clean default
run: $(rom)
ifdef LIBSFX_RUNCMD
ifndef breakpoints
$(LIBSFX_RUNCMD) $(run_args)
else
$(LIBSFX_RUNCMD) $(run_args) $$($(make_bp) $(breakpoints))
endif
else
@echo NB! To enable running set LIBSFX_RUNCMD, for example \(macOS\):
@echo \ \ \ \ export LIBSFX_RUNCMD\=\'open -a \~/bsnes/bsnes+.app --args \$$\(realpath \$$\(rom\)\)\'
endif
clean:
@rm -f $(rom) *.$(debug_sym_ext) *.$(debug_map_ext) *.$(debug_nfo_ext) $(derived_files) $(smp_overlays_products) $(clean_files)
@rm -frd $(obj_dir) $(obj_dir_sfx) $(clean_dirs)
# Prerequisite rules
$(derived_files) : $(cfg_files)
$(smp_overlays_obj) : $(derived_files) $(cfg_files) $(headers) $(libsfx_headers)
$(obj) : $(smp_overlays_products) $(derived_files) $(cfg_files) $(headers) $(libsfx_headers)
$(obj_gsu) : $(derived_files) $(cfg_files) $(headers) $(libsfx_headers)
$(obj_smp) : $(derived_files) $(cfg_files) $(headers) $(libsfx_headers)
$(obj_sfx) : $(cfg_files) $(libsfx_headers)
$(obj_smp_sfx) : $(cfg_files) $(libsfx_headers)
# Link
$(rom) : $(obj_sfx) $(obj_smp_sfx) $(obj) $(obj_smp) $(obj_gsu)
$(ld) $(ldflags) -C Map.cfg -o $@ $^
$(sfcheck) $@ -f
# Project obj : src
$(obj_dir)/%.o : %.s | $(smp_overlays_products)
@mkdir -pv $(dir $@)
$(as) $(asflags) $(pkg_asflags) -o $@ $<
$(obj_dir)/%.o700 : %.s700
@mkdir -pv $(dir $@)
$(as) $(asflags) $(pkg_asflags) -D TARGET_SMP -o $@ $<
$(obj_dir)/%.ogs : %.sgs
@mkdir -pv $(dir $@)
$(as) $(asflags) $(pkg_asflags) -D TARGET_GSU -o $@ $<
# libSFX obj : src
$(obj_dir_sfx)/%.o : $(libsfx_inc)/%.s
@mkdir -pv $(dir $@)
$(as) $(asflags) $(pkg_asflags) -o $@ $<
$(obj_dir_sfx)/%.o700 : $(libsfx_inc)/%.s700
@mkdir -pv $(dir $@)
$(as) $(asflags) $(pkg_asflags) -D TARGET_SMP -o $@ $<
# Derived file transformations
$(filter %.palette,$(derived_files)) : %.palette : %
$(superfamiconv) palette $(palette_flags) --in-image $* --out-data $@
$(filter %.tiles,$(derived_files)) : %.tiles : % %.palette
$(superfamiconv) tiles $(tiles_flags) --in-image $* --in-palette $*.palette --out-data $@
$(filter %.map,$(derived_files)) : %.map : % %.palette %.tiles
$(superfamiconv) map $(map_flags) --in-image $* --in-palette $*.palette --in-tiles $*.tiles --out-data $@
$(filter %.m7d,$(derived_files)) : %.m7d : % %.palette %.tiles
$(superfamiconv) map $(map_flags) --mode snes_mode7 --in-image $* --in-palette $*.palette --in-tiles $*.tiles --out-m7-data $@
$(filter %.brr,$(derived_files)): %.brr : %.wav
@rm -f $@
$(brr_enc) $(brr_flags) $< $@
$(filter %.lz4,$(derived_files)) : %.lz4 : %
$(lz4_compress) $(lz4_flags) $* $@
@touch $@