Compare commits

..

16 Commits
v0.15 ... v0.23

Author SHA1 Message Date
Lars Melchior
6f053907f5 check if package added before find_package (#115) 2020-05-07 23:54:10 +02:00
Ryan Mast
84b31b560a Fix wiki link to preparing projects for CPM.cmake (#114) 2020-04-30 09:45:12 +02:00
Lars Melchior
3fff3ca70d Call FetchContent_GetProperties before DOWNLOAD_ONLY population (#113)
* call FetchContent_GetProperties before checking populated

* fix old CPM.cmake links

* update description

* bump version
2020-04-29 12:40:12 +02:00
Lars Melchior
ca33abc236 create FindXXX.cmake modules for added CPM packages (#112)
* create FindXXX.cmake modules for added CPM packages

* don't use list(PREPEND) as it unsupported on older CMake versions

* use CMAKE_BINARY_DIR

* ensure CPM.cmake is initialized only once

* import CPM.cmake in modules
2020-04-29 09:18:54 +02:00
Giuseppe Cesarano
119eaee84d Fixed link in README.md (#111)
The section `Adding CPM` was poiting to non exinsting wiki page
2020-04-26 16:44:16 +02:00
Lars Melchior
00b5e80f88 Fix offline cache support (#110)
* use cmake -E true as empty command

* use cmake without arguments as valid empty command
2020-04-25 01:27:27 +02:00
Lars Melchior
618cdefa0d Support CPM_SOURCE_CACHE on windows (#109)
* use semicolon as empty command and add test

* remove platform specific test coverage

* only run actions for pushes and prs into master (avoid duplication)

* remove cache test as it's already part of the test suite

* update version

* test cache reuse
2020-04-25 00:51:18 +02:00
Lars Melchior
464ba554c2 suppress package not found warning in CPMFindPackage (#103) 2020-04-14 09:35:31 +02:00
Lars Melchior
90558aa9cb Add GitHub action to publish CPM on release (#102)
* add github action to publish cpm on release

* add checkout step

* update readme
2020-03-16 20:37:30 +01:00
Lars Melchior
95c5426d25 remove code artifact from previous PR (#101) 2020-03-16 20:09:15 +01:00
Lars Melchior
cea81872cf Add fmt example and document version from git tag (#100)
* add VERSION_PREFIX argument

* add fmt example

* add VERSION_PREFIX note to readme

* rollback, add info about determining version from git tag

* rollback version
2020-03-16 20:00:30 +01:00
Lars Melchior
01b69aad82 add support for SOURCE_DIR argument (#97)
* add support for SOURCE_DIR argument

* simplify code

* add test

* update comment
2020-02-25 13:29:21 +01:00
Lars Melchior
775a235880 default to "ON" for options without explicit value definition (#96)
* default to "ON" for options without explicit value definition

* increment CPM version

* compare the correct lengths
2020-02-06 11:50:55 +01:00
Lars Melchior
f8d4e959bb abort when nested (bugfix) (#94)
* abort when nested (bugfix)

* update error message

* update error message
2019-10-25 20:52:22 +02:00
Lars Melchior
4c7aa36dff Update README.md 2019-10-25 16:01:47 +02:00
Lars Melchior
3a4516265c Add GitHub workflows (#93)
* add mac workflow

* rename and add windows and ubuntu workflows

* update tests

* update tests

* update travis
2019-10-24 18:14:58 +02:00
22 changed files with 366 additions and 112 deletions

22
.github/workflows/macos.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: MacOS
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- name: run tests
run: |
cmake -Htest -Bbuild/test
cmake --build build/test --target test-verbose

21
.github/workflows/publish.yaml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: Publish
on:
push:
tags:
- '*'
jobs:
build:
name: Publish CPM.cmake
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Upload CPM.cmake to release
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: cmake/CPM.cmake
asset_name: CPM.cmake
tag: ${{ github.ref }}
overwrite: true

29
.github/workflows/ubuntu.yml vendored Normal file
View File

@@ -0,0 +1,29 @@
name: Ubuntu
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: install updates
run: |
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
- name: run tests
run: |
cmake -Htest -Bbuild/test
cmake --build build/test --target test-verbose

22
.github/workflows/windows.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: Windows
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- name: run tests
run: |
cmake -Htest -Bbuild/test
cmake --build build/test --target test-verbose

View File

@@ -45,6 +45,6 @@ before_install:
script: script:
# unit tests # unit tests
- cmake -Htest -Bbuild/test - cmake -Htest -Bbuild/test
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/test --target test - cmake --build build/test --target test-verbose
# build examples # build examples
- python3 examples/build_all.py - python3 examples/build_all.py

View File

@@ -1,4 +1,7 @@
[![Build Status](https://travis-ci.com/TheLartians/CPM.cmake.svg?branch=master)](https://travis-ci.com/TheLartians/CPM.cmake) [![Build Status](https://travis-ci.com/TheLartians/CPM.cmake.svg?branch=master)](https://travis-ci.com/TheLartians/CPM.cmake)
[![Actions Status](https://github.com/TheLartians/CPM.cmake/workflows/MacOS/badge.svg)](https://github.com/TheLartians/CPM.cmake/actions)
[![Actions Status](https://github.com/TheLartians/CPM.cmake/workflows/Windows/badge.svg)](https://github.com/TheLartians/CPM.cmake/actions)
[![Actions Status](https://github.com/TheLartians/CPM.cmake/workflows/Ubuntu/badge.svg)](https://github.com/TheLartians/CPM.cmake/actions)
<p align="center"> <p align="center">
<img src="./logo/CPM.png" height="100" /> <img src="./logo/CPM.png" height="100" />
@@ -33,6 +36,7 @@ CPMAddPackage(
The origin may be specified by a `GIT_REPOSITORY`, but other sources, such as direct URLs, are [also supported](https://cmake.org/cmake/help/v3.11/module/ExternalProject.html#external-project-definition). The origin may be specified by a `GIT_REPOSITORY`, but other sources, such as direct URLs, are [also supported](https://cmake.org/cmake/help/v3.11/module/ExternalProject.html#external-project-definition).
If `GIT_TAG` hasn't been explicitly specified it defaults to `v(VERSION)`, a common convention for git projects. If `GIT_TAG` hasn't been explicitly specified it defaults to `v(VERSION)`, a common convention for git projects.
On the other hand, if `VERSION` hasn't been explicitly specified, CPM can automatically identify the version from the git tag in some common cases.
`GIT_TAG` can also be set to a specific commit or a branch name such as `master` to always download the most recent version. `GIT_TAG` can also be set to a specific commit or a branch name such as `master` to always download the most recent version.
The optional argument `FIND_PACKAGE_ARGUMENTS` can be specified to a string of parameters that will be passed to `find_package` if enabled (see below). The optional argument `FIND_PACKAGE_ARGUMENTS` can be specified to a string of parameters that will be passed to `find_package` if enabled (see below).
@@ -69,7 +73,7 @@ CPMAddPackage(
target_link_libraries(tests Catch2) target_link_libraries(tests Catch2)
``` ```
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for complete examples with source code or the [wiki](https://github.com/TheLartians/CPM/wiki/More-Snippets) for example snippets. See the [examples directory](https://github.com/TheLartians/CPM.cmake/tree/master/examples) for complete examples with source code or the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/More-Snippets) for example snippets.
## Adding CPM ## Adding CPM
@@ -77,10 +81,10 @@ To add CPM to your current project, simply add `cmake/CPM.cmake` to your project
```bash ```bash
mkdir -p cmake mkdir -p cmake
wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM.cmake/master/cmake/CPM.cmake wget -O cmake/CPM.cmake https://github.com/TheLartians/CPM.cmake/releases/latest/download/CPM.cmake
``` ```
You can also use CMake to download CPM for you. See the [wiki](https://github.com/TheLartians/CPM/wiki/Adding-CPM) for more details. You can also use CMake to download CPM for you. See the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/Downloading-CPM.cmake-in-CMake) for more details.
## Updating CPM ## Updating CPM
@@ -99,13 +103,13 @@ Dependencies using CPM will automatically use the updated script of the outermos
## Limitations ## Limitations
- **No pre-built binaries** For every new build directory, all dependencies are initially downloaded and built from scratch. To avoid extra downloads it is recommend to set the [`CPM_SOURCE_CACHE`](#CPM_SOURCE_CACHE) environmental variable. Using a caching compiler such as [ccahe](https://github.com/TheLartians/Ccache.cmake) can drastically reduce build time. - **No pre-built binaries** For every new build directory, all dependencies are initially downloaded and built from scratch. To avoid extra downloads it is recommend to set the [`CPM_SOURCE_CACHE`](#CPM_SOURCE_CACHE) environmental variable. Using a caching compiler such as [ccache](https://github.com/TheLartians/Ccache.cmake) can drastically reduce build time.
- **Dependent on good CMakeLists** Many libraries do not have CMakeLists that work well for subprojects. Luckily this is slowly changing, however, until then, some manual configuration may be required (see the snippets [below](#snippets) for examples). For best practices on preparing projects for CPM, see the [wiki](https://github.com/TheLartians/CPM/wiki/Preparing-projects-for-CPM). - **Dependent on good CMakeLists** Many libraries do not have CMakeLists that work well for subprojects. Luckily this is slowly changing, however, until then, some manual configuration may be required (see the snippets [below](#snippets) for examples). For best practices on preparing projects for CPM, see the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/Preparing-projects-for-CPM.cmake).
- **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 a warning. This can be resolved by adding a new version of the dependency in the outermost project. - **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 a warning. This can be resolved by adding a new version of the dependency in the outermost project.
For projects with more complex needs and where an extra setup step doesn't matter, it may be worth to check out an external C++ package manager such as [vcpkg](https://github.com/microsoft/vcpkg), [conan](https://conan.io) or [hunter](https://github.com/ruslo/hunter). For projects with more complex needs and where an extra setup step doesn't matter, it may be worth to check out an external C++ package manager such as [vcpkg](https://github.com/microsoft/vcpkg), [conan](https://conan.io) or [hunter](https://github.com/ruslo/hunter).
Dependencies added with `CPMFindPackages` should work with external package managers. Dependencies added with `CPMFindPackage` should work with external package managers.
This can also be enabled for all CPM dependencies by enabling [`CPM_USE_LOCAL_PACKAGES`](#cpmuselocalpackages). Additionally, the option [`CPM_USE_LOCAL_PACKAGES`](#cpmuselocalpackages) will enable `find_package` for all CPM dependencies.
## Options ## Options
@@ -136,7 +140,7 @@ These options can also be set as environmental variables.
## Snippets ## Snippets
These examples demonstrate how to include some well-known projects with CPM. These examples demonstrate how to include some well-known projects with CPM.
See the [wiki](https://github.com/TheLartians/CPM/wiki/More-Snippets) for more snippets. See the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/More-Snippets) for more snippets.
### [Catch2](https://github.com/catchorg/Catch2) ### [Catch2](https://github.com/catchorg/Catch2)
@@ -266,4 +270,4 @@ For a full example on using CPM to download and configure lua with sol2 see [her
### Full Examples ### Full Examples
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for full examples with source code and check out the [wiki](https://github.com/TheLartians/CPM/wiki/More-Snippets) for many more example snippets. See the [examples directory](https://github.com/TheLartians/CPM.cmake/tree/master/examples) for full examples with source code and check out the [wiki](https://github.com/TheLartians/CPM.cmake/wiki/More-Snippets) for many more example snippets.

View File

@@ -1,6 +1,6 @@
# TheLartians/CPM - A simple Git dependency manager # CPM.cmake - CMake's missing package manager
# ================================================= # ===========================================
# See https://github.com/TheLartians/CPM for usage and update instructions. # See https://github.com/TheLartians/CPM.cmake for usage and update instructions.
# #
# MIT License # MIT License
# ----------- # -----------
@@ -28,26 +28,36 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR) cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
set(CURRENT_CPM_VERSION 0.15) set(CURRENT_CPM_VERSION 0.23)
if(CPM_DIRECTORY) if(CPM_DIRECTORY)
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR}) if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION}) if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION})
message(AUTHOR_WARNING "${CPM_INDENT} \ message(AUTHOR_WARNING "${CPM_INDENT} \
A dependency is using a more recent CPM (${NEW_CPM_VERSION}) than the current project (${CPM_VERSION}). \ A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \
It is recommended to upgrade CPM to the most recent version. \ It is recommended to upgrade CPM to the most recent version. \
See https://github.com/TheLartians/CPM for more information." See https://github.com/TheLartians/CPM.cmake for more information."
) )
endif() endif()
return()
endif()
get_property(CPM_INITIALIZED GLOBAL "" PROPERTY CPM_INITIALIZED SET)
if (CPM_INITIALIZED)
return()
endif() endif()
endif() endif()
set_property(GLOBAL PROPERTY CPM_INITIALIZED true)
option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES}) option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES})
option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY}) option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY})
option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL})
option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" $ENV{CPM_DONT_UPDATE_MODULE_PATH})
set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "") set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "")
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "") set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
set(CPM_FILE ${CMAKE_CURRENT_LIST_FILE} CACHE INTERNAL "")
set(CPM_PACKAGES "" CACHE INTERNAL "") set(CPM_PACKAGES "" CACHE INTERNAL "")
set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)") set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)")
@@ -59,6 +69,15 @@ endif()
set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies") set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies")
if (NOT CPM_DONT_UPDATE_MODULE_PATH)
set(CPM_MODULE_PATH "${CMAKE_BINARY_DIR}/CPM_modules" CACHE INTERNAL "")
# remove old modules
FILE(REMOVE_RECURSE ${CPM_MODULE_PATH})
file(MAKE_DIRECTORY ${CPM_MODULE_PATH})
# locally added CPM modules should override global packages
set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}")
endif()
include(FetchContent) include(FetchContent)
include(CMakeParseArguments) include(CMakeParseArguments)
@@ -69,16 +88,25 @@ endif()
function(cpm_find_package NAME VERSION) function(cpm_find_package NAME VERSION)
string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") string(REPLACE " " ";" EXTRA_ARGS "${ARGN}")
find_package(${NAME} ${VERSION} ${EXTRA_ARGS}) find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET)
if(${CPM_ARGS_NAME}_FOUND) if(${CPM_ARGS_NAME}_FOUND)
message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${${CPM_ARGS_NAME}_VERSION}") message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${VERSION}")
CPMRegisterPackage(${CPM_ARGS_NAME} "${${CPM_ARGS_NAME}_VERSION}") CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}")
set(CPM_PACKAGE_FOUND YES PARENT_SCOPE) set(CPM_PACKAGE_FOUND YES PARENT_SCOPE)
else() else()
set(CPM_PACKAGE_FOUND NO PARENT_SCOPE) set(CPM_PACKAGE_FOUND NO PARENT_SCOPE)
endif() endif()
endfunction() endfunction()
# Create a custom FindXXX.cmake module for a CPM package
# This prevents `find_package(NAME)` from finding the system library
function(CPMCreateModuleFile Name)
if (NOT CPM_DONT_UPDATE_MODULE_PATH)
# erase any previous modules
FILE(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake "include(${CPM_FILE})\n${ARGN}\nset(${Name}_FOUND TRUE)")
endif()
endfunction()
# Find a package locally or fallback to CPMAddPackage # Find a package locally or fallback to CPMAddPackage
function(CPMFindPackage) function(CPMFindPackage)
set(oneValueArgs set(oneValueArgs
@@ -88,13 +116,28 @@ function(CPMFindPackage)
) )
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN})
if (NOT DEFINED CPM_ARGS_VERSION)
if (DEFINED CPM_ARGS_GIT_TAG)
cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION)
endif()
if (NOT DEFINED CPM_ARGS_VERSION)
set(CPM_ARGS_VERSION 0)
endif()
endif()
if (CPM_DOWNLOAD_ALL) if (CPM_DOWNLOAD_ALL)
CPMAddPackage(${ARGN}) CPMAddPackage(${ARGN})
cpm_export_variables() cpm_export_variables()
return() return()
endif() endif()
CPMCheckIfPackageAlreadyAdded(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}")
if (CPM_PACKAGE_ALREADY_ADDED)
cpm_export_variables()
return()
endif()
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
if(NOT CPM_PACKAGE_FOUND) if(NOT CPM_PACKAGE_FOUND)
@@ -104,6 +147,33 @@ function(CPMFindPackage)
endfunction() endfunction()
# checks if a package has been added before
function(CPMCheckIfPackageAlreadyAdded CPM_ARGS_NAME CPM_ARGS_VERSION CPM_ARGS_OPTIONS)
if ("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES)
CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION)
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})
cpm_get_fetch_properties(${CPM_ARGS_NAME})
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}")
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}")
SET(${CPM_ARGS_NAME}_ADDED NO)
SET(CPM_PACKAGE_ALREADY_ADDED YES PARENT_SCOPE)
cpm_export_variables()
else()
SET(CPM_PACKAGE_ALREADY_ADDED NO PARENT_SCOPE)
endif()
endfunction()
# Download and add a package from source # Download and add a package from source
function(CPMAddPackage) function(CPMAddPackage)
@@ -123,19 +193,7 @@ function(CPMAddPackage)
OPTIONS OPTIONS
) )
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}")
if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY)
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
if(CPM_PACKAGE_FOUND)
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 DEFINED CPM_ARGS_VERSION) if (NOT DEFINED CPM_ARGS_VERSION)
if (DEFINED CPM_ARGS_GIT_TAG) if (DEFINED CPM_ARGS_GIT_TAG)
@@ -166,28 +224,24 @@ function(CPMAddPackage)
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
endif() endif()
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES) CPMCheckIfPackageAlreadyAdded(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}")
CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) if (CPM_PACKAGE_ALREADY_ADDED)
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})
cpm_get_fetch_properties(${CPM_ARGS_NAME})
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}")
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}")
SET(${CPM_ARGS_NAME}_ADDED NO)
cpm_export_variables() cpm_export_variables()
return() return()
endif() endif()
if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY)
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
if(CPM_PACKAGE_FOUND)
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()
CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION}) CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})
if (CPM_ARGS_OPTIONS) if (CPM_ARGS_OPTIONS)
@@ -207,28 +261,30 @@ function(CPMAddPackage)
if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND) if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND)
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND})
else() elseif(DEFINED CPM_ARGS_SOURCE_DIR)
if (CPM_SOURCE_CACHE AND NOT DEFINED CPM_ARGS_SOURCE_DIR) set(FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR})
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) elseif (CPM_SOURCE_CACHE)
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
list(SORT origin_parameters) set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
string(SHA1 origin_hash "${origin_parameters}") list(SORT origin_parameters)
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) string(SHA1 origin_hash "${origin_parameters}")
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory}) set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
if (EXISTS ${download_directory}) list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":") if (EXISTS ${download_directory})
set(PACKAGE_INFO "${download_directory}") # disable the download command to allow offline builds
else() list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}")
# remove timestamps so CMake will re-download the dependency set(PACKAGE_INFO "${download_directory}")
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild) else()
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}") # remove timestamps so CMake will re-download the dependency
endif() file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
endif() endif()
endif() endif()
cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS}) cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS})
cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY}) cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
cpm_get_fetch_properties(${CPM_ARGS_NAME}) cpm_get_fetch_properties(${CPM_ARGS_NAME})
CPMCreateModuleFile(${CPM_ARGS_NAME} "CPMAddPackage(${ARGN})")
SET(${CPM_ARGS_NAME}_ADDED YES) SET(${CPM_ARGS_NAME}_ADDED YES)
cpm_export_variables() cpm_export_variables()
endfunction() endfunction()
@@ -290,7 +346,8 @@ function (cpm_fetch_package PACKAGE DOWNLOAD_ONLY)
set(CPM_OLD_INDENT "${CPM_INDENT}") set(CPM_OLD_INDENT "${CPM_INDENT}")
set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:")
if(${DOWNLOAD_ONLY}) if(${DOWNLOAD_ONLY})
if(NOT "${PACKAGE}_POPULATED") FetchContent_GetProperties(${PACKAGE})
if(NOT ${PACKAGE}_POPULATED)
FetchContent_Populate(${PACKAGE}) FetchContent_Populate(${PACKAGE})
endif() endif()
else() else()
@@ -302,9 +359,15 @@ endfunction()
# splits a package option # splits a package option
function(cpm_parse_option OPTION) function(cpm_parse_option OPTION)
string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION}) string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION})
string(LENGTH ${OPTION} OPTION_LENGTH)
string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH) string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH)
math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") if (OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH)
string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) # no value for key provided, assume user wants to set option to "ON"
set(OPTION_VALUE "ON")
else()
math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1")
string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE)
endif()
set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE) set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE)
set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE) set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE)
endfunction() endfunction()

View File

@@ -20,3 +20,15 @@ endfunction()
function(ASSERTION_FAILED) function(ASSERTION_FAILED)
message(FATAL_ERROR "assertion failed: ${ARGN}") message(FATAL_ERROR "assertion failed: ${ARGN}")
endfunction() endfunction()
function(ASSERT_EXISTS file)
if (NOT EXISTS ${file})
message(FATAL_ERROR "assertion failed: file ${file} does not exist")
endif()
endfunction()
function(ASSERT_NOT_EXISTS file)
if (EXISTS ${file})
message(FATAL_ERROR "assertion failed: file ${file} exists")
endif()
endfunction()

0
examples/build_all.py Normal file → Executable file
View File

View File

@@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleCatch2) project(CPMExampleCatch2)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ---- # ---- Dependencies ----
include(../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
@@ -26,16 +22,9 @@ CPMAddPackage(
add_executable(CPMExampleCatch2 main.cpp) add_executable(CPMExampleCatch2 main.cpp)
target_link_libraries(CPMExampleCatch2 fibonacci Catch2) target_link_libraries(CPMExampleCatch2 fibonacci Catch2)
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror") set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ---- # ---- Enable testing ----
ENABLE_TESTING() ENABLE_TESTING()
ADD_TEST(CPMExampleCatch2 CPMExampleCatch2) 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()

View File

@@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleCXXOpts) project(CPMExampleCXXOpts)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ---- # ---- Dependencies ----
include(../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
@@ -23,4 +19,4 @@ CPMAddPackage(
add_executable(CPMExampleCXXOpts main.cpp) add_executable(CPMExampleCXXOpts main.cpp)
target_link_libraries(CPMExampleCXXOpts cxxopts) target_link_libraries(CPMExampleCXXOpts cxxopts)
set_target_properties(CPMExampleCXXOpts PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror") set_target_properties(CPMExampleCXXOpts PROPERTIES CXX_STANDARD 17)

View File

@@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleDoctest) project(CPMExampleDoctest)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ---- # ---- Dependencies ----
include(../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
@@ -26,16 +22,9 @@ CPMAddPackage(
add_executable(CPMExampleDoctest main.cpp) add_executable(CPMExampleDoctest main.cpp)
target_link_libraries(CPMExampleDoctest fibonacci doctest) target_link_libraries(CPMExampleDoctest fibonacci doctest)
set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror") set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ---- # ---- Enable testing ----
ENABLE_TESTING() ENABLE_TESTING()
ADD_TEST(CPMExampleDoctest CPMExampleDoctest) 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()

View File

@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMJSONExample)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME fmt
GIT_TAG 6.1.2
GITHUB_REPOSITORY fmtlib/fmt
)
# ---- Executable ----
add_executable(CPMFmtExample "main.cpp")
set_target_properties(CPMFmtExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMFmtExample fmt)

6
examples/fmt/main.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <fmt/format.h>
int main(){
fmt::print("Hello, {}!\n", "world");
return 0;
}

View File

@@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleGtest) project(CPMExampleGtest)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ---- # ---- Dependencies ----
include(../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
@@ -23,7 +19,7 @@ CPMAddPackage(
VERSION 1.8.1 VERSION 1.8.1
OPTIONS OPTIONS
"INSTALL_GTEST OFF" "INSTALL_GTEST OFF"
"gtest_force_shared_crt ON" "gtest_force_shared_crt"
) )
# ---- Create binary ---- # ---- Create binary ----
@@ -36,10 +32,3 @@ set_target_properties(CPMExampleGtest PROPERTIES CXX_STANDARD 17)
enable_testing() enable_testing()
add_test(CPMExampleGtest CPMExampleGtest) add_test(CPMExampleGtest CPMExampleGtest)
# ---- Add code coverage ----
if (${ENABLE_TEST_COVERAGE})
set_target_properties(CPMExampleGtest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
target_link_options(CPMExampleGtest PUBLIC "--coverage")
endif()

View File

@@ -14,4 +14,4 @@ foreach(test ${tests})
) )
endforeach() endforeach()
add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose) add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} -C Debug --verbose)

View File

@@ -8,6 +8,7 @@ set(CPM_SOURCE_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/CPM")
set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/cache) set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/cache)
function(clear_cache) function(clear_cache)
message(STATUS "clearing CPM cache")
FILE(REMOVE_RECURSE ${CPM_SOURCE_CACHE_DIR}) FILE(REMOVE_RECURSE ${CPM_SOURCE_CACHE_DIR})
if (EXISTS "${CPM_SOURCE_CACHE_DIR}") if (EXISTS "${CPM_SOURCE_CACHE_DIR}")
@@ -104,6 +105,16 @@ if (NOT EXISTS "${CPM_SOURCE_CACHE_DIR}/fibonacci")
ASSERTION_FAILED("fibonacci not in cache") ASSERTION_FAILED("fibonacci not in cache")
endif() endif()
## Reuse cached packages for other build
execute_process(
COMMAND
${CMAKE_COMMAND} -E env "CPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" ${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/cache" "-B${TEST_BUILD_DIR}-2"
RESULT_VARIABLE ret
)
ASSERT_EQUAL(${ret} "0")
## Overwrite CPM_SOURCE_CACHE with argument ## Overwrite CPM_SOURCE_CACHE with argument
reset_test() reset_test()

30
test/unit/modules.cmake Normal file
View File

@@ -0,0 +1,30 @@
include(CMakePackageConfigHelpers)
include(${CPM_PATH}/testing.cmake)
set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/modules)
function(initProjectWithDependency TEST_DEPENDENCY_NAME)
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/test_project/CMakeLists.txt.in"
"${CMAKE_CURRENT_LIST_DIR}/test_project/CMakeLists.txt"
INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/junk
)
execute_process(
COMMAND
${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/test_project" "-B${TEST_BUILD_DIR}"
RESULT_VARIABLE ret
)
ASSERT_EQUAL(${ret} "0")
endfunction()
initProjectWithDependency(A)
ASSERT_EXISTS(${TEST_BUILD_DIR}/CPM_modules)
ASSERT_EXISTS(${TEST_BUILD_DIR}/CPM_modules/FindA.cmake)
ASSERT_NOT_EXISTS(${TEST_BUILD_DIR}/CPM_modules/FindB.cmake)
initProjectWithDependency(B)
ASSERT_NOT_EXISTS(${TEST_BUILD_DIR}/CPM_modules/FindA.cmake)
ASSERT_EXISTS(${TEST_BUILD_DIR}/CPM_modules/FindB.cmake)

View File

@@ -0,0 +1,21 @@
include(CMakePackageConfigHelpers)
include(${CPM_PATH}/testing.cmake)
set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/source_dir)
set(TEST_DEPENDENCY_NAME Dependency)
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/test_project/CMakeLists.txt.in"
"${CMAKE_CURRENT_LIST_DIR}/test_project/CMakeLists.txt"
INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/junk
)
execute_process(
COMMAND
${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/test_project" "-B${TEST_BUILD_DIR}"
RESULT_VARIABLE ret
)
ASSERT_EQUAL(${ret} "0")

1
test/unit/test_project/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/CMakeLists.txt

View File

@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleCatch2)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ----
include(@CPM_PATH@/CPM.cmake)
CPMAddPackage(
NAME @TEST_DEPENDENCY_NAME@
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependency
)
# ---- check if generated modules override find_package ----
if (@test_check_find_package@)
find_package(@TEST_DEPENDENCY_NAME@ REQUIRED)
endif()
# ---- Call dependency method to validate correct addition of directory ----
dependency_function()

View File

@@ -0,0 +1,4 @@
function(dependency_function)
message("called external method")
endfunction()