Only apply tools for top-level directory (#36)

* update CPM.cmake

* only apply tools for top-level directory
This commit is contained in:
Lars Melchior 2020-04-29 10:21:19 +02:00 committed by GitHub
parent e362045719
commit 6a5efe38c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 17 deletions

View file

@ -14,10 +14,6 @@ if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.") message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.")
endif() endif()
# --- Import tools ----
include(cmake/tools.cmake)
# ---- Add dependencies via CPM ---- # ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info # see https://github.com/TheLartians/CPM.cmake for more info

View file

@ -28,7 +28,7 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR) cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
set(CURRENT_CPM_VERSION 0.18) set(CURRENT_CPM_VERSION 0.21)
if(CPM_DIRECTORY) if(CPM_DIRECTORY)
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR}) if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
@ -41,14 +41,23 @@ See https://github.com/TheLartians/CPM.cmake for more information."
endif() endif()
return() return()
endif() endif()
get_property(CPM_INITIALIZED GLOBAL "" PROPERTY CPM_INITIALIZED SET)
if (CPM_INITIALIZED)
return()
endif()
endif() endif()
set_property(GLOBAL PROPERTY CPM_INITIALIZED true)
option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES}) option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES})
option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY}) option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY})
option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL})
option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" $ENV{CPM_DONT_UPDATE_MODULE_PATH})
set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "") set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "")
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "") set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
set(CPM_FILE ${CMAKE_CURRENT_LIST_FILE} CACHE INTERNAL "")
set(CPM_PACKAGES "" CACHE INTERNAL "") set(CPM_PACKAGES "" CACHE INTERNAL "")
set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)") set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)")
@ -60,6 +69,15 @@ endif()
set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies") set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies")
if (NOT CPM_DONT_UPDATE_MODULE_PATH)
set(CPM_MODULE_PATH "${CMAKE_BINARY_DIR}/CPM_modules" CACHE INTERNAL "")
# remove old modules
FILE(REMOVE_RECURSE ${CPM_MODULE_PATH})
file(MAKE_DIRECTORY ${CPM_MODULE_PATH})
# locally added CPM modules should override global packages
set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}")
endif()
include(FetchContent) include(FetchContent)
include(CMakeParseArguments) include(CMakeParseArguments)
@ -80,6 +98,15 @@ function(cpm_find_package NAME VERSION)
endif() endif()
endfunction() endfunction()
# Create a custom FindXXX.cmake module for a CPM package
# This prevents `find_package(NAME)` from finding the system library
function(CPMCreateModuleFile Name)
if (NOT CPM_DONT_UPDATE_MODULE_PATH)
# erase any previous modules
FILE(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake "include(${CPM_FILE})\n${ARGN}\nset(${Name}_FOUND TRUE)")
endif()
endfunction()
# Find a package locally or fallback to CPMAddPackage # Find a package locally or fallback to CPMAddPackage
function(CPMFindPackage) function(CPMFindPackage)
set(oneValueArgs set(oneValueArgs
@ -167,7 +194,7 @@ function(CPMAddPackage)
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
endif() endif()
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES) if ("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES)
CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION)
if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION}) if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION})
message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).") message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).")
@ -218,7 +245,8 @@ function(CPMAddPackage)
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory}) list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory})
if (EXISTS ${download_directory}) if (EXISTS ${download_directory})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":") # disable the download command to allow offline builds
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}")
set(PACKAGE_INFO "${download_directory}") set(PACKAGE_INFO "${download_directory}")
else() else()
# remove timestamps so CMake will re-download the dependency # remove timestamps so CMake will re-download the dependency
@ -230,6 +258,7 @@ function(CPMAddPackage)
cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS}) cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS})
cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY}) cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
cpm_get_fetch_properties(${CPM_ARGS_NAME}) cpm_get_fetch_properties(${CPM_ARGS_NAME})
CPMCreateModuleFile(${CPM_ARGS_NAME} "CPMAddPackage(${ARGN})")
SET(${CPM_ARGS_NAME}_ADDED YES) SET(${CPM_ARGS_NAME}_ADDED YES)
cpm_export_variables() cpm_export_variables()
endfunction() endfunction()

View file

@ -1,16 +1,16 @@
# this file contains a list of tools that can be activated and downloaded on-demand # this file contains a list of tools that can be activated and downloaded on-demand
# each tool is enabled during configuration by passing an additional `-DUSE_<TOOL>=<VALUE>` argument to CMake # each tool is enabled during configuration by passing an additional `-DUSE_<TOOL>=<VALUE>` argument to CMake
# determine if a tool has already been enabled # only activate tools for top level project
foreach(TOOL USE_SANITIZER;USE_CCACHE) if (NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
get_property(${TOOL}_ENABLED GLOBAL "" PROPERTY ${TOOL}_ENABLED SET) return()
endforeach() endif()
include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake)
# enables sanitizers support using the the `USE_SANITIZER` flag # enables sanitizers support using the the `USE_SANITIZER` flag
# available values are: Address, Memory, MemoryWithOrigins, Undefined, Thread, Leak, 'Address;Undefined' # available values are: Address, Memory, MemoryWithOrigins, Undefined, Thread, Leak, 'Address;Undefined'
if (USE_SANITIZER AND NOT USE_SANITIZER_ENABLED) if (USE_SANITIZER)
set_property(GLOBAL PROPERTY USE_SANITIZER_ENABLED true)
CPMAddPackage( CPMAddPackage(
NAME StableCoder-cmake-scripts NAME StableCoder-cmake-scripts
GITHUB_REPOSITORY StableCoder/cmake-scripts GITHUB_REPOSITORY StableCoder/cmake-scripts
@ -22,9 +22,7 @@ endif()
# enables CCACHE support through the USE_CCACHE flag # enables CCACHE support through the USE_CCACHE flag
# possible values are: YES, NO or equivalent # possible values are: YES, NO or equivalent
if (USE_CCACHE AND NOT USE_CCACHE_ENABLED) if (USE_CCACHE)
set_property(GLOBAL PROPERTY USE_CCACHE_ENABLED true)
CPMAddPackage( CPMAddPackage(
NAME Ccache.cmake NAME Ccache.cmake
GITHUB_REPOSITORY TheLartians/Ccache.cmake GITHUB_REPOSITORY TheLartians/Ccache.cmake

View file

@ -4,6 +4,10 @@ project(GreeterStandalone
LANGUAGES CXX LANGUAGES CXX
) )
# --- Import tools ----
include(../cmake/tools.cmake)
# ---- Dependencies ---- # ---- Dependencies ----
include(../cmake/CPM.cmake) include(../cmake/CPM.cmake)

View file

@ -9,6 +9,10 @@ project(GreeterTests
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF) option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF) option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF)
# --- Import tools ----
include(../cmake/tools.cmake)
# ---- Dependencies ---- # ---- Dependencies ----
include(../cmake/CPM.cmake) include(../cmake/CPM.cmake)