Compare commits

..

28 Commits
v0.5 ... v0.7.1

Author SHA1 Message Date
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
7 changed files with 153 additions and 79 deletions

View File

@@ -41,10 +41,10 @@ before_install:
- 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

View File

@@ -2,15 +2,20 @@
# 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 GIT dependency manager written in CMake. The main use-case is abstracting CMake's `FetchContent` and managing dependencies in small to medium sized projects.
# Usage
## 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. CMake options can also be supplied with the package.
```cmake
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# create project
project(MyProject)
# add dependencies
@@ -18,34 +23,47 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/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
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-project my-project.cpp)
set_target_properties(my-project PROPERTIES CXX_STANDARD 17)
target_link_libraries(my-project LarsParser)
```
# Adding CPM
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples.
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
## Options
- **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).
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.
## 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-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.

View File

@@ -1,3 +1,30 @@
# 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)
if(CPM_DIRECTORY)
@@ -6,29 +33,41 @@ if(CPM_DIRECTORY)
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 0.7.1 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
@@ -41,57 +80,74 @@ 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})
message(STATUS "CPM: ADD remote package ${CPM_ARGS_NAME}@${CPM_ARGS_GIT_TAG}")
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})
else()
MESSAGE(ERROR "CPM could not find the local package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION}")
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()
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})")
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})
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})
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()

View File

@@ -14,29 +14,29 @@ CPMAddPackage(
VERSION 0.7
)
# adding LHC again will be ignored as the package has already been added
# will be ignored as newer version already added
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.1
VERSION 0.2
)
# 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)
# 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 (ignored as already added)
# depends on LHC and Glue
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
@@ -46,10 +46,10 @@ CPMAddPackage(
)
# add executable
set (CMAKE_CXX_STANDARD 17)
add_executable(cpm-test test.cpp)
target_link_libraries(cpm-test LHC LarsParser Glue)
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 cpm-test)
add_test(cpm-test-complex cpm-test-complex)

View File

@@ -12,10 +12,10 @@ CPMAddPackage(
)
# 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)
set_target_properties(cpm-test-simple PROPERTIES CXX_STANDARD 17)
target_link_libraries(cpm-test-simple LarsParser)
# tests
enable_testing()
add_test(cpm-test cpm-test)
add_test(cpm-test-simple cpm-test-simple)