mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-19 16:57:56 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a582911527 | ||
|
|
0a07f53d6d | ||
|
|
b81a29b598 | ||
|
|
0adcd87add | ||
|
|
95634c30fd | ||
|
|
bf205f77b4 | ||
|
|
610448d275 | ||
|
|
dd63d9fcdb | ||
|
|
f8899a5912 | ||
|
|
48b84b8cdf | ||
|
|
d3d8a6ab4f | ||
|
|
923265e7ae | ||
|
|
f2ad294ef3 | ||
|
|
3e65078ce7 | ||
|
|
946019a336 | ||
|
|
2d95c66fa4 | ||
|
|
77a118d3ce | ||
|
|
4dee712eef | ||
|
|
3c0d201c76 | ||
|
|
4c22b73ebd | ||
|
|
50aa889be8 | ||
|
|
e4bbd6e215 | ||
|
|
e368fce6c3 | ||
|
|
f8e571e416 | ||
|
|
e565e35397 | ||
|
|
702f413801 | ||
|
|
03b2dd0cb9 | ||
|
|
4badcddc5d |
@@ -34,12 +34,17 @@ before_install:
|
||||
# Update compilers
|
||||
- eval "${MATRIX_EVAL}"
|
||||
- echo "CC=$CC CXX=$CXX"
|
||||
# Install a supported cmake version (>= 3.14)
|
||||
- wget -O cmake.sh https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.sh
|
||||
- sudo sh cmake.sh --skip-license --exclude-subdir --prefix=/usr/local
|
||||
- export PATH=/usr/local/bin:$PATH
|
||||
- cmake --version
|
||||
|
||||
script:
|
||||
- cmake -Htests/simple -Bbuild/simple
|
||||
- cmake -Hexamples/simple -Bbuild/simple
|
||||
- cmake --build build/simple
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/simple --target test
|
||||
- cmake -Htests/complex -Bbuild/complex
|
||||
- cmake -Hexamples/complex -Bbuild/complex
|
||||
- cmake --build build/complex
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/complex --target test
|
||||
|
||||
47
README.md
47
README.md
@@ -2,42 +2,63 @@
|
||||
|
||||
# CPM
|
||||
|
||||
CPM is a very simple package manager written in Cmake based on the amazing [DownloadProject](https://github.com/Crascit/DownloadProject) script. It is extremely easy to use and drastically simplifies the inclusion of other Cmake-based projects from github.
|
||||
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.
|
||||
|
||||
# Supported projects
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
project(MyParser)
|
||||
# create project
|
||||
project(MyProject)
|
||||
|
||||
# add dependencies
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
VERSION 1.2
|
||||
VERSION 1.8
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
GIT_TAG master # optional
|
||||
GIT_TAG v1.8 # optional here, as indirectly defined by VERSION
|
||||
OPTIONS # optional CMake arguments passed to the dependency
|
||||
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
|
||||
)
|
||||
|
||||
# add executable
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
add_executable(my-parser my-parser.cpp)
|
||||
target_link_libraries(cpm-test LarsParser)
|
||||
add_executable(my-project my-project.cpp)
|
||||
target_link_libraries(my-project LarsParser)
|
||||
```
|
||||
|
||||
# Installation
|
||||
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples.
|
||||
|
||||
To add CPM to your current project, copy the scripts in the `cmake` directory into you current project project. The command below will perform this automatically.
|
||||
# Adding CPM
|
||||
|
||||
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.
|
||||
|
||||
```bash
|
||||
wget -qO- https://github.com/TheLartians/CPM/releases/download/v0.1/cmake.zip | bsdtar -xvf-
|
||||
wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master/cmake/CPM.cmake
|
||||
```
|
||||
|
||||
# Options
|
||||
|
||||
If you set the CMake option `CPM_REMOTE_PACKAGES_ONLY` to `On`, packages will always be fetched via the URL. Setting `CPM_LOCAL_PACKAGES_ONLY` to `On` will only add packages via `find_package`.
|
||||
|
||||
# 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.
|
||||
|
||||
# Limitations
|
||||
|
||||
- First version used: in diamond 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 possibility not automatically update dependencies. To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
|
||||
- **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.
|
||||
- **No auto-update** To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
|
||||
|
||||
132
cmake/CPM.cmake
132
cmake/CPM.cmake
@@ -1,63 +1,127 @@
|
||||
set(_CPM_Dir "${CMAKE_CURRENT_LIST_DIR}")
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
if(CPM_DIRECTORY)
|
||||
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
|
||||
set(CPM_PACKAGES "" CACHE INTERNAL "")
|
||||
|
||||
option(CPM_LOCAL_PACKAGES_ONLY "Use only locally installed packages" OFF)
|
||||
option(CPM_REMOTE_PACKAGES_ONLY "Always download packages" OFF)
|
||||
|
||||
include(FetchContent)
|
||||
include(CMakeParseArguments)
|
||||
include(${_CPM_Dir}/DownloadProject.cmake)
|
||||
|
||||
function(CPMHasPackage)
|
||||
|
||||
if(NOT CPM_INDENT)
|
||||
set(CPM_INDENT "CPM:")
|
||||
endif()
|
||||
|
||||
function(CPM_REGISTER_PACKAGE PACKAGE VERSION)
|
||||
LIST(APPEND CPM_PACKAGES ${CPM_ARGS_NAME})
|
||||
set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "")
|
||||
CPM_SET_PACKAGE_VERSION(${PACKAGE} ${VERSION})
|
||||
endfunction()
|
||||
|
||||
function(CPM_SET_PACKAGE_VERSION PACKAGE VERSION)
|
||||
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(CPMAddPackage)
|
||||
set(options QUIET)
|
||||
|
||||
set(oneValueArgs
|
||||
NAME
|
||||
GIT_REPOSITORY
|
||||
VERSION
|
||||
GIT_TAG
|
||||
BINARY_DIR
|
||||
)
|
||||
|
||||
set(multiValueArgs "")
|
||||
set(multiValueArgs
|
||||
OPTIONS
|
||||
)
|
||||
|
||||
cmake_parse_arguments(CPM_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
|
||||
if (NOT CPM_PACKAGES)
|
||||
set(CPM_PACKAGES "")
|
||||
if (CPM_ARGS_VERSION AND NOT CPM_ARGS_OPTIONS AND NOT ${CPM_REMOTE_PACKAGES_ONLY})
|
||||
find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} QUIET)
|
||||
|
||||
if(${CPM_PACKAGE_FOUND})
|
||||
message(STATUS "CPM: adding local package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
|
||||
set_target_properties(${CPM_ARGS_NAME}
|
||||
PROPERTIES
|
||||
IMPORTED_GLOBAL True
|
||||
)
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (NOT CPM_ARGS_BINARY_DIR)
|
||||
set(CPM_ARGS_BINARY_DIR ${CMAKE_BINARY_DIR}/CPM-projects/${CPM_ARGS_NAME})
|
||||
endif()
|
||||
|
||||
if (NOT CPM_PROJECT_DIR)
|
||||
set(CPM_PROJECT_DIR "${CPM_ARGS_BINARY_DIR}")
|
||||
if (NOT CPM_ARGS_VERSION)
|
||||
set(CPM_ARGS_VERSION 0)
|
||||
endif()
|
||||
|
||||
if (NOT CPM_ARGS_GIT_TAG)
|
||||
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
|
||||
endif()
|
||||
|
||||
SET(CPM_TARGET_CMAKE_FILE "${CPM_PROJECT_DIR}")
|
||||
|
||||
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
|
||||
message(STATUS "CPM: package ${CPM_ARGS_NAME} already added")
|
||||
else()
|
||||
message(STATUS "CPM: adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
|
||||
# update package data
|
||||
LIST(APPEND CPM_PACKAGES ${CPM_ARGS_NAME})
|
||||
# save package data
|
||||
set(CPM_PACKAGES "${CPM_PACKAGES}" CACHE INTERNAL "CPM Packages")
|
||||
|
||||
configure_file(
|
||||
"${_CPM_Dir}/CPMProject.CMakeLists.cmake.in"
|
||||
"${CPM_TARGET_CMAKE_FILE}/CMakeLists.txt"
|
||||
@ONLY
|
||||
)
|
||||
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})")
|
||||
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}})")
|
||||
else()
|
||||
message(STATUS "${CPM_INDENT} NOT ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})")
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
CPM_FETCH_PACKAGE(${CPM_ARGS_NAME})
|
||||
return()
|
||||
endif()
|
||||
|
||||
if (NOT TARGET ${CPM_ARGS_NAME})
|
||||
add_subdirectory(${CPM_TARGET_CMAKE_FILE} ${CPM_ARGS_BINARY_DIR})
|
||||
CPM_REGISTER_PACKAGE(${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})
|
||||
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)
|
||||
set(CPM_OLD_INDENT "${CPM_INDENT}")
|
||||
set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:")
|
||||
FetchContent_MakeAvailable(${PACKAGE})
|
||||
set(CPM_INDENT "${CPM_OLD_INDENT}")
|
||||
endfunction()
|
||||
@@ -1,27 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
if(TARGET @CPM_ARGS_NAME@)
|
||||
return()
|
||||
endif()
|
||||
|
||||
find_package(@CPM_ARGS_NAME@ @CPM_ARGS_VERSION@ QUIET)
|
||||
|
||||
if(${PACKAGE_FOUND})
|
||||
set_target_properties(@CPM_ARGS_NAME@
|
||||
PROPERTIES
|
||||
IMPORTED_GLOBAL True
|
||||
)
|
||||
else()
|
||||
|
||||
download_project(
|
||||
PROJ @CPM_ARGS_NAME@
|
||||
GIT_REPOSITORY @CPM_ARGS_GIT_REPOSITORY@
|
||||
GIT_TAG @CPM_ARGS_GIT_TAG@
|
||||
UPDATE_DISCONNECTED 1
|
||||
GIT_SHALLOW 1
|
||||
PREFIX @CPM_ARGS_BINARY_DIR@/dl
|
||||
QUIET
|
||||
)
|
||||
|
||||
add_subdirectory(${@CPM_ARGS_NAME@_SOURCE_DIR} ${@CPM_ARGS_NAME@_BINARY_DIR})
|
||||
endif()
|
||||
@@ -1,17 +0,0 @@
|
||||
# Distributed under the OSI-approved MIT License. See accompanying
|
||||
# file LICENSE or https://github.com/Crascit/DownloadProject for details.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8.2)
|
||||
|
||||
project(${DL_ARGS_PROJ}-download NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(${DL_ARGS_PROJ}-download
|
||||
${DL_ARGS_UNPARSED_ARGUMENTS}
|
||||
SOURCE_DIR "${DL_ARGS_SOURCE_DIR}"
|
||||
BINARY_DIR "${DL_ARGS_BINARY_DIR}"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
||||
@@ -1,182 +0,0 @@
|
||||
# Distributed under the OSI-approved MIT License. See accompanying
|
||||
# file LICENSE or https://github.com/Crascit/DownloadProject for details.
|
||||
#
|
||||
# MODULE: DownloadProject
|
||||
#
|
||||
# PROVIDES:
|
||||
# download_project( PROJ projectName
|
||||
# [PREFIX prefixDir]
|
||||
# [DOWNLOAD_DIR downloadDir]
|
||||
# [SOURCE_DIR srcDir]
|
||||
# [BINARY_DIR binDir]
|
||||
# [QUIET]
|
||||
# ...
|
||||
# )
|
||||
#
|
||||
# Provides the ability to download and unpack a tarball, zip file, git repository,
|
||||
# etc. at configure time (i.e. when the cmake command is run). How the downloaded
|
||||
# and unpacked contents are used is up to the caller, but the motivating case is
|
||||
# to download source code which can then be included directly in the build with
|
||||
# add_subdirectory() after the call to download_project(). Source and build
|
||||
# directories are set up with this in mind.
|
||||
#
|
||||
# The PROJ argument is required. The projectName value will be used to construct
|
||||
# the following variables upon exit (obviously replace projectName with its actual
|
||||
# value):
|
||||
#
|
||||
# projectName_SOURCE_DIR
|
||||
# projectName_BINARY_DIR
|
||||
#
|
||||
# The SOURCE_DIR and BINARY_DIR arguments are optional and would not typically
|
||||
# need to be provided. They can be specified if you want the downloaded source
|
||||
# and build directories to be located in a specific place. The contents of
|
||||
# projectName_SOURCE_DIR and projectName_BINARY_DIR will be populated with the
|
||||
# locations used whether you provide SOURCE_DIR/BINARY_DIR or not.
|
||||
#
|
||||
# The DOWNLOAD_DIR argument does not normally need to be set. It controls the
|
||||
# location of the temporary CMake build used to perform the download.
|
||||
#
|
||||
# The PREFIX argument can be provided to change the base location of the default
|
||||
# values of DOWNLOAD_DIR, SOURCE_DIR and BINARY_DIR. If all of those three arguments
|
||||
# are provided, then PREFIX will have no effect. The default value for PREFIX is
|
||||
# CMAKE_BINARY_DIR.
|
||||
#
|
||||
# The QUIET option can be given if you do not want to show the output associated
|
||||
# with downloading the specified project.
|
||||
#
|
||||
# In addition to the above, any other options are passed through unmodified to
|
||||
# ExternalProject_Add() to perform the actual download, patch and update steps.
|
||||
# The following ExternalProject_Add() options are explicitly prohibited (they
|
||||
# are reserved for use by the download_project() command):
|
||||
#
|
||||
# CONFIGURE_COMMAND
|
||||
# BUILD_COMMAND
|
||||
# INSTALL_COMMAND
|
||||
# TEST_COMMAND
|
||||
#
|
||||
# Only those ExternalProject_Add() arguments which relate to downloading, patching
|
||||
# and updating of the project sources are intended to be used. Also note that at
|
||||
# least one set of download-related arguments are required.
|
||||
#
|
||||
# If using CMake 3.2 or later, the UPDATE_DISCONNECTED option can be used to
|
||||
# prevent a check at the remote end for changes every time CMake is run
|
||||
# after the first successful download. See the documentation of the ExternalProject
|
||||
# module for more information. It is likely you will want to use this option if it
|
||||
# is available to you. Note, however, that the ExternalProject implementation contains
|
||||
# bugs which result in incorrect handling of the UPDATE_DISCONNECTED option when
|
||||
# using the URL download method or when specifying a SOURCE_DIR with no download
|
||||
# method. Fixes for these have been created, the last of which is scheduled for
|
||||
# inclusion in CMake 3.8.0. Details can be found here:
|
||||
#
|
||||
# https://gitlab.kitware.com/cmake/cmake/commit/bdca68388bd57f8302d3c1d83d691034b7ffa70c
|
||||
# https://gitlab.kitware.com/cmake/cmake/issues/16428
|
||||
#
|
||||
# If you experience build errors related to the update step, consider avoiding
|
||||
# the use of UPDATE_DISCONNECTED.
|
||||
#
|
||||
# EXAMPLE USAGE:
|
||||
#
|
||||
# include(DownloadProject)
|
||||
# download_project(PROJ googletest
|
||||
# GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
# GIT_TAG master
|
||||
# UPDATE_DISCONNECTED 1
|
||||
# QUIET
|
||||
# )
|
||||
#
|
||||
# add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
|
||||
#
|
||||
#========================================================================================
|
||||
|
||||
|
||||
set(_DownloadProjectDir "${CMAKE_CURRENT_LIST_DIR}")
|
||||
|
||||
include(CMakeParseArguments)
|
||||
|
||||
function(download_project)
|
||||
|
||||
set(options QUIET)
|
||||
set(oneValueArgs
|
||||
PROJ
|
||||
PREFIX
|
||||
DOWNLOAD_DIR
|
||||
SOURCE_DIR
|
||||
BINARY_DIR
|
||||
# Prevent the following from being passed through
|
||||
CONFIGURE_COMMAND
|
||||
BUILD_COMMAND
|
||||
INSTALL_COMMAND
|
||||
TEST_COMMAND
|
||||
)
|
||||
set(multiValueArgs "")
|
||||
|
||||
cmake_parse_arguments(DL_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
|
||||
# Hide output if requested
|
||||
if (DL_ARGS_QUIET)
|
||||
set(OUTPUT_QUIET "OUTPUT_QUIET")
|
||||
else()
|
||||
unset(OUTPUT_QUIET)
|
||||
message(STATUS "Downloading/updating ${DL_ARGS_PROJ}")
|
||||
endif()
|
||||
|
||||
# Set up where we will put our temporary CMakeLists.txt file and also
|
||||
# the base point below which the default source and binary dirs will be.
|
||||
# The prefix must always be an absolute path.
|
||||
if (NOT DL_ARGS_PREFIX)
|
||||
set(DL_ARGS_PREFIX "${CMAKE_BINARY_DIR}")
|
||||
else()
|
||||
get_filename_component(DL_ARGS_PREFIX "${DL_ARGS_PREFIX}" ABSOLUTE
|
||||
BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
endif()
|
||||
if (NOT DL_ARGS_DOWNLOAD_DIR)
|
||||
set(DL_ARGS_DOWNLOAD_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-download")
|
||||
endif()
|
||||
|
||||
# Ensure the caller can know where to find the source and build directories
|
||||
if (NOT DL_ARGS_SOURCE_DIR)
|
||||
set(DL_ARGS_SOURCE_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-src")
|
||||
endif()
|
||||
if (NOT DL_ARGS_BINARY_DIR)
|
||||
set(DL_ARGS_BINARY_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-build")
|
||||
endif()
|
||||
set(${DL_ARGS_PROJ}_SOURCE_DIR "${DL_ARGS_SOURCE_DIR}" PARENT_SCOPE)
|
||||
set(${DL_ARGS_PROJ}_BINARY_DIR "${DL_ARGS_BINARY_DIR}" PARENT_SCOPE)
|
||||
|
||||
# The way that CLion manages multiple configurations, it causes a copy of
|
||||
# the CMakeCache.txt to be copied across due to it not expecting there to
|
||||
# be a project within a project. This causes the hard-coded paths in the
|
||||
# cache to be copied and builds to fail. To mitigate this, we simply
|
||||
# remove the cache if it exists before we configure the new project. It
|
||||
# is safe to do so because it will be re-generated. Since this is only
|
||||
# executed at the configure step, it should not cause additional builds or
|
||||
# downloads.
|
||||
file(REMOVE "${DL_ARGS_DOWNLOAD_DIR}/CMakeCache.txt")
|
||||
|
||||
# Create and build a separate CMake project to carry out the download.
|
||||
# If we've already previously done these steps, they will not cause
|
||||
# anything to be updated, so extra rebuilds of the project won't occur.
|
||||
# Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
|
||||
# has this set to something not findable on the PATH.
|
||||
configure_file("${_DownloadProjectDir}/DownloadProject.CMakeLists.cmake.in"
|
||||
"${DL_ARGS_DOWNLOAD_DIR}/CMakeLists.txt")
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
|
||||
-D "CMAKE_MAKE_PROGRAM:FILE=${CMAKE_MAKE_PROGRAM}"
|
||||
.
|
||||
RESULT_VARIABLE result
|
||||
${OUTPUT_QUIET}
|
||||
WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for ${DL_ARGS_PROJ} failed: ${result}")
|
||||
endif()
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build .
|
||||
RESULT_VARIABLE result
|
||||
${OUTPUT_QUIET}
|
||||
WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "Build step for ${DL_ARGS_PROJ} failed: ${result}")
|
||||
endif()
|
||||
|
||||
endfunction()
|
||||
55
examples/complex/CMakeLists.txt
Normal file
55
examples/complex/CMakeLists.txt
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
add_executable(cpm-test-complex main.cpp)
|
||||
target_link_libraries(cpm-test-complex LHC LarsParser Glue)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test-complex cpm-test-complex)
|
||||
52
examples/complex/main.cpp
Normal file
52
examples/complex/main.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#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;
|
||||
}
|
||||
@@ -8,14 +8,14 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.3
|
||||
VERSION 1.7
|
||||
)
|
||||
|
||||
# add executable
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
add_executable(cpm-test test.cpp)
|
||||
target_link_libraries(cpm-test LarsParser)
|
||||
add_executable(cpm-test-simple main.cpp)
|
||||
target_link_libraries(cpm-test-simple LarsParser)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test cpm-test)
|
||||
add_test(cpm-test-simple cpm-test-simple)
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <lars/parser_generator.h>
|
||||
#include <lars/parser/generator.h>
|
||||
|
||||
int main() {
|
||||
lars::ParserGenerator<float> g;
|
||||
@@ -1,41 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
project(CPMTest)
|
||||
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.3
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsEvent
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Event.git
|
||||
VERSION 1.0
|
||||
GIT_TAG master
|
||||
)
|
||||
|
||||
# Add project that depends on previous project
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.3
|
||||
)
|
||||
|
||||
# add project twice
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.3
|
||||
)
|
||||
|
||||
# add executable
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
add_executable(cpm-test test.cpp)
|
||||
target_link_libraries(cpm-test LHC LarsEvent LarsParser)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test cpm-test)
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <lars/parser_generator.h>
|
||||
#include <lars/event.h>
|
||||
|
||||
int main() {
|
||||
// Define the return value
|
||||
int result = 1;
|
||||
|
||||
// Define grammar and evaluation rules
|
||||
lars::ParserGenerator<float> g;
|
||||
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
|
||||
|
||||
// create an event
|
||||
lars::Event<float> onResult;
|
||||
onResult.connect([&](float v){ result = !(int(v) == 5); });
|
||||
|
||||
// emit the result of a parsed string
|
||||
onResult.notify(g.run("1 + 2 * (3+4)/2 - 3"));
|
||||
|
||||
// return the result
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user