Compare commits

...

5 Commits

Author SHA1 Message Date
Lars Melchior
c3c7e2d9a3 add on-demand download script to release (#146) 2020-08-20 09:32:03 +02:00
Lars Melchior
30b98af416 Add link to tutorial (#143) 2020-08-12 13:41:40 +02:00
Lars Melchior
5063c7d992 Add built with CPM.cmake section (#142)
* add built with cpm.cmake section

* update text

* explicitly set width
2020-08-11 14:49:21 +02:00
Lars Melchior
3cfb309f3c add comparisons to other approaches (#141) 2020-08-11 12:49:37 +02:00
Lars Melchior
25603ac4ad Update publish script to set CPM.cmake version automatically (#139)
* update publish script to set CPM.cmake version automatically

* set a minimum development version to avoid deprecated error handler call
2020-08-04 13:12:35 +02:00
4 changed files with 95 additions and 5 deletions

View File

@@ -11,11 +11,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set CPM version by tag
run: |
mkdir dist
sed "s/0.27.2-development-version/${GITHUB_REF/refs\/tags\/v}/g" cmake/CPM.cmake > dist/CPM.cmake
sed "s/0.27.2-development-version/${GITHUB_REF/refs\/tags\/v}/g" cmake/get_cpm.cmake > dist/get_cpm.cmake
- name: Upload CPM.cmake to release
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: cmake/CPM.cmake
asset_name: CPM.cmake
tag: ${{ github.ref }}
file: dist/*.cmake
file_glob: true
overwrite: true

View File

@@ -10,7 +10,7 @@
# Setup-free CMake dependency management
CPM.cmake is a CMake script that adds dependency management capabilities to CMake.
It's built as a thin wrapper around CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module that adds version control, caching and a simple API.
It's built as a thin wrapper around CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module that adds version control, caching, a simple API [and more](#comparison-to-pure-fetchcontent--externalproject).
## Manage everything
@@ -49,6 +49,8 @@ After calling `CPMAddPackage` or `CPMFindPackage`, the following variables are d
The difference between `CPMFindPackage` and `CPMAddPackage` is that `CPMFindPackage` will try to find a local dependency via CMake's `find_package` and fallback to `CPMAddPackage` if the dependency is not found.
This behaviour can be also modified globally via [CPM options](#options).
See [this medium article](https://medium.com/swlh/cpm-an-awesome-dependency-manager-for-c-with-cmake-3c53f4376766) for a short tutorial on using CPM.cmake.
## Full CMakeLists Example
```cmake
@@ -73,7 +75,7 @@ CPMAddPackage(
target_link_libraries(tests Catch2)
```
See the [examples directory](https://github.com/TheLartians/CPM.cmake/tree/master/examples) for complete examples with source code or the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/More-Snippets) for example snippets.
See the [examples directory](https://github.com/TheLartians/CPM.cmake/tree/master/examples) for complete examples with source code and check [below](#snippets) or in the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/More-Snippets) for example snippets.
## Adding CPM
@@ -111,6 +113,33 @@ For projects with more complex needs and where an extra setup step doesn't matte
Dependencies added with `CPMFindPackage` should work with external package managers.
Additionally, the option [`CPM_USE_LOCAL_PACKAGES`](#cpmuselocalpackages) will enable `find_package` for all CPM dependencies.
## Comparison to FindPackage
The usual way to add libraries in CMake projects is to call `find_package(<PackageName>)` and to link against libraries defined in a `<PackageName>_LIBRARIES` variable.
While simple, this may lead to unpredictable builds, as it requires the library to be installed on the system and it is unclear which version of the library has been added.
Additionally, it is difficult to cross-compile projects (e.g. for mobile), as the dependencies will need to be rebuilt manually for each targeted architecture.
CPM.cmake allows dependencies to be unambiguously defined and builds them from source.
Note that the behaviour differs from `find_package`, as variables exported to the parent scope (such as `<PackageName>_LIBRARIES`) will not be visible after adding a package using CPM.cmake.
The behaviour can be [achieved manually](https://github.com/TheLartians/CPM.cmake/issues/132#issuecomment-644955140), if required.
## Comparison to pure FetchContent / ExternalProject
CPM.cmake is a wrapper for CMake's FetchContent module and adds a number of features that turn it into a useful dependency manager.
The most notable features are:
- A simpler to use API
- Version checking: CPM.cmake will check the version number of any added dependency and omit a warning if another dependency requires a more recent version.
- Options: any Options passed to a dependency are stored and compared on later use, so if another dependency tries to add an existing dependency with incompatible options a warning will be emitted to the user.
- Offline builds: CPM.cmake will override CMake's download and update commands, which allows new builds to be configured while offline if all dependencies [are available locally](#cpm_source_cache).
- Automatic shallow clone: if a version tag (e.g. `v2.2.0`) is provided and `CPM_SOURCE_CACHE` is used, CPM.cmake will perform a shallow clone of the dependency, which should be significantly faster while using less storage than a full clone.
- Overridable: all `CPMAddPackage` can be configured to use `find_package` by setting a [CMake flag](#cpm_use_local_packages), making it easy to integrate into projects that may require local versioning through the system's package manager.
- [Package lock files](#package-lock) for easier transitive dependency management.
- Dependencies can be overridden [per-build](#local-package-override) using CMake CLI parameters.
ExternalProject works similarly as FetchContent, however waits with adding dependencies until build time.
This has a quite a few disadvantages, especially as it makes using custom toolchains / cross-compiling very difficult and can lead to problems with nested dependencies.
## Options
### CPM_SOURCE_CACHE
@@ -166,6 +195,41 @@ cmake --build build --target cpm-update-package-lock
See the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/Package-lock) for more info.
## Built with CPM.cmake
Some amazing projects that are built using the CPM.cmake package manager.
If you know others, feel free to add them here through a PR.
<table>
<tr>
<td>
<a href="https://otto-project.github.io">
<p align="center">
<img src="https://avatars2.githubusercontent.com/u/40357059?s=200&v=4" alt="otto-project" height=100pt width=100pt />
</p>
<p align="center"><b>OTTO - The Open Source GrooveBox</b></p>
</a>
</td>
<td>
<a href="https://maphi.app">
<p align="center">
<img src="https://user-images.githubusercontent.com/4437447/89892751-71af8000-dbd7-11ea-9d87-4fe107845069.png" alt="maphi" height=100pt width=100pt />
</p>
<p align="center"><b>Maphi - the Math App</b></p>
</a>
</td>
<td>
<a href="https://github.com/TheLartians/ModernCppStarter">
<p align="center">
<img src="https://repository-images.githubusercontent.com/254842585/4dfa7580-7ffb-11ea-99d0-46b8fe2f4170" alt="modern-cpp-starter" height=100pt width=200pt />
</p>
<p align="center"><b>ModernCppStarter</b></p>
</a>
</td>
</tr>
</table>
## Snippets
These examples demonstrate how to include some well-known projects with CPM.

View File

@@ -28,7 +28,7 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
set(CURRENT_CPM_VERSION 0.27.2)
set(CURRENT_CPM_VERSION 0.27.2-development-version)
if(CPM_DIRECTORY)
if(NOT CPM_DIRECTORY STREQUAL CMAKE_CURRENT_LIST_DIR)

19
cmake/get_cpm.cmake Normal file
View File

@@ -0,0 +1,19 @@
set(CPM_DOWNLOAD_VERSION 0.27.2-development-version)
if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()
include(${CPM_DOWNLOAD_LOCATION})