diff --git a/tests/build_command_test.py b/tests/build_command_test.py index 616481e7..84f61c59 100644 --- a/tests/build_command_test.py +++ b/tests/build_command_test.py @@ -27,7 +27,7 @@ class CommandTest(unittest.TestCase): self.probe.write_text('import json, pathlib, sys\n' 'pathlib.Path(sys.argv[1]).write_text(json.dumps(sys.argv[2:]))\n') - def ninja(self, target, command, posix=False): + def ninja(self, target, command, posix=False, bindings=''): if posix: # Ninja on Unix uses sh -c. An expanded response file lets Windows exercise those same shell bytes too. shell = subprocess.list2cmdline([SHELL]) if os.name == 'nt' else shlex.quote(SHELL) @@ -35,7 +35,7 @@ class CommandTest(unittest.TestCase): f' rspfile = $out.rsp\n rspfile_content = {command}\n') else: text = f'rule probe\n command = {command}\n' - (self.folder / 'build.ninja').write_text(text + f'build {target}: probe\n', encoding='utf-8') + (self.folder / 'build.ninja').write_text(bindings + text + f'build {target}: probe\n', encoding='utf-8') env = dict(os.environ, BUILD_QUOTE_TEST='expanded', MSYS2_ARG_CONV_EXCL='*') run = subprocess.run([NINJA, '-f', 'build.ninja', target], cwd=self.folder, env=env, capture_output=True, text=True, timeout=30) @@ -80,6 +80,33 @@ class CommandTest(unittest.TestCase): run = subprocess.run([str(self.folder / target)], timeout=10) self.assertEqual(run.returncode, 0) + @unittest.skipUnless(SHELL and (os.environ.get('CC') or shutil.which('clang')), 'needs a POSIX shell and clang') + def test_linker_writes_the_map_in_the_literal_build_directory(self): + self.link_map(posix=True) + + @unittest.skipUnless(os.name == 'nt' and (os.environ.get('CC') or shutil.which('clang')), 'native Windows linker') + def test_windows_linker_keeps_the_map_path_as_one_argument(self): + self.link_map(posix=False) + + def link_map(self, posix): + output = self.folder / "O'Reilly $BUILD_QUOTE_TEST (output), comma" + output.mkdir() + source = self.folder / 'link.c' + source.write_text('int main(void) { return 0; }\n') + compiler = os.environ.get('CC') or shutil.which('clang') + target = 'link.exe' if os.name == 'nt' else 'link' + with patch.object(build, 'WIN', not posix), patch.object(build, 'vma_include', return_value=self.folder), \ + patch.object(build, 'runtime_sources', return_value=[]): + # Use production Ninja bindings, but link only our own tiny program, without runtime or game inputs. + ninja, _, _ = build.ninja_file(self.folder, output, {}, 1) + bindings = '\n'.join(line for line in ninja.splitlines() + if line.startswith(('builddir = ', 'ldflags = '))) + '\n' + command = (f'{build.cmd_arg(Path(compiler).as_posix())} {build.cmd_path(source)} ' + f'$ldflags -o {build.cmd_arg(target)}') + self.ninja(target, command, posix=posix, bindings=bindings) + self.assertTrue((output / 'totk.map').is_file(), 'the linker must write the map at the requested path') + self.assertIn('main', (output / 'totk.map').read_text()) + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/tools/build_game.py b/tools/build_game.py index 8fe09243..6200e22c 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -161,15 +161,13 @@ if WIN: # _WIN32_WINNT: Windows 10, the port's minimum. MSYS2's headers default to it, llvm-mingw's (the release toolchain) # to Windows 7, which hides the per-monitor DPI functions gfx_window_win.c and dpi_probe_win.c call. RT_FLAGS.append('-D_WIN32_WINNT=0x0A00') - # -Xlinker passes the map path as one argument; -Wl, would split it at any comma in the build dir path - LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '-Xlinker', '"--Map=$builddir/totk.map"'] # the map feeds tools/prof_report.py + LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216'] LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx static (movies); libzstd static (resolution.c) VMA_INCLUDE = UCRT.parent / 'include' / 'vma' # vk_mem_alloc.h; MSYS2 package vulkan-memory-allocator else: # -no-pie: the lifted modules' function tables (hundreds of thousands of pointers) need no load-time relocation. # $ORIGIN: a libSDL3.so.0 next to totk comes before the system's (the release's portable folder). - LINK_FLAGS = ['-fuse-ld=lld', '-no-pie', '-Wl,-z,noexecstack', "'-Wl,-rpath,$$ORIGIN'", - '"-Wl,-Map=$builddir/totk.map"'] + LINK_FLAGS = ['-fuse-ld=lld', '-no-pie', '-Wl,-z,noexecstack', "'-Wl,-rpath,$$ORIGIN'"] LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] VMA_INCLUDE = Path('/usr/include') # vk_mem_alloc.h; Arch (AUR) vulkan-memory-allocator, Debian libvulkan-memory-allocator-dev # Without a system vk_mem_alloc.h, the header the Windows release pins (tools/make_release.py, VMA 3.4.0) is fetched once @@ -508,6 +506,8 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=(), runtime_lib=Non ffx / 'spirv')) # FidelityFX/host headers, ffx_shader_blobs.h, ffx_shaders_vk.h rt_inc = f'{inc} -I {cmd_path(out)} -I {vma} -I {imgui} {ffx_inc}' # romfs_config.h, vk_mem_alloc.h (+ deps headers), imgui.h libs = [f'-L {cmd_path(DEPS / "lib")}'] + (LLVM_LINK_LIBS if WIN else LINK_LIBS) if DEPS else LINK_LIBS + # The map feeds tools/prof_report.py. Quote its literal path before Ninja expansion; -Xlinker preserves commas. + map_arg = cmd_arg('--Map=' + flag_path(out / 'totk.map')) lines = [ '# Written by tools/build_game.py; do not edit.', 'ninja_required_version = 1.10', @@ -515,7 +515,7 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=(), runtime_lib=Non cc_line(), genflags_line(RUNTIME, gen), f'rtflags = {" ".join(RT_FLAGS)} {rt_inc}', - f'ldflags = {" ".join(LINK_FLAGS)}', + f'ldflags = {" ".join(LINK_FLAGS)} -Xlinker {map_arg}', f'libs = {" ".join(libs)}', '', 'pool gen',