diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 10ee582b12..b2728cc15b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1075,7 +1075,12 @@ if(BUILD_TESTING) target_link_libraries(tiny_game_fixed PRIVATE xgameruntime_stub) target_compile_definitions(tiny_game_fixed PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS) target_compile_options(tiny_game_fixed PRIVATE /W4 /permissive- /utf-8 /EHsc) - target_link_options(tiny_game_fixed PRIVATE /FIXED /DYNAMICBASE:NO /BASE:0x140000000) + # /INCREMENTAL:NO, here and on the fixtures linked with /NODEFAULTLIB below: such an image can have no base + # relocations, and VS 2026 18.6's linker (MSVC 14.51) crashes on an incremental relink of one ("LNK1000: Internal + # error during IncrCalcPtrs"). CMake's Ninja and Makefile generators relink every target once in each clean Debug + # or RelWithDebInfo build (/debug /INCREMENTAL by default), with only its manifest resource changed, to embed it. + # Fixed in VS 2026 18.7; tests/test_runtime_incremental_link.py keeps a new fixture of this kind covered. + target_link_options(tiny_game_fixed PRIVATE /FIXED /DYNAMICBASE:NO /BASE:0x140000000 /INCREMENTAL:NO) set_target_properties(tiny_game_fixed PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/game/$" ) @@ -1195,7 +1200,7 @@ if(BUILD_TESTING) add_library(tiny_tls_cb_only SHARED test/fixtures/tiny_tls_cb_only.cpp) target_compile_definitions(tiny_tls_cb_only PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(tiny_tls_cb_only PRIVATE /W4 /permissive- /utf-8 /GS-) - target_link_options(tiny_tls_cb_only PRIVATE /NODEFAULTLIB /ENTRY:DllMainCRTStartup) + target_link_options(tiny_tls_cb_only PRIVATE /NODEFAULTLIB /ENTRY:DllMainCRTStartup /INCREMENTAL:NO) target_link_libraries(tiny_tls_cb_only PRIVATE kernel32) set_target_properties(tiny_tls_cb_only PROPERTIES OUTPUT_NAME tiny_tls_cb_only @@ -1280,7 +1285,7 @@ if(BUILD_TESTING) # DllMain's only work is that one call. No game files. add_library(dll_thread_calls SHARED test/fixtures/dll_thread_calls.cpp) target_compile_options(dll_thread_calls PRIVATE /W4 /permissive- /utf-8 /GS-) - target_link_options(dll_thread_calls PRIVATE /NODEFAULTLIB /ENTRY:dll_thread_calls_main) + target_link_options(dll_thread_calls PRIVATE /NODEFAULTLIB /ENTRY:dll_thread_calls_main /INCREMENTAL:NO) target_link_libraries(dll_thread_calls PRIVATE kernel32) set_target_properties(dll_thread_calls PROPERTIES OUTPUT_NAME dll_thread_calls @@ -1315,7 +1320,7 @@ if(BUILD_TESTING) if(GAMEINPUT_LIBRARY) add_library(gameinput_host SHARED test/fixtures/gameinput_host.cpp) target_compile_options(gameinput_host PRIVATE /W4 /permissive- /utf-8 /GS-) - target_link_options(gameinput_host PRIVATE /NODEFAULTLIB /ENTRY:gameinput_host_main) + target_link_options(gameinput_host PRIVATE /NODEFAULTLIB /ENTRY:gameinput_host_main /INCREMENTAL:NO) target_link_libraries(gameinput_host PRIVATE kernel32 ${GAMEINPUT_LIBRARY}) set_target_properties(gameinput_host PROPERTIES OUTPUT_NAME gameinput_host diff --git a/tests/test_runtime_incremental_link.py b/tests/test_runtime_incremental_link.py new file mode 100644 index 0000000000..5b931f0256 --- /dev/null +++ b/tests/test_runtime_incremental_link.py @@ -0,0 +1,46 @@ +"""The runtime fixtures that can have no base relocations link with /INCREMENTAL:NO (no build run). + +VS 2026 18.6's linker (MSVC 14.51) crashes with "LNK1000: Internal error during IncrCalcPtrs" on an incremental +relink of an image with no base relocations. CMake's Ninja and Makefile generators relink every target once, with only +its manifest resource changed, in each clean Debug or RelWithDebInfo build (default link flags /debug /INCREMENTAL). +A fixture linked with /FIXED has its relocations stripped, and one linked with /NODEFAULTLIB has no CRT and often no +absolute address to relocate, so each of them links with /INCREMENTAL:NO. +https://developercommunity.visualstudio.com/t/MSVC-v1451-Linker-Crash-in-IncrCalcPtrs/11090307 + + python -I tests/test_runtime_incremental_link.py +""" +import os +from pathlib import Path +import re +import unittest + +ROOT = Path(__file__).resolve().parents[1] +CMAKE = Path(os.environ.get("LEONIDA_RUNTIME_CMAKE", str(ROOT / "runtime" / "CMakeLists.txt"))) + + +def link_options(text): + """Each target named in a target_link_options call, with every option the calls give it.""" + options = {} + for target, args in re.findall(r"target_link_options\(\s*([A-Za-z0-9_]+)\s+([^)]*)\)", text): + options.setdefault(target, []).extend(args.split()) + return options + + +class FixturesWithoutRelocationsLinkNonIncrementally(unittest.TestCase): + def test_every_fixed_or_crt_free_target_links_with_incremental_no(self): + options = link_options(CMAKE.read_text(encoding="utf-8")) + targets = {t for t, opts in options.items() if "/FIXED" in opts or "/NODEFAULTLIB" in opts} + # The scan has to see the fixtures that crashed the linker, or it checks nothing. + self.assertLessEqual({"tiny_game_fixed", "dll_thread_calls", "gameinput_host"}, targets) + missing = sorted(t for t in targets if "/INCREMENTAL:NO" not in options[t]) + self.assertEqual(missing, [], "linked with /FIXED or /NODEFAULTLIB but not /INCREMENTAL:NO") + + def test_the_scan_reads_options_across_lines(self): + text = ("target_link_options(a PRIVATE /FIXED\n /INCREMENTAL:NO)\n" + "target_link_options(b PRIVATE /NODEFAULTLIB)\n") + self.assertEqual(link_options(text), {"a": ["PRIVATE", "/FIXED", "/INCREMENTAL:NO"], + "b": ["PRIVATE", "/NODEFAULTLIB"]}) + + +if __name__ == "__main__": + unittest.main()