mirror of
https://github.com/ethteck/kh1.git
synced 2024-11-30 08:50:45 +00:00
210 lines
5.3 KiB
Python
Executable File
210 lines
5.3 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
import argparse
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Dict, List, Set, Union
|
|
|
|
import ninja_syntax
|
|
|
|
ROOT = Path(__file__).parent.resolve()
|
|
TOOLS_DIR = ROOT / "tools"
|
|
SPLAT_DIR = TOOLS_DIR / "splat"
|
|
|
|
sys.path.append(str(SPLAT_DIR))
|
|
|
|
import segtypes.common.asm
|
|
import segtypes.common.bin
|
|
import segtypes.common.c
|
|
import segtypes.common.databin
|
|
import segtypes.common.bss
|
|
import segtypes.common.data
|
|
from segtypes.linker_entry import LinkerEntry
|
|
import split
|
|
|
|
YAML_FILE = "kh.jp.yaml"
|
|
BASENAME = "SLPS_251.05"
|
|
LD_PATH = f"{BASENAME}.ld"
|
|
ELF_PATH = f"build/{BASENAME}"
|
|
MAP_PATH = f"build/{BASENAME}.map"
|
|
PRE_ELF_PATH = f"build/{BASENAME}.elf"
|
|
|
|
COMMON_INCLUDES = "-Iinclude -isystem include/sdk/ee -isystem include/gcc"
|
|
COMPILER_DIR = f"{TOOLS_DIR}/cc/ee-gcc2.96/bin"
|
|
COMPILE_CMD = (
|
|
f"{COMPILER_DIR}/ee-gcc -c -B {COMPILER_DIR}/ee- {COMMON_INCLUDES} -O2 -G0 -g"
|
|
)
|
|
|
|
WIBO_VER = "0.6.4"
|
|
|
|
|
|
def exec_shell(command: List[str]) -> str:
|
|
ret = subprocess.run(
|
|
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
|
)
|
|
return ret.stdout
|
|
|
|
|
|
def clean():
|
|
if os.path.exists(".splache"):
|
|
os.remove(".splache")
|
|
shutil.rmtree("asm", ignore_errors=True)
|
|
shutil.rmtree("assets", ignore_errors=True)
|
|
shutil.rmtree("build", ignore_errors=True)
|
|
|
|
|
|
def write_permuter_settings():
|
|
with open("permuter_settings.toml", "w") as f:
|
|
f.write(
|
|
f"""compiler_command = "{COMPILE_CMD} -D__GNUC__"
|
|
assembler_command = "mips-linux-gnu-as -march=r5900 -mabi=eabi -Iinclude"
|
|
compiler_type = "gcc"
|
|
|
|
[preserve_macros]
|
|
|
|
[decompme.compilers]
|
|
"tools/build/cc/gcc/gcc" = "ee-gcc2.96"
|
|
"""
|
|
)
|
|
|
|
|
|
def build_stuff(linker_entries: List[LinkerEntry]):
|
|
built_objects: Set[Path] = set()
|
|
|
|
def build(
|
|
object_paths: Union[Path, List[Path]],
|
|
src_paths: List[Path],
|
|
task: str,
|
|
variables: Dict[str, str] = {},
|
|
implicit_outputs: List[str] = [],
|
|
):
|
|
if not isinstance(object_paths, list):
|
|
object_paths = [object_paths]
|
|
|
|
object_strs = [str(obj) for obj in object_paths]
|
|
|
|
for object_path in object_paths:
|
|
if object_path.suffix == ".o":
|
|
built_objects.add(object_path)
|
|
ninja.build(
|
|
outputs=object_strs,
|
|
rule=task,
|
|
inputs=[str(s) for s in src_paths],
|
|
variables=variables,
|
|
implicit_outputs=implicit_outputs,
|
|
)
|
|
|
|
ninja = ninja_syntax.Writer(open(str(ROOT / "build.ninja"), "w"), width=9999)
|
|
|
|
# Rules
|
|
cross = "mips-linux-gnu-"
|
|
|
|
ld_args = f"-EL -T undefined_syms.txt -T undefined_syms_auto.txt -T undefined_funcs_auto.txt -Map $mapfile -T $in -o $out"
|
|
|
|
ninja.rule(
|
|
"as",
|
|
description="as $in",
|
|
command=f"cpp {COMMON_INCLUDES} $in -o - | {cross}as -no-pad-sections -EL -march=5900 -mabi=eabi -Iinclude -o $out",
|
|
)
|
|
|
|
ninja.rule(
|
|
"cc",
|
|
description="cc $in",
|
|
command=f"{COMPILE_CMD} $in -o $out && {cross}strip $out -N dummy-symbol-name",
|
|
)
|
|
|
|
ninja.rule(
|
|
"ld",
|
|
description="link $out",
|
|
command=f"{cross}ld {ld_args}",
|
|
)
|
|
|
|
ninja.rule(
|
|
"sha1sum",
|
|
description="sha1sum $in",
|
|
command="sha1sum -c $in && touch $out",
|
|
)
|
|
|
|
ninja.rule(
|
|
"elf",
|
|
description="elf $out",
|
|
command=f"{cross}objcopy $in $out -O binary",
|
|
)
|
|
|
|
for entry in linker_entries:
|
|
seg = entry.segment
|
|
|
|
if seg.type[0] == ".":
|
|
continue
|
|
|
|
if entry.object_path is None:
|
|
continue
|
|
|
|
if isinstance(seg, segtypes.common.asm.CommonSegAsm) or isinstance(
|
|
seg, segtypes.common.data.CommonSegData
|
|
):
|
|
build(entry.object_path, entry.src_paths, "as")
|
|
elif isinstance(seg, segtypes.common.c.CommonSegC):
|
|
build(entry.object_path, entry.src_paths, "cc")
|
|
elif isinstance(seg, segtypes.common.databin.CommonSegDatabin):
|
|
build(entry.object_path, entry.src_paths, "as")
|
|
else:
|
|
print(f"ERROR: Unsupported build segment type {seg.type}")
|
|
sys.exit(1)
|
|
|
|
ninja.build(
|
|
PRE_ELF_PATH,
|
|
"ld",
|
|
LD_PATH,
|
|
implicit=[str(obj) for obj in built_objects],
|
|
variables={"mapfile": MAP_PATH},
|
|
)
|
|
|
|
ninja.build(
|
|
ELF_PATH,
|
|
"elf",
|
|
PRE_ELF_PATH,
|
|
)
|
|
|
|
ninja.build(
|
|
ELF_PATH + ".ok",
|
|
"sha1sum",
|
|
"checksum.sha1",
|
|
implicit=[ELF_PATH],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Configure the project")
|
|
parser.add_argument(
|
|
"-c",
|
|
"--clean",
|
|
help="Clean extraction and build artifacts",
|
|
action="store_true",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
exec_shell(["wibo"])
|
|
except FileNotFoundError:
|
|
print("ERROR: wibo does not appear to be accessible")
|
|
print("To install it, please download it and put it in your PATH:")
|
|
print(
|
|
f" wget https://github.com/decompals/wibo/releases/download/{WIBO_VER}/wibo && chmod +x wibo && sudo mv wibo /usr/bin/"
|
|
)
|
|
sys.exit(1)
|
|
|
|
if args.clean:
|
|
clean()
|
|
|
|
split.main([YAML_FILE], modes="all", verbose=False)
|
|
|
|
linker_entries = split.linker_writer.entries
|
|
|
|
build_stuff(linker_entries)
|
|
|
|
write_permuter_settings()
|