Compare commits

...

26 Commits
v0.9 ... v0.10

Author SHA1 Message Date
Lars Melchior
bf5e9ebafb Add DEPENDENCY_ADDED property (#50)
* add DEPENDENCY_ADDED property

* update examples

* Update README.md

* Update README.md

* Update CPM.cmake

* Update README.md
2019-05-20 01:52:55 +02:00
Lars Melchior
275a12f2d8 Update README.md 2019-05-19 21:48:31 +02:00
Lars Melchior
6878cf622b Update README.md 2019-05-19 21:48:11 +02:00
Lars Melchior
4f57bd4b5b Update README.md 2019-05-19 20:41:19 +02:00
Lars Melchior
bf1996b9f4 Update README.md 2019-05-19 20:39:40 +02:00
Lars Melchior
f72944e897 Update README.md 2019-05-19 20:15:30 +02:00
Lars Melchior
79cbebb4f9 Update README.md 2019-05-19 20:14:23 +02:00
Lars Melchior
d3b23be4e2 Update README.md 2019-05-19 20:04:44 +02:00
Lars Melchior
5352247b4d Update README.md 2019-05-19 19:38:32 +02:00
Lars Melchior
798e4e35bf Update README.md 2019-05-19 19:30:55 +02:00
Lars Melchior
7d6297c5c6 Update CMakeLists.txt 2019-05-19 19:04:27 +02:00
Lars Melchior
e0bfe05874 update examples 2019-05-19 19:03:56 +02:00
Lars Melchior
530fc8d42f Update README.md 2019-05-19 19:02:33 +02:00
Lars Melchior
a086f637fb Update README.md 2019-05-19 15:08:34 +02:00
Lars Melchior
5203eb6e30 Update README.md 2019-05-17 18:44:42 +02:00
Lars Melchior
7a8599cf34 Update README.md 2019-05-17 18:43:52 +02:00
Lars Melchior
bd3732706a Update README.md 2019-05-17 18:42:24 +02:00
Lars Melchior
84d6e53c65 Update README.md 2019-05-17 18:42:05 +02:00
Lars Melchior
7bbc667df1 add imported flag (#48) 2019-05-17 18:21:36 +02:00
Lars Melchior
f5050169b0 Update README.md 2019-05-17 18:15:08 +02:00
Lars Melchior
a7ab78f3bf Update README.md 2019-05-17 18:13:37 +02:00
Lars Melchior
b1ef60175a New examples (#47)
* simplify yaml build

* add simple_match
2019-05-17 18:07:31 +02:00
Lars Melchior
8253393c75 add yaml example 2019-05-17 17:32:52 +02:00
Lars Melchior
f9ba3f2457 Update README.md 2019-05-17 15:58:02 +02:00
Lars Melchior
77e74910bc Update README.md 2019-05-17 15:14:47 +02:00
Lars Melchior
55caef9286 Update README.md 2019-05-17 15:13:54 +02:00
11 changed files with 228 additions and 29 deletions

View File

@@ -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).

View File

@@ -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)

View File

@@ -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 ----

View File

@@ -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 ----

View 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
View 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;
}

View 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)

View 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;
}

View 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
View 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;
}

View 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