forked from gbdev/gb-asm-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
119 lines (98 loc) · 3.56 KB
/
Makefile
File metadata and controls
119 lines (98 loc) · 3.56 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
# You can set the name of the .gb ROM file here
PROJECTNAME = GalacticArmada
SRCDIR = src
LIBDIR = libs
OBJDIR = obj
DSTDIR = dist
RESDIR = $(SRCDIR)/resources
ASMDIR = $(SRCDIR)/main
RESSPRITES = $(RESDIR)/sprites
RESBACKGROUNDS = $(RESDIR)/backgrounds
GENDIR = $(SRCDIR)/generated
GENSPRITES = $(GENDIR)/sprites
GENBACKGROUNDS = $(GENDIR)/backgrounds
BINS = $(DSTDIR)/$(PROJECTNAME).gb
# Tools
RGBDS ?=
ASM := $(RGBDS)rgbasm
GFX := $(RGBDS)rgbgfx
LINK := $(RGBDS)rgblink
FIX := $(RGBDS)rgbfix
# Tool flags
FIXFLAGS := -v -p 0xFF
# https://stackoverflow.com/a/18258352
# Make does not offer a recursive wild card function, so here's one:
rwildcard = $(foreach d,\
$(wildcard $(1:=/*)), \
$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d) \
)
# https://stackoverflow.com/a/16151140
# This makes it so every entry in a space-delimited list appears only once
unique = $(if $1,\
$(firstword $1) $(call unique,$(filter-out $(firstword $1),$1)) \
)
# Collect ASM sources from ASMDIR and LIBDIR.
ASMSOURCES_COLLECTED = \
$(call rwildcard,$(ASMDIR),*.asm) $(call rwildcard,$(LIBDIR),*.asm)
OBJS = $(patsubst %.asm,$(OBJDIR)/%.o,$(notdir $(ASMSOURCES_COLLECTED)))
all: $(BINS)
# ANCHOR: generate-graphics
NEEDED_GRAPHICS = \
$(GENSPRITES)/player-ship.2bpp \
$(GENSPRITES)/enemy-ship.2bpp \
$(GENSPRITES)/bullet.2bpp \
$(GENBACKGROUNDS)/text-font.2bpp \
$(GENBACKGROUNDS)/star-field.tilemap \
$(GENBACKGROUNDS)/title-screen.tilemap
# Generate sprites, ensuring the containing directories have been created.
$(GENSPRITES)/%.2bpp: $(RESSPRITES)/%.png | $(GENSPRITES)
$(GFX) -c "#FFFFFF,#cfcfcf,#686868,#000000;" --columns -o $@ $<
# Generate background tile set, ensuring the containing directories have been created.
$(GENBACKGROUNDS)/%.2bpp: $(RESBACKGROUNDS)/%.png | $(GENBACKGROUNDS)
$(GFX) -c "#FFFFFF,#cbcbcb,#414141,#000000;" -o $@ $<
# Generate background tile map *and* tile set, ensuring the containing directories
# have been created.
$(GENBACKGROUNDS)/%.tilemap: $(RESBACKGROUNDS)/%.png | $(GENBACKGROUNDS)
$(GFX) -c "#FFFFFF,#cbcbcb,#414141,#000000;" \
--tilemap $@ \
--unique-tiles \
-o $(GENBACKGROUNDS)/$*.2bpp \
$<
# ANCHOR_END: generate-graphics
compile.bat: Makefile
@echo "REM Automatically generated from Makefile" > compile.bat
@make -sn | sed y/\\/\\\\/\\\\\\\^/ | grep -v make >> compile.bat
# ANCHOR: generate-objects
# Extract directories from collected ASM sources and append "%.asm" to each one,
# creating a wildcard-rule.
ASMSOURCES_DIRS = $(patsubst %,%%.asm,\
$(call unique,$(dir $(ASMSOURCES_COLLECTED))) \
)
# This is a Makefile "macro".
# It defines a %.o target from a corresponding %.asm, ensuring the
# "prepare" step has ran and the graphics are already generated.
define object-from-asm
$(OBJDIR)/%.o: $1 | $(OBJDIR) $(NEEDED_GRAPHICS)
$$(ASM) -o $$@ $$<
endef
# Run the macro for each directory listed in ASMSOURCES_DIRS, thereby
# creating the appropriate targets.
$(foreach i, $(ASMSOURCES_DIRS), $(eval $(call object-from-asm,$i)))
# ANCHOR_END: generate-objects
# Link and build the final ROM.
$(BINS): $(OBJS) | $(DSTDIR)
$(LINK) -n $(DSTDIR)/$(PROJECTNAME).sym -o $@ $^
$(FIX) $(FIXFLAGS) $@
# Ensure directories for generated files exist.
define ensure-directory
$1:
mkdir -p $$@
endef
PREPARE_DIRECTORIES = \
$(OBJDIR) $(GENSPRITES) $(GENBACKGROUNDS) $(DSTDIR)
$(foreach i, $(PREPARE_DIRECTORIES), $(eval $(call ensure-directory,$i)))
# Clean up generated directories.
clean:
rm -rfv $(PREPARE_DIRECTORIES)
# Declare these targets as "not actually files".
.PHONY: clean all