Compare commits

..

51 Commits
v0.5 ... v0.9

Author SHA1 Message Date
Lars Melchior
f57476ee43 Update README.md 2019-05-17 14:50:27 +02:00
Lars Melchior
db4f2bf7bb Update README.md 2019-05-17 14:47:35 +02:00
Lars Melchior
8d39e34ff2 v0.9 (#46)
* always get properties

* update examples

* update travis

* add doctest example

* update example runner
2019-05-17 14:46:54 +02:00
Lars Melchior
c2a413300e Update README.md 2019-05-11 15:44:22 +02:00
Lars Melchior
5177860756 Update README.md 2019-05-11 15:12:06 +02:00
Lars Melchior
5dad9d4e9d update examples (#45) 2019-05-10 16:20:53 +02:00
Lars Melchior
539974785e Update README.md 2019-05-10 16:03:46 +02:00
Lars Melchior
e4a0cb4980 Update README.md 2019-05-10 13:10:48 +02:00
Lars Melchior
a8ccc42f43 Update README.md 2019-05-10 12:03:09 +02:00
Lars Melchior
f92ed18514 Update README.md 2019-05-10 09:56:57 +02:00
Lars Melchior
153c196c18 Update README.md 2019-05-10 09:56:34 +02:00
Lars Melchior
d91befd7ce Update README.md 2019-05-10 09:55:48 +02:00
Lars Melchior
978c101c7e Update README.md 2019-05-09 13:50:10 +02:00
Lars Melchior
8da680df9b Update README.md 2019-05-09 13:45:44 +02:00
Lars Melchior
6943b38afb Update README.md 2019-05-09 13:36:23 +02:00
Lars Melchior
cf042cf1ac Update README.md 2019-05-09 13:31:18 +02:00
Lars Melchior
bebea37c4a Update CPM.cmake 2019-05-09 13:29:12 +02:00
Lars Melchior
137bda299d Update CPM.cmake (#44)
* Update CPM.cmake

* Update CPM.cmake
2019-05-08 21:36:51 +02:00
Lars Melchior
4d557d128f Update README.md 2019-05-06 10:21:58 +02:00
Lars Melchior
9021499c52 Update CPM.cmake 2019-05-05 11:47:28 +02:00
Lars Melchior
9b97b64da2 Correct version output 2019-05-04 10:39:39 +02:00
Lars Melchior
df752ed5d3 Update CPM.cmake 2019-05-04 10:32:20 +02:00
Lars Melchior
f00f5517a6 Update CPM.cmake (#43)
- Increase Version to 0.8
- Add version check
- Add DOWNLOAD_ONLY option
2019-05-04 10:23:02 +02:00
Lars Melchior
a97b32824e bugfix (#42) 2019-04-28 12:17:30 +02:00
Lars Melchior
e558d795f9 Update CPM.cmake (#41) 2019-04-26 15:44:36 +02:00
Lars Melchior
76748fe542 remove Codacy badge (does not support CMake) (#40) 2019-04-24 13:10:18 +02:00
Lars Melchior
e453671327 Update README.md (#39)
* Update README.md

* Update README.md
2019-04-24 12:47:15 +02:00
Lars Melchior
5192f713d2 Default to remote packages (#38)
* Update CPM.cmake

* Update CPM.cmake

* Update README.md
2019-04-22 20:01:41 +02:00
Lars Melchior
282b98423a Update README.md (#37) 2019-04-22 19:22:23 +02:00
Lars Melchior
2ff0f5f58f Update README.md (#36) 2019-04-20 18:20:01 +02:00
Lars Melchior
9343f7c69a Update README.md (#35) 2019-04-20 12:33:24 +02:00
Lars Melchior
044edb1fd2 add origin comment (#34)
* Update CPM.cmake

* Update CPM.cmake

* add licence to script

* Update CPM.cmake

* Update CPM.cmake
2019-04-18 14:21:45 +02:00
Lars Melchior
07a4d626a1 Remove debug log (#33) 2019-04-18 13:54:43 +02:00
Lars Melchior
0d529f73cd use set_target_properties (#32) 2019-04-18 12:47:22 +02:00
Lars Melchior
96dba3b03c Update README.md (#31) 2019-04-18 12:43:54 +02:00
Lars Melchior
ba256b71c1 Update README.md (#30)
* Update README.md

* Update README.md
2019-04-18 12:35:17 +02:00
Lars Melchior
b3a875e2dd use CPM_LOCAL_PACKAGES_ONLY again (#29) 2019-04-17 22:05:59 +02:00
Lars Melchior
a582911527 Update README.md (#28) 2019-04-17 15:35:00 +02:00
Lars Melchior
0a07f53d6d Update README.md (#27) 2019-04-17 15:24:29 +02:00
Lars Melchior
b81a29b598 Update README.md (#26)
* Update README.md

* Update README.md

* Update README.md

* Update README.md
2019-04-17 15:10:48 +02:00
Lars Melchior
0adcd87add Update README.md (#25)
* Update README.md

* Update README.md

* Update README.md
2019-04-17 13:58:29 +02:00
Lars Melchior
95634c30fd only download if no options are provided (#24) 2019-04-17 13:36:08 +02:00
Lars Melchior
bf205f77b4 Update README.md (#23) 2019-04-17 13:25:48 +02:00
Lars Melchior
610448d275 Update README.md (#22) 2019-04-17 13:11:16 +02:00
Lars Melchior
dd63d9fcdb Dev (#21)
* better debug output

* update example

* update readme
2019-04-17 13:01:56 +02:00
Lars Melchior
f8899a5912 Update README.md (#19)
* Update README.md

* Update README.md

* Update README.md
2019-04-17 12:52:42 +02:00
Lars Melchior
48b84b8cdf rename examples test.cpp -> main.cpp (#20) 2019-04-16 19:57:05 +02:00
Lars Melchior
d3d8a6ab4f rename tests -> examples (#18) 2019-04-15 17:54:53 +02:00
Lars Melchior
923265e7ae Update README.md (#17) 2019-04-15 14:42:17 +02:00
Lars Melchior
f2ad294ef3 only use add_package when VERSION is present (#16) 2019-04-15 14:22:43 +02:00
Lars Melchior
3e65078ce7 Update README.md (#15)
* Update README.md

* Update README.md
2019-04-15 14:06:21 +02:00
18 changed files with 616 additions and 227 deletions

View File

@@ -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 -Htests/simple -Bbuild/simple
- cmake --build build/simple
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/simple --target test
- cmake -Htests/complex -Bbuild/complex
- cmake --build build/complex
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/complex --target test
- python3 examples/run_all.py

133
README.md
View File

@@ -2,50 +2,145 @@
# CPM
CPM is a minimalistic package manager written in Cmake using `find_package` and `FetchContent` as a fallback to download non locally installed packages.
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.
# Usage
## Supported projects
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.
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.
```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)
# create project
project(MyProject)
# add dependencies
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake)
include(cmake/CPM.cmake)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.8
OPTIONS
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# add executable
set (CMAKE_CXX_STANDARD 17)
add_executable(my-project my-project.cpp)
target_link_libraries(my-project LarsParser)
add_executable(myProject myProject.cpp)
set_target_properties(myProject PROPERTIES CXX_STANDARD 17)
target_link_libraries(myProject LarsParser)
```
# Adding CPM
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples with source code.
To add CPM to your current project, simply include add `cmake/CPM.cmake` to your projects `cmake` directory. The command below will perform this automatically.
## 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
```
# Advantages
## Updating CPM
- **Auto handle dependencies** Users of your projects do not need to worry about dependencies, everything is handled automatically.
- **Reproducable builds** Using git tags it is ensured that a project will always be in the same state everywhere.
- **No installation required** No need to install any third-party package managers. Just copy the files from the CMake directory and you're good to go.
- **Cross-Plattform** As CPM adds projects as cmake subdirectories, it is compatible with all cmake toolchains and generators.
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.
# Limitations
## Snipplets
- **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).
- **No auto-update** To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
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
- **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.
- **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`@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.

View File

@@ -1,38 +1,88 @@
# TheLartians/CPM - A simple Git dependency manager
# =================================================
# See https://github.com/TheLartians/CPM for usage and update instructions.
#
# MIT License
# -----------
#[[
Copyright (c) 2019 Lars Melchior
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
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 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()
endif()
endif()
option(CPM_LOCAL_PACKAGES_ONLY "Use only locally installed packages" OFF)
option(CPM_REMOTE_PACKAGES_ONLY "Always download packages" OFF)
set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "")
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
set(CPM_PACKAGES "" CACHE INTERNAL "")
option(CPM_USE_LOCAL_PACKAGES "Use locally installed packages (find_package)" OFF)
option(CPM_LOCAL_PACKAGES_ONLY "Use only locally installed packages" OFF)
include(FetchContent)
include(CMakeParseArguments)
if(NOT CPM_INDENT)
set(CPM_INDENT "CPM:")
endif()
function(CPMRegisterPackage PACKAGE VERSION)
LIST(APPEND CPM_PACKAGES ${CPM_ARGS_NAME})
list(APPEND CPM_PACKAGES ${PACKAGE})
set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "")
set(PACKAGE_VERSION_VARIABLE "CPM_PACKAGE_${PACKAGE}_VERSION")
set(${PACKAGE_VERSION_VARIABLE} ${VERSION} CACHE INTERNAL "")
set("CPM_PACKAGE_${PACKAGE}_VERSION" ${VERSION} CACHE INTERNAL "")
endfunction()
function(CPMGetPreviousPackageVersion PACKAGE)
set(PACKAGE_VERSION_VARIABLE "CPM_PACKAGE_${PACKAGE}_VERSION")
set(CPM_PREVIOUS_PACKAGE_VERSION "${${PACKAGE_VERSION_VARIABLE}}" PARENT_SCOPE)
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(CPMAddPackage)
set(oneValueArgs
NAME
VERSION
GIT_TAG
DOWNLOAD_ONLY
)
set(multiValueArgs
@@ -41,57 +91,99 @@ function(CPMAddPackage)
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if (NOT CPM_ARGS_GIT_TAG)
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
endif()
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
CPMGetPreviousPackageVersion(${CPM_ARGS_NAME})
message(STATUS "CPM: SKIP ${CPM_ARGS_NAME}@${CPM_ARGS_GIT_TAG}: already addded package ${CPM_ARGS_NAME}@${CPM_PREVIOUS_PACKAGE_VERSION}")
return()
endif()
CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_GIT_TAG})
if (CPM_ARGS_OPTIONS)
foreach(OPTION ${CPM_ARGS_OPTIONS})
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_VALUE} CACHE INTERNAL "")
endforeach()
endif()
if (NOT ${CPM_REMOTE_PACKAGES_ONLY})
if(${CPM_USE_LOCAL_PACKAGES} OR ${CPM_LOCAL_PACKAGES_ONLY})
find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} QUIET)
set(CPM_PACKAGE_FOUND ${CPM_ARGS_NAME}_FOUND)
if(${CPM_PACKAGE_FOUND})
message(STATUS "CPM: ADD local package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
message(STATUS "CPM: adding local package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
set_target_properties(${CPM_ARGS_NAME}
PROPERTIES
IMPORTED_GLOBAL True
)
return()
endif()
if(${CPM_LOCAL_PACKAGES_ONLY})
message(SEND_ERROR "CPM: ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})")
endif()
endif()
if (NOT ${CPM_LOCAL_PACKAGES_ONLY})
if (NOT CPM_ARGS_VERSION)
set(CPM_ARGS_VERSION 0)
endif()
message(STATUS "CPM: ADD remote package ${CPM_ARGS_NAME}@${CPM_ARGS_GIT_TAG}")
if (NOT CPM_ARGS_GIT_TAG)
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
endif()
set(CPM_PACKAGE_CONTENT ${CPM_ARGS_NAME}_CONTENT)
FetchContent_Declare(
${CPM_PACKAGE_CONTENT}
GIT_TAG ${CPM_ARGS_GIT_TAG}
${CPM_ARGS_UNPARSED_ARGUMENTS}
)
FetchContent_MakeAvailable(${CPM_PACKAGE_CONTENT})
if(CPM_ARGS_DOWNLOAD_ONLY)
set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY})
else()
MESSAGE(ERROR "CPM could not find the local package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
set(DOWNLOAD_ONLY NO)
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})
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})
CPM_PARSE_OPTION(${OPTION})
if(NOT "${${OPTION_KEY}}" STREQUAL ${OPTION_VALUE})
message(WARNING "${CPM_INDENT} ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})")
endif()
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()
CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})
if (CPM_ARGS_OPTIONS)
foreach(OPTION ${CPM_ARGS_OPTIONS})
CPM_PARSE_OPTION(${OPTION})
set(${OPTION_KEY} ${OPTION_VALUE} CACHE INTERNAL "")
endforeach()
endif()
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)
message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${GIT_TAG})")
FetchContent_Declare(
${PACKAGE}
GIT_TAG ${GIT_TAG}
${ARGN}
)
endfunction()
function (CPM_FETCH_PACKAGE PACKAGE DOWNLOAD_ONLY)
set(CPM_OLD_INDENT "${CPM_INDENT}")
set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:")
if(${DOWNLOAD_ONLY})
if(NOT "${PACKAGE}_POPULATED")
FetchContent_Populate(${PACKAGE})
endif()
else()
FetchContent_MakeAvailable(${PACKAGE})
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()

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

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

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

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

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

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

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

View File

@@ -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
)
# adding LHC again will be ignored as the package has already been added
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.1
)
# language bindings
# uses git tag instead of version identifier
# depends on visitor library that depends on Event library and LHC (ignored as already added)
# configuration arguments passed via OPTIONS. these will all be set internally
set(GLUE_ENABLE_LUA ON)
CPMAddPackage(
NAME Glue
GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
OPTIONS
"GLUE_ENABLE_LUA ON"
"GLUE_BUILD_LUA ON"
)
# parser library
# depends on LHC (ignored as already added)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.8
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# add executable
set (CMAKE_CXX_STANDARD 17)
add_executable(cpm-test test.cpp)
target_link_libraries(cpm-test LHC LarsParser Glue)
# tests
enable_testing()
add_test(cpm-test cpm-test)

View File

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

View File

@@ -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
set (CMAKE_CXX_STANDARD 17)
add_executable(cpm-test test.cpp)
target_link_libraries(cpm-test LarsParser)
# tests
enable_testing()
add_test(cpm-test cpm-test)

View File

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