papermario/configure.py

344 lines
14 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2021-01-14 04:56:02 +01:00
import re
2021-01-14 11:25:55 +01:00
import os, sys
2021-01-14 04:56:02 +01:00
from glob import glob
import ninja_syntax
from argparse import ArgumentParser
import asyncio
from subprocess import PIPE
sys.path.append(os.path.dirname(__file__) + "/tools/n64splat")
import split
2021-01-14 04:56:02 +01:00
2021-01-14 05:16:42 +01:00
INCLUDE_ASM_RE = re.compile(r"_INCLUDE_ASM\([^,]+, ([^,]+), ([^,)]+)") # note _ prefix
2021-01-14 04:56:02 +01:00
CPPFLAGS = "-Iinclude -Isrc -D _LANGUAGE_C -D _FINALROM -ffreestanding -DF3DEX_GBI_2 -D_MIPS_SZLONG=32"
2021-01-14 12:57:39 +01:00
NPC_SPRITES = "world_goombario world_kooper world_bombette world_parakarry world_bow world_watt world_sushie world_lakilester battle_goombario battle_kooper battle_bombette battle_parakarry battle_bow battle_watt battle_sushie battle_lakilester kooper_without_shell world_eldstar world_mamar world_skolar world_muskular world_misstar world_klevar world_kalmar battle_eldstar battle_mamar battle_skolar battle_muskular battle_misstar battle_klevar battle_kalmar twink jr_troopa spiked_jr_troopa spiked_para_jr_troopa mage_jr_troopa para_jr_troopa goomba spiked_goomba paragoomba koopa_troopa para_troopa fuzzy bob_omb bullet_bill bill_blaster monty_mole cleft pokey battle_bandit buzzy_beetle swooper stone_chomp putrid_piranha piranha_plant sentinel world_clubba battle_clubba shy_guy groove_guy sky_guy pyro_guy spy_guy medi_guy fuzzipede jungle_guy heart_plant hurt_plant m_bush bubble kent_c_koopa dayzee lakitu spiny bzzap ruff_puff spike_top duplighost albino_dino blooper baby_blooper gulpit dry_bones thrown_bone bony_beetle magikoopa flying_magikoopa world_koopatrol koopatrol hammer_bros bush_basic bush_blocky bush_dry bush_leafy bush_matted world_kammy battle_kammy goomba_bros goomba_king spiky_goomnut dark_toad koopa_bros buzzar tutankoopa chain_chomp world_tubba battle_tubba tubbas_heart big_lantern_ghost shy_squad_guy marshal_guy stilt_guy stilt_guy_unfold shy_stack_guy shy_stack_unfold shy_stack_damage shy_stack_rock general_guy general_guy_bomb tank_guy lava_piranha_head petit_piranha lava_bud huff_n_puff tuff_puff monstar crystal_king world_bowser battle_bowser luigi toad three_sisters vanna_t toad_kid toad_guard harry_t toad_minister postmaster conductor_toad train_station_toad fishmael artist_toad koopa koopa_without_shell world_bob_omb whacka dryite mouser boo yoshi yoshi_kid raven bubulb penguin shiver_toad world_bandit goompa goombaria gooma goompapa goomama the_master chan lee merlon chet_rippo rowf minh_t russ_t tayce_t fice_t bartender chanterelle rip_cheato chuck_quizmo merluvlee merlar merlow star_kid kolorado_wife koopa_koot kolorado battle_kolorado archeologist nomadimouse world_merlee battle_merlee disguised_moustafa moustafa oaklie bootler yakkey gourmet_guy village_leader leaders_friend rafael_raven tolielup gate_flower petunia posie lily rosie sun lakilulu ninji mayor_penguin mayor_penguin_wife penguin_patrol herringway merle star_rod fire coin parade_peach parade_koopas parade_burnt_bowser parade_luigi parade_partners parade_yoshis parade_kolorados parade_chicks parade_ice_show parade_toads parade_batons parade_drums parade_flags parade_horns parade_tubba_balloon parade_wizards parade_mario parade_shy_guys parade_twink leaf".split(" ")
2021-01-14 04:56:02 +01:00
def obj(path: str):
2021-01-14 11:25:55 +01:00
if not path.startswith("$builddir/"):
path = "$builddir/" + path
return path + ".o"
2021-01-14 12:13:33 +01:00
def read_splat(splat_config: str):
2021-01-14 11:25:55 +01:00
import argparse
import yaml
# Load config
with open(splat_config) as f:
config = yaml.safe_load(f.read())
options = config.get("options")
assert options.get("ld_o_replace_extension", True) == False
# Initialize segments
all_segments = split.initialize_segments(options, splat_config, config["segments"])
2021-01-14 11:25:55 +01:00
objects = set()
2021-01-14 12:13:33 +01:00
segments = {}
2021-01-14 11:25:55 +01:00
for segment in all_segments:
for subdir, path, obj_type, start in segment.get_ld_files():
2021-01-14 12:13:33 +01:00
path = subdir + "/" + path
2021-01-14 11:25:55 +01:00
2021-01-14 12:13:33 +01:00
objects.add(path)
segments[path] = segment
2021-01-14 11:25:55 +01:00
2021-01-14 12:13:33 +01:00
# note: `objects` lacks .o extensions
return objects, segments
2021-01-14 04:56:02 +01:00
async def shell(cmd: str):
async with task_sem:
proc = await asyncio.create_subprocess_shell(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = await proc.communicate()
assert proc.returncode == 0, f"{cmd} failed: {stderr}"
2021-01-14 05:16:42 +01:00
return stdout.decode("utf-8"), stderr.decode("utf-8")
2021-01-14 04:56:02 +01:00
2021-01-14 11:25:55 +01:00
async def task(coro):
global num_tasks, num_tasks_done
await coro
num_tasks_done += 1
print(f"\r{(num_tasks_done / num_tasks) * 100:.0f}%", end="")
2021-01-14 12:57:39 +01:00
async def build_c_file(c_file: str, generated_headers):
2021-01-14 05:16:42 +01:00
# preprocess c_file, but simply put an _ in front of INCLUDE_ASM and SCRIPT
stdout, stderr = await shell(f"{cpp} {CPPFLAGS} '-DINCLUDE_ASM(...)=_INCLUDE_ASM(__VA_ARGS__)' '-DSCRIPT(...)=_SCRIPT(__VA_ARGS__)' {c_file} -o -")
2021-01-14 04:56:02 +01:00
2021-01-14 05:16:42 +01:00
# search for macro usage (note _ prefix)
uses_dsl = "_SCRIPT(" in stdout
2021-01-14 04:56:02 +01:00
s_deps = []
for line in stdout.splitlines():
2021-01-14 05:16:42 +01:00
if line.startswith("_INCLUDE_ASM"):
2021-01-14 04:56:02 +01:00
match = INCLUDE_ASM_RE.match(line)
if match:
s_deps.append("asm/nonmatchings/" + eval(match[1]) + "/" + match[2] + ".s")
# add build task to ninja
2021-01-14 12:57:39 +01:00
n.build(obj(c_file), "cc_dsl" if uses_dsl else "cc", c_file, implicit=s_deps, order_only=generated_headers)
2021-01-14 04:56:02 +01:00
2021-01-14 11:25:55 +01:00
def build_yay0_file(bin_file: str):
yay0_file = f"$builddir/{os.path.splitext(bin_file)[0]}.Yay0"
n.build(yay0_file, "yay0compress", bin_file, implicit=["tools/Yay0compress"])
build_bin_object(yay0_file)
2021-01-14 04:56:02 +01:00
2021-01-14 11:25:55 +01:00
def build_bin_object(bin_file: str):
n.build(obj(bin_file), "bin", bin_file)
2021-01-14 04:56:02 +01:00
2021-01-14 12:13:33 +01:00
def build_image(f: str, segment):
path, img_type, png = f.rsplit(".", 2)
out = "$builddir/" + path + "." + img_type + ".png"
flags = ""
if img_type != "palette":
if segment.flip_horizontal:
flags += "--flip-x"
if segment.flip_vertical:
flags += "--flip-y"
2021-01-14 13:25:28 +01:00
n.build(out, "img", path + ".png", implicit="tools/convert_image.py", variables={
2021-01-14 12:13:33 +01:00
"img_type": img_type,
"img_flags": flags,
})
build_bin_object(out)
2021-01-14 11:49:14 +01:00
2021-01-14 04:56:02 +01:00
async def main():
global n, cpp, task_sem, num_tasks, num_tasks_done
parser = ArgumentParser(description="Generates build.ninja")
parser.add_argument("--cpp", help="C preprocessor command")
args = parser.parse_args()
# on macOS, /usr/bin/cpp defaults to clang rather than gcc (but we need gcc's)
if args.cpp is None and sys.platform == "darwin" and "Free Software Foundation" not in await shell("cpp --version")[0]:
print("error: use 'brew' to obtain GNU cpp and run again with '--cpp cpp-10'")
exit(1)
cpp = args.cpp or "cpp"
2021-01-14 11:49:14 +01:00
task_sem = asyncio.Semaphore(8)
2021-01-14 04:56:02 +01:00
2021-01-14 13:49:13 +01:00
# compile n64splat dependencies
await shell("make -C tools/n64splat")
# split assets
split.main(
"baserom.z64",
"tools/splat.yaml",
".",
[ "ld", "bin", "Yay0", "PaperMarioMapFS", "PaperMarioMessages", "img", "PaperMarioNpcSprites" ],
False,
False,
)
# generate build.ninja
2021-01-14 04:56:02 +01:00
n = ninja_syntax.Writer(open("build.ninja", "w"), width=120)
n.variable("builddir", "build")
2021-01-14 13:05:55 +01:00
n.variable("target", "papermario")
2021-01-14 04:56:02 +01:00
n.variable("cross", "mips-linux-gnu-")
n.variable("python", sys.executable)
n.variable("os", "mac" if sys.platform == "darwin" else "linux")
n.variable("iconv", "tools/iconv.py UTF-8 SHIFT-JIS" if sys.platform == "darwin" else "iconv --from UTF-8 --to SHIFT-JIS")
n.variable("cppflags", f"{CPPFLAGS} -Wcomment")
n.variable("cflags", "-O2 -quiet -G 0 -mcpu=vr4300 -mfix4300 -mips3 -mgp32 -mfp32 -Wuninitialized -Wshadow")
n.newline()
n.rule("cc",
command=f"{cpp} $cppflags $in -o - | $iconv | tools/$os/cc1 $cflags -o - | tools/$os/mips-nintendo-nu64-as -EB -G 0 - -o $out",
description="cc $in",
depfile="$out.d",
deps="gcc")
n.rule("cc_dsl",
command=f"{cpp} $cppflags $in -o - | $python tools/compile_dsl_macros.py | $iconv | tools/$os/cc1 $cflags -o - | tools/$os/mips-nintendo-nu64-as -EB -G 0 - -o $out",
description="cc (with dsl) $in",
depfile="$out.d",
deps="gcc")
n.newline()
2021-01-14 13:05:55 +01:00
n.rule("cpp",
command=f"{cpp} -P -DBUILD_DIR=$builddir $in -o $out",
description="cc (with dsl) $in",
depfile="$out.d",
deps="gcc")
n.newline()
2021-01-14 11:25:55 +01:00
n.rule("yay0compress",
command=f"tools/Yay0compress $in $out",
description="compress $in")
n.newline()
n.rule("bin",
command="${cross}ld -r -b binary $in -o $out",
description="bin $in")
n.newline()
2021-01-14 11:49:14 +01:00
n.rule("as",
command="${cross}as -EB -march=vr4300 -mtune=vr4300 -Iinclude $in -o $out",
description="assemble $in")
n.newline()
2021-01-14 11:25:55 +01:00
2021-01-14 12:57:39 +01:00
# $img_type, $img_flags
2021-01-14 11:59:55 +01:00
n.rule("img",
command="$python tools/convert_image.py $img_type $in $out $img_flags",
description="image $in")
n.newline()
2021-01-14 13:25:28 +01:00
# $sprite_id, $sprite_dir, $sprite_name
2021-01-14 12:57:39 +01:00
n.rule("sprite_animations_h",
command="$python tools/gen_sprite_animations_h.py $out $sprite_dir $sprite_id",
2021-01-14 13:25:28 +01:00
description="sprite_animations_h $sprite_name ($sprite_id)")
n.rule("npc_sprite",
command="$python tools/compile_npc_sprite.py $out $sprite_dir",
description="npc_sprite $sprite_name ($sprite_id)")
n.rule("npc_sprites",
command="$python tools/compile_npc_sprites.py $out $in",
description="package npc sprites")
2021-01-14 13:05:55 +01:00
n.newline()
n.rule("ld_addrs_h",
command="grep -E \"[^\. ]+ =\" $in -o | sed 's/^/extern void* /; s/ =/;/' > $out",
description="ld_addrs_h $in")
2021-01-14 12:57:39 +01:00
n.newline()
# $msg_combine_headers
n.rule("msg_combine",
command="$python tools/msg/combine.py $out $in --headers $msg_combine_headers",
description="combine messages")
n.rule("msg",
command="$python tools/msg/parse_compile.py $in $out",
description="msg $in")
n.newline()
objects, segments = read_splat("tools/splat.yaml") # no .o extensions!
2021-01-14 11:25:55 +01:00
c_files = (f for f in objects if f.endswith(".c")) # glob("src/**/*.c", recursive=True)
2021-01-14 13:05:55 +01:00
n.comment("target")
n.build("$builddir/$target.ld", "cpp", "$target.ld")
# TODO ...
n.newline()
2021-01-14 11:25:55 +01:00
2021-01-14 12:57:39 +01:00
# generated headers
n.comment("generated headers")
generated_headers = []
def add_generated_header(h: str):
generated_headers.append(h)
2021-01-14 12:57:39 +01:00
if not os.path.exists(h):
# mkdir -p
os.makedirs(os.path.dirname(h), exist_ok=True)
# touch it so cpp doesn't complain if its #included
open(h, "w").close()
# mark it as really old so ninja builds it
os.utime(h, (0, 0))
2021-01-14 13:05:55 +01:00
return h
n.build(add_generated_header("include/ld_addrs.h"), "ld_addrs_h", "$builddir/$target.ld")
# messages
msg_files = glob("src/**/*.msg", recursive=True) + glob("msg/**/*.msg", recursive=True)
for msg_file in msg_files:
n.build(
f"$builddir/{msg_file}.bin",
"msg",
msg_file,
implicit="tools/msg/parse_compile.py",
)
n.build(
"$builddir/msg.bin",
"msg_combine",
[f"$builddir/{msg_file}.bin" for msg_file in msg_files],
implicit="tools/msg/combine.py",
implicit_outputs=[add_generated_header(f"{msg_file}.h") for msg_file in msg_files],
variables={ "msg_combine_headers": [f"{msg_file}.h" for msg_file in msg_files] }
)
n.build("$builddir/msg.o", "bin", "$builddir/msg.bin")
# sprites
2021-01-14 13:25:28 +01:00
npc_sprite_yay0s = []
2021-01-14 12:57:39 +01:00
for sprite_id, sprite_name in enumerate(NPC_SPRITES, 1):
2021-01-14 13:25:28 +01:00
sources = glob(f"sprite/npc/{sprite_name}/**/*.*", recursive=True)
variables = {
"sprite_name": sprite_name,
"sprite_dir": f"sprite/npc/{sprite_name}",
"sprite_id": sprite_id,
}
# generated header
2021-01-14 13:05:55 +01:00
n.build(
add_generated_header(f"include/sprite/npc/{sprite_name}.h"),
"sprite_animations_h",
2021-01-14 13:25:28 +01:00
implicit=sources + ["tools/gen_sprite_animations_h.py"],
variables=variables,
2021-01-14 13:05:55 +01:00
)
2021-01-14 13:25:28 +01:00
# sprite bin/yay0
n.build(
f"$builddir/sprite/npc/{sprite_name}",
"npc_sprite",
implicit=sources + ["tools/compile_npc_sprite.py"],
variables=variables,
)
yay0 = f"$builddir/sprite/npc/{sprite_name}.Yay0"
npc_sprite_yay0s.append(yay0)
n.build(
yay0,
"yay0compress",
f"$builddir/sprite/npc/{sprite_name}",
implicit=["tools/Yay0compress"],
)
2021-01-14 12:57:39 +01:00
n.newline()
# fast tasks
n.comment("data")
2021-01-14 11:49:14 +01:00
for f in objects:
2021-01-14 12:13:33 +01:00
segment = segments[f]
2021-01-14 11:49:14 +01:00
if f.endswith(".c"):
continue # these are handled later
elif f.endswith(".Yay0"):
build_yay0_file(os.path.splitext(f)[0] + ".bin")
elif f.endswith(".bin"):
build_bin_object(f)
elif f.endswith(".data"):
n.build(obj(f), "as", "asm/" + f + ".s")
elif f.endswith(".rodata"):
n.build(obj(f), "as", "asm/" + f[2:] + ".s")
elif f.endswith(".s"):
n.build(obj(f), "as", f)
2021-01-14 11:59:55 +01:00
elif f.endswith(".png"):
2021-01-14 12:13:33 +01:00
build_image(f, segment)
2021-01-14 13:25:28 +01:00
elif f == "sprite/npc":
# combine sprites
n.build(f"$builddir/{f}.bin", "npc_sprites", npc_sprite_yay0s, implicit="tools/compile_npc_sprites.py")
2021-01-14 13:25:28 +01:00
n.build(obj(f), "bin", f"$builddir/{f}.bin")
elif f == "/msg":
continue # done already above
2021-01-14 11:49:14 +01:00
else:
print("warning: dont know what to do with object " + f)
2021-01-14 11:25:55 +01:00
n.newline()
2021-01-14 12:57:39 +01:00
# slow tasks generated concurrently
2021-01-14 11:25:55 +01:00
n.comment("c")
2021-01-14 12:57:39 +01:00
tasks = [task(build_c_file(f, generated_headers)) for f in c_files]
2021-01-14 04:56:02 +01:00
num_tasks = len(tasks)
num_tasks_done = 0
await asyncio.gather(*tasks)
2021-01-14 11:25:55 +01:00
print("")
2021-01-14 13:49:13 +01:00
n.newline()
n.rule("cc_modern_exe", command="cc $in -O3 -o $out")
n.build("tools/Yay0compress", "cc_modern_exe", "tools/Yay0compress.c")
2021-01-14 04:56:02 +01:00
if __name__ == "__main__":
asyncio.run(main())