Skip to content

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 the CMAKE_TRY_COMPILE_TARGET_TYPE variable:

  1. The default value, EXECUTABLE, enables both compiler and linker checks.

  2. The STATIC_LIBRARY value enables only compiler checks.

To mimic Autotools' behaviour, we disabled linker checks by setting CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY globally (perhaps not the best design). This effectively separates the entire CMake script into regions where CMAKE_TRY_COMPILE_TARGET_TYPE is:

  • unset

  • set to STATIC_LIBRARY

  • set 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_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.

Merge request reports

Loading