Skip to content

Downloading CPM.cmake in CMake

Juan Ramos edited this page Nov 25, 2021 · 17 revisions

As outlined in the README, there is more than one way to include CPM.cmake in your project. In addition to using the command line, you can use CMake to download a specific release of CPM.cmake to your project and include it automatically.

set(CPM_DOWNLOAD_VERSION 0.34.0)

if(CPM_SOURCE_CACHE)
  set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
  set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
  set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
  file(DOWNLOAD
       https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
       ${CPM_DOWNLOAD_LOCATION}
  )
endif()

include(${CPM_DOWNLOAD_LOCATION})

To update the version of CPM.cmake that is included, just update the CPM_DOWNLOAD_VERSION variable with the correct version number, omitting the v prefix.

Note that downloading CPM.cmake using the script above will not allow you to create new builds of your project when offline, even if all dependencies are available in the CPM_SOURCE_CACHE. Therefore, especially for libraries, it is recommended to add the CPM.cmake script to the source repository.

Clone this wiki locally