mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-18 15:17:30 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a97b32824e | ||
|
|
e558d795f9 | ||
|
|
76748fe542 | ||
|
|
e453671327 | ||
|
|
5192f713d2 | ||
|
|
282b98423a | ||
|
|
2ff0f5f58f | ||
|
|
9343f7c69a | ||
|
|
044edb1fd2 | ||
|
|
07a4d626a1 | ||
|
|
0d529f73cd | ||
|
|
96dba3b03c | ||
|
|
ba256b71c1 | ||
|
|
b3a875e2dd | ||
|
|
a582911527 | ||
|
|
0a07f53d6d | ||
|
|
b81a29b598 | ||
|
|
0adcd87add | ||
|
|
95634c30fd | ||
|
|
bf205f77b4 | ||
|
|
610448d275 | ||
|
|
dd63d9fcdb | ||
|
|
f8899a5912 | ||
|
|
48b84b8cdf | ||
|
|
d3d8a6ab4f | ||
|
|
923265e7ae | ||
|
|
f2ad294ef3 | ||
|
|
3e65078ce7 |
@@ -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
|
||||
|
||||
46
README.md
46
README.md
@@ -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.
|
||||
|
||||
154
cmake/CPM.cmake
154
cmake/CPM.cmake
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user