2018-10-19 00:45:50 +02:00
.SUFFIXES :
2019-04-28 19:36:07 +02:00
.DEFAULT_GOAL := all
2018-10-19 00:45:50 +02:00
2019-04-28 19:36:07 +02:00
################################################
# #
# CONSTANT DEFINITIONS #
# #
################################################
2018-10-19 00:45:50 +02:00
# Directory constants
SRCDIR = src
BINDIR = bin
OBJDIR = obj
DEPSDIR = deps
# Program constants
RGBASM = rgbasm
RGBLINK = rgblink
RGBFIX = rgbfix
2019-03-01 13:21:36 +01:00
MKDIR = $( shell which mkdir)
2018-10-19 00:45:50 +02:00
2019-04-28 19:36:07 +02:00
ROMFile = $( BINDIR) /$( ROMName) .$( ROMExt)
# Project-specific configuration
i n c l u d e M a k e f i l e . c o n f
2018-10-19 00:45:50 +02:00
# Argument constants
ASFLAGS += -E -h -i $( SRCDIR) / -i $( SRCDIR) /constants/ -i $( SRCDIR) /macros/ -p $( FillValue)
2019-04-28 19:36:07 +02:00
LDFLAGS += -d -p $( FillValue)
FXFLAGS += -j -f lh -i $( GameID) -k $( NewLicensee) -l $( OldLicensee) -m $( MBCType) -n $( ROMVersion) -p $( FillValue) -r $( SRAMSize) -t $( GameTitle)
2018-10-19 00:45:50 +02:00
# The list of "root" ASM files that RGBASM will be invoked on
ASMFILES := $( wildcard $( SRCDIR) /*.asm)
2019-04-28 19:36:07 +02:00
################################################
# #
# RESOURCE FILES #
# #
################################################
# Define how to compress files (same recipe for any file)
%.pb16 : %
src/tools/pb16.py $< $@
2019-04-28 19:38:16 +02:00
# RGBGFX generates tilemaps with sequential tile IDs, which works fine for $8000 mode but not $8800 mode; `bit7ify.py` takes care to flip bit 7 so maps become $8800-compliant
%.bit7.tilemap : src /tools /bit 7ify .py %.tilemap
$^ $@
2019-04-28 19:36:07 +02:00
CLEANTARGETS := $( BINDIR) $( DEPSDIR) $( OBJDIR) dummy # The list of things that must be cleared; expanded by the resource Makefiles
INITTARGETS :=
# Include all resource Makefiles
# This must be done before we include `$(DEPSDIR)/all` otherwise `dummy` has no prereqs
i n c l u d e $( wildcard $ ( SRCDIR ) /res /*/Makefile )
2018-10-19 00:45:50 +02:00
# `all` (Default target): build the ROM
2019-04-28 19:36:07 +02:00
all : $( ROMFile )
2018-10-19 00:45:50 +02:00
.PHONY : all
# `clean`: Clean temp and bin files
clean :
-rm -rf $( CLEANTARGETS)
2019-04-28 19:36:07 +02:00
.PHONY : clean
2018-10-19 00:45:50 +02:00
# `rebuild`: Build everything from scratch
2019-04-28 19:36:07 +02:00
# It's important to do these two in order if we're using more than one job
2018-10-19 00:45:50 +02:00
rebuild :
$( MAKE) clean
$( MAKE) all
2019-04-28 19:36:07 +02:00
.PHONY : rebuild
2018-10-19 00:45:50 +02:00
# `dummy` is a dummy target to build the resource files necessary for RGBASM to not fail on compilation
# It's made an actual file to avoid an infinite compilation loop
# INITTARGETS is defined by the resource Makefiles
dummy : $( INITTARGETS )
@echo "THIS FILE ENSURES THAT COMPILATION GOES RIGHT THE FIRST TIME, DO NOT DELETE" > $@
# `.d` files are generated as dependency lists of the "root" ASM files, to save a lot of hassle.
2019-04-28 19:36:07 +02:00
# > Obj files also depend on `dummy` to ensure all the binary files are present, so RGBASM doesn't choke on them not being present;
2018-10-19 00:45:50 +02:00
# > This would cause the first compilation to never finish, thus Make never knows to build the binary files, thus deadlocking everything.
2019-04-28 19:36:07 +02:00
# Compiling also generates dependency files!
# Also add all obj dependencies to the deps file too, so Make knows to remake it
# RGBDS is stupid, so dependency files cannot be generated if obj files aren't,
# so if a dep file is missing but an obj is there, we need to delete the object and start over
$(DEPSDIR)/%.d : $( OBJDIR ) /%.o ;
$(OBJDIR)/%.o : DEPFILE = $( DEPSDIR ) /$*.d
$(OBJDIR)/%.o : $( SRCDIR ) /%.asm dummy
2019-03-01 13:21:36 +01:00
@$( MKDIR) -p $( DEPSDIR)
@$( MKDIR) -p $( OBJDIR)
2019-01-25 19:01:53 +01:00
set -e; \
2019-04-28 19:36:07 +02:00
TMP_DEPFILE = $$ ( mktemp) ; \
$( RGBASM) -M $$ TMP_DEPFILE $( ASFLAGS) -o $@ $<; \
sed 's,\($*\)\.o[ :]*,\1.o $(DEPFILE): ,g' < $$ TMP_DEPFILE > $( DEPFILE) ; \
for line in $$ ( cut -d ":" -f 2 $$ TMP_DEPFILE) ; do if [ " $$ line " != " $< " ] ; then echo " $$ line: ; " >> $( DEPFILE) ; fi ; done ; \
rm $$ TMP_DEPFILE
2018-10-19 00:45:50 +02:00
# Include (and potentially remake) all dependency files
2019-04-28 19:36:07 +02:00
# Remove duplicated recipes (`sort | uniq`), hence using yet another file grouping everything
# Also filter out lines already defined in the resource Makefiles because defining two rules for the same file causes Bad Things(tm) (`grep`)
SPACE :=
SPACE +=
# Yes this "space" hack is NEEDED. I don't like where I'm going anymore, either
$(DEPSDIR)/all : $( patsubst $ ( SRCDIR ) /%.asm ,$ ( DEPSDIR ) /%.d ,$ ( ASMFILES ) )
cat $^ | sort | uniq | grep -vE " ^( $( subst .,\\ .,$( subst $( SPACE) ,| ,$( strip $( INITTARGETS) ) ) ) ): ; " > $@
i f n e q ( $( MAKECMDGOALS ) , c l e a n )
i n c l u d e $( DEPSDIR ) / a l l
e n d i f
2018-10-19 00:45:50 +02:00
# How to make the ROM
2019-04-28 19:36:07 +02:00
$(ROMFile) : $( patsubst $ ( SRCDIR ) /%.asm ,$ ( OBJDIR ) /%.o ,$ ( ASMFILES ) )
2019-03-01 13:21:36 +01:00
@$( MKDIR) -p $( BINDIR)
2018-10-19 00:45:50 +02:00
$( RGBASM) $( ASFLAGS) -o $( OBJDIR) /build_date.o $( SRCDIR) /res/build_date.asm
2019-04-28 19:36:07 +02:00
set -e; \
TMP_ROM = $$ ( mktemp) ; \
$( RGBLINK) $( LDFLAGS) -o $$ TMP_ROM -m $( @:.gb= .map) -n $( @:.gb= .sym) $^ $( OBJDIR) /build_date.o; \
$( RGBFIX) $( FXFLAGS) $$ TMP_ROM; \
mv $$ TMP_ROM $( ROMFile)