Skip to content

Commit 8954092

Browse files
hjmjohnsonbaylesj
andauthored
ENH: Prevent cmake in source builds (#1091)
* 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. Co-authored-by: Jordan Bayles <[email protected]>
1 parent ceae0e3 commit 8954092

5 files changed

+67
-4
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ project(jsoncpp
7575
message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
7676
set(PROJECT_SOVERSION 24)
7777

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

CONTRIBUTING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you wish to install to a directory other than /usr/local, set an environment
1919
DESTDIR=/path/to/install/dir
2020

2121
Then,
22-
22+
```sh
2323
cd jsoncpp/
2424
BUILD_TYPE=debug
2525
#BUILD_TYPE=release
@@ -35,6 +35,7 @@ Then,
3535
#meson test --no-rebuild --print-errorlogs
3636

3737
sudo ninja install
38+
```
3839

3940
## Building and testing with other build systems
4041
See https://github.com/open-source-parsers/jsoncpp/wiki/Building

appveyor.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
clone_folder: c:\projects\jsoncpp
22

33
environment:
4+
45
matrix:
56
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
67
CMAKE_GENERATOR: Visual Studio 14 2015
@@ -13,11 +14,15 @@ environment:
1314

1415
build_script:
1516
- cmake --version
16-
- cd c:\projects\jsoncpp
17-
- cmake -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX:PATH=%CD:\=/%/install -DBUILD_SHARED_LIBS:BOOL=ON .
17+
# The build script starts in root.
18+
- set JSONCPP_FOLDER=%cd%
19+
- set JSONCPP_BUILD_FOLDER=%JSONCPP_FOLDER%\build\release
20+
- mkdir -p %JSONCPP_BUILD_FOLDER%
21+
- cd %JSONCPP_BUILD_FOLDER%
22+
- cmake -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX:PATH=%CD:\=/%/install -DBUILD_SHARED_LIBS:BOOL=ON %JSONCPP_FOLDER%
1823
# Use ctest to make a dashboard build:
1924
# - ctest -D Experimental(Start|Update|Configure|Build|Test|Coverage|MemCheck|Submit)
20-
# NOTE: Testing on window is not yet finished:
25+
# NOTE: Testing on windows is not yet finished:
2126
# - ctest -C Release -D ExperimentalStart -D ExperimentalConfigure -D ExperimentalBuild -D ExperimentalTest -D ExperimentalSubmit
2227
- ctest -C Release -D ExperimentalStart -D ExperimentalConfigure -D ExperimentalBuild -D ExperimentalSubmit
2328
# Final step is to verify that installation succeeds

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()

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)