2019-04-09 15:54:11 +02:00
[](https://travis-ci.com/TheLartians/CPM)
2019-04-09 15:27:47 +02:00
# CPM
2019-04-09 16:04:04 +02:00
2019-05-17 14:46:54 +02:00
CPM is a simple dependency manager written in CMake built on top of CMake's built-in [FetchContent ](https://cmake.org/cmake/help/latest/module/FetchContent.html ) module.
2019-04-15 14:42:17 +02:00
2019-04-24 12:47:15 +02:00
## Supported projects
2019-04-15 14:42:17 +02:00
Any project that you can add via `add_subdirectory` should already work with CPM.
2019-04-09 16:04:04 +02:00
2019-04-24 12:47:15 +02:00
## Usage
2019-04-09 16:04:04 +02:00
2019-05-17 18:42:24 +02:00
After `CPM.cmake` has been added to your project, you can call `CPMAddPackage` to add and recursively fetch dependencies at configure time. `CPMAddPackage` takes the following named arguments.
2019-05-17 14:46:54 +02:00
```cmake
CPMAddPackage(
NAME # The dependency name (usually chosen to match the target name)
VERSION # The minimum version of the dependency (optional, defaults to 0)
OPTIONS # Configuration options passed to the dependency (optional)
DOWNLOAD_ONLY # If set, the project is downloaded, but not configured (optional)
2019-05-17 15:13:54 +02:00
[...] # Origin options, see below
2019-05-17 14:46:54 +02:00
)
```
2019-05-17 15:58:02 +02:00
The origin is usually defined as a git repository and tag, but [svn revisions and direct URLs are also supported ](https://cmake.org/cmake/help/latest/module/FetchContent.html#declaring-content-details ).
2019-05-17 15:13:54 +02:00
If a `GIT_TAG` hasn't been explicitly specified it defaults to `v(VERSION)` , a common convention for github projects.
2019-05-17 15:58:02 +02:00
It can also be set to a branch name such as `master` to download the most recent version.
2019-05-17 14:46:54 +02:00
2019-05-17 18:43:52 +02:00
After calling `CPMAddPackage` , targets defined in the dependency can be added and the variables `(DEPENDENCY)_SOURCE_DIR` and `(DEPENDENCY)_BINARY_DIR` are set to the source and binary directory of the dependency.
2019-05-17 14:46:54 +02:00
2019-05-17 14:50:27 +02:00
## Full Example
2019-04-09 16:04:04 +02:00
```cmake
2019-04-11 10:13:28 +02:00
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2019-04-09 16:04:04 +02:00
2019-04-15 14:42:17 +02:00
# create project
2019-04-14 15:33:57 +02:00
project(MyProject)
2019-04-09 16:04:04 +02:00
# add dependencies
2019-05-10 16:03:46 +02:00
include(cmake/CPM.cmake)
2019-04-09 16:04:04 +02:00
CPMAddPackage(
NAME LarsParser
2019-04-17 13:01:56 +02:00
VERSION 1.8
2019-04-09 16:35:36 +02:00
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
2019-05-17 14:46:54 +02:00
OPTIONS
2019-04-17 13:01:56 +02:00
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
2019-04-09 16:04:04 +02:00
)
# add executable
2019-05-06 10:21:58 +02:00
add_executable(myProject myProject.cpp)
set_target_properties(myProject PROPERTIES CXX_STANDARD 17)
target_link_libraries(myProject LarsParser)
2019-04-09 16:04:04 +02:00
```
2019-04-09 16:35:15 +02:00
2019-05-17 14:50:27 +02:00
See the [examples directory ](https://github.com/TheLartians/CPM/tree/master/examples ) for more examples with source code.
2019-05-17 14:46:54 +02:00
2019-05-09 13:45:44 +02:00
## Adding CPM
To add CPM to your current project, simply add `cmake/CPM.cmake` to your project's `cmake` directory. The command below will perform this automatically.
```bash
wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master/cmake/CPM.cmake
```
2019-05-17 14:46:54 +02:00
## Updating CPM
To update CPM to the newest version, simply update the script in the project's cmake directory, for example by running the command above. Dependencies using CPM will automatically use the updated script of the outermost project.
## Snipplets
2019-05-17 15:13:54 +02:00
These examples demonstrate how to include some well-known projects with CPM.
2019-05-09 13:31:18 +02:00
2019-05-17 18:15:08 +02:00
### [Catch2](https://github.com/catchorg/Catch2.git)
2019-05-09 13:31:18 +02:00
2019-05-10 12:03:09 +02:00
Has a CMakeLists.txt that supports `add_subdirectory` .
2019-05-09 13:31:18 +02:00
```cmake
CPMAddPackage(
NAME Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
VERSION 2.5.0
)
```
2019-05-17 18:15:08 +02:00
### [google/benchmark](https://github.com/google/benchmark.git)
2019-05-09 13:31:18 +02:00
2019-05-17 14:46:54 +02:00
Has a CMakeLists.txt that supports `add_subdirectory` , but needs some configuring to work without external dependencies.
2019-05-10 12:03:09 +02:00
2019-05-09 13:31:18 +02:00
```cmake
CPMAddPackage(
NAME benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
VERSION 1.4.1
OPTIONS
"BENCHMARK_ENABLE_TESTING Off"
)
2019-05-09 13:36:23 +02:00
# needed to compile with C++17
2019-05-09 13:31:18 +02:00
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
```
2019-05-17 18:15:08 +02:00
### [Simple match](https://github.com/jbandela/simple_match.git)
2019-05-17 18:13:37 +02:00
Header-only library without releases or CMakeLists.txt, target must be created manually.
```cmake
CPMAddPackage(
NAME simple_match
GIT_REPOSITORY https://github.com/jbandela/simple_match.git
GIT_TAG a3ab17f3d98db302de68ad85ed399a42ae41889e
DOWNLOAD_ONLY True
)
add_library(simple_match INTERFACE IMPORTED)
target_include_directories(simple_match INTERFACE "${simple_match_SOURCE_DIR}/include")
```
2019-05-17 18:15:08 +02:00
### [Lua](https://www.lua.org)
2019-05-09 13:31:18 +02:00
2019-05-17 18:13:37 +02:00
Library without CMakeLists.txt, target must be created manually.
2019-05-10 12:03:09 +02:00
2019-05-09 13:31:18 +02:00
```cmake
CPMAddPackage(
NAME lua
GIT_REPOSITORY https://github.com/lua/lua.git
VERSION 5-3-4
GIT_SHALLOW YES
2019-05-10 12:03:09 +02:00
DOWNLOAD_ONLY YES
2019-05-09 13:31:18 +02:00
)
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
2019-05-11 15:44:22 +02:00
add_library(lua STATIC ${lua_sources})
2019-05-09 13:31:18 +02:00
target_include_directories(lua
PUBLIC
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
)
```
2019-05-17 14:46:54 +02:00
## Local packages
2019-04-18 12:35:17 +02:00
2019-05-17 14:46:54 +02:00
CPM can be configured to use `find_package` to search for locally installed dependencies first.
If `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will error when dependency is not found locally.
2019-04-15 14:06:21 +02:00
2019-04-24 12:47:15 +02:00
## Advantages
2019-04-10 16:43:57 +02:00
2019-04-17 13:58:29 +02:00
- **Small repos** CPM takes care of project dependencies, allowing you to focus on creating small, well-tested frameworks.
- **Cross-Plattform** CPM adds projects via `add_subdirectory` , which is compatible with all cmake toolchains and generators.
2019-04-15 14:06:21 +02:00
- **Reproducable builds** By using versioning via git tags it is ensured that a project will always be in the same state everywhere.
2019-04-17 12:52:42 +02:00
- **No installation required** No need to install anything. Just add the script to your project and you're good to go.
2019-04-17 13:58:29 +02:00
- **No Setup required** There is a good chance your existing projects already work as CPM dependencies.
2019-05-17 14:46:54 +02:00
- **Simple source distribution** CPM makes including projects with source files easy, reducing the need for monolithic header files.
2019-04-10 16:43:57 +02:00
2019-04-24 12:47:15 +02:00
## Limitations
2019-04-09 16:35:15 +02:00
2019-05-17 14:46:54 +02:00
- **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 an error. This can be resolved by updating the outermost dependency version.
- **No auto-update** To update a dependency, version must be adapted manually and there is no way for CPM to figure out the most recent version.
- **No pre-built binaries** Unless they are installed or included in the linked repository.
2019-05-17 15:14:47 +02:00
For projects with more complex needs and where an extra setup step doesn't matter, it is worth to check out fully featured C++ package managers such as [conan ](https://conan.io ) or [hunter ](https://github.com/ruslo/hunter ).