diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 1b859ce..6575dac 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -1,6 +1,6 @@ -# TheLartians/CPM - A simple Git dependency manager -# ================================================= -# See https://github.com/TheLartians/CPM for usage and update instructions. +# CPM.cmake - CMake's missing package manager +# =========================================== +# See https://github.com/TheLartians/CPM.cmake for usage and update instructions. # # MIT License # ----------- @@ -28,11 +28,11 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -set(CURRENT_CPM_VERSION 0.21) +set(CURRENT_CPM_VERSION 0.26.2) if(CPM_DIRECTORY) - if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR}) - if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION}) + if(NOT CPM_DIRECTORY STREQUAL CMAKE_CURRENT_LIST_DIR) + if (CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) message(AUTHOR_WARNING "${CPM_INDENT} \ A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ It is recommended to upgrade CPM to the most recent version. \ @@ -54,6 +54,8 @@ option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependenc 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_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" $ENV{CPM_DONT_UPDATE_MODULE_PATH}) +option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" $ENV{CPM_DONT_CREATE_PACKAGE_LOCK}) +option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK "Add all packages added through CPM.cmake to the package lock" $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK}) set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "") set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "") @@ -78,6 +80,11 @@ if (NOT CPM_DONT_UPDATE_MODULE_PATH) set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") endif() +if (NOT CPM_DONT_CREATE_PACKAGE_LOCK) + set(CPM_PACKAGE_LOCK_FILE "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" CACHE INTERNAL "") + file(WRITE ${CPM_PACKAGE_LOCK_FILE} "# CPM Package Lock\n# This file should be committed to version control\n\n") +endif() + include(FetchContent) include(CMakeParseArguments) @@ -90,8 +97,8 @@ function(cpm_find_package NAME VERSION) string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) if(${CPM_ARGS_NAME}_FOUND) - message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${${CPM_ARGS_NAME}_VERSION}") - CPMRegisterPackage(${CPM_ARGS_NAME} "${${CPM_ARGS_NAME}_VERSION}") + message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${VERSION}") + CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") set(CPM_PACKAGE_FOUND YES PARENT_SCOPE) else() set(CPM_PACKAGE_FOUND NO PARENT_SCOPE) @@ -116,10 +123,22 @@ function(CPMFindPackage) ) cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) - + + if (NOT DEFINED CPM_ARGS_VERSION) + if (DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + if (CPM_DOWNLOAD_ALL) CPMAddPackage(${ARGN}) - cpm_export_variables() + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + CPMCheckIfPackageAlreadyAdded(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}") + if (CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) return() endif() @@ -127,21 +146,47 @@ function(CPMFindPackage) if(NOT CPM_PACKAGE_FOUND) CPMAddPackage(${ARGN}) - cpm_export_variables() + cpm_export_variables(${CPM_ARGS_NAME}) endif() endfunction() +# checks if a package has been added before +function(CPMCheckIfPackageAlreadyAdded CPM_ARGS_NAME CPM_ARGS_VERSION CPM_ARGS_OPTIONS) + if ("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) + CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_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}).") + endif() + if (CPM_ARGS_OPTIONS) + foreach(OPTION ${CPM_ARGS_OPTIONS}) + cpm_parse_option(${OPTION}) + if(NOT "${${OPTION_KEY}}" STREQUAL "${OPTION_VALUE}") + message(WARNING "${CPM_INDENT} ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})") + endif() + endforeach() + endif() + cpm_get_fetch_properties(${CPM_ARGS_NAME}) + SET(${CPM_ARGS_NAME}_ADDED NO) + SET(CPM_PACKAGE_ALREADY_ADDED YES PARENT_SCOPE) + cpm_export_variables(${CPM_ARGS_NAME}) + else() + SET(CPM_PACKAGE_ALREADY_ADDED NO PARENT_SCOPE) + endif() +endfunction() + # Download and add a package from source function(CPMAddPackage) set(oneValueArgs NAME + FORCE VERSION GIT_TAG DOWNLOAD_ONLY GITHUB_REPOSITORY GITLAB_REPOSITORY + GIT_REPOSITORY SOURCE_DIR DOWNLOAD_COMMAND FIND_PACKAGE_ARGUMENTS @@ -153,10 +198,75 @@ function(CPMAddPackage) cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + # Set default values for arguments + + if (NOT DEFINED CPM_ARGS_VERSION) + if (DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_ARGS_DOWNLOAD_ONLY) + set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) + else() + set(DOWNLOAD_ONLY NO) + endif() + + if (DEFINED CPM_ARGS_GITHUB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") + endif() + + if (DEFINED CPM_ARGS_GITLAB_REPOSITORY) + list(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") + endif() + + if (DEFINED CPM_ARGS_GIT_REPOSITORY) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) + if (NOT DEFINED CPM_ARGS_GIT_TAG) + set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) + endif() + endif() + + if (DEFINED CPM_ARGS_GIT_TAG) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) + endif() + + # Check if package has been added before + CPMCheckIfPackageAlreadyAdded(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}") + if (CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for manual overrides + if (NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") + set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) + set(CPM_${CPM_ARGS_NAME}_SOURCE "") + CPMAddPackage( + NAME ${CPM_ARGS_NAME} + SOURCE_DIR ${PACKAGE_SOURCE} + FORCE True + ) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for available declaration + if (NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") + set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) + set(CPM_DECLARATION_${CPM_ARGS_NAME} "") + CPMAddPackage(${declaration}) + cpm_export_variables(${CPM_ARGS_NAME}) + # checking again to ensure version and option compatibility + CPMCheckIfPackageAlreadyAdded(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}") + return() + endif() + if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) if(CPM_PACKAGE_FOUND) + cpm_export_variables(${CPM_ARGS_NAME}) return() endif() @@ -165,58 +275,7 @@ function(CPMAddPackage) endif() endif() - if (NOT DEFINED CPM_ARGS_VERSION) - if (DEFINED CPM_ARGS_GIT_TAG) - cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) - endif() - if (NOT DEFINED CPM_ARGS_VERSION) - set(CPM_ARGS_VERSION 0) - endif() - endif() - - if (NOT DEFINED CPM_ARGS_GIT_TAG) - set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) - endif() - - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) - - if(CPM_ARGS_DOWNLOAD_ONLY) - set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) - else() - set(DOWNLOAD_ONLY NO) - endif() - - if (CPM_ARGS_GITHUB_REPOSITORY) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") - endif() - - if (CPM_ARGS_GITLAB_REPOSITORY) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") - endif() - - if ("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) - CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_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}).") - endif() - if (CPM_ARGS_OPTIONS) - foreach(OPTION ${CPM_ARGS_OPTIONS}) - cpm_parse_option(${OPTION}) - if(NOT "${${OPTION_KEY}}" STREQUAL ${OPTION_VALUE}) - message(WARNING "${CPM_INDENT} ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})") - endif() - endforeach() - endif() - cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY}) - cpm_get_fetch_properties(${CPM_ARGS_NAME}) - SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}") - SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}") - SET(${CPM_ARGS_NAME}_ADDED NO) - cpm_export_variables() - return() - endif() - - CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION}) + CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") if (CPM_ARGS_OPTIONS) foreach(OPTION ${CPM_ARGS_OPTIONS}) @@ -225,28 +284,28 @@ function(CPMAddPackage) endforeach() endif() - set(FETCH_CONTENT_DECLARE_EXTRA_OPTS "") - if (DEFINED CPM_ARGS_GIT_TAG) set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") + elseif (DEFINED CPM_ARGS_SOURCE_DIR) + set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") else() set(PACKAGE_INFO "${CPM_ARGS_VERSION}") endif() if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND) - set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) - elseif(DEFINED CPM_ARGS_SOURCE_DIR) - set(FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) + elseif (DEFINED CPM_ARGS_SOURCE_DIR) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) elseif (CPM_SOURCE_CACHE) string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) list(SORT origin_parameters) string(SHA1 origin_hash "${origin_parameters}") set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) - list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) if (EXISTS ${download_directory}) # disable the download command to allow offline builds - list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}") + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}") set(PACKAGE_INFO "${download_directory}") else() # remove timestamps so CMake will re-download the dependency @@ -255,23 +314,82 @@ function(CPMAddPackage) endif() endif() - 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_get_fetch_properties(${CPM_ARGS_NAME}) CPMCreateModuleFile(${CPM_ARGS_NAME} "CPMAddPackage(${ARGN})") + + if (CPM_PACKAGE_LOCK_ENABLED) + if ((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) + cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + elseif(CPM_ARGS_SOURCE_DIR) + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") + else() + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + endif() + endif() + + cpm_declare_fetch("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}") + cpm_fetch_package("${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}") + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + SET(${CPM_ARGS_NAME}_ADDED YES) - cpm_export_variables() + cpm_export_variables("${CPM_ARGS_NAME}") endfunction() +# Fetch a previously declared package +macro(CPMGetPackage Name) + if (DEFINED "CPM_DECLARATION_${Name}") + CPMAddPackage( + NAME ${Name} + ) + else() + message(SEND_ERROR "Cannot retrieve package ${Name}: no declaration available") + endif() +endmacro() + # export variables available to the caller to the parent scope # expects ${CPM_ARGS_NAME} to be set -macro(cpm_export_variables) - SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE) - SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" PARENT_SCOPE) - SET(${CPM_ARGS_NAME}_ADDED "${${CPM_ARGS_NAME}_ADDED}" PARENT_SCOPE) +macro(cpm_export_variables name) + SET(${name}_SOURCE_DIR "${${name}_SOURCE_DIR}" PARENT_SCOPE) + SET(${name}_BINARY_DIR "${${name}_BINARY_DIR}" PARENT_SCOPE) + SET(${name}_ADDED "${${name}_ADDED}" PARENT_SCOPE) endmacro() -# declares that a package has been added to CPM +# declares a package, so that any call to CPMAddPackage for the +# package name will use these arguments instead. +# Previous declarations will not be overriden. +macro(CPMDeclarePackage Name) + if (NOT DEFINED "CPM_DECLARATION_${Name}") + set("CPM_DECLARATION_${Name}" "${ARGN}") + endif() +endmacro() + +function(cpm_add_to_package_lock Name) + if (NOT CPM_DONT_CREATE_PACKAGE_LOCK) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name} \"${ARGN}\")\n") + endif() +endfunction() + +function(cpm_add_comment_to_package_lock Name) + if (NOT CPM_DONT_CREATE_PACKAGE_LOCK) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name} \"${ARGN}\")\n") + endif() +endfunction() + +# includes the package lock file if it exists and creates a target +# `cpm-write-package-lock` to update it +macro(CPMUsePackageLock file) + if (NOT CPM_DONT_CREATE_PACKAGE_LOCK) + get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) + if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + if (NOT TARGET cpm-update-package-lock) + add_custom_target(cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + set(CPM_PACKAGE_LOCK_ENABLED true) + endif() +endmacro() + +# registers a package that has been added to CPM function(CPMRegisterPackage PACKAGE VERSION) list(APPEND CPM_PACKAGES ${PACKAGE}) set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "") @@ -292,8 +410,7 @@ function (cpm_declare_fetch PACKAGE VERSION INFO) return() endif() - FetchContent_Declare( - ${PACKAGE} + FetchContent_Declare(${PACKAGE} ${ARGN} ) endfunction() @@ -311,22 +428,22 @@ endfunction() # downloads a previously declared package via FetchContent function (cpm_fetch_package PACKAGE DOWNLOAD_ONLY) - if (${CPM_DRY_RUN}) message(STATUS "${CPM_INDENT} package ${PACKAGE} not fetched (dry run)") return() endif() - set(CPM_OLD_INDENT "${CPM_INDENT}") - set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") - if(${DOWNLOAD_ONLY}) - if(NOT "${PACKAGE}_POPULATED") + if(DOWNLOAD_ONLY) + FetchContent_GetProperties(${PACKAGE}) + if(NOT ${PACKAGE}_POPULATED) FetchContent_Populate(${PACKAGE}) endif() else() + set(CPM_OLD_INDENT "${CPM_INDENT}") + set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") FetchContent_MakeAvailable(${PACKAGE}) + set(CPM_INDENT "${CPM_OLD_INDENT}") endif() - set(CPM_INDENT "${CPM_OLD_INDENT}") endfunction() # splits a package option