diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..a5780af6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,61 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +project( + CPM.cmake + VERSION 0.37.0 + LANGUAGES NONE +) + +include(CMakePackageConfigHelpers) +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmakeConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmakeConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM.cmake" +) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmakeConfigVersion.cmake" COMPATIBILITY SameMinorVersion + ARCH_INDEPENDENT +) + +# For consumer using FetchContent +if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) + # Check if the user download the repo with git and not in an official release + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git" AND IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git") + find_package(Git REQUIRED QUIET) + # Generate a git-describe version string from Git repository tags. + execute_process( + COMMAND ${GIT_EXECUTABLE} tag --points-at HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE CPM_TAG + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT GIT_DESCRIBE_ERROR_CODE AND NOT CPM_TAG STREQUAL "") + set(CPM_RELEASE TRUE) + else() + set(CPM_RELEASE FALSE) + endif() + else() + set(CPM_RELEASE TRUE) + endif() +endif() + +# Trick to use the find_package +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY) +# Unset in case CPM has been used to install itself +unset(CPM.cmake_FOUND) +set(CPM.cmake_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "" FORCE) +find_package(CPM.cmake ${PROJECT_VERSION} REQUIRED CONFIG) + +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + # Without it : Unable to determine default CMAKE_INSTALL_LIBDIR directory because no target + # architecture is known. Please enable at least one language before including GNUInstallDirs. + enable_language(C) + include(GNUInstallDirs) + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmakeConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmakeConfigVersion.cmake" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM.cmake" + ) +endif() diff --git a/README.md b/README.md index 815e76e2..8d23101e 100644 --- a/README.md +++ b/README.md @@ -112,9 +112,25 @@ The command below will perform this automatically. mkdir -p cmake wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake ``` - You can also download CPM.cmake directly from your project's `CMakeLists.txt`. See the [wiki](https://github.com/cpm-cmake/CPM.cmake/wiki/Downloading-CPM.cmake-in-CMake) for more details. +### Preliminary : +You can use `FetchContent` to obtain CPM from the official repository : +```cmake +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +project(MyProject) + +include(FetchContent) +FetchContent_Declare( + CPM.cmake + GIT_REPOSITORY https://github.com/cpm-cmake/CPM.cmake.git + GIT_TAG v0.38.0 +) +FetchContent_MakeAvailable(CPM.cmake) + +CPMAddPackage("gh:fmtlib/fmt#7.1.3") +``` + ## Updating CPM To update CPM to the newest version, update the script in the project's root directory, for example by running the command above. diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 60b8fd6c..c074cd05 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -42,7 +42,11 @@ if(NOT COMMAND cpm_message) endfunction() endif() -set(CURRENT_CPM_VERSION 1.0.0-development-version) +if(DEFINED CPM_VERSION AND CPM_RELEASE) + set(CURRENT_CPM_VERSION "${CPM_VERSION}${CPM_DEVELOPMENT}") +else() + set(CURRENT_CPM_VERSION 1.0.0-development-version) +endif() get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) if(CPM_DIRECTORY) diff --git a/cmake/CPM.cmakeConfig.cmake.in b/cmake/CPM.cmakeConfig.cmake.in new file mode 100644 index 00000000..d27028c0 --- /dev/null +++ b/cmake/CPM.cmakeConfig.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/CPM.cmake" NO_POLICY_SCOPE) + +check_required_components(CPM)