Files
psxrecomp/runtime/hash_codegen.cmake
Matthew Stanley bc5ec35998 cli: generate builds the static overlay shard an AOT profile declares
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 10:28:14 -07:00

55 lines
2.2 KiB
CMake

# hash_codegen.cmake — generate overlay_codegen_hash.h with a content hash of the
# recompiler's codegen sources, so the overlay cache path (gcc/<arch-abi>/cg<N>_<hash>/)
# changes automatically whenever the emitter changes. This closes the gap where a
# pure-emitter change within the same manual PSX_OVERLAY_CODEGEN_VER produced a
# different DLL the cache happily reused (a stale-but-cgN load — the v0.3.0 black
# screen). Both the runtime loader (via overlay_api.h) and compile_overlays.py read
# the generated PSX_OVERLAY_CODEGEN_HASH, so the two sides never drift.
#
# Invoked as: cmake -DOUT=<header> "-DSRCS=<a>;<b>;..." -P hash_codegen.cmake
# Re-runs whenever a listed source changes (it's an add_custom_command DEPENDS).
#
# Or, outside any build, with the canonical list resolved here:
# cmake -DOUT=<header> -DPSXRECOMP_CODEGEN_HASH_ROOT=<psxrecomp> -P hash_codegen.cmake
# tools/aot_overlay_pipeline.py `static` does this: a static overlay shard is
# generated before the runtime is ever built, and compile_overlays' stale-
# recompiler guard needs this header to exist.
if(NOT DEFINED SRCS)
if(NOT DEFINED PSXRECOMP_CODEGEN_HASH_ROOT)
message(FATAL_ERROR
"hash_codegen.cmake: pass -DSRCS=<list> or "
"-DPSXRECOMP_CODEGEN_HASH_ROOT=<psxrecomp root>")
endif()
include("${CMAKE_CURRENT_LIST_DIR}/codegen_hash_sources.cmake")
set(SRCS ${PSXRECOMP_CODEGEN_HASH_SRCS})
endif()
set(_cat "")
foreach(f IN LISTS SRCS)
if(EXISTS "${f}")
file(READ "${f}" _c)
string(APPEND _cat "${_c}")
endif()
endforeach()
string(SHA256 _h "${_cat}")
string(SUBSTRING "${_h}" 0 8 _h8)
set(_content "/* AUTO-GENERATED by hash_codegen.cmake — do not edit, do not commit.
* Content hash of the recompiler codegen sources; folded into the overlay cache
* path so an emitter change never reuses a stale DLL. */
#pragma once
#define PSX_OVERLAY_CODEGEN_HASH 0x${_h8}u
")
# Only rewrite when the value actually changes, so dependents don't rebuild on
# every invocation.
if(EXISTS "${OUT}")
file(READ "${OUT}" _old)
if(_old STREQUAL _content)
return()
endif()
endif()
file(WRITE "${OUT}" "${_content}")
message(STATUS "overlay codegen hash: 0x${_h8}")