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-10 09:56:57 +02:00
CPM is a simple GIT 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-09 13:36:23 +02:00
To add a new dependency to your project simply add the Projects target name, the git URL and the version. If the git tag for this version does not match the pattern `v$VERSION` , then the exact branch or tag can be specified with the `GIT_TAG` argument. CMake options can also be supplied with the package. If a package is not CMake compaitible it can still be downloaded with the `DOWNLOAD_ONLY` flag. See below for usage examples.
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-04-17 13:01:56 +02:00
GIT_TAG v1.8 # optional here, as indirectly defined by VERSION
OPTIONS # optional CMake arguments passed to the dependency
"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-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-09 13:31:18 +02:00
## Examples
### Catch2
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
)
```
### google/benchmark
2019-05-10 13:10:48 +02:00
Has a CMakeLists.txt that supports `add_subdirectory` , but needs some configuring.
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"
"BENCHMARK_USE_LIBCXX ON"
)
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)
```
### Lua
2019-05-10 12:03:09 +02:00
Has no CMakeLists.txt, target must be created manually.
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
)
CPMGetProperties(lua)
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-11 15:12:06 +02:00
### robin_hood::unordered_map
Has CMakeLists.txt, but it seems it is only used for testing.
Therefore we must create or own target.
```cmake
CPMAddPackage(
NAME RobinHood
VERSION 3.2.7
GIT_REPOSITORY https://github.com/martinus/robin-hood-hashing.git
DOWNLOAD_ONLY Yes
)
CPMGetProperties(RobinHood)
add_library(RobinHood INTERFACE IMPORTED)
target_include_directories(RobinHood INTERFACE "${RobinHood_SOURCE_DIR}/src/include")
```
2019-04-24 12:47:15 +02:00
## Updating CPM
2019-04-18 12:35:17 +02:00
2019-04-18 12:43:54 +02:00
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.
2019-04-18 12:35:17 +02:00
2019-05-09 13:36:23 +02:00
## Global Options
2019-04-15 14:06:21 +02:00
2019-04-22 20:01:41 +02:00
Setting the CMake option `CPM_USE_LOCAL_PACKAGES` will activate finding packages via `find_package` . If the option `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will only use local packages.
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-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-04-20 12:33:24 +02:00
- **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C` (v1.1) and `A` depends on `B` depends on `C` (v1.2)) the first added dependency will be used (in this case `C` @1 .1). If the current version is older than the version beeing added, or if provided options are incompatible, a CMake warning will be emitted. To resolve, add the new version of the common dependency to the outer project.
2019-04-10 16:43:57 +02:00
- **No auto-update** To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
2019-04-20 18:20:01 +02:00
- **No pre-built binaries** Unless they are installed or included in the linked repository.