sotn-decomp/Makefile.saturn.mk
Luciano Ciccariello 45a71c03ac
Rewrite Saturn make rules (#660)
## What

Enhance the existing `Makefile` to build new rules for the Saturn side
of the decomp. This should allow more flexibility when adding new
overlays or when tuning existing rules.

## Changes

I separated part of the Saturn build process in the separate file
`Makefile.saturn.mk`. I realise that naming it `saturn.mk` would have
been enough, but I pre-pended `Makefile.` so it can be found right below
the main `Makefile` when listing files in an alphabetic order. I plan to
do the same with the psx and psp toolchain, therefore you will find
`include Makefile.*.mk` in the main `Makefile`.

I deleted all the game building process done with Docker. Now that we
have an established way to do it natively I think it is no longer
required. We can always run the entire buildchain within a Docker
container instead of having `_native` and `_docker`. Now all the
`_native` references are removed. `build_saturn_native` is now
`build_saturn`.

`check_saturn` is no longer responsible of stripping the ELF into a
binary. That is now part of `build_saturn`.

I removed the references to `_li.o` (e.g. `alucard_li.o`) and used
`.elf` instead, which is closer to how the PSX build chain works. If
`_li.o` was a better preference, please let me know.

I am no longer using `./compile_dosemu.sh`. Instead I am using the new
`$(DOSEMU)` to directly invoke the tool within the Makefile. I have done
that to reduce the amount of dependent files.

I tried minimising duplication as much as possible. We now have a list
of overlays found in `SATURN_OVL_TARGETS`. Each expected output triggers
a series of dependencies so seamlessly build everything. `Makefile` is
smart enough to call `$(SATURN_TOOLCHAIN)` only once. If the game was
already built but just one source or symbol file changed, triggering a
new `build_saturn` will only compile the modified overlay.

The Saturn ADPCM files are now extracted in `assets/` instead of
`build/`. I think `assets/` should contain all the uncompressed and
uncooked files. The list of PCM file is not hardcoded. Instead I am now
using `$(wildcard disks/saturn/SD/*.PCM)`. This now means the tool tries
to convert PCMs from `SDD0.PCM` to `SDF0.PCM` with no success. As the
tool is silently failing I decided to leave it as I wrote it.

## Problems

I rewrote everything thinking about concurrency in mind. But `make -j
build_saturn` gives some unexpected output on `stdout`. I did not dig
too much into it. I suspect it might be dosemu. This is not a stopper as
we were not using `-j` when building the game anyway.

I also noticed doing `VERSION=saturn make build` calls
`mipsel-linux-gnu-ld` for `stage_02`. I suspect it is calling the rule
`$(MAIN_TARGET).elf: $(MAIN_O_FILES)` and simply moving `include
Makefile.*.mk` above it should fix it. But then it would cause the same
problem if I split the PSX rules into their own separate file. We never
used `make build` by setting the env variable `VERSION`, so this is not
either a breaking change or a stopper.

## Post thoughts

I am happy with what I achieved so far. I used the knowledge I
accumulated when maintaining the PSX counterpart. Since I now better
understand how `make` works, I was able to make some better decisions in
the Saturn counterpart. For example triggering a new build when the
symbol list changes is something the PSX build chain lacks of. I think
in the future it would be nice to trigger `make extract` when either the
YAML or the symbol list changes.
2023-10-03 21:42:08 +01:00

119 lines
5.1 KiB
Makefile

SATURN_BUILD_DIR := build/saturn
SATURN_ASSETS_DIR := assets/saturn
SATURN_OVL_TARGETS := GAME ALUCARD STAGE_02 WARP T_BAT
SATURN_LIB_TARGETS := lib/gfs lib/spr lib/dma lib/scl lib/csh lib/per lib/cdc lib/mth lib/bup lib/sys
DOSEMU := dosemu -quiet -dumb -f ./dosemurc -K . -E
SATURN_TOOLCHAIN := bin/cygnus-2.7-96Q3-bin
CC1_SATURN := $(SATURN_BUILD_DIR)/CC1.EXE
SATURN_SPLITTER_DIR := $(TOOLS_DIR)/saturn-splitter
SATURN_SPLITTER_APP := $(SATURN_SPLITTER_DIR)/rust-dis/target/release/rust-dis
SATURN_ADPCM_EXTRACT_APP := $(SATURN_SPLITTER_DIR)/adpcm-extract/target/release/adpcm-extract
SATURN_OVL_PRGS := $(addprefix $(SATURN_BUILD_DIR)/,$(addsuffix .PRG,$(SATURN_OVL_TARGETS)))
SATURN_LIB_OBJECTS := $(addprefix $(SATURN_BUILD_DIR)/,$(addsuffix .o,$(SATURN_LIB_TARGETS)))
SATURN_PCM_FILES := $(wildcard disks/saturn/SD/*.PCM)
SATURN_WAV_FILES := $(patsubst disks/saturn/SD/%.PCM,$(SATURN_ASSETS_DIR)/SD/%.wav,$(SATURN_PCM_FILES))
.PHONY: saturn
saturn: build_saturn check_saturn
.PHONY: check_saturn
check_saturn:
sha1sum --check config/check.saturn.sha
.PHONY: build_saturn
build_saturn: $(SATURN_BUILD_DIR)/0.BIN $(SATURN_OVL_PRGS)
.PHONY: extract_saturn
extract_saturn: $(SATURN_SPLITTER_APP)
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/game.prg.yaml
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/t_bat.prg.yaml
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/zero.bin.yaml
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/stage_02.prg.yaml
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/warp.prg.yaml
$(SATURN_SPLITTER_APP) $(CONFIG_DIR)/saturn/alucard.prg.yaml
.PHONY: extract_disk_saturn
extract_disk_saturn:
bchunk disks/sotn.saturn.bin disks/sotn.saturn.cue disks/sotn.saturn.iso
7z x disks/sotn.saturn.iso01.iso -odisks/saturn/ || true
.PHONY: extract_saturn_pcm
extract_saturn_pcm: $(SATURN_WAV_FILES)
.PHONY: diff_saturn
diff_saturn:
sh-elf-objdump -z -m sh2 -b binary -D ./build/saturn/$(FILENAME) > ./build/saturn/$(FILENAME)-ours.txt && \
sh-elf-objdump -z -m sh2 -b binary -D ./disks/saturn/$(FILENAME) > ./build/saturn/$(FILENAME)-theirs.txt && \
diff ./build/saturn/$(FILENAME)-ours.txt ./build/saturn/$(FILENAME)-theirs.txt > ./build/saturn/$(FILENAME)-diff.txt || true
$(SATURN_BUILD_DIR)/0.BIN: $(SATURN_BUILD_DIR)/zero.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/GAME.PRG: $(SATURN_BUILD_DIR)/game.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/ALUCARD.PRG: $(SATURN_BUILD_DIR)/alucard.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/STAGE_02.PRG: $(SATURN_BUILD_DIR)/stage_02.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/WARP.PRG: $(SATURN_BUILD_DIR)/warp.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/T_BAT.PRG: $(SATURN_BUILD_DIR)/t_bat.elf
sh-elf-objcopy $< -O binary $@
$(SATURN_BUILD_DIR)/zero.elf: $(SATURN_BUILD_DIR)/zero.o $(SATURN_LIB_OBJECTS) config/saturn/zero_syms.txt config/saturn/game_syms.txt config/saturn/zero_user_syms.txt
cd $(SATURN_BUILD_DIR) && \
sh-elf-ld -verbose --no-check-sections -nostdlib \
-o zero.elf \
-Map zero.map \
-T ../../config/saturn/zero.ld \
-T ../../config/saturn/zero_syms.txt \
-T ../../config/saturn/game_syms.txt \
-T ../../config/saturn/zero_user_syms.txt \
zero.o $(addsuffix .o,$(SATURN_LIB_TARGETS))
$(SATURN_BUILD_DIR)/%.elf: $(SATURN_BUILD_DIR)/%.o config/saturn/zero_syms.txt config/saturn/game_syms.txt config/saturn/%_user_syms.txt
cd $(SATURN_BUILD_DIR) && \
sh-elf-ld -verbose --no-check-sections -nostdlib \
-o $*.elf \
-Map $*.map \
-T ../../config/saturn/$*.ld \
-T ../../config/saturn/zero_syms.txt \
-T ../../config/saturn/game_syms.txt \
-T ../../config/saturn/$*_user_syms.txt \
$*.o
$(SATURN_BUILD_DIR)/lib/%.o: $(SRC_DIR)/saturn/lib/%.c $(CC1_SATURN)
mkdir -p $(dir $@)
cd $(SATURN_BUILD_DIR) && $(DOSEMU) "GCC.EXE -c -I./ -O0 -m2 -fsigned-char lib/$*.c -o lib/$*.o"
$(SATURN_BUILD_DIR)/%.o: $(SRC_DIR)/saturn/%.c $(CC1_SATURN)
mkdir -p $(dir $@)
cd $(SATURN_BUILD_DIR) && $(DOSEMU) "GCC.EXE -c -I./ -O2 -m2 -fsigned-char $*.c -o $*.o"
$(CC1_SATURN): $(SATURN_TOOLCHAIN)
mkdir -p $(dir $@)
cp -r $(SATURN_TOOLCHAIN)/* $(SATURN_BUILD_DIR)
cp ./src/saturn/macro.inc $(SATURN_BUILD_DIR)
cp -r ./src/saturn/*.c $(SATURN_BUILD_DIR)
cp -r ./src/saturn/*.h $(SATURN_BUILD_DIR)
cp -r ./src/saturn/lib $(SATURN_BUILD_DIR)/lib
cp -r ./include/saturn $(SATURN_BUILD_DIR)/saturn
mkdir -p $(SATURN_BUILD_DIR)/asm/saturn/
cp -r ./asm/saturn/game $(SATURN_BUILD_DIR)/asm/saturn/game
cp -r ./asm/saturn/t_bat $(SATURN_BUILD_DIR)/asm/saturn/t_bat
cp -r ./asm/saturn/zero $(SATURN_BUILD_DIR)/asm/saturn/zero
cp -r ./asm/saturn/stage_02 $(SATURN_BUILD_DIR)/asm/saturn/stage_02
cp -r ./asm/saturn/warp $(SATURN_BUILD_DIR)/asm/saturn/warp
cp -r ./asm/saturn/alucard $(SATURN_BUILD_DIR)/asm/saturn/alucard
touch $(CC1_SATURN)
$(SATURN_SPLITTER_APP):
git submodule init $(SATURN_SPLITTER_DIR)
git submodule update $(SATURN_SPLITTER_DIR)
cd $(SATURN_SPLITTER_DIR)/rust-dis && cargo build --release
cd $(SATURN_SPLITTER_DIR)/adpcm-extract && cargo build --release
assets/saturn/SD/%.wav: disks/saturn/SD/%.PCM $(SATURN_SPLITTER_APP)
mkdir -p $(ASSET_DIR)/saturn/SD
$(SATURN_ADPCM_EXTRACT_APP) $< $@