File: PreventInSourceBuilds.cmake

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (41 lines) | stat: -rw-r--r-- 1,167 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# PreventInSourceBuilds.cmake
#
# Forbids in-source builds.
#
# Rationale:
# There is no good reason to run CMake from the main source directory,
# as this would mess up the directory with compilation output.
# If CMake is nonetheless run in-source, then this is most likely by accident.
# This script is meant to prevent this specific accident.
#
# Usage:
# Insert the line "include(PreventInSourceBuilds)" near the top
# of the top-level CMakeLists.txt.
#
# License:
# No rights reserved (CC0).
#
# Author:
# Scientific Computing Group of JCNS-MLZ.
#
# Copyright:
# Forschungszentrum Jülich GmbH 2021

function(prevent_in_source_builds)
  # 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. \
Rather create a dedicated build directory and run CMake there. \
To clean up after this aborted in-place compilation:
  rm -r CMakeCache.txt CMakeFiles
")
  endif()
endfunction()

prevent_in_source_builds()