diff --git a/tools/build/configure.py b/tools/build/configure.py index f3d283c7d9c..01f11783f31 100755 --- a/tools/build/configure.py +++ b/tools/build/configure.py @@ -357,6 +357,24 @@ def write_ninja_for_tools(ninja: ninja_syntax.Writer): ninja.build(CRC_TOOL, "cc_tool", f"{BUILD_TOOLS}/rom/n64crc.c") +def does_iconv_work() -> bool: + # run iconv and see if it works + stdin = "エリア OMO2_1".encode("utf-8") + + def run(command, stdin): + sub = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, input=stdin) + return sub.stdout + + expected_stdout = run(["tools/build/iconv.py", "UTF-8", "CP932"], stdin) + actual_stdout = run(["iconv", "--from", "UTF-8", "--to", "CP932"], stdin) + return expected_stdout == actual_stdout + + +use_python_iconv = not does_iconv_work() +if use_python_iconv: + print("warning: iconv doesn't work, using python implementation") + + class Configure: def __init__(self, version: str): self.version = version @@ -677,7 +695,10 @@ def build( if version == "ique": encoding = "EUC-JP" - iconv = f"iconv --from UTF-8 --to {encoding}" + if use_python_iconv: + iconv = f"tools/build/iconv.py UTF-8 {encoding}" + else: + iconv = f"iconv --from UTF-8 --to {encoding}" # use tools/sjis-escape.py for src/battle/area/tik2/area.c if version != "ique" and seg.dir.parts[-3:] == ("battle", "area", "tik2") and seg.name == "area": diff --git a/tools/build/iconv.py b/tools/build/iconv.py new file mode 100755 index 00000000000..55ed94730f5 --- /dev/null +++ b/tools/build/iconv.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +import argparse +import sys + +parser = argparse.ArgumentParser(description="Convert a file from one encoding to another") +parser.add_argument("f") +parser.add_argument("t") +parser.add_argument("infile", nargs="?", type=argparse.FileType("r"), default=sys.stdin) +parser.add_argument("outfile", nargs="?", type=argparse.FileType("w"), default=sys.stdout) + +args = parser.parse_args() + + +def main(args): + sys.stdin.reconfigure(encoding=args.f) + in_data = args.infile.read() + sys.stdout.reconfigure(encoding=args.t) + args.outfile.write(in_data) + + +if __name__ == "__main__": + main(parser.parse_args())