mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-19 07:37:42 -05:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f57476ee43 | ||
|
|
db4f2bf7bb | ||
|
|
8d39e34ff2 | ||
|
|
c2a413300e | ||
|
|
5177860756 | ||
|
|
5dad9d4e9d | ||
|
|
539974785e | ||
|
|
e4a0cb4980 | ||
|
|
a8ccc42f43 | ||
|
|
f92ed18514 | ||
|
|
153c196c18 | ||
|
|
d91befd7ce | ||
|
|
978c101c7e | ||
|
|
8da680df9b | ||
|
|
6943b38afb | ||
|
|
cf042cf1ac | ||
|
|
bebea37c4a | ||
|
|
137bda299d | ||
|
|
4d557d128f | ||
|
|
9021499c52 | ||
|
|
9b97b64da2 | ||
|
|
df752ed5d3 |
12
.travis.yml
12
.travis.yml
@@ -1,11 +1,14 @@
|
||||
|
||||
language: cpp
|
||||
sudo: require
|
||||
dist: xenial
|
||||
|
||||
common_sources: &all_sources
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-trusty
|
||||
- llvm-toolchain-trusty-6.0
|
||||
|
||||
python:
|
||||
- 3.7
|
||||
|
||||
matrix:
|
||||
include:
|
||||
@@ -41,10 +44,5 @@ before_install:
|
||||
- cmake --version
|
||||
|
||||
script:
|
||||
- cmake -Hexamples/simple -Bbuild/simple
|
||||
- cmake --build build/simple
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/simple --target test
|
||||
- cmake -Hexamples/complex -Bbuild/complex
|
||||
- cmake --build build/complex
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/complex --target test
|
||||
- python3 examples/run_all.py
|
||||
|
||||
105
README.md
105
README.md
@@ -2,7 +2,7 @@
|
||||
|
||||
# CPM
|
||||
|
||||
CPM is a simple GIT dependency manager written in CMake. The main use-case is abstracting CMake's `FetchContent` and managing dependencies in small to medium sized projects.
|
||||
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.
|
||||
|
||||
## Supported projects
|
||||
|
||||
@@ -10,7 +10,26 @@ Any project that you can add via `add_subdirectory` should already work with CPM
|
||||
|
||||
## Usage
|
||||
|
||||
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.
|
||||
After `CPM.cmake` has been added to your project, you can call `CPMAddPackage` for every dependency of the project with the following named parameters.
|
||||
|
||||
```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)
|
||||
[...] # Source options, 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.
|
||||
|
||||
After calling `CPMAddPackage`, the variables `(DEPENDENCY)_SOURCE_DIR` and `(DEPENDENCY)_BINARY_DIR` are set, where `(DEPENDENCY)` is the name of the dependency.
|
||||
|
||||
## Full Example
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
@@ -19,24 +38,23 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
project(MyProject)
|
||||
|
||||
# add dependencies
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake)
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
VERSION 1.8
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
GIT_TAG v1.8 # optional here, as indirectly defined by VERSION
|
||||
OPTIONS # optional CMake arguments passed to the dependency
|
||||
OPTIONS
|
||||
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
|
||||
)
|
||||
|
||||
# add executable
|
||||
add_executable(my-project my-project.cpp)
|
||||
set_target_properties(my-project PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(my-project LarsParser)
|
||||
add_executable(myProject myProject.cpp)
|
||||
set_target_properties(myProject PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(myProject LarsParser)
|
||||
```
|
||||
|
||||
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples.
|
||||
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples with source code.
|
||||
|
||||
## Adding CPM
|
||||
|
||||
@@ -50,9 +68,65 @@ wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master
|
||||
|
||||
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.
|
||||
|
||||
## Options
|
||||
## Snipplets
|
||||
|
||||
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.
|
||||
These are some small snipplets demonstrating how to include some projects used with CPM.
|
||||
|
||||
### Catch2
|
||||
|
||||
Has a CMakeLists.txt that supports `add_subdirectory`.
|
||||
|
||||
```cmake
|
||||
CPMAddPackage(
|
||||
NAME Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
VERSION 2.5.0
|
||||
)
|
||||
```
|
||||
|
||||
### google/benchmark
|
||||
|
||||
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
|
||||
VERSION 1.4.1
|
||||
OPTIONS
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
)
|
||||
|
||||
# needed to compile with C++17
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
```
|
||||
|
||||
### Lua
|
||||
|
||||
Has no CMakeLists.txt, so a 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})
|
||||
|
||||
target_include_directories(lua
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
|
||||
)
|
||||
```
|
||||
|
||||
## Local packages
|
||||
|
||||
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.
|
||||
|
||||
## Advantages
|
||||
|
||||
@@ -61,9 +135,12 @@ Setting the CMake option `CPM_USE_LOCAL_PACKAGES` will activate finding packages
|
||||
- **Reproducable builds** By using versioning via git tags it is ensured that a project will always be in the same state everywhere.
|
||||
- **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.
|
||||
|
||||
## Limitations
|
||||
|
||||
- **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.
|
||||
- **No auto-update** To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
|
||||
- **No pre-built binaries** Unless they are installed or included in the linked repository.
|
||||
- **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.
|
||||
|
||||
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.
|
||||
|
||||
@@ -28,14 +28,15 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
set(CURRENT_CPM_VERSION 0.8)
|
||||
set(CURRENT_CPM_VERSION 0.9)
|
||||
|
||||
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 newer CPM version (${CPM_VERSION}) than the current project (${CURRENT_CPM_VERSION}). \
|
||||
It is recommended to upgrade CPM to the most recent version. See https://github.com/TheLartians/CPM for more information.\
|
||||
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.\
|
||||
")
|
||||
endif()
|
||||
return()
|
||||
@@ -124,7 +125,7 @@ function(CPMAddPackage)
|
||||
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
|
||||
CPM_GET_PACKAGE_VERSION(${CPM_ARGS_NAME})
|
||||
if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION})
|
||||
message(WARNING "${CPM_INDENT} newer package ${CPM_ARGS_NAME} requested (${CPM_ARGS_VERSION}, currently using ${CPM_PACKAGE_VERSION})")
|
||||
message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).")
|
||||
endif()
|
||||
if (CPM_ARGS_OPTIONS)
|
||||
foreach(OPTION ${CPM_ARGS_OPTIONS})
|
||||
@@ -135,6 +136,9 @@ function(CPMAddPackage)
|
||||
endforeach()
|
||||
endif()
|
||||
CPM_FETCH_PACKAGE(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
|
||||
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)
|
||||
return()
|
||||
endif()
|
||||
|
||||
@@ -149,6 +153,9 @@ function(CPMAddPackage)
|
||||
|
||||
CPM_DECLARE_PACKAGE(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${CPM_ARGS_GIT_TAG} "${CPM_ARGS_UNPARSED_ARGUMENTS}")
|
||||
CPM_FETCH_PACKAGE(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
|
||||
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)
|
||||
endfunction()
|
||||
|
||||
function (CPM_DECLARE_PACKAGE PACKAGE VERSION GIT_TAG)
|
||||
@@ -173,3 +180,10 @@ function (CPM_FETCH_PACKAGE PACKAGE DOWNLOAD_ONLY)
|
||||
endif()
|
||||
set(CPM_INDENT "${CPM_OLD_INDENT}")
|
||||
endfunction()
|
||||
|
||||
function (CPMGetProperties PACKAGE)
|
||||
FetchContent_GetProperties(${PACKAGE})
|
||||
string(TOLOWER ${PACKAGE} lpackage)
|
||||
SET(${PACKAGE}_SOURCE_DIR "${${lpackage}_SOURCE_DIR}" PARENT_SCOPE)
|
||||
SET(${PACKAGE}_BINARY_DIR "${${lpackage}_BINARY_DIR}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
28
examples/benchmark/CMakeLists.txt
Normal file
28
examples/benchmark/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME fibonacci
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME benchmark
|
||||
GIT_REPOSITORY https://github.com/google/benchmark.git
|
||||
VERSION 1.4.1
|
||||
OPTIONS
|
||||
"BENCHMARK_ENABLE_TESTING Off"
|
||||
)
|
||||
|
||||
# patch google benchmark target
|
||||
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
|
||||
|
||||
# ---- Executable ----
|
||||
|
||||
add_executable(CPMExampleBenchmark "main.cpp")
|
||||
set_target_properties(CPMExampleBenchmark PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(CPMExampleBenchmark fibonacci benchmark)
|
||||
36
examples/benchmark/main.cpp
Normal file
36
examples/benchmark/main.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
|
||||
#include <fibonacci.h>
|
||||
|
||||
|
||||
std::vector<unsigned> createTestNumbers(){
|
||||
std::vector<unsigned> v;
|
||||
for (int i=0;i<25;++i) v.emplace_back(i);
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
std::shuffle(v.begin(), v.end(), g);
|
||||
return v;
|
||||
}
|
||||
|
||||
void fibonnacci(benchmark::State& state) {
|
||||
auto numbers = createTestNumbers();
|
||||
for (auto _ : state) {
|
||||
for (auto v: numbers) benchmark::DoNotOptimize(fibonnacci(v));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(fibonnacci);
|
||||
|
||||
void fastFibonacci(benchmark::State& state) {
|
||||
auto numbers = createTestNumbers();
|
||||
for (auto _ : state) {
|
||||
for (auto v: numbers) benchmark::DoNotOptimize(fastFibonacci(v));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(fastFibonacci);
|
||||
|
||||
BENCHMARK_MAIN();
|
||||
39
examples/catch2/CMakeLists.txt
Normal file
39
examples/catch2/CMakeLists.txt
Normal file
@@ -0,0 +1,39 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Options ----
|
||||
|
||||
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME fibonacci
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
VERSION 2.5.0
|
||||
)
|
||||
|
||||
# ---- Create binary ----
|
||||
|
||||
add_executable(CPMExampleCatch2 main.cpp)
|
||||
target_link_libraries(CPMExampleCatch2 fibonacci Catch2)
|
||||
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
|
||||
|
||||
# ---- Enable testing ----
|
||||
|
||||
ENABLE_TESTING()
|
||||
ADD_TEST(CPMExampleCatch2 CPMExampleCatch2)
|
||||
|
||||
# ---- Add code coverage ----
|
||||
|
||||
if (${ENABLE_TEST_COVERAGE})
|
||||
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
|
||||
target_link_options(CPMExampleCatch2 PUBLIC "--coverage")
|
||||
endif()
|
||||
20
examples/catch2/main.cpp
Normal file
20
examples/catch2/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <fibonacci.h>
|
||||
|
||||
TEST_CASE("fibonnacci"){
|
||||
REQUIRE(fibonnacci(0) == 0);
|
||||
REQUIRE(fibonnacci(1) == 1);
|
||||
REQUIRE(fibonnacci(2) == 1);
|
||||
REQUIRE(fibonnacci(3) == 2);
|
||||
REQUIRE(fibonnacci(4) == 3);
|
||||
REQUIRE(fibonnacci(5) == 5);
|
||||
REQUIRE(fibonnacci(13) == 233);
|
||||
}
|
||||
|
||||
TEST_CASE("fastFibonnacci"){
|
||||
for (unsigned i=0; i<25; ++i){
|
||||
REQUIRE(fibonnacci(i) == fastFibonacci(i));
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
project(CPMTest)
|
||||
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
|
||||
|
||||
# ignore locally installed projects for reproducable builds
|
||||
set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "")
|
||||
|
||||
# util library
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.7
|
||||
)
|
||||
|
||||
# will be ignored as newer version already added
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.2
|
||||
)
|
||||
|
||||
# language bindings
|
||||
# uses git tag instead of version identifier
|
||||
# depends on visitor library that depends on Event library and LHC
|
||||
# CMake configuration arguments passed via OPTIONS
|
||||
CPMAddPackage(
|
||||
NAME Glue
|
||||
GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
|
||||
VERSION 0.5.1
|
||||
OPTIONS
|
||||
"GLUE_ENABLE_LUA ON"
|
||||
"GLUE_BUILD_LUA ON"
|
||||
)
|
||||
|
||||
# parser library
|
||||
# depends on LHC and Glue
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.8
|
||||
OPTIONS
|
||||
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
|
||||
)
|
||||
|
||||
# add executable
|
||||
add_executable(cpm-test-complex main.cpp)
|
||||
set_target_properties(cpm-test-complex PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(cpm-test-complex LHC LarsParser Glue)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test-complex cpm-test-complex)
|
||||
@@ -1,52 +0,0 @@
|
||||
#include <lars/parser/extension.h>
|
||||
#include <lars/lua_glue.h>
|
||||
#include <stdexcept>
|
||||
|
||||
int main() {
|
||||
// create lua state
|
||||
auto lua = lars::LuaState();
|
||||
lua.open_libs();
|
||||
|
||||
// create extensions
|
||||
lars::Extension extensions;
|
||||
|
||||
// add parser library to extension
|
||||
extensions.add_extension("parser", lars::extensions::parser());
|
||||
|
||||
// connect parser extension to lua
|
||||
extensions.connect(lua.get_glue());
|
||||
|
||||
// create a parser
|
||||
lua.run(R"(
|
||||
NumberMap = parser.Program.create()
|
||||
|
||||
NumberMap:setRule("Whitespace", "[ \t]")
|
||||
NumberMap:setSeparatorRule("Whitespace")
|
||||
|
||||
NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e)
|
||||
local N = e:size()-1
|
||||
local res = {}
|
||||
for i=0,N do
|
||||
local a = e:get(i)
|
||||
res[a:get(0):evaluate()] = a:get(1):evaluate()
|
||||
end
|
||||
return res
|
||||
end)
|
||||
|
||||
NumberMap:setRule("KeyValue", "Number ':' Number")
|
||||
|
||||
NumberMap:setRuleWithCallback("Number", "'-'? [0-9]+", function(e) return tonumber(e:string()); end)
|
||||
|
||||
NumberMap:setStartRule("Object")
|
||||
)");
|
||||
|
||||
// parse a string
|
||||
lua.run("m = NumberMap:run('{1:3, 2:-1, 3:42}')");
|
||||
|
||||
// check result
|
||||
if (lua.get_numeric("m[1]") != 3) throw std::runtime_error("unexpected result");
|
||||
if (lua.get_numeric("m[2]") != -1) throw std::runtime_error("unexpected result");
|
||||
if (lua.get_numeric("m[3]") != 42) throw std::runtime_error("unexpected result");
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
examples/doctest/CMakeLists.txt
Normal file
41
examples/doctest/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
# ---- Options ----
|
||||
|
||||
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME fibonacci
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME doctest
|
||||
GIT_REPOSITORY https://github.com/onqtam/doctest.git
|
||||
VERSION 2.3.2
|
||||
GIT_TAG 2.3.2
|
||||
GIT_SHALLOW True
|
||||
)
|
||||
|
||||
# ---- Create binary ----
|
||||
|
||||
add_executable(CPMExampleDoctest main.cpp)
|
||||
target_link_libraries(CPMExampleDoctest fibonacci doctest)
|
||||
set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
|
||||
|
||||
# ---- Enable testing ----
|
||||
|
||||
ENABLE_TESTING()
|
||||
ADD_TEST(CPMExampleDoctest CPMExampleDoctest)
|
||||
|
||||
# ---- Add code coverage ----
|
||||
|
||||
if (${ENABLE_TEST_COVERAGE})
|
||||
set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
|
||||
target_link_options(CPMExampleDoctest PUBLIC "--coverage")
|
||||
endif()
|
||||
20
examples/doctest/main.cpp
Normal file
20
examples/doctest/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <fibonacci.h>
|
||||
|
||||
TEST_CASE("fibonnacci"){
|
||||
CHECK(fibonnacci(0) == 0);
|
||||
CHECK(fibonnacci(1) == 1);
|
||||
CHECK(fibonnacci(2) == 1);
|
||||
CHECK(fibonnacci(3) == 2);
|
||||
CHECK(fibonnacci(4) == 3);
|
||||
CHECK(fibonnacci(5) == 5);
|
||||
CHECK(fibonnacci(13) == 233);
|
||||
}
|
||||
|
||||
TEST_CASE("fastFibonnacci"){
|
||||
for (unsigned i=0; i<25; ++i){
|
||||
CHECK(fibonnacci(i) == fastFibonacci(i));
|
||||
}
|
||||
}
|
||||
28
examples/parser-lua/CMakeLists.txt
Normal file
28
examples/parser-lua/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
# ---- Dependencies ----
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Glue
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
|
||||
VERSION 0.8.1
|
||||
OPTIONS
|
||||
"GLUE_ENABLE_LUA ON"
|
||||
"GLUE_BUILD_LUA ON"
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.9
|
||||
OPTIONS
|
||||
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
|
||||
)
|
||||
|
||||
# ---- Create binary ----
|
||||
|
||||
add_executable(parser-lua main.cpp)
|
||||
set_target_properties(parser-lua PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(parser-lua LHC LarsParser Glue)
|
||||
46
examples/parser-lua/main.cpp
Normal file
46
examples/parser-lua/main.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <lars/parser/extension.h>
|
||||
#include <glue/lua.h>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
auto lua = glue::LuaState();
|
||||
lua.openStandardLibs();
|
||||
|
||||
lua["parser"] = lars::glue::parser();
|
||||
|
||||
lua.run(R"(
|
||||
wordParser = parser.Program.create()
|
||||
|
||||
wordParser:setRule("Whitespace", "[ \t]")
|
||||
wordParser:setSeparatorRule("Whitespace")
|
||||
|
||||
wordParser:setRule("Word", "[a-zA-Z]+")
|
||||
|
||||
wordParser:setRuleWithCallback("Words", "Word*", function(e)
|
||||
local N = e:size()
|
||||
local res = {}
|
||||
for i=0,N-1 do res[#res+1] = e:get(i):string() end
|
||||
return res
|
||||
end)
|
||||
|
||||
wordParser:setStartRule("Words")
|
||||
)");
|
||||
|
||||
lua.run(R"(
|
||||
while true do
|
||||
print("please enter some words or 'quit' to exit");
|
||||
local input = io.read();
|
||||
if input == "quit" then os.exit() end
|
||||
local result
|
||||
ok, err = pcall(function() result = wordParser:run(input) end)
|
||||
if ok then
|
||||
print("you entered " .. #result .. " words!")
|
||||
else
|
||||
print("error: " .. tostring(err))
|
||||
end
|
||||
end
|
||||
)");
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
examples/parser/CMakeLists.txt
Normal file
15
examples/parser/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
# add dependencies
|
||||
include(../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.7
|
||||
)
|
||||
|
||||
# add executable
|
||||
add_executable(calculator main.cpp)
|
||||
set_target_properties(calculator PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(calculator LarsParser)
|
||||
57
examples/parser/main.cpp
Normal file
57
examples/parser/main.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <cmath>
|
||||
|
||||
#include <lars/parser/generator.h>
|
||||
|
||||
int main() {
|
||||
using namespace std;
|
||||
using VariableMap = unordered_map<string, float>;
|
||||
|
||||
lars::ParserGenerator<float, VariableMap &> calculator;
|
||||
|
||||
auto &g = calculator;
|
||||
g.setSeparator(g["Whitespace"] << "[\t ]");
|
||||
|
||||
g["Expression"] << "Set | Sum";
|
||||
g["Set" ] << "Name '=' Sum" >> [](auto e, auto &v){ return v[e[0].string()] = e[1].evaluate(v); };
|
||||
g["Sum" ] << "Add | Subtract | Product";
|
||||
g["Product" ] << "Multiply | Divide | Exponent";
|
||||
g["Exponent" ] << "Power | Atomic";
|
||||
g["Atomic" ] << "Number | Brackets | Variable";
|
||||
g["Brackets" ] << "'(' Sum ')'";
|
||||
g["Add" ] << "Sum '+' Product" >> [](auto e, auto &v){ return e[0].evaluate(v) + e[1].evaluate(v); };
|
||||
g["Subtract" ] << "Sum '-' Product" >> [](auto e, auto &v){ return e[0].evaluate(v) - e[1].evaluate(v); };
|
||||
g["Multiply" ] << "Product '*' Exponent" >> [](auto e, auto &v){ return e[0].evaluate(v) * e[1].evaluate(v); };
|
||||
g["Divide" ] << "Product '/' Exponent" >> [](auto e, auto &v){ return e[0].evaluate(v) / e[1].evaluate(v); };
|
||||
g["Power" ] << "Atomic ('^' Exponent)" >> [](auto e, auto &v){ return pow(e[0].evaluate(v), e[1].evaluate(v)); };
|
||||
g["Variable" ] << "Name" >> [](auto e, auto &v){ return v[e[0].string()]; };
|
||||
g["Name" ] << "[a-zA-Z] [a-zA-Z0-9]*";
|
||||
g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e, auto &){ return stod(e.string()); };
|
||||
|
||||
g.setStart(g["Expression"]);
|
||||
|
||||
cout << "Enter an expression to be evaluated or 'quit' to exit.\n";
|
||||
|
||||
VariableMap variables;
|
||||
|
||||
while (true) {
|
||||
string str;
|
||||
cout << "> ";
|
||||
getline(cin,str);
|
||||
if(str == "q" || str == "quit"){ break; }
|
||||
try {
|
||||
auto result = calculator.run(str, variables);
|
||||
cout << str << " = " << result << endl;
|
||||
} catch (lars::SyntaxError &error) {
|
||||
auto syntax = error.syntax;
|
||||
cout << " ";
|
||||
cout << string(syntax->begin, ' ');
|
||||
cout << string(syntax->length(), '~');
|
||||
cout << "^\n";
|
||||
cout << " " << "Syntax error while parsing " << syntax->rule->name << endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
29
examples/run_all.py
Normal file
29
examples/run_all.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from pathlib import Path
|
||||
from subprocess import PIPE, run
|
||||
|
||||
examples = [
|
||||
x for x in Path(__file__).parent.iterdir() if x.is_dir() and (x / 'CMakeLists.txt').exists()
|
||||
]
|
||||
|
||||
assert(len(examples) > 0)
|
||||
|
||||
def runCommand(command):
|
||||
print('- %s' % command)
|
||||
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
|
||||
if result.returncode != 0:
|
||||
print("error while running '%s':\n" % command, ' ' + str(result.stderr).replace('\n','\n '))
|
||||
exit(result.returncode)
|
||||
return result.stdout
|
||||
|
||||
print('')
|
||||
for example in examples:
|
||||
print("running example %s" % example.name)
|
||||
print("================" + ('=' * len(example.name)))
|
||||
project = Path(".") / 'build' / example.name
|
||||
configure = runCommand('cmake -H%s -B%s -DCMAKE_BUILD_TYPE=RelWithDebInfo' % (example, project))
|
||||
print(' ' + '\n '.join([line for line in configure.split('\n') if 'CPM:' in line]))
|
||||
build = runCommand('cmake --build %s -j4' % (project))
|
||||
print(' ' + '\n '.join([line for line in build.split('\n') if 'Built target' in line]))
|
||||
print('')
|
||||
@@ -1,21 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
project(CPMTest)
|
||||
|
||||
# add dependencies
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.7
|
||||
)
|
||||
|
||||
# add executable
|
||||
add_executable(cpm-test-simple main.cpp)
|
||||
set_target_properties(cpm-test-simple PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(cpm-test-simple LarsParser)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test-simple cpm-test-simple)
|
||||
@@ -1,27 +0,0 @@
|
||||
#include <lars/parser/generator.h>
|
||||
|
||||
int main() {
|
||||
lars::ParserGenerator<float> g;
|
||||
|
||||
// Define grammar and evaluation rules
|
||||
g.setSeparator(g["Whitespace"] << "[\t ]");
|
||||
g["Sum" ] << "Add | Subtract | Product";
|
||||
g["Product" ] << "Multiply | Divide | Atomic";
|
||||
g["Atomic" ] << "Number | '(' Sum ')'";
|
||||
g["Add" ] << "Sum '+' Product" >> [](auto e){ return e[0].evaluate() + e[1].evaluate(); };
|
||||
g["Subtract"] << "Sum '-' Product" >> [](auto e){ return e[0].evaluate() - e[1].evaluate(); };
|
||||
g["Multiply"] << "Product '*' Atomic" >> [](auto e){ return e[0].evaluate() * e[1].evaluate(); };
|
||||
g["Divide" ] << "Product '/' Atomic" >> [](auto e){ return e[0].evaluate() / e[1].evaluate(); };
|
||||
g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e){ return stof(e.string()); };
|
||||
g.setStart(g["Sum"]);
|
||||
|
||||
// Execute a string
|
||||
float result = g.run("1 + 2 * (3+4)/2 - 3");
|
||||
|
||||
// validate result
|
||||
if (result == 5) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user