From 20d6b58b5eee712d533b5d24da1a16952e430fab Mon Sep 17 00:00:00 2001 From: Mateusz Ozga Date: Thu, 12 Sep 2024 14:25:29 +0000 Subject: [PATCH] This commit continas: 1. Enable cache if available: 2. The function to prevent in-source builds --- CMakeLists.txt | 6 ++++++ cmake/Cache.cmake | 30 ++++++++++++++++++++++++++++++ cmake/PreventInSourceBuilds.cmake | 12 ++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 cmake/Cache.cmake create mode 100644 cmake/PreventInSourceBuilds.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index bf42b6aa15..9d5f053fc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,12 @@ if(INSTANCES_ONLY) set(CK_ENABLE_INSTANCES_ONLY "ON") endif() +# enable prevent in source build +include(PreventInSourceBuilds) + +# enable cache system +include(Cache) + include(getopt) # CK version file to record release version as well as git commit hash diff --git a/cmake/Cache.cmake b/cmake/Cache.cmake new file mode 100644 index 0000000000..a192e7756d --- /dev/null +++ b/cmake/Cache.cmake @@ -0,0 +1,30 @@ +option(ENABLE_CACHE "Enable cache if available" ON) +if(NOT ENABLE_CACHE) + return() +endif() + +set(CACHE_OPTION + "sccache" + CACHE STRING "Compiler cache to be used") +set(CACHE_OPTION_VALUES "sccache" "ccache") +set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES}) +list( + FIND + CACHE_OPTION_VALUES + ${CACHE_OPTION} + CACHE_OPTION_INDEX) + +if(${CACHE_OPTION_INDEX} EQUAL -1) + message( + STATUS + "Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}") +endif() + +find_program(CACHE_BINARY ${CACHE_OPTION}) +if(CACHE_BINARY) + message(STATUS "${CACHE_OPTION} found and enabled") + set(CMAKE_CXX_COMPILER_LAUNCHER ${CACHE_BINARY}) + set(CMAKE_C_COMPILER_LAUNCHER ${CACHE_BINARY}) +else() + message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it") +endif() diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake new file mode 100644 index 0000000000..3a48ccaeb3 --- /dev/null +++ b/cmake/PreventInSourceBuilds.cmake @@ -0,0 +1,12 @@ +function(AssureOutOfSourceBuilds) + get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) + get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + + if("${srcdir}" STREQUAL "${bindir}") + message("Warning: in-source builds are disabled") + message("Please create a separate build directory and run cmake from there") + message(FATAL_ERROR "Quitting configuration") + endif() +endfunction() + +assureOutOfSourceBuilds()