Skip to content

Commit a3dccb5

Browse files
committed
ENH: Prevent cmake in source builds
Building directly inside the root of the source tree can cause problems where the build intermediate files overwrite or conflict with the intended source code files. This modification identifies this problem and issues failure messages and suggestions to over come the problem with more robust build suggestion.
1 parent 645cd04 commit a3dccb5

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Diff for: CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ project(JSONCPP
7676
message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
7777
set( JSONCPP_SOVERSION 22 )
7878

79+
include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake)
80+
include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake)
81+
7982
option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
8083
option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
8184
option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)

Diff for: include/PreventInBuildInstalls.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string(TOLOWER "${CMAKE_INSTALL_PREFIX}" _PREFIX)
2+
string(TOLOWER "${ITK_BINARY_DIR}" _BUILD)
3+
if("${_PREFIX}" STREQUAL "${_BUILD}")
4+
message(FATAL_ERROR
5+
"The current CMAKE_INSTALL_PREFIX points at the build tree:\n"
6+
" ${CMAKE_INSTALL_PREFIX}\n"
7+
"This is not supported."
8+
)
9+
endif()

Diff for: include/PreventInSourceBuilds.cmake

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# This function will prevent in-source builds
3+
function(AssureOutOfSourceBuilds)
4+
# make sure the user doesn't play dirty with symlinks
5+
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
6+
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
7+
8+
# disallow in-source builds
9+
if("${srcdir}" STREQUAL "${bindir}")
10+
message("######################################################")
11+
message("# jsoncpp should not be configured & built in the jsoncpp source directory")
12+
message("# You must run cmake in a build directory.")
13+
message("# For example:")
14+
message("# mkdir jsoncpp-Sandbox ; cd jsoncpp-sandbox")
15+
message("# git clone https://github.com/open-source-parsers/jsoncpp.git # or download & unpack the source tarball")
16+
message("# mkdir jsoncpp-build")
17+
message("# this will create the following directory structure")
18+
message("#")
19+
message("# jsoncpp-Sandbox")
20+
message("# +--jsoncpp")
21+
message("# +--jsoncpp-build")
22+
message("#")
23+
message("# Then you can proceed to configure and build")
24+
message("# by using the following commands")
25+
message("#")
26+
message("# cd jsoncpp-build")
27+
message("# cmake ../jsoncpp # or ccmake, or cmake-gui ")
28+
message("# make")
29+
message("#")
30+
message("# NOTE: Given that you already tried to make an in-source build")
31+
message("# CMake have already created several files & directories")
32+
message("# in your source tree. run 'git status' to find them and")
33+
message("# remove them by doing:")
34+
message("#")
35+
message("# cd jsoncpp-Sandbox/jsoncpp")
36+
message("# git clean -n -d")
37+
message("# git clean -f -d")
38+
message("# git checkout --")
39+
message("#")
40+
message("######################################################")
41+
message(FATAL_ERROR "Quitting configuration")
42+
endif()
43+
endfunction()
44+
45+
AssureOutOfSourceBuilds()

0 commit comments

Comments
 (0)