This was requested in #31359 (comment).
From #31359 (comment):
(Almost?) every CMake check internally uses the
try_compile()
command, whose behaviour, in turn, depends on theCMAKE_TRY_COMPILE_TARGET_TYPE
variable:
The default value,
EXECUTABLE
, enables both compiler and linker checks.The
STATIC_LIBRARY
value enables only compiler checks.To mimic Autotools’ behaviour, we disabled linker checks by setting
CMAKE_TRY_COMPILE_TARGET_TYPE
toSTATIC_LIBRARY
globally (perhaps not the best design). This effectively separates the entire CMake script into regions whereCMAKE_TRY_COMPILE_TARGET_TYPE
is:
unset
set to
STATIC_LIBRARY
set to
EXECUTABLE
From #31359 (comment):
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_TYPE
globally. 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.