mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-19 07:37:42 -05:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf5e9ebafb | ||
|
|
275a12f2d8 | ||
|
|
6878cf622b | ||
|
|
4f57bd4b5b | ||
|
|
bf1996b9f4 | ||
|
|
f72944e897 | ||
|
|
79cbebb4f9 | ||
|
|
d3b23be4e2 | ||
|
|
5352247b4d | ||
|
|
798e4e35bf | ||
|
|
7d6297c5c6 | ||
|
|
e0bfe05874 | ||
|
|
530fc8d42f | ||
|
|
a086f637fb | ||
|
|
5203eb6e30 | ||
|
|
7a8599cf34 | ||
|
|
bd3732706a | ||
|
|
84d6e53c65 | ||
|
|
7bbc667df1 | ||
|
|
f5050169b0 | ||
|
|
a7ab78f3bf | ||
|
|
b1ef60175a | ||
|
|
8253393c75 | ||
|
|
f9ba3f2457 | ||
|
|
77e74910bc | ||
|
|
55caef9286 |
79
README.md
79
README.md
@@ -2,7 +2,8 @@
|
||||
|
||||
# CPM
|
||||
|
||||
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.
|
||||
CPM is a CMake script that adds dependency management capabilities to CMake.
|
||||
It's built as an extension of CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module that adds version control and simpler usage.
|
||||
|
||||
## Supported projects
|
||||
|
||||
@@ -10,24 +11,29 @@ Any project that you can add via `add_subdirectory` should already work with CPM
|
||||
|
||||
## Usage
|
||||
|
||||
After `CPM.cmake` has been added to your project, you can call `CPMAddPackage` for every dependency of the project with the following named parameters.
|
||||
After `CPM.cmake` has been added to your project, the function `CPMAddPackage` can be used to fetch and configure all dependencies.
|
||||
Afterwards all targets defined in the dependencies can be used.
|
||||
`CPMAddPackage` takes the following named arguments.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME # The dependency name (usually chosen to match the target name)
|
||||
NAME # The name of the dependency (should be chosen to match the main target's 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)
|
||||
[...] # Source options, see below
|
||||
[...] # Origin paramters forwarded to FetchContent_Declare, see below
|
||||
)
|
||||
```
|
||||
|
||||
The command downloads the project defined by the source options if a newer version hasn't been included before.
|
||||
The source is usually a git repository, but svn and direct urls are als supported.
|
||||
See the [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) documentation for all available options.
|
||||
If a `GIT_TAG` hasn't been explicitly specified it defaults to `v$VERSION` which is a common convention for github projects.
|
||||
The origin is usually specified by a `GIT_REPOSITORY`, but [svn revisions and direct URLs are also supported](https://cmake.org/cmake/help/latest/module/FetchContent.html#declaring-content-details).
|
||||
If `GIT_TAG` hasn't been explicitly specified it defaults to `v(VERSION)`, a common convention for github projects.
|
||||
`GIT_TAG` can also be set to a branch name such as `master` to download the most recent version.
|
||||
|
||||
After calling `CPMAddPackage`, the variables `(DEPENDENCY)_SOURCE_DIR` and `(DEPENDENCY)_BINARY_DIR` are set, where `(DEPENDENCY)` is the name of the dependency.
|
||||
Besides downloading and to configuring the dependency, the following variables are defined in the local scope, where `(DEPENDENCY)` is the name of the dependency.
|
||||
|
||||
- `(DEPENDENCY)_SOURCE_DIR` is the path to the source of the dependency.
|
||||
- `(DEPENDENCY)_BINARY_DIR` is the path to the build directory of the dependency.
|
||||
- `(DEPENDENCY)_ADDED` is set to `YES` if the dependency has not been added before, otherwise it is set to `NO`.
|
||||
|
||||
## Full Example
|
||||
|
||||
@@ -70,9 +76,9 @@ To update CPM to the newest version, simply update the script in the project's c
|
||||
|
||||
## Snipplets
|
||||
|
||||
These are some small snipplets demonstrating how to include some projects used with CPM.
|
||||
These examples demonstrate how to include some well-known projects with CPM.
|
||||
|
||||
### Catch2
|
||||
### [Catch2](https://github.com/catchorg/Catch2.git)
|
||||
|
||||
Has a CMakeLists.txt that supports `add_subdirectory`.
|
||||
|
||||
@@ -84,7 +90,9 @@ CPMAddPackage(
|
||||
)
|
||||
```
|
||||
|
||||
### google/benchmark
|
||||
See [here](https://github.com/TheLartians/CPM/blob/master/examples/doctest/CMakeLists.txt) for doctest example.
|
||||
|
||||
### [google/benchmark](https://github.com/google/benchmark.git)
|
||||
|
||||
Has a CMakeLists.txt that supports `add_subdirectory`, but needs some configuring to work without external dependencies.
|
||||
|
||||
@@ -94,33 +102,53 @@ CPMAddPackage(
|
||||
GIT_REPOSITORY https://github.com/google/benchmark.git
|
||||
VERSION 1.4.1
|
||||
OPTIONS
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
)
|
||||
|
||||
# needed to compile with C++17
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
```
|
||||
|
||||
### Lua
|
||||
### [nlohmann/json](https://github.com/nlohmann/json)
|
||||
|
||||
Has no CMakeLists.txt, so a target must be created manually.
|
||||
Header-only library with a huge git repositoy.
|
||||
Instead of downloading the whole repositoy, we fetch the zip included with the release and create our own target.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME nlohmann_json
|
||||
VERSION 3.6.1
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip
|
||||
URL_HASH SHA256=69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf
|
||||
)
|
||||
|
||||
if (nlohmann_json_ADDED)
|
||||
add_library(nlohmann_json INTERFACE)
|
||||
target_include_directories(nlohmann_json INTERFACE ${nlohmann_json_SOURCE_DIR})
|
||||
endif()
|
||||
```
|
||||
|
||||
### [Lua](https://www.lua.org)
|
||||
|
||||
Library without CMakeLists.txt, target must be created manually.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME lua
|
||||
GIT_REPOSITORY https://github.com/lua/lua.git
|
||||
VERSION 5-3-4
|
||||
GIT_SHALLOW YES
|
||||
DOWNLOAD_ONLY YES
|
||||
)
|
||||
|
||||
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
|
||||
add_library(lua STATIC ${lua_sources})
|
||||
if (lua_ADDED)
|
||||
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
|
||||
add_library(lua STATIC ${lua_sources})
|
||||
|
||||
target_include_directories(lua
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
|
||||
)
|
||||
target_include_directories(lua
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
|
||||
)
|
||||
endif()
|
||||
```
|
||||
|
||||
## Local packages
|
||||
@@ -130,12 +158,13 @@ If `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will error when dependency is not found
|
||||
|
||||
## Advantages
|
||||
|
||||
- **Small repos** CPM takes care of project dependencies, allowing you to focus on creating small, well-tested frameworks.
|
||||
- **Small and reusable projects** CPM takes care of project dependencies, allowing developers 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.
|
||||
- **Reproducable builds** By using versioning via git tags it is ensured that a project will always be in the same state everywhere.
|
||||
- **Recursive dependencies** Ensures that no dependency is added twice and is added in the minimum required version.
|
||||
- **No installation required** No need to install anything. Just add the script to your project and you're good to go.
|
||||
- **No Setup required** There is a good chance your existing projects already work as CPM dependencies.
|
||||
- **Simple source distribution** CPM makes including projects with source files easy, reducing the need for monolithic header files.
|
||||
- **Simple source distribution** CPM makes including projects with source files and dependencies easy, reducing the need for monolithic header files.
|
||||
|
||||
## Limitations
|
||||
|
||||
@@ -143,4 +172,4 @@ If `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will error when dependency is not found
|
||||
- **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.
|
||||
|
||||
For projects with more complex needs and 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) instead.
|
||||
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).
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
set(CURRENT_CPM_VERSION 0.9)
|
||||
set(CURRENT_CPM_VERSION 0.10)
|
||||
|
||||
if(CPM_DIRECTORY)
|
||||
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
|
||||
@@ -139,6 +139,7 @@ function(CPMAddPackage)
|
||||
CPMGetProperties(${CPM_ARGS_NAME})
|
||||
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 NO PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
|
||||
@@ -156,6 +157,7 @@ function(CPMAddPackage)
|
||||
CPMGetProperties(${CPM_ARGS_NAME})
|
||||
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 YES PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function (CPM_DECLARE_PACKAGE PACKAGE VERSION GIT_TAG)
|
||||
|
||||
@@ -18,8 +18,10 @@ CPMAddPackage(
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
)
|
||||
|
||||
# patch google benchmark target
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
if (benchmark_ADDED)
|
||||
# patch google benchmark target
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
endif()
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ CPMAddPackage(
|
||||
GIT_REPOSITORY https://github.com/onqtam/doctest.git
|
||||
VERSION 2.3.2
|
||||
GIT_TAG 2.3.2
|
||||
GIT_SHALLOW True
|
||||
)
|
||||
|
||||
# ---- Create binary ----
|
||||
|
||||
24
examples/json/CMakeLists.txt
Normal file
24
examples/json/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME nlohmann_json
|
||||
VERSION 3.6.1
|
||||
# not using the repo as it takes forever to clone
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip
|
||||
URL_HASH SHA256=69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf
|
||||
)
|
||||
|
||||
if(nlohmann_json_ADDED)
|
||||
add_library(nlohmann_json INTERFACE)
|
||||
target_include_directories(nlohmann_json INTERFACE ${nlohmann_json_SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
add_executable(CPMJSONExample "main.cpp")
|
||||
set_target_properties(CPMJSONExample PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(CPMJSONExample nlohmann_json)
|
||||
25
examples/json/main.cpp
Normal file
25
examples/json/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
int main(){
|
||||
|
||||
nlohmann::json json = {
|
||||
{"pi", 3.141},
|
||||
{"happy", true},
|
||||
{"name", "Niels"},
|
||||
{"nothing", nullptr},
|
||||
{"answer", {
|
||||
{"everything", 42}
|
||||
}},
|
||||
{"list", {1, 0, 2}},
|
||||
{"object", {
|
||||
{"currency", "USD"},
|
||||
{"value", 42.99}
|
||||
}}
|
||||
};
|
||||
|
||||
std::cout << "declared JSON object: " << std::setw(2) << json << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
examples/simple_match/CMakeLists.txt
Normal file
24
examples/simple_match/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME simple_match
|
||||
GIT_REPOSITORY https://github.com/jbandela/simple_match.git
|
||||
GIT_TAG a3ab17f3d98db302de68ad85ed399a42ae41889e
|
||||
DOWNLOAD_ONLY True
|
||||
)
|
||||
|
||||
if(simple_match_ADDED)
|
||||
add_library(simple_match INTERFACE IMPORTED)
|
||||
target_include_directories(simple_match INTERFACE "${simple_match_SOURCE_DIR}/include")
|
||||
endif()
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
add_executable(CPMSimpleMatchExample "main.cpp")
|
||||
set_target_properties(CPMSimpleMatchExample PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(CPMSimpleMatchExample simple_match)
|
||||
|
||||
34
examples/simple_match/main.cpp
Normal file
34
examples/simple_match/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <simple_match/simple_match.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char ** argv){
|
||||
using namespace simple_match;
|
||||
using namespace simple_match::placeholders;
|
||||
|
||||
std::string input;
|
||||
std::cout << "please enter a number or 'quit' to exit" << std::endl;
|
||||
|
||||
while (true) {
|
||||
std::cout << "> ";
|
||||
std::getline(std::cin, input);
|
||||
if (input == "quit") { break; }
|
||||
int x;
|
||||
try {
|
||||
x = std::stoi(input);
|
||||
} catch(std::invalid_argument &) {
|
||||
std::cout << "invalid input" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
match(x,
|
||||
1, []() {std::cout << "The answer is one\n"; },
|
||||
2, []() {std::cout << "The answer is two\n"; },
|
||||
_x < 10, [](auto&& a) {std::cout << "The answer " << a << " is less than 10\n"; },
|
||||
10 < _x < 20, [](auto&& a) {std::cout << "The answer " << a << " is between 10 and 20 exclusive\n"; },
|
||||
_, []() {std::cout << "Did not match\n"; }
|
||||
);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/yaml/CMakeLists.txt
Normal file
25
examples/yaml/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME yaml-cpp
|
||||
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
|
||||
# 0.6.2 uses depricated CMake syntax
|
||||
VERSION 0.6.3
|
||||
# 0.6.3 is not released yet, so use the most recent commit
|
||||
GIT_TAG 012269756149ae99745b6dafefd415843d7420bb
|
||||
OPTIONS
|
||||
"YAML_CPP_BUILD_TESTS Off"
|
||||
"YAML_CPP_BUILD_CONTRIB Off"
|
||||
"YAML_CPP_BUILD_TOOLS Off"
|
||||
)
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
add_executable(CPMYamlExample "main.cpp")
|
||||
set_target_properties(CPMYamlExample PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(CPMYamlExample yaml-cpp)
|
||||
|
||||
14
examples/yaml/main.cpp
Normal file
14
examples/yaml/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char ** argv){
|
||||
if (argc != 2) {
|
||||
std::cout << "usage: " << argv[0] << " <path to yaml file>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
YAML::Node config = YAML::LoadFile(argv[1]);
|
||||
std::cout << "Parsed YAML:\n" << config << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
examples/yaml/monsters.yaml
Normal file
21
examples/yaml/monsters.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
- name: Ogre
|
||||
position: [0, 5, 0]
|
||||
powers:
|
||||
- name: Club
|
||||
damage: 10
|
||||
- name: Fist
|
||||
damage: 8
|
||||
- name: Dragon
|
||||
position: [1, 0, 10]
|
||||
powers:
|
||||
- name: Fire Breath
|
||||
damage: 25
|
||||
- name: Claws
|
||||
damage: 15
|
||||
- name: Wizard
|
||||
position: [5, -3, 0]
|
||||
powers:
|
||||
- name: Acid Rain
|
||||
damage: 50
|
||||
- name: Staff
|
||||
damage: 3
|
||||
Reference in New Issue
Block a user