From e789b7542ac0bc36aa8180bce60a27ac810caf70 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 28 Jul 2021 12:47:05 +0100 Subject: [PATCH] configure: allow specification of cflags on a per-file basis in splat.yaml (#345) * fix #339 * add --debug and --non-matching configure flags --- tools/build/configure.py | 81 ++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/tools/build/configure.py b/tools/build/configure.py index 0d26810954..4e9f8be197 100755 --- a/tools/build/configure.py +++ b/tools/build/configure.py @@ -13,10 +13,6 @@ DO_SHA1_CHECK = True CPPFLAGS = "-w -Iver/$version/build/include -Iinclude -Isrc -D _LANGUAGE_C -D _FINALROM -D VERSION=$version " \ "-ffreestanding -DF3DEX_GBI_2 -D_MIPS_SZLONG=32 -MD -MF $out.d" -CFLAGS = "-O2 -quiet -fno-common -G0 -mcpu=vr4300 -mfix4300 -mips3 -mgp32 -mfp32 -Wuninitialized -Wshadow -Wmissing-braces -fforce-addr" -CFLAGS_NUSYS = "-O2 -quiet -fno-common -G0 -mcpu=vr4300 -mfix4300 -mips3 -mgp32 -mfp32 -Wuninitialized -Wshadow -Wmissing-braces" -CFLAGS_LIBULTRA = "-O2 -quiet -fno-common -G0 -mcpu=vr4300 -mfix4300 -mips3 -mgp32 -mfp32 -Wuninitialized -Wshadow -Wmissing-braces" - ASFLAGS = "-EB -G 0" # Paths: @@ -41,7 +37,7 @@ def exec_shell(command: List[str]) -> str: ret = subprocess.run(command, stdout=subprocess.PIPE, text=True) return ret.stdout -def write_ninja_rules(ninja: ninja_syntax.Writer, cpp: str): +def write_ninja_rules(ninja: ninja_syntax.Writer, cpp: str, cppflags: str, extra_cflags: str): # platform-specific if sys.platform == "darwin": os_dir = "mac" @@ -63,6 +59,8 @@ def write_ninja_rules(ninja: ninja_syntax.Writer, cpp: str): cc1 = f"{BUILD_TOOLS}/{os_dir}/cc1" nu64as = f"{BUILD_TOOLS}/{os_dir}/mips-nintendo-nu64-as" + cflags = "-O2 -quiet -fno-common -G0 -mcpu=vr4300 -mfix4300 -mips3 -mgp32 -mfp32 -Wuninitialized -Wshadow -Wmissing-braces " + extra_cflags + ninja.variable("python", sys.executable) ninja.rule("ld", @@ -81,29 +79,15 @@ def write_ninja_rules(ninja: ninja_syntax.Writer, cpp: str): ) ninja.rule("cc", - description="cc($version) $in", - command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} $in -o - | {iconv} | {cc1} {CFLAGS} -o - | {nu64as} {ASFLAGS} - -o $out'", - depfile="$out.d", - deps="gcc", - ) - - ninja.rule("cc_nusys", - description="cc($version) $in", - command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} $in -o - | {iconv} | {cc1} {CFLAGS_NUSYS} -o - | {nu64as} {ASFLAGS} - -o $out'", - depfile="$out.d", - deps="gcc", - ) - - ninja.rule("cc_libultra", - description="cc($version) $in", - command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} $in -o - | {iconv} | {cc1} {CFLAGS_LIBULTRA} -o - | {nu64as} {ASFLAGS} - -o $out'", + description="cc($version) $in $cflags", + command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} {cppflags} $in -o - | {iconv} | {cc1} {cflags} $cflags -o - | {nu64as} {ASFLAGS} - -o $out'", depfile="$out.d", deps="gcc", ) ninja.rule("cc_dsl", - description="cc_dsl($version) $in", - command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} $in -o - | $python {BUILD_TOOLS}/cc_dsl/compile_script.py | {iconv} | {cc1} {CFLAGS} -o - | {nu64as} {ASFLAGS} - -o $out'", + description="cc_dsl($version) $in $cflags", + command=f"bash -o pipefail -c '{cpp} {CPPFLAGS} {cppflags} $in -o - | $python {BUILD_TOOLS}/cc_dsl/compile_script.py | {iconv} | {cc1} {cflags} $cflags -o - | {nu64as} {ASFLAGS} - -o $out'", depfile="$out.d", deps="gcc", ) @@ -294,7 +278,7 @@ class Configure: if task == "yay0": implicit.append(YAY0_COMPRESS_TOOL) - elif task in ["cc", "cc_dsl", "cc_nusys", "cc_libultra"]: + elif task in ["cc", "cc_dsl"]: order_only.append("generated_headers_" + self.version) ninja.build( @@ -315,18 +299,29 @@ class Configure: elif isinstance(seg, segtypes.n64.asm.N64SegAsm) or (isinstance(seg, segtypes.n64.data.N64SegData) and not seg.type[0] == "."): build(entry.object_path, entry.src_paths, "as") elif isinstance(seg, segtypes.n64.c.N64SegC) or (isinstance(seg, segtypes.n64.data.N64SegData) and seg.type[0] == "."): - task = "cc" - if "nusys" in entry.src_paths[0].parts: - task = "cc_nusys" - elif "os" in entry.src_paths[0].parts: - task = "cc_libultra" - else: - with entry.src_paths[0].open() as f: - s = f.read() - if "SCRIPT(" in s or "#pragma SCRIPT" or "#include \"world/common/foliage.inc.c\"" in s: - task = "cc_dsl" + cflags = None + if isinstance(seg.yaml, dict): + cflags = seg.yaml.get("cflags") + elif len(seg.yaml) >= 4: + cflags = seg.yaml[3] - build(entry.object_path, entry.src_paths, task) + # default cflags where not specified + if cflags is None: + if "nusys" in entry.src_paths[0].parts: + cflags = "" + elif "os" in entry.src_paths[0].parts: # libultra + cflags = "" + else: # papermario + cflags = "-fforce-addr" + + # check for dsl + task = "cc" + with entry.src_paths[0].open() as f: + s = f.read() + if "SCRIPT(" in s or "#pragma SCRIPT" in s or "#include \"world/common/foliage.inc.c\"" in s: + task = "cc_dsl" + + build(entry.object_path, entry.src_paths, task, variables={"cflags": cflags}) # images embedded inside data aren't linked, but they do need to be built into .inc.c files if isinstance(seg, segtypes.n64.group.N64SegGroup): @@ -603,6 +598,8 @@ if __name__ == "__main__": parser.add_argument("--splat", default="tools/splat", help="Path to splat tool to use") parser.add_argument("--split-code", action="store_true", help="Re-split code segments to asm files") parser.add_argument("--no-split-assets", action="store_true", help="Don't split assets from the baserom(s)") + parser.add_argument("-d", "--debug", action="store_true", help="Generate debugging information") + parser.add_argument("-n", "--non-matching", action="store_true", help="Compile nonmatching code. Combine with --debug for more detailed debug info") args = parser.parse_args() exec_shell(["make", "-C", str(ROOT / args.splat)]) @@ -647,12 +644,24 @@ if __name__ == "__main__": rm_recursive(ROOT / f"ver/{version}/build") rm_recursive(ROOT / f"ver/{version}/.splat_cache") + cflags = "" + cppflags = "" + if args.non_matching: + cppflags += " -DNON_MATCHING" + + if args.debug: + cflags += " -ggdb3" # we can generate more accurate debug info in non-matching mode + cppflags += " -DDEBUG" # e.g. affects ASSERT macro + elif args.debug: + # g1 doesn't affect codegen + cflags += " -g1" + # add splat to python import path sys.path.append(str((ROOT / args.splat).resolve())) ninja = ninja_syntax.Writer(open(str(ROOT / "build.ninja"), "w"), width=9999) - write_ninja_rules(ninja, args.cpp or "cpp") + write_ninja_rules(ninja, args.cpp or "cpp", cppflags, cflags) write_ninja_for_tools(ninja) skip_files = set()