From 5aa83db436135272bcb51ec6824ee3c4275285e4 Mon Sep 17 00:00:00 2001 From: Jcw87 Date: Fri, 6 Jan 2023 03:42:42 -0800 Subject: [PATCH] dol2asm: generate makefiles that use header dependencies for smarter rebuilds --- Makefile | 2 + tools/libdol2asm/exporter/makefile.py | 24 ++++++++-- tools/transform-dep.py | 69 +++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100755 tools/transform-dep.py diff --git a/Makefile b/Makefile index 64d37dddd6..8a5cbc7ff9 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,8 @@ LDFLAGS := -unused -map $(MAP) -fp hard -nodefaults -w off # Compiler flags CFLAGS += -Cpp_exceptions off -proc gekko -fp hard -O3 -nodefaults -str pool,readonly,reuse -RTTI off -maxerrors 5 -enum int $(INCLUDES) +DEPFLAGS := $(if $(DISABLE_DEPS),,-MD) + # O4,p for init.c $(BUILD_DIR)/src/init.o: CFLAGS := -Cpp_exceptions off -proc gekko -fp hard -O4,p -nodefaults -str pool,readonly,reuse -RTTI off -maxerrors 5 -enum int $(INCLUDES) diff --git a/tools/libdol2asm/exporter/makefile.py b/tools/libdol2asm/exporter/makefile.py index a0070431e1..ecdf81454a 100644 --- a/tools/libdol2asm/exporter/makefile.py +++ b/tools/libdol2asm/exporter/makefile.py @@ -58,11 +58,19 @@ async def create_library(library: Library): #await builder.write(f"\t@$(STRIP) -d -R .dead -R .comment {target_path}") await builder.write("") - await builder.write(f"{o_path}/%.o: {cpp_path}/%.cpp") + await builder.write(f"{o_path}/%.o: {cpp_path}/%.cpp {o_path}/%.d") await builder.write(f"\t@mkdir -p $(@D)") await builder.write(f"\t@echo building... $<") await builder.write(f"\t@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).cpp") - await builder.write(f"\t@$(CC) $(CFLAGS) $({prefix}_CFLAGS) -c -o $@ $(basename $@).cpp") + await builder.write(f"\t@$(CC) $(CFLAGS) $({prefix}_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).cpp") + await builder.write("\t@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi") + await builder.write("") + + await builder.write("ifndef DISABLE_DEPS") + await builder.write(f"{prefix}_D_FILES := $({prefix}_O_FILES:.o=.d)") + await builder.write(f"$({prefix}_D_FILES):") + await builder.write(f"include $(wildcard $({prefix}_D_FILES))") + await builder.write("endif") await builder.write("") debug(f"generated Makefile: '{makefile_path}'") @@ -146,11 +154,19 @@ async def create_rel(module: Module, rel_path: Path): await builder.write(f"\t@$(LD) -opt_partial -strip_partial $({prefix}_LDFLAGS) -o $({prefix}_TARGET) @{input_file}") await builder.write("") - await builder.write(f"{o_path}/%.o: {cpp_path}/%.cpp") + await builder.write(f"{o_path}/%.o: {cpp_path}/%.cpp {o_path}/%.d") await builder.write(f"\t@echo [{module.index:>3}] building $@") await builder.write(f"\t@mkdir -p $(@D)") await builder.write(f"\t@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).cpp") - await builder.write(f"\t@$(CC) $(CFLAGS) $({prefix}_CFLAGS) -c -o $@ $(basename $@).cpp") + await builder.write(f"\t@$(CC) $(CFLAGS) $({prefix}_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).cpp") + await builder.write("\t@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi") + await builder.write("") + + await builder.write("ifndef DISABLE_DEPS") + await builder.write(f"{prefix}_D_FILES := $({prefix}_O_FILES:.o=.d)") + await builder.write(f"$({prefix}_D_FILES):") + await builder.write(f"include $(wildcard $({prefix}_D_FILES))") + await builder.write("endif") await builder.write("") for library in libraries[1:]: diff --git a/tools/transform-dep.py b/tools/transform-dep.py new file mode 100755 index 0000000000..bdb29e5013 --- /dev/null +++ b/tools/transform-dep.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# borrowed from prime-decomp +import argparse +import os +from platform import uname + +if os.name != 'nt': + wineprefix = os.environ.get('WINEPREFIX', os.path.join(os.environ['HOME'], '.wine')) + winedevices = os.path.join(wineprefix, 'dosdevices') + +def in_wsl() -> bool: + return 'microsoft-standard' in uname().release + +def import_d_file(in_file) -> str: + out_lines = [] + + with open(in_file) as file: + for idx, line in enumerate(file): + if idx == 0: + if line.endswith(' \\\n'): + out_lines.append(line[:-3].replace('\\', '/') + " \\\n") + else: + out_lines.append(line.replace('\\', '/')) + else: + suffix = '' + if line.endswith(' \\\n'): + suffix = ' \\' + path = line.lstrip()[:-3] + else: + path = line.strip() + # lowercase drive letter + path = path[0].lower() + path[1:] + if os.name == 'nt': + path = path.replace('\\', '/') + elif path[0] == 'z': + # shortcut for z: + path = path[2:].replace('\\', '/') + elif in_wsl(): + path = path[0:1] + path[2:] + path = os.path.join('/mnt', path.replace('\\', '/')) + else: + # use $WINEPREFIX/dosdevices to resolve path + path = os.path.realpath(os.path.join(winedevices, path.replace('\\', '/'))) + out_lines.append(f"\t{path}{suffix}\n") + + return ''.join(out_lines) + +def main(): + parser = argparse.ArgumentParser( + description="""Transform a .d file from Wine paths to normal paths""" + ) + parser.add_argument( + "d_file", + help="""Dependency file in""", + ) + parser.add_argument( + "d_file_out", + help="""Dependency file out""", + ) + args = parser.parse_args() + + output = import_d_file(args.d_file) + + with open(args.d_file_out, "w", encoding="UTF-8") as f: + f.write(output) + + +if __name__ == "__main__": + main() \ No newline at end of file