From fb703d8275f575a66b96347a8006afd230afb071 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 07:30:59 +0100 Subject: [PATCH 01/16] Add install and FetchContent --- CMakeLists.txt | 44 ++++++++++++++++++++++++++++++++++++++++ cmake/CPM.cmake | 6 +++++- cmake/CPMConfig.cmake.in | 5 +++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt create mode 100644 cmake/CPMConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..205f4cb4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +project(CPM VERSION 0.37.0 LANGUAGES NONE) + +option(CPM_INSTALL "Install CPM" OFF) + +include(CMakePackageConfigHelpers) +configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPMConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/CPMConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" + ) +write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT) + +# For consumer using FetchContent +if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) + # Trick to use the find_package + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY) + set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") + find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) + # 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_BINARY_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() + endif() +endif() + +if(CPM_INSTALL) + ## 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}/CPMConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM") +endif() diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 60b8fd6c..0f39f293 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(${PROJECT_NAME} STREQUAL "CPM" AND CPM_RELEASE) + set(CURRENT_CPM_VERSION "${PROJECT_VERSION}") +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/CPMConfig.cmake.in b/cmake/CPMConfig.cmake.in new file mode 100644 index 00000000..d27028c0 --- /dev/null +++ b/cmake/CPMConfig.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/CPM.cmake" NO_POLICY_SCOPE) + +check_required_components(CPM) From 6b7ae0dc4ec46814a4468dda4f93e7b86fb55336 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 07:33:45 +0100 Subject: [PATCH 02/16] cmake-format --- CMakeLists.txt | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 205f4cb4..ddbc734f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,30 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(CPM VERSION 0.37.0 LANGUAGES NONE) +project( + CPM + VERSION 0.37.0 + LANGUAGES NONE +) option(CPM_INSTALL "Install CPM" OFF) include(CMakePackageConfigHelpers) -configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPMConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/CPMConfig.cmake" - INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" - ) -write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT) +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPMConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/CPMConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" +) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" COMPATIBILITY SameMajorVersion + ARCH_INDEPENDENT +) # For consumer using FetchContent if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) # Trick to use the find_package - configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY + ) set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) # Check if the user download the repo with git and not in an official release @@ -37,8 +47,14 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) endif() if(CPM_INSTALL) - ## 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. + # 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}/CPMConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM") + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/CPMConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" + ) endif() From eb8461dabb032212037ad32ad450532fc2a23ecf Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 07:44:44 +0100 Subject: [PATCH 03/16] Fix --- cmake/CPM.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 0f39f293..f2b17283 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -42,7 +42,7 @@ if(NOT COMMAND cpm_message) endfunction() endif() -if(${PROJECT_NAME} STREQUAL "CPM" AND CPM_RELEASE) +if("${PROJECT_NAME}" STREQUAL "CPM" AND CPM_RELEASE) set(CURRENT_CPM_VERSION "${PROJECT_VERSION}") else() set(CURRENT_CPM_VERSION 1.0.0-development-version) From 0711ac195c56d3060ecb636dd5462aaf7ce4b226 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 07:49:52 +0100 Subject: [PATCH 04/16] cmake-format --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ddbc734f..e2a356b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,6 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY ) set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") - find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) # 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) @@ -44,6 +43,7 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) set(CPM_RELEASE FALSE) endif() endif() + find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) endif() if(CPM_INSTALL) From a70c6138a579af1e123955d4c8753b5cf194d0f5 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 16:47:36 +0100 Subject: [PATCH 05/16] Update CMakeLists.txt Co-authored-by: Cristian Le --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2a356b3..92698105 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) endif() -if(CPM_INSTALL) +if (NOT 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) From 8ae0a091879f31116eb4f62f3a10fc0ab5fabdb0 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 16:50:59 +0100 Subject: [PATCH 06/16] Update cmake/CPM.cmake Co-authored-by: Cristian Le --- cmake/CPM.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index f2b17283..3c19fd16 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -42,8 +42,8 @@ if(NOT COMMAND cpm_message) endfunction() endif() -if("${PROJECT_NAME}" STREQUAL "CPM" AND CPM_RELEASE) - set(CURRENT_CPM_VERSION "${PROJECT_VERSION}") +if(DEFINED CPM_VERSION) + set(CURRENT_CPM_VERSION "${CPM_VERSION}${CPM_DEVELOPMENT}") else() set(CURRENT_CPM_VERSION 1.0.0-development-version) endif() From 3e7e6d6458df6898b800d540d71cbbc707a4ce84 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 16:52:41 +0100 Subject: [PATCH 07/16] Update CMakeLists.txt --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 92698105..9fc4031f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,6 @@ project( LANGUAGES NONE ) -option(CPM_INSTALL "Install CPM" OFF) - include(CMakePackageConfigHelpers) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPMConfig.cmake.in" From d908cfe8b4b8fc9562219627a87d12ffd38f27c2 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 16:58:05 +0100 Subject: [PATCH 08/16] cmake-format --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fc4031f..054589fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ configure_package_config_file( ) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" COMPATIBILITY SameMajorVersion - ARCH_INDEPENDENT + ARCH_INDEPENDENT ) # For consumer using FetchContent @@ -44,15 +44,15 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) endif() -if (NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) +if(NOT 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}/CPMConfig.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" ) endif() From d2975d0d49f603dad66f314a7b31850da27bca22 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 17:15:04 +0100 Subject: [PATCH 09/16] Update CMakeLists.txt --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 054589fa..989503d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,11 +40,13 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) else() set(CPM_RELEASE FALSE) endif() + else() + set(CPM_RELEASE TRUE) endif() find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) endif() -if(NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) +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) From 14bb75f2d597e871e5299fb62127101e915533a2 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 18:12:23 +0100 Subject: [PATCH 10/16] Update CMakeLists.txt --- CMakeLists.txt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 989503d6..b389b4e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,18 +19,13 @@ write_basic_package_version_file( # For consumer using FetchContent if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) - # Trick to use the find_package - configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY - ) - set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") # 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_BINARY_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE CPM_TAG RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE OUTPUT_STRIP_TRAILING_WHITESPACE @@ -43,7 +38,7 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) else() set(CPM_RELEASE TRUE) endif() - find_package(CPM CONFIG REQUIRED NO_POLICY_SCOPE) + include(cmake/CPM.cmake) endif() if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) From a0663be91b55d1428af4a4aea2675f233eee49f1 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 18:16:02 +0100 Subject: [PATCH 11/16] Update CMakeLists.txt --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b389b4e2..4f4f6246 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,11 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) else() set(CPM_RELEASE TRUE) endif() + # Trick to use the find_package + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY + ) + set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") include(cmake/CPM.cmake) endif() From fa5058cf0b5106927ce334413187dec09168e2e1 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 18:21:43 +0100 Subject: [PATCH 12/16] Update CPM.cmake --- cmake/CPM.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 3c19fd16..c074cd05 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -42,7 +42,7 @@ if(NOT COMMAND cpm_message) endfunction() endif() -if(DEFINED CPM_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) From 03027e00958552d16bb3e0433fffdf9d1cf0fce1 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 16 Feb 2023 18:57:25 +0100 Subject: [PATCH 13/16] Update CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f4f6246..79a90d88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,7 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY ) - set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}") + set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE) include(cmake/CPM.cmake) endif() From deeefc3bc7e231c78ee1ae1cbd829971093352fa Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 2 Mar 2023 15:56:00 +0100 Subject: [PATCH 14/16] Updates based on comments --- CMakeLists.txt | 29 ++++++++++--------- README.md | 18 +++++++++++- ...nfig.cmake.in => CPM.cmakeConfig.cmake.in} | 0 3 files changed, 32 insertions(+), 15 deletions(-) rename cmake/{CPMConfig.cmake.in => CPM.cmakeConfig.cmake.in} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79a90d88..2861374d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,19 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) project( - CPM + CPM.cmake VERSION 0.37.0 LANGUAGES NONE ) include(CMakePackageConfigHelpers) configure_package_config_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPMConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/CPMConfig.cmake" - INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM" + "${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}/CPMConfigVersion.cmake" COMPATIBILITY SameMajorVersion + "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmakeConfigVersion.cmake" COMPATIBILITY SameMinorVersion ARCH_INDEPENDENT ) @@ -38,23 +38,24 @@ if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) else() set(CPM_RELEASE TRUE) endif() - # Trick to use the find_package - configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake" "${CMAKE_CURRENT_BINARY_DIR}/CPM.cmake" COPYONLY - ) - set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE) - include(cmake/CPM.cmake) 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}") +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}/CPMConfig.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/CPMConfigVersion.cmake" + 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" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CPM.cmake" ) endif() diff --git a/README.md b/README.md index 815e76e2..08db9ad1 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 for 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/CPMConfig.cmake.in b/cmake/CPM.cmakeConfig.cmake.in similarity index 100% rename from cmake/CPMConfig.cmake.in rename to cmake/CPM.cmakeConfig.cmake.in From f02381c346b5542f6869fef773d0e0afb3040afd Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 2 Mar 2023 16:09:14 +0100 Subject: [PATCH 15/16] Updates based on comments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08db9ad1..8d23101e 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/d 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 for the official repository : +You can use `FetchContent` to obtain CPM from the official repository : ```cmake cmake_minimum_required(VERSION 3.14 FATAL_ERROR) project(MyProject) From fbf5763a12ec1a5ed1712fdced8ed19e893d6985 Mon Sep 17 00:00:00 2001 From: flagarde Date: Thu, 2 Mar 2023 16:24:12 +0100 Subject: [PATCH 16/16] Updates based on comments --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2861374d..a5780af6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ endif() 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}") +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)