Compare commits

..

3 Commits

Author SHA1 Message Date
Lars Melchior
4425bd38cf Add FORCE parameter (#126)
* add scope protection

* rename to cpm_clean_argument_scope and update patch version

* undo unnecessary scope cleaning

* undo some changes

* consistent define checks
2020-05-29 15:37:49 +02:00
Lars Melchior
6da5c38dae Add package lock section to the readme (#125)
* add package lock section to the readme

* clarify position of package lock
2020-05-29 12:24:55 +02:00
Lars Melchior
76d7b27459 Fix typo and wording (#124)
* Fix typo

* Update README.md
2020-05-29 10:15:09 +02:00
2 changed files with 45 additions and 26 deletions

View File

@@ -105,7 +105,7 @@ Dependencies using CPM will automatically use the updated script of the outermos
- **No pre-built binaries** For every new build directory, all dependencies are initially downloaded and built from scratch. To avoid extra downloads it is recommend to set the [`CPM_SOURCE_CACHE`](#CPM_SOURCE_CACHE) environmental variable. Using a caching compiler such as [ccache](https://github.com/TheLartians/Ccache.cmake) can drastically reduce build time.
- **Dependent on good CMakeLists** Many libraries do not have CMakeLists that work well for subprojects. Luckily this is slowly changing, however, until then, some manual configuration may be required (see the snippets [below](#snippets) for examples). For best practices on preparing projects for CPM, see the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/Preparing-projects-for-CPM.cmake).
- **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C`@1.1 and `B`, which itself depends on `C`@1.2 the first added dependency will be used (in this case `C`@1.1). In this case, B requires a newer version of `C` than `A`, so CPM will emit a warning. This can be easily resolved by adding a new version of the dependency in the outermost project, or by introducing a [package lock file](https://github.com/TheLartians/CPM.cmake/wiki/Package-lock).
- **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C`@1.1 and `B`, which itself depends on `C`@1.2 the first added dependency will be used (in this case `C`@1.1). In this case, B requires a newer version of `C` than `A`, so CPM will emit a warning. This can be easily resolved by adding a new version of the dependency in the outermost project, or by introducing a [package lock file](#package-lock).
For projects with more complex needs and where an extra setup step doesn't matter, it may be worth to check out an external C++ package manager such as [vcpkg](https://github.com/microsoft/vcpkg), [conan](https://conan.io) or [hunter](https://github.com/ruslo/hunter).
Dependencies added with `CPMFindPackage` should work with external package managers.
@@ -139,9 +139,32 @@ These options can also be set as environmental variables.
## Local package override
Library developers are often in the situation where they work on a locally checked out dependency at the same time as on a consumer.
It is possible to override the consumer's dependency version with the local one by supplying the CMake option `CPM_<dependency name>_SOURCE`.
For example, to use the local version of `Dep` at the absolute path `/path/to/dep`, the consumer can be build with `cmake -H. -Bbuild -DCPM_Dep_SOURCE=/path/to/dep`.
Library developers are often in the situation where they work on a locally checked out dependency at the same time as on a consumer project.
It is possible to override the consumer's dependency with the version by supplying the CMake option `CPM_<dependency name>_SOURCE` set to the absolute path of the local library.
For example, to use the local version of the dependency `Dep` at the path `/path/to/dep`, the consumer can be built with the following command.
```bash
cmake -Bbuild -DCPM_Dep_SOURCE=/path/to/dep
```
## Package lock
In large projects with many transitive dependencies, it can be useful to introduce a package lock file.
This will list all CPM.cmake dependencies and can be used to update dependencies without modifying the original `CMakeLists.txt`.
To use a package lock, add the following line directly after including CPM.cmake.
```cmake
CPMUsePackageLock(package-lock.cmake)
```
To create or update the package lock file, build the `cpm-update-package-lock` target.
```bash
cmake -Bbuild
cmake --build build --target cpm-update-package-lock
```
See the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/Package-lock) for more info.
## Snippets

View File

@@ -28,7 +28,7 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
set(CURRENT_CPM_VERSION 0.26)
set(CURRENT_CPM_VERSION 0.26.1)
if(CPM_DIRECTORY)
if(NOT CPM_DIRECTORY STREQUAL CMAKE_CURRENT_LIST_DIR)
@@ -128,9 +128,6 @@ function(CPMFindPackage)
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 (CPM_DOWNLOAD_ALL)
@@ -158,13 +155,13 @@ endfunction()
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})
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})
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()
@@ -183,6 +180,7 @@ function(CPMAddPackage)
set(oneValueArgs
NAME
FORCE
VERSION
GIT_TAG
DOWNLOAD_ONLY
@@ -206,9 +204,6 @@ function(CPMAddPackage)
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(CPM_ARGS_DOWNLOAD_ONLY)
@@ -217,11 +212,11 @@ function(CPMAddPackage)
set(DOWNLOAD_ONLY NO)
endif()
if (CPM_ARGS_GITHUB_REPOSITORY)
if (DEFINED CPM_ARGS_GITHUB_REPOSITORY)
set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git")
endif()
if (CPM_ARGS_GITLAB_REPOSITORY)
if (DEFINED CPM_ARGS_GITLAB_REPOSITORY)
list(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
endif()
@@ -232,7 +227,7 @@ function(CPMAddPackage)
endif()
endif()
if (CPM_ARGS_GIT_TAG)
if (DEFINED CPM_ARGS_GIT_TAG)
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
endif()
@@ -244,19 +239,20 @@ function(CPMAddPackage)
endif()
# Check for manual overrides
if (NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "")
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_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "")
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})
@@ -279,7 +275,7 @@ function(CPMAddPackage)
endif()
endif()
CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})
CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}")
if (CPM_ARGS_OPTIONS)
foreach(OPTION ${CPM_ARGS_OPTIONS})
@@ -290,7 +286,7 @@ function(CPMAddPackage)
if (DEFINED CPM_ARGS_GIT_TAG)
set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}")
elseif(DEFINED CPM_ARGS_SOURCE_DIR)
elseif (DEFINED CPM_ARGS_SOURCE_DIR)
set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}")
else()
set(PACKAGE_INFO "${CPM_ARGS_VERSION}")
@@ -298,7 +294,7 @@ function(CPMAddPackage)
if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND)
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND})
elseif(DEFINED CPM_ARGS_SOURCE_DIR)
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)
@@ -330,12 +326,12 @@ function(CPMAddPackage)
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})
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_ARGS_NAME})
cpm_export_variables("${CPM_ARGS_NAME}")
endfunction()
# Fetch a previously declared package
@@ -360,7 +356,7 @@ endmacro()
# declares a package, so that any call to CPMAddPackage for the
# package name will use these arguments instead
macro(CPMDeclarePackage Name)
if (NOT DEFINED "CPM_DECLARATION_${Name}")
if (NOT DEFINED "${CPM_DECLARATION_${Name}}")
set("CPM_DECLARATION_${Name}" "${ARGN}")
endif()
endmacro()