Skip to content

Commit

Permalink
refactor build system
Browse files Browse the repository at this point in the history
  • Loading branch information
camthesaxman committed Jan 11, 2018
1 parent d4afe49 commit b5667bd
Show file tree
Hide file tree
Showing 94 changed files with 477 additions and 538 deletions.
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ matrix:
env: _="Build"
script:
- ./build_tools.sh
- make -j2 compare_ruby
- make -j2 compare_ruby_rev1
- make -j2 compare_ruby_rev2
- make -j2 compare_sapphire
- make -j2 compare_sapphire_rev1
- make -j2 compare_sapphire_rev2
- make -j2 ruby
- make -j2 ruby_rev1
- make -j2 ruby_rev2
- make -j2 sapphire
- make -j2 sapphire_rev1
- make -j2 sapphire_rev2
- os: linux
env: _="Formatting"
addons:
Expand All @@ -53,7 +53,7 @@ matrix:
- rsync -avhI graphics-de/ graphics/
script:
- ./build_tools.sh
- make -j2 compare_ruby_de
- make -j2 compare_sapphire_de
- make -j2 ruby_de
- make -j2 sapphire_de
allow_failures:
- env: _="Formatting"
275 changes: 133 additions & 142 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,181 +1,172 @@
SHELL := /bin/bash -o pipefail

AS := $(DEVKITARM)/bin/arm-none-eabi-as
ASFLAGS := -mcpu=arm7tdmi
include config.mk

CC1 := tools/agbcc/bin/agbcc
override CFLAGS += -mthumb-interwork -Wimplicit -Wparentheses -Wunused -Werror -O2 -fhex-asm

CPP := $(DEVKITARM)/bin/arm-none-eabi-cpp
CPPFLAGS := -I tools/agbcc/include -iquote include -nostdinc -undef -Werror -Wno-trigraphs
#### Tools ####

LD := $(DEVKITARM)/bin/arm-none-eabi-ld
SHELL := /bin/bash -o pipefail
AS := $(DEVKITARM)/bin/arm-none-eabi-as
CC1 := tools/agbcc/bin/agbcc
CPP := $(DEVKITARM)/bin/arm-none-eabi-cpp
LD := $(DEVKITARM)/bin/arm-none-eabi-ld
OBJCOPY := $(DEVKITARM)/bin/arm-none-eabi-objcopy
SHA1SUM := sha1sum -c
GBAGFX := tools/gbagfx/gbagfx
RSFONT := tools/rsfont/rsfont
AIF2PCM := tools/aif2pcm/aif2pcm
MID2AGB := tools/mid2agb/mid2agb
PREPROC := tools/preproc/preproc
SCANINC := tools/scaninc/scaninc
RAMSCRGEN := tools/ramscrgen/ramscrgen

OBJCOPY := $(DEVKITARM)/bin/arm-none-eabi-objcopy
ASFLAGS := -mcpu=arm7tdmi -I include --defsym $(VERSION)=1 --defsym REVISION=$(REVISION) --defsym $(LANGUAGE)=1
CC1FLAGS := -mthumb-interwork -Wimplicit -Wparentheses -Wunused -Werror -O2 -fhex-asm
CPPFLAGS := -I tools/agbcc/include -iquote include -nostdinc -undef -Werror -Wno-trigraphs -D $(VERSION) -D REVISION=$(REVISION) -D $(LANGUAGE)


#### Files ####

ROM := poke$(BUILD_NAME).gba
MAP := $(ROM:%.gba=%.map)

SUBDIRS := \
sound \
sound/songs \
asm \
data \
src \
src/battle \
src/battle/anim \
src/field \
src/debug \
src/scene \
src/pokemon \
src/engine \
src/libs
BUILD_DIR := build/$(BUILD_NAME)

C_SOURCES := $(foreach dir, $(SUBDIRS), $(wildcard $(dir)/*.c))
ASM_SOURCES := $(foreach dir, $(SUBDIRS), $(wildcard $(dir)/*.s))

C_OBECTS := $(addprefix $(BUILD_DIR)/, $(C_SOURCES:%.c=%.o))
ASM_OBJECTS := $(addprefix $(BUILD_DIR)/, $(ASM_SOURCES:%.s=%.o))
ALL_OBJECTS := $(C_OBJECTS) $(ASM_OBJECTS)

LIBGCC := tools/agbcc/lib/libgcc.a
LIBC := tools/agbcc/lib/libc.a
LIBGCC := tools/agbcc/lib/libgcc.a

SHA1 := sha1sum -c
LD_SCRIPT := $(BUILD_DIR)/ld_script.ld

GFX := tools/gbagfx/gbagfx
AIF := tools/aif2pcm/aif2pcm
MID := tools/mid2agb/mid2agb
SCANINC := tools/scaninc/scaninc
PREPROC := tools/preproc/preproc
RAMSCRGEN := tools/ramscrgen/ramscrgen
# Special configurations required for lib files
%src/libs/siirtc.o: CC1FLAGS := -mthumb-interwork
%src/libs/agb_flash.o: CC1FLAGS := -O1 -mthumb-interwork
%src/libs/agb_flash_1m.o: CC1FLAGS := -O1 -mthumb-interwork
%src/libs/agb_flash_mx.o: CC1FLAGS := -O1 -mthumb-interwork
%src/libs/m4a_2.o: CC1 := tools/agbcc/bin/old_agbcc
%src/libs/m4a_4.o: CC1 := tools/agbcc/bin/old_agbcc

REVISION := 0

VERSIONS := ruby sapphire ruby_rev1 sapphire_rev1 ruby_rev2 sapphire_rev2 ruby_de sapphire_de
#### Main Rules ####

# Clear the default suffixes.
.SUFFIXES:
# Disable dependency scanning when NODEP is used for quick building
ifeq ($(NODEP),)
$(BUILD_DIR)/src/%.o: C_FILE = $(*D)/$(*F).c
$(BUILD_DIR)/src/%.o: C_DEP = $(shell $(SCANINC) -I include $(wildcard $(C_FILE:$(BUILD_DIR)/=)))
$(BUILD_DIR)/asm/%.o: ASM_DEP = $(shell $(SCANINC) asm/$(*F).s)
$(BUILD_DIR)/data/%.o: ASM_DEP = $(shell $(SCANINC) data/$(*F).s)
endif

# Secondary expansion is required for dependency variables in object rules.
.SECONDEXPANSION:

# Clear the default suffixes
.SUFFIXES:

# Don't delete intermediate files
.PRECIOUS: %.1bpp %.4bpp %.8bpp %.gbapal %.lz %.rl %.pcm %.bin sound/direct_sound_samples/cry_%.bin

.PHONY: all clean tidy all_versions compare compare_all \
$(VERSIONS) $(VERSIONS:%=compare_%)
# Create build subdirectories
$(shell mkdir -p $(addprefix $(BUILD_DIR)/, $(SUBDIRS)))

all: $(ROM)
ifeq ($(COMPARE),1)
@$(SHA1SUM) $(BUILD_NAME).sha1
endif

clean: tidy
$(RM) sound/direct_sound_samples/*.bin
$(RM) $(ALL_OBJECTS)
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.rl' \) -exec rm {} +

tidy:
$(RM) poke$(BUILD_NAME){.gba,.elf,.map}
$(RM) -r build

$(shell mkdir -p build/ $(VERSIONS:%=build/%/{,asm,data,src{,/battle{,/anim},/field,/debug,/scene,/pokemon,/engine,/libs}}))
%.gba: %.elf
$(OBJCOPY) -O binary --gap-fill 0xFF --pad-to 0x9000000 $< $@

C_SRCS := $(shell find src -iname "*.c")
ASM_SRCS := $(wildcard asm/*.s)
DATA_ASM_SRCS := $(wildcard data/*.s)
%.elf: $(LD_SCRIPT) $(ALL_OBJECTS)
cd $(BUILD_DIR) && $(LD) -T ld_script.ld -Map ../../$(MAP) -o ../../$@ ../../$(LIBGCC) ../../$(LIBC)

SONG_SRCS := $(wildcard sound/songs/*.s)
SONG_OBJS := $(SONG_SRCS:%.s=%.o)
$(LD_SCRIPT): $(BUILD_DIR)/sym_bss.ld $(BUILD_DIR)/sym_common.ld $(BUILD_DIR)/sym_ewram.ld
cd $(BUILD_DIR) && sed -f ../../ld_script.sed ../../ld_script.txt | sed "s#tools/#../../tools/#g" >ld_script.ld
$(BUILD_DIR)/sym_bss.ld: sym_bss.txt
cd $(BUILD_DIR) && ../../$(RAMSCRGEN) .bss ../../sym_bss.txt $(LANGUAGE) >sym_bss.ld
$(BUILD_DIR)/sym_common.ld: sym_common.txt $(C_OBECTS) $(wildcard common_syms/*.txt)
cd $(BUILD_DIR) && ../../$(RAMSCRGEN) COMMON ../../sym_common.txt $(LANGUAGE) -c src,../../common_syms >sym_common.ld
$(BUILD_DIR)/sym_ewram.ld: sym_ewram.txt
cd $(BUILD_DIR) && ../../$(RAMSCRGEN) ewram_data ../../sym_ewram.txt $(LANGUAGE) >sym_ewram.ld

all: ruby
@:
$(BUILD_DIR)/%.o: %.c $$(C_DEP)
$(CPP) $(CPPFLAGS) $< -o $(BUILD_DIR)/$*.i
$(PREPROC) $(BUILD_DIR)/$*.i charmap.txt | $(CC1) $(CC1FLAGS) -o $(BUILD_DIR)/$*.s
@printf ".text\n\t.align\t2, 0\n" >> $(BUILD_DIR)/$*.s
@$(AS) $(ASFLAGS) -o $@ $(BUILD_DIR)/$*.s

all_versions: $(VERSIONS)
@:
# Only .s files in data need preproc
$(BUILD_DIR)/data/%.o: data/%.s $$(ASM_DEP)
$(PREPROC) $< charmap.txt | $(CPP) -I include | $(AS) $(ASFLAGS) -o $@

# For contributors to make sure a change didn't affect the contents of the ROM.
compare: compare_ruby
compare_all: $(VERSIONS:%=compare_%)
$(BUILD_DIR)/%.o: %.s $$(ASM_DEP)
$(AS) $(ASFLAGS) $< -o $@

clean: tidy
rm -f sound/direct_sound_samples/*.bin
rm -f $(SONG_OBJS)
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.rl' \) -exec rm {} +
# "friendly" target names for convenience sake
ruby: ; @$(MAKE) --no-print-directory VERSION=RUBY
ruby_rev1: ; @$(MAKE) --no-print-directory VERSION=RUBY REVISION=1
ruby_rev2: ; @$(MAKE) --no-print-directory VERSION=RUBY REVISION=2
sapphire: ; @$(MAKE) --no-print-directory VERSION=SAPPHIRE
sapphire_rev1: ; @$(MAKE) --no-print-directory VERSION=SAPPHIRE REVISION=1
sapphire_rev2: ; @$(MAKE) --no-print-directory VERSION=SAPPHIRE REVISION=1
ruby_de: ; @$(MAKE) --no-print-directory VERSION=RUBY LANGUAGE=GERMAN
sapphire_de: ; @$(MAKE) --no-print-directory VERSION=SAPPHIRE LANGUAGE=GERMAN

tidy:
rm -f $(VERSIONS:%=poke%{.gba,.elf,.map})
rm -r build/*

#### Graphics Rules ####

GFX_OPTS :=

include castform.mk
include tilesets.mk
include fonts.mk
include misc.mk
include override.mk

%.s: ;
%.png: ;
%.pal: ;
%.aif: ;

%.1bpp: %.png ; $(GFX) $< $@
%.4bpp: %.png ; $(GFX) $< $@
%.8bpp: %.png ; $(GFX) $< $@
%.gbapal: %.pal ; $(GFX) $< $@
%.gbapal: %.png ; $(GFX) $< $@
%.lz: % ; $(GFX) $< $@
%.rl: % ; $(GFX) $< $@
sound/direct_sound_samples/cry_%.bin: sound/direct_sound_samples/cry_%.aif ; $(AIF) $< $@ --compress
%.bin: %.aif ; $(AIF) $< $@
sound/songs/%.s: sound/songs/%.mid
cd $(@D) && ../../$(MID) $(<F)

%src/libs/siirtc.o: CFLAGS := -mthumb-interwork

%src/libs/agb_flash.o: CFLAGS := -O -mthumb-interwork
%src/libs/agb_flash_1m.o: CFLAGS := -O -mthumb-interwork
%src/libs/agb_flash_mx.o: CFLAGS := -O -mthumb-interwork
%.1bpp: %.png ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.4bpp: %.png ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.8bpp: %.png ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.gbapal: %.pal ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.gbapal: %.png ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.lz: % ; $(GBAGFX) $< $@ $(GFX_OPTS)
%.rl: % ; $(GBAGFX) $< $@ $(GFX_OPTS)

%src/libs/m4a_2.o: CC1 := tools/agbcc/bin/old_agbcc
%src/libs/m4a_4.o: CC1 := tools/agbcc/bin/old_agbcc

$(SONG_OBJS): %.o: %.s
$(AS) $(ASFLAGS) -I sound -o $@ $<
#### Sound Rules ####

sound/direct_sound_samples/cry_%.bin: sound/direct_sound_samples/cry_%.aif
$(AIF2PCM) $< $@ --compress

define VERSION_RULES
sound/%.bin: sound/%.aif
$(AIF2PCM) $< $@

$1_C_OBJS := $$(C_SRCS:%.c=build/$1/%.o)
$1_ASM_OBJS := $$(ASM_SRCS:%.s=build/$1/%.o)
$1_DATA_ASM_OBJS := $$(DATA_ASM_SRCS:%.s=build/$1/%.o)

ifeq ($$(NODEP),)
build/$1/src/%.o: c_path = $$(*D)/$$(*F).c
build/$1/src/%.o: c_dep = $$(shell $$(SCANINC) -I include $$(wildcard $$(c_path:build/$1/=)))
build/$1/asm/%.o: asm_dep = $$(shell $$(SCANINC) asm/$$(*F).s)
build/$1/data/%.o: asm_dep = $$(shell $$(SCANINC) data/$$(*F).s)
endif

$1_OBJS := $$($1_C_OBJS) $$($1_ASM_OBJS) $$($1_DATA_ASM_OBJS) $$(SONG_OBJS)
$1_OBJS_REL := $$($1_OBJS:build/$1/%=%)
$1_OBJS_REL := $$($1_OBJS_REL:sound/%=../../sound/%)

$$($1_C_OBJS): VERSION := $2
$$($1_C_OBJS): REVISION := $3
$$($1_C_OBJS): LANGUAGE := $4
build/$1/%.o : %.c $$$$(c_dep)
@$$(CPP) $$(CPPFLAGS) -D $$(VERSION) -D REVISION=$$(REVISION) -D $$(LANGUAGE) $$< -o build/$1/$$*.i
@$$(PREPROC) build/$1/$$*.i charmap.txt | $$(CC1) $$(CFLAGS) -o build/$1/$$*.s
@printf ".text\n\t.align\t2, 0\n" >> build/$1/$$*.s
$$(AS) $$(ASFLAGS) -o $$@ build/$1/$$*.s

$$($1_ASM_OBJS): VERSION := $2
$$($1_ASM_OBJS): REVISION := $3
$$($1_ASM_OBJS): LANGUAGE := $4
build/$1/asm/%.o: asm/%.s $$$$(asm_dep)
$$(AS) $$(ASFLAGS) --defsym $$(VERSION)=1 --defsym REVISION=$$(REVISION) --defsym $$(LANGUAGE)=1 -o $$@ $$<

$$($1_DATA_ASM_OBJS): VERSION := $2
$$($1_DATA_ASM_OBJS): REVISION := $3
$$($1_DATA_ASM_OBJS): LANGUAGE := $4
build/$1/data/%.o: data/%.s $$$$(asm_dep)
$$(PREPROC) $$< charmap.txt | $$(CPP) -I include | $$(AS) $$(ASFLAGS) --defsym $$(VERSION)=1 --defsym REVISION=$$(REVISION) --defsym $$(LANGUAGE)=1 -o $$@

build/$1/sym_bss.ld: LANGUAGE := $4
build/$1/sym_bss.ld: sym_bss.txt
cd build/$1 && ../../$$(RAMSCRGEN) .bss ../../sym_bss.txt $$(LANGUAGE) >sym_bss.ld

build/$1/sym_common.ld: LANGUAGE := $4
build/$1/sym_common.ld: sym_common.txt $$($1_C_OBJS) $$(wildcard common_syms/*.txt)
cd build/$1 && ../../$$(RAMSCRGEN) COMMON ../../sym_common.txt $$(LANGUAGE) -c src,../../common_syms >sym_common.ld

build/$1/sym_ewram.ld: LANGUAGE := $4
build/$1/sym_ewram.ld: sym_ewram.txt
cd build/$1 && ../../$$(RAMSCRGEN) ewram_data ../../sym_ewram.txt $$(LANGUAGE) >sym_ewram.ld

build/$1/ld_script.ld: ld_script.txt build/$1/sym_bss.ld build/$1/sym_common.ld build/$1/sym_ewram.ld
cd build/$1 && sed -f ../../ld_script.sed ../../ld_script.txt | sed "s#tools/#../../tools/#g" | sed "s#sound/#../../sound/#g" >ld_script.ld

poke$1.elf: build/$1/ld_script.ld $$($1_OBJS)
cd build/$1 && $$(LD) -T ld_script.ld -Map ../../poke$1.map -o ../../$$@ $$($1_OBJS_REL) ../../$$(LIBGCC) ../../$$(LIBC)

poke$1.gba: %.gba: %.elf
$$(OBJCOPY) -O binary --gap-fill 0xFF --pad-to 0x9000000 $$< $$@

compare_$1: poke$1.gba
@$$(SHA1) $1.sha1

$1: poke$1.gba
@:
endef

$(eval $(call VERSION_RULES,ruby,RUBY,0,ENGLISH))
$(eval $(call VERSION_RULES,ruby_rev1,RUBY,1,ENGLISH))
$(eval $(call VERSION_RULES,ruby_rev2,RUBY,2,ENGLISH))
$(eval $(call VERSION_RULES,sapphire,SAPPHIRE,0,ENGLISH))
$(eval $(call VERSION_RULES,sapphire_rev1,SAPPHIRE,1,ENGLISH))
$(eval $(call VERSION_RULES,sapphire_rev2,SAPPHIRE,2,ENGLISH))
$(eval $(call VERSION_RULES,ruby_de,RUBY,0,GERMAN))
$(eval $(call VERSION_RULES,sapphire_de,SAPPHIRE,0,GERMAN))
sound/songs/%.s: sound/songs/%.mid
cd $(@D) && ../../$(MID2AGB) $(<F)
2 changes: 1 addition & 1 deletion asm/battle_1.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.include "constants/gba_constants.inc"

.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/battle_9.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/battle_anim_812C144.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/battle_anim_813F0F4.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/battle_intro.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/contest_ai.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
2 changes: 1 addition & 1 deletion asm/contest_effect.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.include "include/macros.inc"

.syntax unified

Expand Down
Loading

0 comments on commit b5667bd

Please sign in to comment.