cmake: Do not modify `CMAKE_TRY_COMPILE_TARGET_TYPE` globally
This was requested in https://github.com/bitcoin/bitcoin/pull/31359#issuecomment-2515287092.
From https://github.com/bitcoin/bitcoin/pull/31359#issuecomment-2511246212:
(Almost?) every CMake check internally uses the
try_compile()command, whose behaviour, in turn, depends on theCMAKE_TRY_COMPILE_TARGET_TYPEvariable:
The default value,
EXECUTABLE, enables both compiler and linker checks.The
STATIC_LIBRARYvalue enables only compiler checks.To mimic Autotools' behaviour, we disabled linker checks by setting
CMAKE_TRY_COMPILE_TARGET_TYPEtoSTATIC_LIBRARYglobally (perhaps not the best design). This effectively separates the entire CMake script into regions whereCMAKE_TRY_COMPILE_TARGET_TYPEis:
unset
set to
STATIC_LIBRARYset to
EXECUTABLE
From https://github.com/bitcoin/bitcoin/pull/31359#issuecomment-2515287092:
This seems very fragile and unintuitive, and the fact that this could silently break at any point is not documented in any way. I don't think other bad design decisions should lead to us having to write even more boilerplate code to fix things that should "just work" (minus the upstream bugs).
Agreed. I forgot that we set
CMAKE_TRY_COMPILE_TARGET_TYPEglobally. And even worse, it's buried in a module. If that upsets CMake internal tests, I think we should undo that.
This PR ensures that CMAKE_TRY_COMPILE_TARGET_TYPE is modified only within local scopes.