@@ -42,7 +42,7 @@ if(NOT COMMAND cpm_message)
4242 endfunction ()
4343endif ()
4444
45- set (CURRENT_CPM_VERSION 0.40.0 )
45+ set (CURRENT_CPM_VERSION 0.40.5 )
4646
4747get_filename_component (CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR} " REALPATH)
4848if (CPM_DIRECTORY)
@@ -162,7 +162,7 @@ set(CPM_SOURCE_CACHE
162162 CACHE PATH "Directory to download CPM dependencies"
163163)
164164
165- if (NOT CPM_DONT_UPDATE_MODULE_PATH)
165+ if (NOT CPM_DONT_UPDATE_MODULE_PATH AND NOT DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR )
166166 set (CPM_MODULE_PATH
167167 "${CMAKE_BINARY_DIR} /CPM_modules"
168168 CACHE INTERNAL ""
@@ -269,10 +269,25 @@ endfunction()
269269# finding the system library
270270function (cpm_create_module_file Name )
271271 if (NOT CPM_DONT_UPDATE_MODULE_PATH)
272- # erase any previous modules
273- file (WRITE ${CPM_MODULE_PATH} /Find ${Name} .cmake
274- "include(\" ${CPM_FILE} \" )\n ${ARGN} \n set(${Name} _FOUND TRUE)"
275- )
272+ if (DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR)
273+ # Redirect find_package calls to the CPM package. This is what FetchContent does when you set
274+ # OVERRIDE_FIND_PACKAGE. The CMAKE_FIND_PACKAGE_REDIRECTS_DIR works for find_package in CONFIG
275+ # mode, unlike the Find${Name}.cmake fallback. CMAKE_FIND_PACKAGE_REDIRECTS_DIR is not defined
276+ # in script mode, or in CMake < 3.24.
277+ # https://cmake.org/cmake/help/latest/module/FetchContent.html#fetchcontent-find-package-integration-examples
278+ string (TOLOWER ${Name} NameLower)
279+ file (WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR} /${NameLower} -config.cmake
280+ "include(\" ${CMAKE_CURRENT_LIST_DIR} /${NameLower} -extra.cmake\" OPTIONAL)\n "
281+ "include(\" ${CMAKE_CURRENT_LIST_DIR} /${Name} Extra.cmake\" OPTIONAL)\n "
282+ )
283+ file (WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR} /${NameLower} -version .cmake
284+ "set(PACKAGE_VERSION_COMPATIBLE TRUE)\n " "set(PACKAGE_VERSION_EXACT TRUE)\n "
285+ )
286+ else ()
287+ file (WRITE ${CPM_MODULE_PATH} /Find ${Name} .cmake
288+ "include(\" ${CPM_FILE} \" )\n ${ARGN} \n set(${Name} _FOUND TRUE)"
289+ )
290+ endif ()
276291 endif ()
277292endfunction ()
278293
@@ -475,15 +490,18 @@ function(cpm_add_patches)
475490
476491 # Find the patch program.
477492 find_program (PATCH_EXECUTABLE patch)
478- if (WIN32 AND NOT PATCH_EXECUTABLE)
493+ if (CMAKE_HOST_WIN32 AND NOT PATCH_EXECUTABLE)
479494 # The Windows git executable is distributed with patch.exe. Find the path to the executable, if
480- # it exists, then search `../../usr/bin` for patch.exe.
495+ # it exists, then search `../usr/bin` and `../ ../usr/bin` for patch.exe.
481496 find_package (Git QUIET )
482497 if (GIT_EXECUTABLE)
483498 get_filename_component (extra_search_path ${GIT_EXECUTABLE} DIRECTORY )
484- get_filename_component (extra_search_path ${extra_search_path} DIRECTORY )
485- get_filename_component (extra_search_path ${extra_search_path} DIRECTORY )
486- find_program (PATCH_EXECUTABLE patch HINTS "${extra_search_path} /usr/bin" )
499+ get_filename_component (extra_search_path_1up ${extra_search_path} DIRECTORY )
500+ get_filename_component (extra_search_path_2up ${extra_search_path_1up} DIRECTORY )
501+ find_program (
502+ PATCH_EXECUTABLE patch HINTS "${extra_search_path_1up} /usr/bin"
503+ "${extra_search_path_2up} /usr/bin"
504+ )
487505 endif ()
488506 endif ()
489507 if (NOT PATCH_EXECUTABLE)
@@ -862,14 +880,39 @@ function(CPMAddPackage)
862880 )
863881
864882 if (NOT CPM_SKIP_FETCH)
883+ # CMake 3.28 added EXCLUDE, SYSTEM (3.25), and SOURCE_SUBDIR (3.18) to FetchContent_Declare.
884+ # Calling FetchContent_MakeAvailable will then internally forward these options to
885+ # add_subdirectory. Up until these changes, we had to call FetchContent_Populate and
886+ # add_subdirectory separately, which is no longer necessary and has been deprecated as of 3.30.
887+ # A Bug in CMake prevents us to use the non-deprecated functions until 3.30.3.
888+ set (fetchContentDeclareExtraArgs "" )
889+ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3" )
890+ if (${CPM_ARGS_EXCLUDE_FROM_ALL} )
891+ list (APPEND fetchContentDeclareExtraArgs EXCLUDE_FROM_ALL )
892+ endif ()
893+ if (${CPM_ARGS_SYSTEM} )
894+ list (APPEND fetchContentDeclareExtraArgs SYSTEM )
895+ endif ()
896+ if (DEFINED CPM_ARGS_SOURCE_SUBDIR)
897+ list (APPEND fetchContentDeclareExtraArgs SOURCE_SUBDIR ${CPM_ARGS_SOURCE_SUBDIR} )
898+ endif ()
899+ # For CMake version <3.28 OPTIONS are parsed in cpm_add_subdirectory
900+ if (CPM_ARGS_OPTIONS AND NOT DOWNLOAD_ONLY)
901+ foreach (OPTION ${CPM_ARGS_OPTIONS} )
902+ cpm_parse_option("${OPTION} " )
903+ set (${OPTION_KEY} "${OPTION_VALUE} " )
904+ endforeach ()
905+ endif ()
906+ endif ()
865907 cpm_declare_fetch(
866- "${CPM_ARGS_NAME} " " ${CPM_ARGS_VERSION} " " ${PACKAGE_INFO} " "${CPM_ARGS_UNPARSED_ARGUMENTS} "
908+ "${CPM_ARGS_NAME} " ${fetchContentDeclareExtraArgs} "${CPM_ARGS_UNPARSED_ARGUMENTS} "
867909 )
868- cpm_fetch_package("${CPM_ARGS_NAME} " populated)
910+
911+ cpm_fetch_package("${CPM_ARGS_NAME} " ${DOWNLOAD_ONLY} populated ${CPM_ARGS_UNPARSED_ARGUMENTS} )
869912 if (CPM_SOURCE_CACHE AND download_directory)
870913 file (LOCK ${download_directory} /../cmake.lock RELEASE)
871914 endif ()
872- if (${populated} )
915+ if (${populated} AND ${CMAKE_VERSION} VERSION_LESS "3.30.3" )
873916 cpm_add_subdirectory(
874917 "${CPM_ARGS_NAME} "
875918 "${DOWNLOAD_ONLY} "
@@ -980,7 +1023,7 @@ function(CPMGetPackageVersion PACKAGE OUTPUT)
9801023endfunction ()
9811024
9821025# declares a package in FetchContent_Declare
983- function (cpm_declare_fetch PACKAGE VERSION INFO )
1026+ function (cpm_declare_fetch PACKAGE)
9841027 if (${CPM_DRY_RUN} )
9851028 cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)" )
9861029 return ()
@@ -1056,7 +1099,7 @@ endfunction()
10561099
10571100# downloads a previously declared package via FetchContent and exports the variables
10581101# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope
1059- function (cpm_fetch_package PACKAGE populated)
1102+ function (cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated)
10601103 set (${populated}
10611104 FALSE
10621105 PARENT_SCOPE
@@ -1071,7 +1114,24 @@ function(cpm_fetch_package PACKAGE populated)
10711114 string (TOLOWER "${PACKAGE} " lower_case_name)
10721115
10731116 if (NOT ${lower_case_name} _POPULATED)
1074- FetchContent_Populate(${PACKAGE} )
1117+ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3" )
1118+ if (DOWNLOAD_ONLY)
1119+ # MakeAvailable will call add_subdirectory internally which is not what we want when
1120+ # DOWNLOAD_ONLY is set. Populate will only download the dependency without adding it to the
1121+ # build
1122+ FetchContent_Populate(
1123+ ${PACKAGE}
1124+ SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR} /${lower_case_name} -src"
1125+ BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR} /${lower_case_name} -build"
1126+ SUBBUILD_DIR "${CPM_FETCHCONTENT_BASE_DIR} /${lower_case_name} -subbuild"
1127+ ${ARGN}
1128+ )
1129+ else ()
1130+ FetchContent_MakeAvailable(${PACKAGE} )
1131+ endif ()
1132+ else ()
1133+ FetchContent_Populate(${PACKAGE} )
1134+ endif ()
10751135 set (${populated}
10761136 TRUE
10771137 PARENT_SCOPE
0 commit comments