Debian Codegraph @

cmake/commons/PreventInSourceBuilds.cmake

raw
1 # PreventInSourceBuilds.cmake
2 #
3 # Forbids in-source builds.
4 #
5 # Rationale:
6 # There is no good reason to run CMake from the main source directory,
7 # as this would mess up the directory with compilation output.
8 # If CMake is nonetheless run in-source, then this is most likely by accident.
9 # This script is meant to prevent this specific accident.
10 #
11 # Usage:
12 # Insert the line "include(PreventInSourceBuilds)" near the top
13 # of the top-level CMakeLists.txt.
14 #
15 # License:
16 # No rights reserved (CC0).
17 #
18 # Author:
19 # Scientific Computing Group of JCNS-MLZ.
20 #
21 # Copyright:
22 # Forschungszentrum Jülich GmbH 2021
23
24 function(prevent_in_source_builds)
25 # make sure the user doesn't play dirty with symlinks
26 get_filename_component(srcdir ${CMAKE_SOURCE_DIR} REALPATH)
27 get_filename_component(bindir ${CMAKE_BINARY_DIR} REALPATH)
28
29 # disallow in-source builds
30 if(srcdir STREQUAL bindir)
31 message(FATAL_ERROR "\
32
33 CMake must not to be run in the source directory. \
34 Rather create a dedicated build directory and run CMake there. \
35 To clean up after this aborted in-place compilation:
36 rm -r CMakeCache.txt CMakeFiles
37 ")
38 endif()
39 endfunction()
40
41 prevent_in_source_builds()