mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-21 01:47:28 -05:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4f055c7aa | ||
|
|
002cf6b64c | ||
|
|
f13c343859 | ||
|
|
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 |
72
README.md
72
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,28 +76,31 @@ 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`.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GITHUB_REPOSITORY catchorg/Catch2
|
||||
VERSION 2.5.0
|
||||
)
|
||||
```
|
||||
|
||||
### google/benchmark
|
||||
See [here](https://github.com/TheLartians/CPM/blob/master/examples/doctest/CMakeLists.txt) for doctest example.
|
||||
Note that we can shorten Github and Gitlab URLs by using `GITHUB_REPOSITORY` or `GITLAB_REPOSITORY`, respectively.
|
||||
|
||||
### [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.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME benchmark
|
||||
GIT_REPOSITORY https://github.com/google/benchmark.git
|
||||
GITHUB_REPOSITORY google/benchmark
|
||||
VERSION 1.4.1
|
||||
OPTIONS
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
@@ -101,19 +110,40 @@ CPMAddPackage(
|
||||
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()
|
||||
```
|
||||
|
||||
Adding large repos works as well, but takes a while for the initial clone.
|
||||
|
||||
### [Lua](https://www.lua.org)
|
||||
|
||||
Lua does not oficially support CMake, so we query the sources and create our own target.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME lua
|
||||
GIT_REPOSITORY https://github.com/lua/lua.git
|
||||
VERSION 5-3-4
|
||||
GIT_SHALLOW YES
|
||||
DOWNLOAD_ONLY YES
|
||||
)
|
||||
|
||||
if (lua_ADDED)
|
||||
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
|
||||
add_library(lua STATIC ${lua_sources})
|
||||
|
||||
@@ -121,6 +151,7 @@ target_include_directories(lua
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
|
||||
)
|
||||
endif()
|
||||
```
|
||||
|
||||
## Local packages
|
||||
@@ -130,12 +161,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 +175,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,23 +28,18 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
set(CURRENT_CPM_VERSION 0.9)
|
||||
set(CURRENT_CPM_VERSION 0.11)
|
||||
|
||||
if(CPM_DIRECTORY)
|
||||
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
|
||||
if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION})
|
||||
message(AUTHOR_WARNING "${CPM_INDENT} \
|
||||
A dependency is using a more recent CPM (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \
|
||||
It is recommended to upgrade CPM to the most recent version. \
|
||||
See https://github.com/TheLartians/CPM for more information.\
|
||||
")
|
||||
CPM_HANDLE_OLD_VERSION(${CURRENT_CPM_VERSION})
|
||||
endif()
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "")
|
||||
|
||||
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
|
||||
set(CPM_PACKAGES "" CACHE INTERNAL "")
|
||||
|
||||
@@ -54,35 +49,21 @@ option(CPM_LOCAL_PACKAGES_ONLY "Use only locally installed packages" OFF)
|
||||
include(FetchContent)
|
||||
include(CMakeParseArguments)
|
||||
|
||||
# Initialize logging prefix
|
||||
if(NOT CPM_INDENT)
|
||||
set(CPM_INDENT "CPM:")
|
||||
endif()
|
||||
|
||||
function(CPMRegisterPackage PACKAGE VERSION)
|
||||
list(APPEND CPM_PACKAGES ${PACKAGE})
|
||||
set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "")
|
||||
set("CPM_PACKAGE_${PACKAGE}_VERSION" ${VERSION} CACHE INTERNAL "")
|
||||
endfunction()
|
||||
|
||||
function(CPM_GET_PACKAGE_VERSION PACKAGE)
|
||||
set(CPM_PACKAGE_VERSION "${CPM_PACKAGE_${PACKAGE}_VERSION}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(CPM_PARSE_OPTION OPTION)
|
||||
string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION})
|
||||
string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH)
|
||||
math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1")
|
||||
string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE)
|
||||
set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE)
|
||||
set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# The main workhorse of CPM
|
||||
function(CPMAddPackage)
|
||||
|
||||
set(oneValueArgs
|
||||
NAME
|
||||
VERSION
|
||||
GIT_TAG
|
||||
DOWNLOAD_ONLY
|
||||
GITHUB_REPOSITORY
|
||||
GITLAB_REPOSITORY
|
||||
)
|
||||
|
||||
set(multiValueArgs
|
||||
@@ -116,12 +97,22 @@ function(CPMAddPackage)
|
||||
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
|
||||
endif()
|
||||
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
|
||||
|
||||
if(CPM_ARGS_DOWNLOAD_ONLY)
|
||||
set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY})
|
||||
else()
|
||||
set(DOWNLOAD_ONLY NO)
|
||||
endif()
|
||||
|
||||
if (CPM_ARGS_GITHUB_REPOSITORY)
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git")
|
||||
endif()
|
||||
|
||||
if (CPM_ARGS_GITLAB_REPOSITORY)
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITHUB_REPOSITORY}.git")
|
||||
endif()
|
||||
|
||||
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
|
||||
CPM_GET_PACKAGE_VERSION(${CPM_ARGS_NAME})
|
||||
if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION})
|
||||
@@ -139,6 +130,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 +148,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)
|
||||
@@ -163,7 +156,6 @@ function (CPM_DECLARE_PACKAGE PACKAGE VERSION GIT_TAG)
|
||||
|
||||
FetchContent_Declare(
|
||||
${PACKAGE}
|
||||
GIT_TAG ${GIT_TAG}
|
||||
${ARGN}
|
||||
)
|
||||
endfunction()
|
||||
@@ -187,3 +179,32 @@ function (CPMGetProperties PACKAGE)
|
||||
SET(${PACKAGE}_SOURCE_DIR "${${lpackage}_SOURCE_DIR}" PARENT_SCOPE)
|
||||
SET(${PACKAGE}_BINARY_DIR "${${lpackage}_BINARY_DIR}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(CPMRegisterPackage PACKAGE VERSION)
|
||||
list(APPEND CPM_PACKAGES ${PACKAGE})
|
||||
set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "")
|
||||
set("CPM_PACKAGE_${PACKAGE}_VERSION" ${VERSION} CACHE INTERNAL "")
|
||||
endfunction()
|
||||
|
||||
function(CPM_GET_PACKAGE_VERSION PACKAGE)
|
||||
set(CPM_PACKAGE_VERSION "${CPM_PACKAGE_${PACKAGE}_VERSION}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(CPM_PARSE_OPTION OPTION)
|
||||
string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION})
|
||||
string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH)
|
||||
math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1")
|
||||
string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE)
|
||||
set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE)
|
||||
set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function (CPM_HANDLE_OLD_VERSION NEW_CPM_VERSION)
|
||||
|
||||
message(AUTHOR_WARNING "${CPM_INDENT} \
|
||||
A dependency is using a more recent CPM (${NEW_CPM_VERSION}) than the current project (${CPM_VERSION}). \
|
||||
It is recommended to upgrade CPM to the most recent version. \
|
||||
See https://github.com/TheLartians/CPM for more information."
|
||||
)
|
||||
|
||||
endfunction()
|
||||
@@ -12,14 +12,16 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
NAME benchmark
|
||||
GIT_REPOSITORY https://github.com/google/benchmark.git
|
||||
GITHUB_REPOSITORY google/benchmark
|
||||
VERSION 1.4.1
|
||||
OPTIONS
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
)
|
||||
|
||||
if (benchmark_ADDED)
|
||||
# patch google benchmark target
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
endif()
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GITHUB_REPOSITORY catchorg/Catch2
|
||||
VERSION 2.5.0
|
||||
)
|
||||
|
||||
|
||||
@@ -16,10 +16,9 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
NAME doctest
|
||||
GIT_REPOSITORY https://github.com/onqtam/doctest.git
|
||||
GITHUB_REPOSITORY onqtam/doctest
|
||||
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