From 2da999d864800a839e42163100f3af0e09880a56 Mon Sep 17 00:00:00 2001 From: Christoph Junghans Date: Thu, 27 Sep 2018 12:18:43 -0600 Subject: [PATCH] cmake: prevent in-source build --- cmake/CMakeLists.txt | 2 ++ cmake/Modules/PreventInSourceBuilds.cmake | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 cmake/Modules/PreventInSourceBuilds.cmake diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index ce8de0b501..20a2c2729d 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -69,6 +69,8 @@ get_lammps_version(${LAMMPS_SOURCE_DIR}/version.h LAMMPS_VERSION) # Cmake modules/macros are in a subdirectory to keep this file cleaner set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Modules) +include(PreventInSourceBuilds) + if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS) #release comes with -O3 by default set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE) diff --git a/cmake/Modules/PreventInSourceBuilds.cmake b/cmake/Modules/PreventInSourceBuilds.cmake new file mode 100644 index 0000000000..20d9d25590 --- /dev/null +++ b/cmake/Modules/PreventInSourceBuilds.cmake @@ -0,0 +1,22 @@ +# - Prevent in-source builds. +# https://stackoverflow.com/questions/1208681/with-cmake-how-would-you-disable-in-source-builds/ + +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(srcdir2 "${CMAKE_SOURCE_DIR}/.." REALPATH) + get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + + # disallow in-source builds + if("${srcdir}" STREQUAL "${bindir}" OR "${srcdir2}" 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()