I would like to prohibit cmake in-source build especially at top-level.
For example prohibited case is cmake -S . -B ., however, I would like to allow cmake -S . -B build
Thus, these following options are not fitted here.
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
I added small script in CMakeLists.txt
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
if("${srcdir}" STREQUAL "${bindir}")
message(FATAL_ERROR "Do not build on the top of the sources")
endif()
It works, but CMakeCache.txt and CMakeFiles are still created. How can I keep my sources clean?
With git, git clean -d -f -x can help me, but I would like to have a solution with cmake itself.