Avoid overriding CMake default install prefix

CMake FetchContent is useful to incorporate subprojects at configure time. FetchContent subproject cache variables can override the top-level project cache, which can be confusing. A particular instance we’ve found problematic is overriding the CMake default install prefix. A top level project desiring to install to the CMake default location will get a surprising result if the child project overrides this.

# DON'T DO THIS

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  # will not take effect without FORCE
  set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR} CACHE PATH "Install top-level directory" FORCE)
endif()

Instead, we recommend projects, including projects intended to be consumed via FetchContent use CMakePresets.json to set the default install directory:

{
  "version": 3,

"configurePresets": [
{
  "name": "default",
  "binaryDir": "${sourceDir}/build",
  "installDir": "${sourceDir}/build"
}
]
}