2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# decomp-toolkit project generator
|
|
|
|
# Generates build.ninja and objdiff.json.
|
|
|
|
#
|
|
|
|
# This generator is intentionally project-agnostic
|
|
|
|
# and shared between multiple projects. Any configuration
|
|
|
|
# specific to a project should be added to `configure.py`.
|
|
|
|
#
|
|
|
|
# If changes are made, please submit a PR to
|
|
|
|
# https://github.com/encounter/dtk-template
|
|
|
|
###
|
|
|
|
|
|
|
|
import io
|
|
|
|
import json
|
2024-02-29 18:21:44 +00:00
|
|
|
import math
|
2023-10-05 03:26:02 +00:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2024-09-04 14:55:19 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Set, Tuple, Union, cast
|
2024-02-29 18:21:44 +00:00
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
from . import ninja_syntax
|
2024-03-04 15:17:51 +00:00
|
|
|
from .ninja_syntax import serialize_path
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
if sys.platform == "cygwin":
|
|
|
|
sys.exit(
|
|
|
|
f"Cygwin/MSYS2 is not supported."
|
|
|
|
f"\nPlease use native Windows Python instead."
|
|
|
|
f"\n(Current path: {sys.executable})"
|
|
|
|
)
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
Library = Dict[str, Any]
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
class Object:
|
|
|
|
def __init__(self, completed: bool, name: str, **options: Any) -> None:
|
|
|
|
self.name = name
|
|
|
|
self.completed = completed
|
|
|
|
self.options: Dict[str, Any] = {
|
2024-09-04 14:55:19 +00:00
|
|
|
"add_to_all": None,
|
2024-03-04 05:47:05 +00:00
|
|
|
"asflags": None,
|
2024-09-04 14:55:19 +00:00
|
|
|
"asm_dir": None,
|
2024-01-24 06:21:46 +00:00
|
|
|
"cflags": None,
|
2024-09-04 14:55:19 +00:00
|
|
|
"extra_asflags": None,
|
2024-01-24 06:21:46 +00:00
|
|
|
"extra_cflags": None,
|
2024-09-04 14:55:19 +00:00
|
|
|
"host": None,
|
|
|
|
"lib": None,
|
2024-01-24 06:21:46 +00:00
|
|
|
"mw_version": None,
|
2024-09-04 14:55:19 +00:00
|
|
|
"progress_category": None,
|
2024-03-04 05:47:05 +00:00
|
|
|
"shift_jis": None,
|
2024-01-24 06:21:46 +00:00
|
|
|
"source": name,
|
2024-09-04 14:55:19 +00:00
|
|
|
"src_dir": None,
|
2024-01-24 06:21:46 +00:00
|
|
|
}
|
|
|
|
self.options.update(options)
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
# Internal
|
|
|
|
self.src_path: Optional[Path] = None
|
|
|
|
self.asm_path: Optional[Path] = None
|
|
|
|
self.src_obj_path: Optional[Path] = None
|
|
|
|
self.asm_obj_path: Optional[Path] = None
|
|
|
|
self.host_obj_path: Optional[Path] = None
|
|
|
|
self.ctx_path: Optional[Path] = None
|
|
|
|
|
|
|
|
def resolve(self, config: "ProjectConfig", lib: Library) -> "Object":
|
|
|
|
# Use object options, then library options
|
|
|
|
obj = Object(self.completed, self.name, **lib)
|
|
|
|
for key, value in self.options.items():
|
|
|
|
if value is not None or key not in obj.options:
|
|
|
|
obj.options[key] = value
|
|
|
|
|
|
|
|
# Use default options from config
|
|
|
|
def set_default(key: str, value: Any) -> None:
|
|
|
|
if obj.options[key] is None:
|
|
|
|
obj.options[key] = value
|
|
|
|
|
|
|
|
set_default("add_to_all", True)
|
|
|
|
set_default("asflags", config.asflags)
|
|
|
|
set_default("asm_dir", config.asm_dir)
|
|
|
|
set_default("host", False)
|
|
|
|
set_default("mw_version", config.linker_version)
|
|
|
|
set_default("shift_jis", config.shift_jis)
|
|
|
|
set_default("src_dir", config.src_dir)
|
|
|
|
|
|
|
|
# Resolve paths
|
|
|
|
build_dir = config.out_path()
|
|
|
|
obj.src_path = Path(obj.options["src_dir"]) / obj.options["source"]
|
|
|
|
if obj.options["asm_dir"] is not None:
|
|
|
|
obj.asm_path = (
|
|
|
|
Path(obj.options["asm_dir"]) / obj.options["source"]
|
|
|
|
).with_suffix(".s")
|
|
|
|
base_name = Path(self.name).with_suffix("")
|
|
|
|
obj.src_obj_path = build_dir / "src" / f"{base_name}.o"
|
|
|
|
obj.asm_obj_path = build_dir / "mod" / f"{base_name}.o"
|
|
|
|
obj.host_obj_path = build_dir / "host" / f"{base_name}.o"
|
|
|
|
obj.ctx_path = build_dir / "src" / f"{base_name}.ctx"
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
class ProgressCategory:
|
|
|
|
def __init__(self, id: str, name: str) -> None:
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
class ProjectConfig:
|
2024-01-24 06:21:46 +00:00
|
|
|
def __init__(self) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
# Paths
|
2024-03-04 15:17:51 +00:00
|
|
|
self.build_dir: Path = Path("build") # Output build files
|
|
|
|
self.src_dir: Path = Path("src") # C/C++/asm source files
|
|
|
|
self.tools_dir: Path = Path("tools") # Python scripts
|
2024-03-05 05:13:39 +00:00
|
|
|
self.asm_dir: Optional[Path] = Path(
|
|
|
|
"asm"
|
|
|
|
) # Override incomplete objects (for modding)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Tooling
|
2024-03-04 05:47:05 +00:00
|
|
|
self.binutils_tag: Optional[str] = None # Git tag
|
2024-03-04 15:17:51 +00:00
|
|
|
self.binutils_path: Optional[Path] = None # If None, download
|
2024-01-24 06:21:46 +00:00
|
|
|
self.dtk_tag: Optional[str] = None # Git tag
|
2024-03-05 01:13:10 +00:00
|
|
|
self.dtk_path: Optional[Path] = None # If None, download
|
2024-01-24 06:21:46 +00:00
|
|
|
self.compilers_tag: Optional[str] = None # 1
|
2024-03-04 15:17:51 +00:00
|
|
|
self.compilers_path: Optional[Path] = None # If None, download
|
2024-01-24 06:21:46 +00:00
|
|
|
self.wibo_tag: Optional[str] = None # Git tag
|
2024-03-04 15:17:51 +00:00
|
|
|
self.wrapper: Optional[Path] = None # If None, download wibo on Linux
|
2024-01-24 06:21:46 +00:00
|
|
|
self.sjiswrap_tag: Optional[str] = None # Git tag
|
2024-03-04 15:17:51 +00:00
|
|
|
self.sjiswrap_path: Optional[Path] = None # If None, download
|
2024-08-13 00:09:10 +00:00
|
|
|
self.objdiff_tag: Optional[str] = None # Git tag
|
|
|
|
self.objdiff_path: Optional[Path] = None # If None, download
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Project config
|
2024-05-18 20:50:32 +00:00
|
|
|
self.non_matching: bool = False
|
2024-01-24 06:21:46 +00:00
|
|
|
self.build_rels: bool = True # Build REL files
|
2024-03-04 15:17:51 +00:00
|
|
|
self.check_sha_path: Optional[Path] = None # Path to version.sha1
|
|
|
|
self.config_path: Optional[Path] = None # Path to config.yml
|
2024-01-24 06:21:46 +00:00
|
|
|
self.generate_map: bool = False # Generate map file(s)
|
2024-03-04 05:47:05 +00:00
|
|
|
self.asflags: Optional[List[str]] = None # Assembler flags
|
2024-01-24 06:21:46 +00:00
|
|
|
self.ldflags: Optional[List[str]] = None # Linker flags
|
2024-09-04 14:55:19 +00:00
|
|
|
self.libs: Optional[List[Library]] = None # List of libraries
|
2024-01-24 06:21:46 +00:00
|
|
|
self.linker_version: Optional[str] = None # mwld version
|
|
|
|
self.version: Optional[str] = None # Version name
|
|
|
|
self.warn_missing_config: bool = False # Warn on missing unit configuration
|
|
|
|
self.warn_missing_source: bool = False # Warn on missing source file
|
|
|
|
self.rel_strip_partial: bool = True # Generate PLFs with -strip_partial
|
2024-08-07 03:08:26 +00:00
|
|
|
self.rel_empty_file: Optional[str] = (
|
|
|
|
None # Object name for generating empty RELs
|
|
|
|
)
|
2024-03-04 05:47:05 +00:00
|
|
|
self.shift_jis = (
|
|
|
|
True # Convert source files from UTF-8 to Shift JIS automatically
|
|
|
|
)
|
2024-05-18 20:50:32 +00:00
|
|
|
self.reconfig_deps: Optional[List[Path]] = (
|
|
|
|
None # Additional re-configuration dependency files
|
|
|
|
)
|
2024-08-07 03:08:26 +00:00
|
|
|
self.custom_build_rules: Optional[List[Dict[str, Any]]] = (
|
|
|
|
None # Custom ninja build rules
|
|
|
|
)
|
|
|
|
self.custom_build_steps: Optional[Dict[str, List[Dict[str, Any]]]] = (
|
|
|
|
None # Custom build steps, types are ["pre-compile", "post-compile", "post-link", "post-build"]
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
# Progress output, progress.json and report.json config
|
2024-01-24 06:21:46 +00:00
|
|
|
self.progress_all: bool = True # Include combined "all" category
|
|
|
|
self.progress_modules: bool = True # Include combined "modules" category
|
|
|
|
self.progress_each_module: bool = (
|
2024-09-04 14:55:19 +00:00
|
|
|
False # Include individual modules, disable for large numbers of modules
|
2024-01-24 06:21:46 +00:00
|
|
|
)
|
2024-09-04 14:55:19 +00:00
|
|
|
self.progress_categories: List[ProgressCategory] = [] # Additional categories
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2023-12-21 01:59:04 +00:00
|
|
|
# Progress fancy printing
|
2024-01-24 06:21:46 +00:00
|
|
|
self.progress_use_fancy: bool = False
|
|
|
|
self.progress_code_fancy_frac: int = 0
|
|
|
|
self.progress_code_fancy_item: str = ""
|
|
|
|
self.progress_data_fancy_frac: int = 0
|
|
|
|
self.progress_data_fancy_item: str = ""
|
2023-12-21 01:59:04 +00:00
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def validate(self) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
required_attrs = [
|
|
|
|
"build_dir",
|
|
|
|
"src_dir",
|
|
|
|
"tools_dir",
|
|
|
|
"check_sha_path",
|
|
|
|
"config_path",
|
|
|
|
"ldflags",
|
|
|
|
"linker_version",
|
|
|
|
"libs",
|
|
|
|
"version",
|
|
|
|
]
|
|
|
|
for attr in required_attrs:
|
|
|
|
if getattr(self, attr) is None:
|
|
|
|
sys.exit(f"ProjectConfig.{attr} missing")
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
# Creates a map of object names to Object instances
|
|
|
|
# Options are fully resolved from the library and object
|
|
|
|
def objects(self) -> Dict[str, Object]:
|
|
|
|
out = {}
|
2024-01-24 06:21:46 +00:00
|
|
|
for lib in self.libs or {}:
|
2024-09-04 14:55:19 +00:00
|
|
|
objects: List[Object] = lib["objects"]
|
|
|
|
for obj in objects:
|
|
|
|
if obj.name in out:
|
|
|
|
sys.exit(f"Duplicate object name {obj.name}")
|
|
|
|
out[obj.name] = obj.resolve(self, lib)
|
|
|
|
return out
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def out_path(self) -> Path:
|
|
|
|
return self.build_dir / str(self.version)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def is_windows() -> bool:
|
2023-10-05 03:26:02 +00:00
|
|
|
return os.name == "nt"
|
|
|
|
|
|
|
|
|
|
|
|
# On Windows, we need this to use && in commands
|
|
|
|
CHAIN = "cmd /c " if is_windows() else ""
|
|
|
|
# Native executable extension
|
|
|
|
EXE = ".exe" if is_windows() else ""
|
|
|
|
|
|
|
|
|
2024-09-18 01:21:54 +00:00
|
|
|
def make_flags_str(flags: Optional[Union[str, List[str]]]) -> str:
|
|
|
|
if flags is None:
|
|
|
|
return ""
|
|
|
|
elif isinstance(flags, list):
|
|
|
|
return " ".join(flags)
|
2024-03-04 05:47:05 +00:00
|
|
|
else:
|
2024-09-18 01:21:54 +00:00
|
|
|
return flags
|
2024-03-04 05:47:05 +00:00
|
|
|
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
# Load decomp-toolkit generated config.json
|
2024-01-24 06:21:46 +00:00
|
|
|
def load_build_config(
|
|
|
|
config: ProjectConfig, build_config_path: Path
|
|
|
|
) -> Optional[Dict[str, Any]]:
|
2023-10-05 03:26:02 +00:00
|
|
|
if not build_config_path.is_file():
|
|
|
|
return None
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def versiontuple(v: str) -> Tuple[int, ...]:
|
2023-10-05 03:26:02 +00:00
|
|
|
return tuple(map(int, (v.split("."))))
|
|
|
|
|
|
|
|
f = open(build_config_path, "r", encoding="utf-8")
|
2024-01-24 06:21:46 +00:00
|
|
|
build_config: Dict[str, Any] = json.load(f)
|
2023-10-05 03:26:02 +00:00
|
|
|
config_version = build_config.get("version")
|
2024-09-04 14:55:19 +00:00
|
|
|
if config_version is None:
|
2024-09-18 01:21:17 +00:00
|
|
|
print("Invalid config.json, regenerating...")
|
2023-10-05 03:26:02 +00:00
|
|
|
f.close()
|
|
|
|
os.remove(build_config_path)
|
|
|
|
return None
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
dtk_version = str(config.dtk_tag)[1:] # Strip v
|
2023-10-05 03:26:02 +00:00
|
|
|
if versiontuple(config_version) < versiontuple(dtk_version):
|
2024-09-18 01:21:17 +00:00
|
|
|
print("Outdated config.json, regenerating...")
|
2023-10-05 03:26:02 +00:00
|
|
|
f.close()
|
|
|
|
os.remove(build_config_path)
|
|
|
|
return None
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
return build_config
|
|
|
|
|
|
|
|
|
|
|
|
# Generate build.ninja and objdiff.json
|
2024-01-24 06:21:46 +00:00
|
|
|
def generate_build(config: ProjectConfig) -> None:
|
2024-09-04 14:55:19 +00:00
|
|
|
config.validate()
|
|
|
|
objects = config.objects()
|
2023-10-05 03:26:02 +00:00
|
|
|
build_config = load_build_config(config, config.out_path() / "config.json")
|
2024-09-04 14:55:19 +00:00
|
|
|
generate_build_ninja(config, objects, build_config)
|
|
|
|
generate_objdiff_config(config, objects, build_config)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Generate build.ninja
|
2024-01-24 06:21:46 +00:00
|
|
|
def generate_build_ninja(
|
2024-09-04 14:55:19 +00:00
|
|
|
config: ProjectConfig,
|
|
|
|
objects: Dict[str, Object],
|
|
|
|
build_config: Optional[Dict[str, Any]],
|
2024-01-24 06:21:46 +00:00
|
|
|
) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
out = io.StringIO()
|
|
|
|
n = ninja_syntax.Writer(out)
|
|
|
|
n.variable("ninja_required_version", "1.3")
|
|
|
|
n.newline()
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
configure_script = Path(os.path.relpath(os.path.abspath(sys.argv[0])))
|
|
|
|
python_lib = Path(os.path.relpath(__file__))
|
|
|
|
python_lib_dir = python_lib.parent
|
2023-10-05 03:26:02 +00:00
|
|
|
n.comment("The arguments passed to configure.py, for rerunning it.")
|
|
|
|
n.variable("configure_args", sys.argv[1:])
|
|
|
|
n.variable("python", f'"{sys.executable}"')
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Variables
|
|
|
|
###
|
|
|
|
n.comment("Variables")
|
2024-09-18 01:21:54 +00:00
|
|
|
n.variable("ldflags", make_flags_str(config.ldflags))
|
2024-09-04 14:55:19 +00:00
|
|
|
if config.linker_version is None:
|
2024-03-04 15:17:51 +00:00
|
|
|
sys.exit("ProjectConfig.linker_version missing")
|
|
|
|
n.variable("mw_version", Path(config.linker_version))
|
2023-10-05 03:26:02 +00:00
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Tooling
|
|
|
|
###
|
|
|
|
n.comment("Tooling")
|
|
|
|
|
|
|
|
build_path = config.out_path()
|
2024-02-29 18:21:44 +00:00
|
|
|
progress_path = build_path / "progress.json"
|
2024-08-13 00:09:10 +00:00
|
|
|
report_path = build_path / "report.json"
|
2023-10-05 03:26:02 +00:00
|
|
|
build_tools_path = config.build_dir / "tools"
|
|
|
|
download_tool = config.tools_dir / "download_tool.py"
|
|
|
|
n.rule(
|
|
|
|
name="download_tool",
|
|
|
|
command=f"$python {download_tool} $tool $out --tag $tag",
|
|
|
|
description="TOOL $out",
|
|
|
|
)
|
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
decompctx = config.tools_dir / "decompctx.py"
|
|
|
|
n.rule(
|
|
|
|
name="decompctx",
|
|
|
|
command=f"$python {decompctx} $in -o $out -d $out.d",
|
|
|
|
description="CTX $in",
|
|
|
|
depfile="$out.d",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
|
2024-08-13 00:09:10 +00:00
|
|
|
cargo_rule_written = False
|
|
|
|
|
|
|
|
def write_cargo_rule():
|
|
|
|
nonlocal cargo_rule_written
|
|
|
|
if not cargo_rule_written:
|
|
|
|
n.pool("cargo", 1)
|
|
|
|
n.rule(
|
|
|
|
name="cargo",
|
|
|
|
command="cargo build --release --manifest-path $in --bin $bin --target-dir $target",
|
|
|
|
description="CARGO $bin",
|
|
|
|
pool="cargo",
|
|
|
|
depfile=Path("$target") / "release" / "$bin.d",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
cargo_rule_written = True
|
|
|
|
|
2024-03-05 06:19:57 +00:00
|
|
|
if config.dtk_path is not None and config.dtk_path.is_file():
|
|
|
|
dtk = config.dtk_path
|
|
|
|
elif config.dtk_path is not None:
|
2023-10-05 03:26:02 +00:00
|
|
|
dtk = build_tools_path / "release" / f"dtk{EXE}"
|
2024-08-13 00:09:10 +00:00
|
|
|
write_cargo_rule()
|
2023-10-05 03:26:02 +00:00
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=dtk,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="cargo",
|
2024-03-05 01:13:10 +00:00
|
|
|
inputs=config.dtk_path / "Cargo.toml",
|
|
|
|
implicit=config.dtk_path / "Cargo.lock",
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
|
|
|
"bin": "dtk",
|
|
|
|
"target": build_tools_path,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
elif config.dtk_tag:
|
|
|
|
dtk = build_tools_path / f"dtk{EXE}"
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=dtk,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="download_tool",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=download_tool,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
|
|
|
"tool": "dtk",
|
|
|
|
"tag": config.dtk_tag,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
sys.exit("ProjectConfig.dtk_tag missing")
|
|
|
|
|
2024-08-13 00:09:10 +00:00
|
|
|
if config.objdiff_path is not None and config.objdiff_path.is_file():
|
|
|
|
objdiff = config.objdiff_path
|
|
|
|
elif config.objdiff_path is not None:
|
|
|
|
objdiff = build_tools_path / "release" / f"objdiff-cli{EXE}"
|
|
|
|
write_cargo_rule()
|
|
|
|
n.build(
|
|
|
|
outputs=objdiff,
|
|
|
|
rule="cargo",
|
|
|
|
inputs=config.objdiff_path / "Cargo.toml",
|
|
|
|
implicit=config.objdiff_path / "Cargo.lock",
|
|
|
|
variables={
|
|
|
|
"bin": "objdiff-cli",
|
|
|
|
"target": build_tools_path,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
elif config.objdiff_tag:
|
|
|
|
objdiff = build_tools_path / f"objdiff-cli{EXE}"
|
|
|
|
n.build(
|
|
|
|
outputs=objdiff,
|
|
|
|
rule="download_tool",
|
|
|
|
implicit=download_tool,
|
|
|
|
variables={
|
|
|
|
"tool": "objdiff-cli",
|
|
|
|
"tag": config.objdiff_tag,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
sys.exit("ProjectConfig.objdiff_tag missing")
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
if config.sjiswrap_path:
|
|
|
|
sjiswrap = config.sjiswrap_path
|
|
|
|
elif config.sjiswrap_tag:
|
|
|
|
sjiswrap = build_tools_path / "sjiswrap.exe"
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=sjiswrap,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="download_tool",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=download_tool,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
|
|
|
"tool": "sjiswrap",
|
|
|
|
"tag": config.sjiswrap_tag,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
sys.exit("ProjectConfig.sjiswrap_tag missing")
|
|
|
|
|
|
|
|
# Only add an implicit dependency on wibo if we download it
|
|
|
|
wrapper = config.wrapper
|
2024-01-24 06:21:46 +00:00
|
|
|
wrapper_implicit: Optional[Path] = None
|
2023-10-05 03:26:02 +00:00
|
|
|
if (
|
|
|
|
config.wibo_tag is not None
|
|
|
|
and sys.platform == "linux"
|
|
|
|
and platform.machine() in ("i386", "x86_64")
|
|
|
|
and config.wrapper is None
|
|
|
|
):
|
|
|
|
wrapper = build_tools_path / "wibo"
|
|
|
|
wrapper_implicit = wrapper
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=wrapper,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="download_tool",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=download_tool,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
|
|
|
"tool": "wibo",
|
|
|
|
"tag": config.wibo_tag,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if not is_windows() and wrapper is None:
|
2024-01-24 06:21:46 +00:00
|
|
|
wrapper = Path("wine")
|
2023-10-05 03:26:02 +00:00
|
|
|
wrapper_cmd = f"{wrapper} " if wrapper else ""
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
compilers_implicit: Optional[Path] = None
|
2023-10-05 03:26:02 +00:00
|
|
|
if config.compilers_path:
|
|
|
|
compilers = config.compilers_path
|
|
|
|
elif config.compilers_tag:
|
|
|
|
compilers = config.build_dir / "compilers"
|
|
|
|
compilers_implicit = compilers
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=compilers,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="download_tool",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=download_tool,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
|
|
|
"tool": "compilers",
|
|
|
|
"tag": config.compilers_tag,
|
|
|
|
},
|
|
|
|
)
|
2024-01-24 06:21:46 +00:00
|
|
|
else:
|
|
|
|
sys.exit("ProjectConfig.compilers_tag missing")
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
binutils_implicit = None
|
|
|
|
if config.binutils_path:
|
|
|
|
binutils = config.binutils_path
|
|
|
|
elif config.binutils_tag:
|
|
|
|
binutils = config.build_dir / "binutils"
|
|
|
|
binutils_implicit = binutils
|
|
|
|
n.build(
|
|
|
|
outputs=binutils,
|
|
|
|
rule="download_tool",
|
|
|
|
implicit=download_tool,
|
|
|
|
variables={
|
|
|
|
"tool": "binutils",
|
|
|
|
"tag": config.binutils_tag,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
sys.exit("ProjectConfig.binutils_tag missing")
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
n.newline()
|
|
|
|
|
2024-08-12 00:07:07 +00:00
|
|
|
###
|
|
|
|
# Helper rule for downloading all tools
|
|
|
|
###
|
|
|
|
n.comment("Download all tools")
|
|
|
|
n.build(
|
|
|
|
outputs="tools",
|
|
|
|
rule="phony",
|
2024-08-13 00:09:10 +00:00
|
|
|
inputs=[dtk, sjiswrap, wrapper, compilers, binutils, objdiff],
|
2024-08-12 00:07:07 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Build rules
|
|
|
|
###
|
|
|
|
compiler_path = compilers / "$mw_version"
|
|
|
|
|
|
|
|
# MWCC
|
|
|
|
mwcc = compiler_path / "mwcceppc.exe"
|
|
|
|
mwcc_cmd = f"{wrapper_cmd}{mwcc} $cflags -MMD -c $in -o $basedir"
|
2024-01-24 06:21:46 +00:00
|
|
|
mwcc_implicit: List[Optional[Path]] = [compilers_implicit or mwcc, wrapper_implicit]
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# MWCC with UTF-8 to Shift JIS wrapper
|
|
|
|
mwcc_sjis_cmd = f"{wrapper_cmd}{sjiswrap} {mwcc} $cflags -MMD -c $in -o $basedir"
|
2024-01-24 06:21:46 +00:00
|
|
|
mwcc_sjis_implicit: List[Optional[Path]] = [*mwcc_implicit, sjiswrap]
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# MWLD
|
|
|
|
mwld = compiler_path / "mwldeppc.exe"
|
|
|
|
mwld_cmd = f"{wrapper_cmd}{mwld} $ldflags -o $out @$out.rsp"
|
2024-01-24 06:21:46 +00:00
|
|
|
mwld_implicit: List[Optional[Path]] = [compilers_implicit or mwld, wrapper_implicit]
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# GNU as
|
|
|
|
gnu_as = binutils / f"powerpc-eabi-as{EXE}"
|
|
|
|
gnu_as_cmd = (
|
|
|
|
f"{CHAIN}{gnu_as} $asflags -o $out $in -MD $out.d"
|
|
|
|
+ f" && {dtk} elf fixup $out $out"
|
|
|
|
)
|
|
|
|
gnu_as_implicit = [binutils_implicit or gnu_as, dtk]
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
if os.name != "nt":
|
|
|
|
transform_dep = config.tools_dir / "transform_dep.py"
|
|
|
|
mwcc_cmd += f" && $python {transform_dep} $basefile.d $basefile.d"
|
|
|
|
mwcc_sjis_cmd += f" && $python {transform_dep} $basefile.d $basefile.d"
|
|
|
|
mwcc_implicit.append(transform_dep)
|
|
|
|
mwcc_sjis_implicit.append(transform_dep)
|
|
|
|
|
|
|
|
n.comment("Link ELF file")
|
|
|
|
n.rule(
|
|
|
|
name="link",
|
|
|
|
command=mwld_cmd,
|
|
|
|
description="LINK $out",
|
|
|
|
rspfile="$out.rsp",
|
|
|
|
rspfile_content="$in_newline",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment("Generate DOL")
|
|
|
|
n.rule(
|
|
|
|
name="elf2dol",
|
|
|
|
command=f"{dtk} elf2dol $in $out",
|
|
|
|
description="DOL $out",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment("MWCC build")
|
|
|
|
n.rule(
|
|
|
|
name="mwcc",
|
|
|
|
command=mwcc_cmd,
|
|
|
|
description="MWCC $out",
|
|
|
|
depfile="$basefile.d",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment("MWCC build (with UTF-8 to Shift JIS wrapper)")
|
|
|
|
n.rule(
|
|
|
|
name="mwcc_sjis",
|
|
|
|
command=mwcc_sjis_cmd,
|
|
|
|
description="MWCC $out",
|
|
|
|
depfile="$basefile.d",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
n.comment("Assemble asm")
|
|
|
|
n.rule(
|
|
|
|
name="as",
|
|
|
|
command=gnu_as_cmd,
|
|
|
|
description="AS $out",
|
|
|
|
depfile="$out.d",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-08-13 00:09:10 +00:00
|
|
|
if len(config.custom_build_rules or {}) > 0:
|
|
|
|
n.comment("Custom project build rules (pre/post-processing)")
|
2024-08-07 03:08:26 +00:00
|
|
|
for rule in config.custom_build_rules or {}:
|
|
|
|
n.rule(
|
2024-09-04 14:55:19 +00:00
|
|
|
name=cast(str, rule.get("name")),
|
|
|
|
command=cast(str, rule.get("command")),
|
2024-08-07 03:08:26 +00:00
|
|
|
description=rule.get("description", None),
|
|
|
|
depfile=rule.get("depfile", None),
|
|
|
|
generator=rule.get("generator", False),
|
|
|
|
pool=rule.get("pool", None),
|
|
|
|
restat=rule.get("restat", False),
|
|
|
|
rspfile=rule.get("rspfile", None),
|
|
|
|
rspfile_content=rule.get("rspfile_content", None),
|
|
|
|
deps=rule.get("deps", None),
|
|
|
|
)
|
|
|
|
n.newline()
|
2024-08-12 00:22:25 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
def write_custom_step(step: str) -> List[str | Path]:
|
|
|
|
implicit: List[str | Path] = []
|
2024-08-07 03:08:26 +00:00
|
|
|
if config.custom_build_steps and step in config.custom_build_steps:
|
|
|
|
n.comment(f"Custom build steps ({step})")
|
|
|
|
for custom_step in config.custom_build_steps[step]:
|
2024-09-04 14:55:19 +00:00
|
|
|
outputs = cast(List[str | Path], custom_step.get("outputs"))
|
2024-08-07 03:08:26 +00:00
|
|
|
|
|
|
|
if isinstance(outputs, list):
|
|
|
|
implicit.extend(outputs)
|
|
|
|
else:
|
|
|
|
implicit.append(outputs)
|
|
|
|
|
|
|
|
n.build(
|
|
|
|
outputs=outputs,
|
2024-09-04 14:55:19 +00:00
|
|
|
rule=cast(str, custom_step.get("rule")),
|
2024-08-07 03:08:26 +00:00
|
|
|
inputs=custom_step.get("inputs", None),
|
|
|
|
implicit=custom_step.get("implicit", None),
|
|
|
|
order_only=custom_step.get("order_only", None),
|
|
|
|
variables=custom_step.get("variables", None),
|
|
|
|
implicit_outputs=custom_step.get("implicit_outputs", None),
|
|
|
|
pool=custom_step.get("pool", None),
|
|
|
|
dyndep=custom_step.get("dyndep", None),
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
return implicit
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
n.comment("Host build")
|
|
|
|
n.variable("host_cflags", "-I include -Wno-trigraphs")
|
|
|
|
n.variable(
|
|
|
|
"host_cppflags",
|
|
|
|
"-std=c++98 -I include -fno-exceptions -fno-rtti -D_CRT_SECURE_NO_WARNINGS -Wno-trigraphs -Wno-c++11-extensions",
|
|
|
|
)
|
|
|
|
n.rule(
|
|
|
|
name="host_cc",
|
|
|
|
command="clang $host_cflags -c -o $out $in",
|
|
|
|
description="CC $out",
|
|
|
|
)
|
|
|
|
n.rule(
|
|
|
|
name="host_cpp",
|
|
|
|
command="clang++ $host_cppflags -c -o $out $in",
|
|
|
|
description="CXX $out",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-08-07 03:08:26 +00:00
|
|
|
# Add all build steps needed before we compile (e.g. processing assets)
|
|
|
|
precompile_implicit = write_custom_step("pre-compile")
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Source files
|
|
|
|
###
|
|
|
|
n.comment("Source files")
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def map_path(path: Path) -> Path:
|
2023-10-05 03:26:02 +00:00
|
|
|
return path.parent / (path.name + ".MAP")
|
|
|
|
|
|
|
|
class LinkStep:
|
2024-01-24 06:21:46 +00:00
|
|
|
def __init__(self, config: Dict[str, Any]) -> None:
|
|
|
|
self.name: str = config["name"]
|
|
|
|
self.module_id: int = config["module_id"]
|
2024-03-04 15:17:51 +00:00
|
|
|
self.ldscript: Optional[Path] = Path(config["ldscript"])
|
2023-10-05 03:26:02 +00:00
|
|
|
self.entry = config["entry"]
|
2024-01-24 06:21:46 +00:00
|
|
|
self.inputs: List[str] = []
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 15:17:51 +00:00
|
|
|
def add(self, obj: Path) -> None:
|
|
|
|
self.inputs.append(serialize_path(obj))
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def output(self) -> Path:
|
2023-10-05 03:26:02 +00:00
|
|
|
if self.module_id == 0:
|
|
|
|
return build_path / f"{self.name}.dol"
|
|
|
|
else:
|
|
|
|
return build_path / self.name / f"{self.name}.rel"
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def partial_output(self) -> Path:
|
2023-10-05 03:26:02 +00:00
|
|
|
if self.module_id == 0:
|
|
|
|
return build_path / f"{self.name}.elf"
|
|
|
|
else:
|
|
|
|
return build_path / self.name / f"{self.name}.plf"
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def write(self, n: ninja_syntax.Writer) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
n.comment(f"Link {self.name}")
|
|
|
|
if self.module_id == 0:
|
|
|
|
elf_path = build_path / f"{self.name}.elf"
|
|
|
|
dol_path = build_path / f"{self.name}.dol"
|
2024-03-04 15:17:51 +00:00
|
|
|
elf_ldflags = f"$ldflags -lcf {serialize_path(self.ldscript)}"
|
2023-10-05 03:26:02 +00:00
|
|
|
if config.generate_map:
|
|
|
|
elf_map = map_path(elf_path)
|
2024-03-04 15:17:51 +00:00
|
|
|
elf_ldflags += f" -map {serialize_path(elf_map)}"
|
2023-10-05 03:26:02 +00:00
|
|
|
else:
|
|
|
|
elf_map = None
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=elf_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="link",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=self.inputs,
|
2024-08-07 03:08:26 +00:00
|
|
|
implicit=[
|
|
|
|
*precompile_implicit,
|
|
|
|
self.ldscript,
|
|
|
|
*mwld_implicit,
|
|
|
|
*postcompile_implicit,
|
|
|
|
],
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit_outputs=elf_map,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={"ldflags": elf_ldflags},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
preplf_path = build_path / self.name / f"{self.name}.preplf"
|
|
|
|
plf_path = build_path / self.name / f"{self.name}.plf"
|
2024-02-29 18:21:44 +00:00
|
|
|
preplf_ldflags = "$ldflags -sdata 0 -sdata2 0 -r"
|
2024-03-04 15:17:51 +00:00
|
|
|
plf_ldflags = f"$ldflags -sdata 0 -sdata2 0 -r1 -lcf {serialize_path(self.ldscript)}"
|
2023-11-22 01:14:13 +00:00
|
|
|
if self.entry:
|
|
|
|
plf_ldflags += f" -m {self.entry}"
|
|
|
|
# -strip_partial is only valid with -m
|
|
|
|
if config.rel_strip_partial:
|
|
|
|
plf_ldflags += " -strip_partial"
|
2023-10-05 03:26:02 +00:00
|
|
|
if config.generate_map:
|
|
|
|
preplf_map = map_path(preplf_path)
|
2024-03-04 15:17:51 +00:00
|
|
|
preplf_ldflags += f" -map {serialize_path(preplf_map)}"
|
2023-10-05 03:26:02 +00:00
|
|
|
plf_map = map_path(plf_path)
|
2024-03-04 15:17:51 +00:00
|
|
|
plf_ldflags += f" -map {serialize_path(plf_map)}"
|
2023-10-05 03:26:02 +00:00
|
|
|
else:
|
|
|
|
preplf_map = None
|
|
|
|
plf_map = None
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=preplf_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="link",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=self.inputs,
|
|
|
|
implicit=mwld_implicit,
|
|
|
|
implicit_outputs=preplf_map,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={"ldflags": preplf_ldflags},
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=plf_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="link",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=self.inputs,
|
|
|
|
implicit=[self.ldscript, preplf_path, *mwld_implicit],
|
|
|
|
implicit_outputs=plf_map,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={"ldflags": plf_ldflags},
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-05-18 20:50:32 +00:00
|
|
|
link_outputs: List[Path] = []
|
2023-10-05 03:26:02 +00:00
|
|
|
if build_config:
|
2024-01-24 06:21:46 +00:00
|
|
|
link_steps: List[LinkStep] = []
|
|
|
|
used_compiler_versions: Set[str] = set()
|
|
|
|
source_inputs: List[Path] = []
|
|
|
|
host_source_inputs: List[Path] = []
|
|
|
|
source_added: Set[Path] = set()
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
def c_build(obj: Object, src_path: Path) -> Optional[Path]:
|
|
|
|
cflags_str = make_flags_str(obj.options["cflags"])
|
|
|
|
if obj.options["extra_cflags"] is not None:
|
|
|
|
extra_cflags_str = make_flags_str(obj.options["extra_cflags"])
|
2024-03-04 05:47:05 +00:00
|
|
|
cflags_str += " " + extra_cflags_str
|
2024-09-04 14:55:19 +00:00
|
|
|
used_compiler_versions.add(obj.options["mw_version"])
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# Avoid creating duplicate build rules
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.src_obj_path is None or obj.src_obj_path in source_added:
|
|
|
|
return obj.src_obj_path
|
|
|
|
source_added.add(obj.src_obj_path)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# Add MWCC build rule
|
2024-09-04 14:55:19 +00:00
|
|
|
lib_name = obj.options["lib"]
|
2024-03-04 05:47:05 +00:00
|
|
|
n.comment(f"{obj.name}: {lib_name} (linked {obj.completed})")
|
|
|
|
n.build(
|
2024-09-04 14:55:19 +00:00
|
|
|
outputs=obj.src_obj_path,
|
|
|
|
rule="mwcc_sjis" if obj.options["shift_jis"] else "mwcc",
|
2024-03-04 05:47:05 +00:00
|
|
|
inputs=src_path,
|
|
|
|
variables={
|
2024-09-04 14:55:19 +00:00
|
|
|
"mw_version": Path(obj.options["mw_version"]),
|
2024-03-04 05:47:05 +00:00
|
|
|
"cflags": cflags_str,
|
2024-09-04 14:55:19 +00:00
|
|
|
"basedir": os.path.dirname(obj.src_obj_path),
|
|
|
|
"basefile": obj.src_obj_path.with_suffix(""),
|
2024-03-04 05:47:05 +00:00
|
|
|
},
|
2024-09-04 14:55:19 +00:00
|
|
|
implicit=(
|
|
|
|
mwcc_sjis_implicit if obj.options["shift_jis"] else mwcc_implicit
|
|
|
|
),
|
2024-03-04 05:47:05 +00:00
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# Add ctx build rule
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.ctx_path is not None:
|
|
|
|
n.build(
|
|
|
|
outputs=obj.ctx_path,
|
|
|
|
rule="decompctx",
|
|
|
|
inputs=src_path,
|
|
|
|
implicit=decompctx,
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# Add host build rule
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["host"] and obj.host_obj_path is not None:
|
2023-10-05 03:26:02 +00:00
|
|
|
n.build(
|
2024-09-04 14:55:19 +00:00
|
|
|
outputs=obj.host_obj_path,
|
2024-03-04 05:47:05 +00:00
|
|
|
rule="host_cc" if src_path.suffix == ".c" else "host_cpp",
|
|
|
|
inputs=src_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
variables={
|
2024-09-04 14:55:19 +00:00
|
|
|
"basedir": os.path.dirname(obj.host_obj_path),
|
|
|
|
"basefile": obj.host_obj_path.with_suffix(""),
|
2023-10-05 03:26:02 +00:00
|
|
|
},
|
|
|
|
)
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["add_to_all"]:
|
|
|
|
host_source_inputs.append(obj.host_obj_path)
|
2024-03-04 05:47:05 +00:00
|
|
|
n.newline()
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["add_to_all"]:
|
|
|
|
source_inputs.append(obj.src_obj_path)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
return obj.src_obj_path
|
2024-03-04 05:47:05 +00:00
|
|
|
|
|
|
|
def asm_build(
|
2024-09-04 14:55:19 +00:00
|
|
|
obj: Object, src_path: Path, obj_path: Optional[Path]
|
2024-03-04 05:47:05 +00:00
|
|
|
) -> Optional[Path]:
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["asflags"] is None:
|
2024-03-04 05:47:05 +00:00
|
|
|
sys.exit("ProjectConfig.asflags missing")
|
2024-09-04 14:55:19 +00:00
|
|
|
asflags_str = make_flags_str(obj.options["asflags"])
|
|
|
|
if obj.options["extra_asflags"] is not None:
|
|
|
|
extra_asflags_str = make_flags_str(obj.options["extra_asflags"])
|
2024-03-04 05:47:05 +00:00
|
|
|
asflags_str += " " + extra_asflags_str
|
|
|
|
|
|
|
|
# Avoid creating duplicate build rules
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj_path is None or obj_path in source_added:
|
|
|
|
return obj_path
|
|
|
|
source_added.add(obj_path)
|
2024-03-04 05:47:05 +00:00
|
|
|
|
|
|
|
# Add assembler build rule
|
2024-09-04 14:55:19 +00:00
|
|
|
lib_name = obj.options["lib"]
|
2024-03-04 05:47:05 +00:00
|
|
|
n.comment(f"{obj.name}: {lib_name} (linked {obj.completed})")
|
|
|
|
n.build(
|
2024-09-04 14:55:19 +00:00
|
|
|
outputs=obj_path,
|
2024-03-04 05:47:05 +00:00
|
|
|
rule="as",
|
|
|
|
inputs=src_path,
|
|
|
|
variables={"asflags": asflags_str},
|
|
|
|
implicit=gnu_as_implicit,
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["add_to_all"]:
|
|
|
|
source_inputs.append(obj_path)
|
2024-03-04 05:47:05 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
return obj_path
|
2024-03-04 05:47:05 +00:00
|
|
|
|
|
|
|
def add_unit(build_obj, link_step: LinkStep):
|
|
|
|
obj_path, obj_name = build_obj["object"], build_obj["name"]
|
2024-09-04 14:55:19 +00:00
|
|
|
obj = objects.get(obj_name)
|
|
|
|
if obj is None:
|
2024-03-04 05:47:05 +00:00
|
|
|
if config.warn_missing_config and not build_obj["autogenerated"]:
|
|
|
|
print(f"Missing configuration for {obj_name}")
|
|
|
|
link_step.add(obj_path)
|
|
|
|
return
|
|
|
|
|
|
|
|
link_built_obj = obj.completed
|
2024-03-14 23:18:01 +00:00
|
|
|
built_obj_path: Optional[Path] = None
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.src_path is not None and obj.src_path.exists():
|
|
|
|
if obj.src_path.suffix in (".c", ".cp", ".cpp"):
|
2024-03-04 05:47:05 +00:00
|
|
|
# Add MWCC & host build rules
|
2024-09-04 14:55:19 +00:00
|
|
|
built_obj_path = c_build(obj, obj.src_path)
|
|
|
|
elif obj.src_path.suffix == ".s":
|
2024-03-04 05:47:05 +00:00
|
|
|
# Add assembler build rule
|
2024-09-04 14:55:19 +00:00
|
|
|
built_obj_path = asm_build(obj, obj.src_path, obj.src_obj_path)
|
2024-03-04 05:47:05 +00:00
|
|
|
else:
|
2024-09-04 14:55:19 +00:00
|
|
|
sys.exit(f"Unknown source file type {obj.src_path}")
|
2024-03-04 05:47:05 +00:00
|
|
|
else:
|
|
|
|
if config.warn_missing_source or obj.completed:
|
2024-09-04 14:55:19 +00:00
|
|
|
print(f"Missing source file {obj.src_path}")
|
2024-03-04 05:47:05 +00:00
|
|
|
link_built_obj = False
|
|
|
|
|
|
|
|
# Assembly overrides
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.asm_path is not None and obj.asm_path.exists():
|
2024-03-04 05:47:05 +00:00
|
|
|
link_built_obj = True
|
2024-09-04 14:55:19 +00:00
|
|
|
built_obj_path = asm_build(obj, obj.asm_path, obj.asm_obj_path)
|
2024-03-04 05:47:05 +00:00
|
|
|
|
|
|
|
if link_built_obj and built_obj_path is not None:
|
|
|
|
# Use the source-built object
|
|
|
|
link_step.add(built_obj_path)
|
|
|
|
elif obj_path is not None:
|
|
|
|
# Use the original (extracted) object
|
|
|
|
link_step.add(obj_path)
|
|
|
|
else:
|
2024-09-04 14:55:19 +00:00
|
|
|
lib_name = obj.options["lib"]
|
|
|
|
sys.exit(
|
|
|
|
f"Missing object for {obj_name}: {obj.src_path} {lib_name} {obj}"
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Add DOL link step
|
|
|
|
link_step = LinkStep(build_config)
|
|
|
|
for unit in build_config["units"]:
|
|
|
|
add_unit(unit, link_step)
|
|
|
|
link_steps.append(link_step)
|
|
|
|
|
|
|
|
if config.build_rels:
|
|
|
|
# Add REL link steps
|
|
|
|
for module in build_config["modules"]:
|
|
|
|
module_link_step = LinkStep(module)
|
|
|
|
for unit in module["units"]:
|
|
|
|
add_unit(unit, module_link_step)
|
2023-11-22 01:14:13 +00:00
|
|
|
# Add empty object to empty RELs
|
|
|
|
if len(module_link_step.inputs) == 0:
|
2024-09-04 14:55:19 +00:00
|
|
|
if config.rel_empty_file is None:
|
2023-11-22 01:14:13 +00:00
|
|
|
sys.exit("ProjectConfig.rel_empty_file missing")
|
|
|
|
add_unit(
|
|
|
|
{
|
|
|
|
"object": None,
|
|
|
|
"name": config.rel_empty_file,
|
|
|
|
"autogenerated": True,
|
|
|
|
},
|
|
|
|
module_link_step,
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
link_steps.append(module_link_step)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
# Check if all compiler versions exist
|
|
|
|
for mw_version in used_compiler_versions:
|
|
|
|
mw_path = compilers / mw_version / "mwcceppc.exe"
|
|
|
|
if config.compilers_path and not os.path.exists(mw_path):
|
|
|
|
sys.exit(f"Compiler {mw_path} does not exist")
|
|
|
|
|
|
|
|
# Check if linker exists
|
2024-01-24 06:21:46 +00:00
|
|
|
mw_path = compilers / str(config.linker_version) / "mwldeppc.exe"
|
2023-10-05 03:26:02 +00:00
|
|
|
if config.compilers_path and not os.path.exists(mw_path):
|
|
|
|
sys.exit(f"Linker {mw_path} does not exist")
|
|
|
|
|
2024-08-07 03:08:26 +00:00
|
|
|
# Add all build steps needed before we link and after compiling objects
|
|
|
|
postcompile_implicit = write_custom_step("post-compile")
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Link
|
|
|
|
###
|
|
|
|
for step in link_steps:
|
|
|
|
step.write(n)
|
2024-05-18 20:50:32 +00:00
|
|
|
link_outputs.append(step.output())
|
2023-10-05 03:26:02 +00:00
|
|
|
n.newline()
|
|
|
|
|
2024-08-07 03:08:26 +00:00
|
|
|
# Add all build steps needed after linking and before GC/Wii native format generation
|
|
|
|
postlink_implicit = write_custom_step("post-link")
|
|
|
|
|
|
|
|
###
|
|
|
|
# Generate DOL
|
|
|
|
###
|
|
|
|
n.build(
|
|
|
|
outputs=link_steps[0].output(),
|
|
|
|
rule="elf2dol",
|
|
|
|
inputs=link_steps[0].partial_output(),
|
|
|
|
implicit=[*postlink_implicit, dtk],
|
|
|
|
)
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Generate RELs
|
|
|
|
###
|
2024-03-04 05:47:05 +00:00
|
|
|
n.comment("Generate REL(s)")
|
|
|
|
flags = "-w"
|
|
|
|
if len(build_config["links"]) > 1:
|
|
|
|
flags += " -q"
|
|
|
|
n.rule(
|
|
|
|
name="makerel",
|
|
|
|
command=f"{dtk} rel make {flags} -c $config $names @$rspfile",
|
|
|
|
description="REL",
|
|
|
|
rspfile="$rspfile",
|
|
|
|
rspfile_content="$in_newline",
|
|
|
|
)
|
2024-09-04 14:55:19 +00:00
|
|
|
generated_rels: List[str] = []
|
2024-03-04 05:47:05 +00:00
|
|
|
for idx, link in enumerate(build_config["links"]):
|
2023-11-22 01:14:13 +00:00
|
|
|
# Map module names to link steps
|
|
|
|
link_steps_local = list(
|
|
|
|
filter(
|
|
|
|
lambda step: step.name in link["modules"],
|
|
|
|
link_steps,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
link_steps_local.sort(key=lambda step: step.module_id)
|
|
|
|
# RELs can be the output of multiple link steps,
|
|
|
|
# so we need to filter out duplicates
|
|
|
|
rels_to_generate = list(
|
|
|
|
filter(
|
|
|
|
lambda step: step.module_id != 0
|
2024-02-29 18:21:44 +00:00
|
|
|
and step.name not in generated_rels,
|
2023-11-22 01:14:13 +00:00
|
|
|
link_steps_local,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if len(rels_to_generate) == 0:
|
|
|
|
continue
|
|
|
|
generated_rels.extend(map(lambda step: step.name, rels_to_generate))
|
|
|
|
rel_outputs = list(
|
|
|
|
map(
|
|
|
|
lambda step: step.output(),
|
|
|
|
rels_to_generate,
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
2024-03-04 05:47:05 +00:00
|
|
|
rel_names = list(
|
|
|
|
map(
|
|
|
|
lambda step: step.name,
|
|
|
|
link_steps_local,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rel_names_arg = " ".join(map(lambda name: f"-n {name}", rel_names))
|
2023-10-05 03:26:02 +00:00
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=rel_outputs,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="makerel",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=list(map(lambda step: step.partial_output(), link_steps_local)),
|
|
|
|
implicit=[dtk, config.config_path],
|
2024-03-04 05:47:05 +00:00
|
|
|
variables={
|
|
|
|
"config": config.config_path,
|
|
|
|
"rspfile": config.out_path() / f"rel{idx}.rsp",
|
|
|
|
"names": rel_names_arg,
|
|
|
|
},
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
2024-08-07 03:08:26 +00:00
|
|
|
# Add all build steps needed post-build (re-building archives and such)
|
|
|
|
postbuild_implicit = write_custom_step("post-build")
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Helper rule for building all source files
|
|
|
|
###
|
|
|
|
n.comment("Build all source files")
|
|
|
|
n.build(
|
|
|
|
outputs="all_source",
|
|
|
|
rule="phony",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=source_inputs,
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Helper rule for building all source files, with a host compiler
|
|
|
|
###
|
|
|
|
n.comment("Build all source files with a host compiler")
|
|
|
|
n.build(
|
|
|
|
outputs="all_source_host",
|
|
|
|
rule="phony",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=host_source_inputs,
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Check hash
|
|
|
|
###
|
|
|
|
n.comment("Check hash")
|
|
|
|
ok_path = build_path / "ok"
|
|
|
|
quiet = "-q " if len(link_steps) > 3 else ""
|
|
|
|
n.rule(
|
|
|
|
name="check",
|
|
|
|
command=f"{dtk} shasum {quiet} -c $in -o $out",
|
|
|
|
description="CHECK $in",
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=ok_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="check",
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=config.check_sha_path,
|
2024-08-07 03:08:26 +00:00
|
|
|
implicit=[dtk, *link_outputs, *postbuild_implicit],
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Calculate progress
|
|
|
|
###
|
|
|
|
n.comment("Calculate progress")
|
|
|
|
n.rule(
|
|
|
|
name="progress",
|
|
|
|
command=f"$python {configure_script} $configure_args progress",
|
|
|
|
description="PROGRESS",
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
outputs=progress_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="progress",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=[ok_path, configure_script, python_lib, config.config_path],
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
|
2024-08-13 00:09:10 +00:00
|
|
|
###
|
|
|
|
# Generate progress report
|
|
|
|
###
|
|
|
|
n.comment("Generate progress report")
|
|
|
|
n.rule(
|
|
|
|
name="report",
|
|
|
|
command=f"{objdiff} report generate -o $out",
|
|
|
|
description="REPORT",
|
|
|
|
)
|
2024-09-04 14:55:19 +00:00
|
|
|
report_implicit: List[str | Path] = [objdiff, "all_source"]
|
2024-08-13 00:09:10 +00:00
|
|
|
n.build(
|
|
|
|
outputs=report_path,
|
|
|
|
rule="report",
|
2024-09-04 14:55:19 +00:00
|
|
|
implicit=report_implicit,
|
2024-08-13 00:09:10 +00:00
|
|
|
)
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
###
|
|
|
|
# Helper tools
|
|
|
|
###
|
|
|
|
# TODO: make these rules work for RELs too
|
|
|
|
dol_link_step = link_steps[0]
|
|
|
|
dol_elf_path = dol_link_step.partial_output()
|
|
|
|
n.comment("Check for mismatching symbols")
|
|
|
|
n.rule(
|
|
|
|
name="dol_diff",
|
|
|
|
command=f"{dtk} -L error dol diff $in",
|
|
|
|
description=f"DIFF {dol_elf_path}",
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=[config.config_path, dol_elf_path],
|
2023-10-05 03:26:02 +00:00
|
|
|
outputs="dol_diff",
|
|
|
|
rule="dol_diff",
|
|
|
|
)
|
|
|
|
n.build(
|
|
|
|
outputs="diff",
|
|
|
|
rule="phony",
|
|
|
|
inputs="dol_diff",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment("Apply symbols from linked ELF")
|
|
|
|
n.rule(
|
|
|
|
name="dol_apply",
|
|
|
|
command=f"{dtk} dol apply $in",
|
|
|
|
description=f"APPLY {dol_elf_path}",
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=[config.config_path, dol_elf_path],
|
2023-10-05 03:26:02 +00:00
|
|
|
outputs="dol_apply",
|
|
|
|
rule="dol_apply",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=[ok_path],
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.build(
|
|
|
|
outputs="apply",
|
|
|
|
rule="phony",
|
|
|
|
inputs="dol_apply",
|
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Split DOL
|
|
|
|
###
|
2024-09-04 14:55:19 +00:00
|
|
|
build_config_path = build_path / "config.json"
|
2023-10-05 03:26:02 +00:00
|
|
|
n.comment("Split DOL into relocatable objects")
|
|
|
|
n.rule(
|
|
|
|
name="split",
|
|
|
|
command=f"{dtk} dol split $in $out_dir",
|
|
|
|
description="SPLIT $in",
|
|
|
|
depfile="$out_dir/dep",
|
|
|
|
deps="gcc",
|
|
|
|
)
|
|
|
|
n.build(
|
2024-01-24 06:21:46 +00:00
|
|
|
inputs=config.config_path,
|
|
|
|
outputs=build_config_path,
|
2023-10-05 03:26:02 +00:00
|
|
|
rule="split",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=dtk,
|
|
|
|
variables={"out_dir": build_path},
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Regenerate on change
|
|
|
|
###
|
|
|
|
n.comment("Reconfigure on change")
|
|
|
|
n.rule(
|
|
|
|
name="configure",
|
|
|
|
command=f"$python {configure_script} $configure_args",
|
|
|
|
generator=True,
|
|
|
|
description=f"RUN {configure_script}",
|
|
|
|
)
|
|
|
|
n.build(
|
|
|
|
outputs="build.ninja",
|
|
|
|
rule="configure",
|
2024-01-24 06:21:46 +00:00
|
|
|
implicit=[
|
|
|
|
build_config_path,
|
|
|
|
configure_script,
|
|
|
|
python_lib,
|
|
|
|
python_lib_dir / "ninja_syntax.py",
|
2024-08-07 03:08:26 +00:00
|
|
|
*(config.reconfig_deps or []),
|
2024-01-24 06:21:46 +00:00
|
|
|
],
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
###
|
|
|
|
# Default rule
|
|
|
|
###
|
|
|
|
n.comment("Default rule")
|
|
|
|
if build_config:
|
2024-05-18 20:50:32 +00:00
|
|
|
if config.non_matching:
|
|
|
|
n.default(link_outputs)
|
|
|
|
else:
|
|
|
|
n.default(progress_path)
|
2023-10-05 03:26:02 +00:00
|
|
|
else:
|
2024-01-24 06:21:46 +00:00
|
|
|
n.default(build_config_path)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Write build.ninja
|
|
|
|
with open("build.ninja", "w", encoding="utf-8") as f:
|
|
|
|
f.write(out.getvalue())
|
|
|
|
out.close()
|
|
|
|
|
|
|
|
|
|
|
|
# Generate objdiff.json
|
2024-01-24 06:21:46 +00:00
|
|
|
def generate_objdiff_config(
|
2024-09-04 14:55:19 +00:00
|
|
|
config: ProjectConfig,
|
|
|
|
objects: Dict[str, Object],
|
|
|
|
build_config: Optional[Dict[str, Any]],
|
2024-01-24 06:21:46 +00:00
|
|
|
) -> None:
|
2024-09-04 14:55:19 +00:00
|
|
|
if build_config is None:
|
2023-10-05 03:26:02 +00:00
|
|
|
return
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
objdiff_config: Dict[str, Any] = {
|
2024-09-04 14:55:19 +00:00
|
|
|
"min_version": "2.0.0-beta.5",
|
2023-10-05 03:26:02 +00:00
|
|
|
"custom_make": "ninja",
|
|
|
|
"build_target": False,
|
|
|
|
"watch_patterns": [
|
|
|
|
"*.c",
|
|
|
|
"*.cp",
|
|
|
|
"*.cpp",
|
|
|
|
"*.h",
|
|
|
|
"*.hpp",
|
2023-11-22 01:14:13 +00:00
|
|
|
"*.inc",
|
2023-10-05 03:26:02 +00:00
|
|
|
"*.py",
|
|
|
|
"*.yml",
|
|
|
|
"*.txt",
|
|
|
|
"*.json",
|
|
|
|
],
|
|
|
|
"units": [],
|
2024-09-04 14:55:19 +00:00
|
|
|
"progress_categories": [],
|
2023-10-05 03:26:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# decomp.me compiler name mapping
|
|
|
|
COMPILER_MAP = {
|
|
|
|
"GC/1.0": "mwcc_233_144",
|
|
|
|
"GC/1.1": "mwcc_233_159",
|
|
|
|
"GC/1.2.5": "mwcc_233_163",
|
|
|
|
"GC/1.2.5e": "mwcc_233_163e",
|
|
|
|
"GC/1.2.5n": "mwcc_233_163n",
|
2024-09-04 14:55:19 +00:00
|
|
|
"GC/1.3": "mwcc_242_53",
|
2024-03-04 05:47:05 +00:00
|
|
|
"GC/1.3.2": "mwcc_242_81",
|
|
|
|
"GC/1.3.2r": "mwcc_242_81r",
|
|
|
|
"GC/2.0": "mwcc_247_92",
|
|
|
|
"GC/2.5": "mwcc_247_105",
|
|
|
|
"GC/2.6": "mwcc_247_107",
|
|
|
|
"GC/2.7": "mwcc_247_108",
|
2024-06-10 04:37:51 +00:00
|
|
|
"GC/3.0a3": "mwcc_41_51213",
|
2024-03-04 05:47:05 +00:00
|
|
|
"GC/3.0a3.2": "mwcc_41_60126",
|
2024-06-10 04:37:51 +00:00
|
|
|
"GC/3.0a3.3": "mwcc_41_60209",
|
|
|
|
"GC/3.0a3.4": "mwcc_42_60308",
|
|
|
|
"GC/3.0a5": "mwcc_42_60422",
|
2024-03-04 05:47:05 +00:00
|
|
|
"GC/3.0a5.2": "mwcc_41_60831",
|
2024-06-10 04:37:51 +00:00
|
|
|
"GC/3.0": "mwcc_41_60831",
|
|
|
|
"Wii/1.0RC1": "mwcc_42_140",
|
2024-03-04 05:47:05 +00:00
|
|
|
"Wii/0x4201_127": "mwcc_42_142",
|
|
|
|
"Wii/1.0a": "mwcc_42_142",
|
2024-06-10 04:37:51 +00:00
|
|
|
"Wii/1.0": "mwcc_43_145",
|
2024-03-04 05:47:05 +00:00
|
|
|
"Wii/1.1": "mwcc_43_151",
|
|
|
|
"Wii/1.3": "mwcc_43_172",
|
2024-06-10 04:37:51 +00:00
|
|
|
"Wii/1.5": "mwcc_43_188",
|
2024-03-04 05:47:05 +00:00
|
|
|
"Wii/1.6": "mwcc_43_202",
|
|
|
|
"Wii/1.7": "mwcc_43_213",
|
|
|
|
}
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
def add_unit(
|
|
|
|
build_obj: Dict[str, Any], module_name: str, progress_categories: List[str]
|
|
|
|
) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
obj_path, obj_name = build_obj["object"], build_obj["name"]
|
|
|
|
base_object = Path(obj_name).with_suffix("")
|
2024-01-24 06:21:46 +00:00
|
|
|
unit_config: Dict[str, Any] = {
|
|
|
|
"name": Path(module_name) / base_object,
|
|
|
|
"target_path": obj_path,
|
2024-09-04 14:55:19 +00:00
|
|
|
"metadata": {
|
|
|
|
"auto_generated": build_obj["autogenerated"],
|
2024-09-05 03:02:42 +00:00
|
|
|
"progress_categories": progress_categories,
|
2024-09-04 14:55:19 +00:00
|
|
|
},
|
2023-10-05 03:26:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
obj = objects.get(obj_name)
|
2024-09-05 03:02:42 +00:00
|
|
|
if obj is None:
|
2023-10-05 03:26:02 +00:00
|
|
|
objdiff_config["units"].append(unit_config)
|
|
|
|
return
|
|
|
|
|
2024-09-05 03:02:42 +00:00
|
|
|
src_exists = obj.src_path is not None and obj.src_path.exists()
|
|
|
|
if src_exists:
|
|
|
|
unit_config["base_path"] = obj.src_obj_path
|
2024-09-29 19:58:15 +00:00
|
|
|
unit_config["metadata"]["source_path"] = obj.src_path
|
2024-09-05 03:02:42 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
cflags = obj.options["cflags"]
|
2023-10-05 03:26:02 +00:00
|
|
|
reverse_fn_order = False
|
|
|
|
if type(cflags) is list:
|
|
|
|
for flag in cflags:
|
|
|
|
if not flag.startswith("-inline "):
|
|
|
|
continue
|
|
|
|
for value in flag.split(" ")[1].split(","):
|
|
|
|
if value == "deferred":
|
|
|
|
reverse_fn_order = True
|
|
|
|
elif value == "nodeferred":
|
|
|
|
reverse_fn_order = False
|
|
|
|
|
2024-03-04 05:47:05 +00:00
|
|
|
# Filter out include directories
|
|
|
|
def keep_flag(flag):
|
|
|
|
return not flag.startswith("-i ") and not flag.startswith("-I ")
|
|
|
|
|
|
|
|
cflags = list(filter(keep_flag, cflags))
|
|
|
|
|
|
|
|
# Add appropriate lang flag
|
2024-09-05 03:02:42 +00:00
|
|
|
if obj.src_path is not None and not any(
|
|
|
|
flag.startswith("-lang") for flag in cflags
|
|
|
|
):
|
|
|
|
if obj.src_path.suffix in (".cp", ".cpp"):
|
|
|
|
cflags.insert(0, "-lang=c++")
|
|
|
|
else:
|
|
|
|
cflags.insert(0, "-lang=c")
|
2024-03-04 05:47:05 +00:00
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
compiler_version = COMPILER_MAP.get(obj.options["mw_version"])
|
2024-03-04 05:47:05 +00:00
|
|
|
if compiler_version is None:
|
2024-09-04 14:55:19 +00:00
|
|
|
print(f"Missing scratch compiler mapping for {obj.options['mw_version']}")
|
2024-03-04 05:47:05 +00:00
|
|
|
else:
|
2024-07-11 18:46:39 +00:00
|
|
|
cflags_str = make_flags_str(cflags)
|
2024-09-04 14:55:19 +00:00
|
|
|
if obj.options["extra_cflags"] is not None:
|
|
|
|
extra_cflags_str = make_flags_str(obj.options["extra_cflags"])
|
2024-07-11 18:46:39 +00:00
|
|
|
cflags_str += " " + extra_cflags_str
|
2024-03-04 05:47:05 +00:00
|
|
|
unit_config["scratch"] = {
|
|
|
|
"platform": "gc_wii",
|
|
|
|
"compiler": compiler_version,
|
2024-07-11 18:46:39 +00:00
|
|
|
"c_flags": cflags_str,
|
2024-03-04 05:47:05 +00:00
|
|
|
}
|
2024-09-05 03:02:42 +00:00
|
|
|
if src_exists:
|
|
|
|
unit_config["scratch"].update(
|
|
|
|
{
|
|
|
|
"ctx_path": obj.ctx_path,
|
|
|
|
"build_ctx": True,
|
|
|
|
}
|
|
|
|
)
|
2024-09-04 14:55:19 +00:00
|
|
|
category_opt: List[str] | str = obj.options["progress_category"]
|
|
|
|
if isinstance(category_opt, list):
|
|
|
|
progress_categories.extend(category_opt)
|
|
|
|
elif category_opt is not None:
|
|
|
|
progress_categories.append(category_opt)
|
2024-09-05 03:02:42 +00:00
|
|
|
unit_config["metadata"].update(
|
|
|
|
{
|
|
|
|
"complete": obj.completed,
|
|
|
|
"reverse_fn_order": reverse_fn_order,
|
|
|
|
"progress_categories": progress_categories,
|
|
|
|
}
|
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
objdiff_config["units"].append(unit_config)
|
|
|
|
|
|
|
|
# Add DOL units
|
|
|
|
for unit in build_config["units"]:
|
2024-09-04 14:55:19 +00:00
|
|
|
progress_categories = []
|
|
|
|
# Only include a "dol" category if there are any modules
|
|
|
|
# Otherwise it's redundant with the global report measures
|
|
|
|
if len(build_config["modules"]) > 0:
|
|
|
|
progress_categories.append("dol")
|
|
|
|
add_unit(unit, build_config["name"], progress_categories)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Add REL units
|
|
|
|
for module in build_config["modules"]:
|
|
|
|
for unit in module["units"]:
|
2024-09-04 14:55:19 +00:00
|
|
|
progress_categories = []
|
|
|
|
if config.progress_modules:
|
|
|
|
progress_categories.append("modules")
|
|
|
|
if config.progress_each_module:
|
|
|
|
progress_categories.append(module["name"])
|
|
|
|
add_unit(unit, module["name"], progress_categories)
|
|
|
|
|
|
|
|
# Add progress categories
|
|
|
|
def add_category(id: str, name: str):
|
|
|
|
objdiff_config["progress_categories"].append(
|
|
|
|
{
|
|
|
|
"id": id,
|
|
|
|
"name": name,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(build_config["modules"]) > 0:
|
|
|
|
add_category("dol", "DOL")
|
|
|
|
if config.progress_modules:
|
|
|
|
add_category("modules", "Modules")
|
|
|
|
if config.progress_each_module:
|
|
|
|
for module in build_config["modules"]:
|
|
|
|
add_category(module["name"], module["name"])
|
|
|
|
for category in config.progress_categories:
|
|
|
|
add_category(category.id, category.name)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Write objdiff.json
|
|
|
|
with open("objdiff.json", "w", encoding="utf-8") as w:
|
2024-01-24 06:21:46 +00:00
|
|
|
|
2024-03-04 15:17:51 +00:00
|
|
|
def unix_path(input: Any) -> str:
|
|
|
|
return str(input).replace(os.sep, "/") if input else ""
|
|
|
|
|
|
|
|
json.dump(objdiff_config, w, indent=4, default=unix_path)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Calculate, print and write progress to progress.json
|
2024-01-24 06:21:46 +00:00
|
|
|
def calculate_progress(config: ProjectConfig) -> None:
|
2024-09-04 14:55:19 +00:00
|
|
|
config.validate()
|
|
|
|
objects = config.objects()
|
2023-10-05 03:26:02 +00:00
|
|
|
out_path = config.out_path()
|
|
|
|
build_config = load_build_config(config, out_path / "config.json")
|
2024-09-04 14:55:19 +00:00
|
|
|
if build_config is None:
|
2023-10-05 03:26:02 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
class ProgressUnit:
|
2024-01-24 06:21:46 +00:00
|
|
|
def __init__(self, name: str) -> None:
|
|
|
|
self.name: str = name
|
|
|
|
self.code_total: int = 0
|
|
|
|
self.code_progress: int = 0
|
|
|
|
self.data_total: int = 0
|
|
|
|
self.data_progress: int = 0
|
|
|
|
self.objects: Set[Object] = set()
|
2024-09-18 01:21:17 +00:00
|
|
|
self.objects_progress: int = 0
|
2024-01-24 06:21:46 +00:00
|
|
|
|
|
|
|
def add(self, build_obj: Dict[str, Any]) -> None:
|
2023-10-05 03:26:02 +00:00
|
|
|
self.code_total += build_obj["code_size"]
|
|
|
|
self.data_total += build_obj["data_size"]
|
|
|
|
|
|
|
|
# Avoid counting the same object in different modules twice
|
|
|
|
include_object = build_obj["name"] not in self.objects
|
|
|
|
if include_object:
|
|
|
|
self.objects.add(build_obj["name"])
|
|
|
|
|
|
|
|
if build_obj["autogenerated"]:
|
|
|
|
# Skip autogenerated objects
|
|
|
|
return
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
obj = objects.get(build_obj["name"])
|
|
|
|
if obj is None or not obj.completed:
|
2023-10-05 03:26:02 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.code_progress += build_obj["code_size"]
|
|
|
|
self.data_progress += build_obj["data_size"]
|
|
|
|
if include_object:
|
|
|
|
self.objects_progress += 1
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def code_frac(self) -> float:
|
2024-09-07 01:40:12 +00:00
|
|
|
if self.code_total == 0:
|
|
|
|
return 1.0
|
2023-10-05 03:26:02 +00:00
|
|
|
return self.code_progress / self.code_total
|
|
|
|
|
2024-01-24 06:21:46 +00:00
|
|
|
def data_frac(self) -> float:
|
2024-09-07 01:40:12 +00:00
|
|
|
if self.data_total == 0:
|
|
|
|
return 1.0
|
2023-10-05 03:26:02 +00:00
|
|
|
return self.data_progress / self.data_total
|
|
|
|
|
2024-09-04 14:55:19 +00:00
|
|
|
progress_units: Dict[str, ProgressUnit] = {}
|
|
|
|
if config.progress_all:
|
|
|
|
progress_units["all"] = ProgressUnit("All")
|
|
|
|
progress_units["dol"] = ProgressUnit("DOL")
|
|
|
|
if len(build_config["modules"]) > 0:
|
|
|
|
if config.progress_modules:
|
|
|
|
progress_units["modules"] = ProgressUnit("Modules")
|
|
|
|
if len(config.progress_categories) > 0:
|
|
|
|
for category in config.progress_categories:
|
|
|
|
progress_units[category.id] = ProgressUnit(category.name)
|
|
|
|
if config.progress_each_module:
|
|
|
|
for module in build_config["modules"]:
|
|
|
|
progress_units[module["name"]] = ProgressUnit(module["name"])
|
|
|
|
|
|
|
|
def add_unit(id: str, unit: Dict[str, Any]) -> None:
|
|
|
|
progress = progress_units.get(id)
|
|
|
|
if progress is not None:
|
|
|
|
progress.add(unit)
|
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
# Add DOL units
|
|
|
|
for unit in build_config["units"]:
|
2024-09-04 14:55:19 +00:00
|
|
|
add_unit("all", unit)
|
|
|
|
add_unit("dol", unit)
|
|
|
|
obj = objects.get(unit["name"])
|
|
|
|
if obj is not None:
|
|
|
|
category_opt = obj.options["progress_category"]
|
|
|
|
if isinstance(category_opt, list):
|
|
|
|
for id in category_opt:
|
|
|
|
add_unit(id, unit)
|
|
|
|
elif category_opt is not None:
|
|
|
|
add_unit(category_opt, unit)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Add REL units
|
|
|
|
for module in build_config["modules"]:
|
|
|
|
for unit in module["units"]:
|
2024-09-04 14:55:19 +00:00
|
|
|
add_unit("all", unit)
|
|
|
|
add_unit("modules", unit)
|
|
|
|
add_unit(module["name"], unit)
|
|
|
|
obj = objects.get(unit["name"])
|
|
|
|
if obj is not None:
|
|
|
|
category_opt = obj.options["progress_category"]
|
|
|
|
if isinstance(category_opt, list):
|
|
|
|
for id in category_opt:
|
|
|
|
add_unit(id, unit)
|
|
|
|
elif category_opt is not None:
|
|
|
|
add_unit(category_opt, unit)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Print human-readable progress
|
|
|
|
print("Progress:")
|
|
|
|
|
2024-09-07 01:40:12 +00:00
|
|
|
for unit in progress_units.values():
|
2024-09-18 01:21:17 +00:00
|
|
|
if len(unit.objects) == 0:
|
2024-09-07 01:40:12 +00:00
|
|
|
continue
|
2023-11-22 01:14:13 +00:00
|
|
|
|
2023-10-05 03:26:02 +00:00
|
|
|
code_frac = unit.code_frac()
|
|
|
|
data_frac = unit.data_frac()
|
|
|
|
print(
|
2024-09-18 01:21:17 +00:00
|
|
|
f" {unit.name}: {code_frac:.2%} code, {data_frac:.2%} data ({unit.objects_progress} / {len(unit.objects)} files)"
|
2023-10-05 03:26:02 +00:00
|
|
|
)
|
|
|
|
print(f" Code: {unit.code_progress} / {unit.code_total} bytes")
|
|
|
|
print(f" Data: {unit.data_progress} / {unit.data_total} bytes")
|
2024-09-18 01:21:17 +00:00
|
|
|
|
|
|
|
if config.progress_use_fancy:
|
|
|
|
unit = progress_units.get("all") or progress_units.get("dol")
|
|
|
|
if unit is None or len(unit.objects) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
code_frac = unit.code_frac()
|
|
|
|
data_frac = unit.data_frac()
|
|
|
|
print(
|
|
|
|
"\nYou have {} out of {} {} and {} out of {} {}.".format(
|
|
|
|
math.floor(code_frac * config.progress_code_fancy_frac),
|
|
|
|
config.progress_code_fancy_frac,
|
|
|
|
config.progress_code_fancy_item,
|
|
|
|
math.floor(data_frac * config.progress_data_fancy_frac),
|
|
|
|
config.progress_data_fancy_frac,
|
|
|
|
config.progress_data_fancy_item,
|
2023-12-21 01:59:04 +00:00
|
|
|
)
|
2024-09-18 01:21:17 +00:00
|
|
|
)
|
2023-10-05 03:26:02 +00:00
|
|
|
|
|
|
|
# Generate and write progress.json
|
2024-01-24 06:21:46 +00:00
|
|
|
progress_json: Dict[str, Any] = {}
|
2024-09-07 01:40:12 +00:00
|
|
|
for id, unit in progress_units.items():
|
2024-09-18 01:21:17 +00:00
|
|
|
if len(unit.objects) == 0:
|
2024-09-07 01:40:12 +00:00
|
|
|
continue
|
|
|
|
progress_json[id] = {
|
2023-10-05 03:26:02 +00:00
|
|
|
"code": unit.code_progress,
|
|
|
|
"code/total": unit.code_total,
|
|
|
|
"data": unit.data_progress,
|
|
|
|
"data/total": unit.data_total,
|
|
|
|
}
|
|
|
|
|
|
|
|
with open(out_path / "progress.json", "w", encoding="utf-8") as w:
|
2023-11-22 01:14:13 +00:00
|
|
|
json.dump(progress_json, w, indent=4)
|