From e94969000a4498296833feda04f3ef21898bf281 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Mon, 6 Mar 2017 16:15:20 +0100 Subject: [PATCH] Block in-source builds and add clean_all target Summary: Add detecting and blocking in-source builds A simple cmake function, which detects if the source and build dir are one and the same. This unfortunately does not catch the case when the build dir is inside the source dir, since we actually want to support that. For example, creating a 'build' directory inside the project source directory should be allowed. Add a clean_all custom target in cmake This target will clean all the files inside the build directory. It should be used with care! A new cmake module is created, since invoking cmake -E remove_directory will delete the directory and we want to keep it. This way, we also avoid using platform specific shell command, i.e `rm -rf` and we can make clean_all smarter in the future. Untrack build/.gitignore from git Reviewers: buda Reviewed By: buda Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D92 --- .gitignore | 2 +- CMakeLists.txt | 7 +++++++ build/.gitignore | 2 -- cmake/clean_all.cmake | 2 ++ cmake/functions.cmake | 16 ++++++++++++++++ init.sh | 3 +++ 6 files changed, 29 insertions(+), 3 deletions(-) delete mode 100644 build/.gitignore create mode 100644 cmake/clean_all.cmake diff --git a/.gitignore b/.gitignore index c5d77708b..3a852fd9b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ release/memgraph_* release/libs/ release/barrier/ release/barrier_* -build/compiled/ +build/ cmake-build-* .idea cmake/DownloadProject/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 0de821407..6ec979ca4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,13 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake) include(functions) # ----------------------------------------------------------------------------- +# We want out of source builds, so that cmake generated files don't get mixed +# with source files. This allows for easier clean up. +disallow_in_source_build() +add_custom_target(clean_all + COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/clean_all.cmake + COMMENT "Removing all files in ${CMAKE_BINARY_DIR}") + # threading find_package(Threads REQUIRED) # ----------------------------------------------------------------------------- diff --git a/build/.gitignore b/build/.gitignore deleted file mode 100644 index 78d910160..000000000 --- a/build/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/* -!.gitignore diff --git a/cmake/clean_all.cmake b/cmake/clean_all.cmake new file mode 100644 index 000000000..13c932f80 --- /dev/null +++ b/cmake/clean_all.cmake @@ -0,0 +1,2 @@ +file(GLOB build_contents "${CMAKE_BINARY_DIR}/*") +file(REMOVE_RECURSE ${build_contents}) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index 109db7944..ac2ab1834 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -27,3 +27,19 @@ MACRO(SUBDIRLIST result curdir) ENDFOREACH() SET(${result} ${dirlist}) ENDMACRO() + +function(disallow_in_source_build) + get_filename_component(src_dir ${CMAKE_SOURCE_DIR} REALPATH) + get_filename_component(bin_dir ${CMAKE_BINARY_DIR} REALPATH) + message(STATUS "SOURCE_DIR" ${src_dir}) + message(STATUS "BINARY_DIR" ${bin_dir}) + # Do we maybe want to limit out-of-source builds to be only inside a + # directory which contains 'build' in name? + if("${src_dir}" STREQUAL "${bin_dir}") + # Unfortunately, we cannot remove CMakeCache.txt and CMakeFiles here + # because they are written after cmake is done. + message(FATAL_ERROR "In source build is not supported! " + "Remove CMakeCache.txt and CMakeFiles and then create a separate " + "directory, e.g. 'build' and run cmake there.") + endif() +endfunction() diff --git a/init.sh b/init.sh index 8a3ac285f..e7d6f2626 100755 --- a/init.sh +++ b/init.sh @@ -5,6 +5,9 @@ for pkg in wget git cmake uuid-dev clang-3.8; do dpkg -s $pkg 2>/dev/null >/dev/null || sudo apt-get -y install $pkg done +# create a default build directory +mkdir -p ./build + # setup libs (download) cd libs ./setup.sh