[cmake] add option to allow in source builds

This commit is contained in:
Armin Novak 2023-12-06 16:25:09 +01:00 committed by akallabeth
parent fad30cce1b
commit cb9678b3d4

View File

@ -7,13 +7,25 @@
# in a dedicated build directory. To prevent users from accidentally running # in a dedicated build directory. To prevent users from accidentally running
# CMake in the source directory, just include this module. # CMake in the source directory, just include this module.
# make sure the user doesn't play dirty with symlinks option(ALLOW_IN_SOURCE_BUILD "[deprecated] Allow building in source tree" OFF)
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
# disallow in-source builds if (ALLOW_IN_SOURCE_BUILD)
if("${srcdir}" STREQUAL "${bindir}") set(CMAKE_DISABLE_SOURCE_CHANGES OFF CACHE INTERNAL "policy")
message(FATAL_ERROR "\ set(CMAKE_DISABLE_IN_SOURCE_BUILD OFF CACHE INTERNAL "policy")
if("${srcdir}" STREQUAL "${bindir}")
message(WARNING "Running in-source-tree build [ALLOW_IN_SOURCE_BUILD=ON]")
endif()
else()
set(CMAKE_DISABLE_SOURCE_CHANGES ON CACHE INTERNAL "policy")
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON CACHE INTERNAL "policy")
# make sure the user doesn't play dirty with symlinks
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
# disallow in-source builds
if("${srcdir}" STREQUAL "${bindir}")
message(FATAL_ERROR "\
CMake must not to be run in the source directory. \ CMake must not to be run in the source directory. \
Rather create a dedicated build directory and run CMake there. \ Rather create a dedicated build directory and run CMake there. \
@ -21,12 +33,14 @@ CMake now already created some files, to clean up after this aborted in-source c
rm -r CMakeCache.txt CMakeFiles rm -r CMakeCache.txt CMakeFiles
or or
git clean -xdf git clean -xdf
If you happen to require in-source-tree builds for some reason rerun with -DALLOW_IN_SOURCE_BUILD=ON
") ")
endif() endif()
# Check for remnants of in source builds # Check for remnants of in source builds
if(EXISTS "${CMAKE_SOURCE_DIR}/CMakeCache.txt" OR EXISTS "${CMAKE_SOURCE_DIR}/CMakeFiles") if(EXISTS "${CMAKE_SOURCE_DIR}/CMakeCache.txt" OR EXISTS "${CMAKE_SOURCE_DIR}/CMakeFiles")
message(FATAL_ERROR " \ message(FATAL_ERROR " \
Remnants of in source CMake run detected, aborting! Remnants of in source CMake run detected, aborting!
@ -34,5 +48,8 @@ To clean up after this aborted in-source compilation:
rm -r CMakeCache.txt CMakeFiles rm -r CMakeCache.txt CMakeFiles
or or
git clean -xdf git clean -xdf
If you happen to require in-source-tree builds for some reason rerun with -DALLOW_IN_SOURCE_BUILD=ON
") ")
endif()
endif() endif()