From 4d9aa6e6c8bfd19745c41e4269061dec898b1f83 Mon Sep 17 00:00:00 2001 From: crueter Date: Thu, 9 Jul 2026 16:55:34 -0400 Subject: [PATCH 1/3] WIP: [cmake] Fix CPMUtil on Windows by avoiding a temp dir Signed-off-by: crueter --- CMakeModules/CPMUtil.cmake | 85 ++++++++++++++------------------------ 1 file changed, 30 insertions(+), 55 deletions(-) diff --git a/CMakeModules/CPMUtil.cmake b/CMakeModules/CPMUtil.cmake index 3144f832c2..6302c5755f 100644 --- a/CMakeModules/CPMUtil.cmake +++ b/CMakeModules/CPMUtil.cmake @@ -95,7 +95,7 @@ macro(echo) execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${message}") else() - message(STATUS "${message}") + message(DEBUG "${message}") endif() endmacro() @@ -122,34 +122,10 @@ endmacro() # Analogous to GNU mktemp, with fallbacks function(mktempdir out) - # shell out to system mktemp if available - find_program(MKTEMP_EXECUTABLE mktemp) - if (MKTEMP_EXECUTABLE) - execute_process(COMMAND mktemp -d - OUTPUT_VARIABLE dir - OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE ret) - - if (ret EQUAL 0) - set(${out} "${dir}" PARENT_SCOPE) - return() - endif() - endif() - string(RANDOM LENGTH 10 rand_str) set(tmp_str "tmp.${rand_str}") - # create something in /tmp if it exists - if(EXISTS "/tmp" AND IS_DIRECTORY "/tmp") - set(dir "/tmp/${tmp_str}") - file(MAKE_DIRECTORY "${dir}" RESULT res) - if (res EQUAL 0) - set(${out} "${dir}" PARENT_SCOPE) - return() - endif() - endif() - - # tmpdir does not exist, extremely legacy mode + # Windows sucks. set(dir "${CMAKE_CURRENT_LIST_DIR}/.tmp/${tmp_str}") file(MAKE_DIRECTORY "${dir}" RESULT res) if (res EQUAL 0) @@ -218,6 +194,7 @@ endfunction() # Download a URL to file, with a sha512 hash # And retry 5 times function(cpm_download url file) + echo("Downloading ${url} to ${file}") list(LENGTH ARGN argn_len) if(argn_len GREATER 0) list(GET ARGN 0 hash) @@ -229,8 +206,7 @@ function(cpm_download url file) foreach(i RANGE 5) file(DOWNLOAD ${url} ${file} ${args} - STATUS ret - LOG log) + STATUS ret) list(GET ret 0 code) if (code EQUAL 0) @@ -339,7 +315,15 @@ function(fetch_package) return() endif() - # Temporary directory. + # paths + cmake_path(ABSOLUTE_PATH ARG_PATH + NORMALIZE + OUTPUT_VARIABLE abs_path) + + cmake_path(GET abs_path PARENT_PATH path_parent) + file(MAKE_DIRECTORY ${path_parent}) + + # Temporary directory for the downloaded archive mktempdir(TMP) # Get filename from URL @@ -348,51 +332,42 @@ function(fetch_package) # Download set(file ${TMP}/${base_filename}) cpm_download(${ARG_URL} ${file} ${ARG_HASH}) - message(DEBUG "Downloaded ${base_filename}") + echo("Downloaded ${base_filename}") - # Extract the downloaded archive - # TODO: Moar error handling - set(dir ${TMP}/${base_filename}-extracted) - file(MAKE_DIRECTORY ${dir}) + # Extract + string(RANDOM LENGTH 8 extract_suffix) + set(extraction_dir "${path_parent}/.extract_${extract_suffix}") + file(REMOVE_RECURSE ${extraction_dir}) + file(MAKE_DIRECTORY ${extraction_dir}) + + echo("Extracting ${file}...") file(ARCHIVE_EXTRACT INPUT ${file} - DESTINATION ${dir}) + DESTINATION ${extraction_dir}) # This is copied near-verbatim from ExternalProject/extractfile.cmake.in # If there's just one subdirectory and nothing else, move it - file(GLOB contents "${dir}/*") - list(REMOVE_ITEM contents "${dir}/.DS_Store") + file(GLOB contents "${extraction_dir}/*") + list(REMOVE_ITEM contents "${extraction_dir}/.DS_Store") list(LENGTH contents n) # If n == 1 and contents points to a directory, this is a GitHub-style pack # In this case contents points to the subdir which will get renamed # If not, contents will point to the parent dir which will get renamed if (NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") - set(contents "${dir}") + set(contents "${extraction_dir}") endif() file(REAL_PATH "${contents}" contents_abs) - # paths - cmake_path(ABSOLUTE_PATH ARG_PATH - NORMALIZE - OUTPUT_VARIABLE abs_path) - - cmake_path(GET abs_path PARENT_PATH path_parent) - cmake_path(GET abs_path FILENAME path_name) - - # rename tmp dir - set(tmp_renamed "${TMP}/${path_name}") - file(RENAME "${contents_abs}" "${tmp_renamed}") - - # now copy - # TODO: Error handling beyond what cmake does???? - file(COPY ${tmp_renamed} DESTINATION ${path_parent}) + # now move to final path + file(REMOVE_RECURSE ${abs_path}) + file(RENAME ${contents_abs} ${abs_path}) # TODO: only echo this in script mode - message(DEBUG "Extracted to ${abs_path}") + message("Extracted to ${abs_path}") # Apply patches apply_patches("${ARG_PATCHES}" "${abs_path}") @@ -401,7 +376,7 @@ function(fetch_package) file(WRITE "${abs_path}/.cpm_patch_key" ${ARG_PATCH_KEY}) # done! :) - file(REMOVE_RECURSE ${TMP}) + file(REMOVE_RECURSE ${TMP} ${extraction_dir}) endfunction() # compute a hash of all patch file contents From 1d06076f962226c5237c25fdf57911a8a7a09853 Mon Sep 17 00:00:00 2001 From: crueter Date: Thu, 9 Jul 2026 17:27:48 -0400 Subject: [PATCH 2/3] try to simplify even further Signed-off-by: crueter --- CMakeModules/CPMUtil.cmake | 49 +++++++------------------------ tools/cpm/cmake/ScriptUtils.cmake | 42 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/CMakeModules/CPMUtil.cmake b/CMakeModules/CPMUtil.cmake index 6302c5755f..c710f6e8e1 100644 --- a/CMakeModules/CPMUtil.cmake +++ b/CMakeModules/CPMUtil.cmake @@ -120,23 +120,6 @@ macro(sleep time) execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${time}) endmacro() -# Analogous to GNU mktemp, with fallbacks -function(mktempdir out) - string(RANDOM LENGTH 10 rand_str) - set(tmp_str "tmp.${rand_str}") - - # Windows sucks. - set(dir "${CMAKE_CURRENT_LIST_DIR}/.tmp/${tmp_str}") - file(MAKE_DIRECTORY "${dir}" RESULT res) - if (res EQUAL 0) - set(${out} "${dir}" PARENT_SCOPE) - return() - endif() - - fatal("Fatal: Could not create temporary directory. " - "Check write permissions to the current directory") -endfunction() - # Get a package's effective URL. function(get_package_url) set(oneValueArgs @@ -323,49 +306,37 @@ function(fetch_package) cmake_path(GET abs_path PARENT_PATH path_parent) file(MAKE_DIRECTORY ${path_parent}) - # Temporary directory for the downloaded archive - mktempdir(TMP) - # Get filename from URL get_filename_component(base_filename ${ARG_URL} NAME) # Download - set(file ${TMP}/${base_filename}) + set(file ${path_parent}/${base_filename}) cpm_download(${ARG_URL} ${file} ${ARG_HASH}) echo("Downloaded ${base_filename}") # Extract - string(RANDOM LENGTH 8 extract_suffix) - set(extraction_dir "${path_parent}/.extract_${extract_suffix}") - file(REMOVE_RECURSE ${extraction_dir}) - file(MAKE_DIRECTORY ${extraction_dir}) - echo("Extracting ${file}...") - file(ARCHIVE_EXTRACT INPUT ${file} - DESTINATION ${extraction_dir}) + DESTINATION ${abs_path}) # This is copied near-verbatim from ExternalProject/extractfile.cmake.in # If there's just one subdirectory and nothing else, move it - file(GLOB contents "${extraction_dir}/*") - list(REMOVE_ITEM contents "${extraction_dir}/.DS_Store") + file(GLOB contents "${abs_path}/*") + list(REMOVE_ITEM contents "${abs_path}/.DS_Store") list(LENGTH contents n) # If n == 1 and contents points to a directory, this is a GitHub-style pack # In this case contents points to the subdir which will get renamed # If not, contents will point to the parent dir which will get renamed - if (NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") - set(contents "${extraction_dir}") + if (n EQUAL 1 AND IS_DIRECTORY "${contents}") + set(temp_path "${abs_path}_tmp") + file(RENAME "${contents}" "${temp_path}") + file(REMOVE_RECURSE "${abs_path}") + file(RENAME "${temp_path}" "${abs_path}") endif() - file(REAL_PATH "${contents}" contents_abs) - - # now move to final path - file(REMOVE_RECURSE ${abs_path}) - file(RENAME ${contents_abs} ${abs_path}) - # TODO: only echo this in script mode message("Extracted to ${abs_path}") @@ -376,7 +347,7 @@ function(fetch_package) file(WRITE "${abs_path}/.cpm_patch_key" ${ARG_PATCH_KEY}) # done! :) - file(REMOVE_RECURSE ${TMP} ${extraction_dir}) + file(REMOVE_RECURSE ${file}) endfunction() # compute a hash of all patch file contents diff --git a/tools/cpm/cmake/ScriptUtils.cmake b/tools/cpm/cmake/ScriptUtils.cmake index dbd908a1ea..183deae14d 100755 --- a/tools/cpm/cmake/ScriptUtils.cmake +++ b/tools/cpm/cmake/ScriptUtils.cmake @@ -19,6 +19,48 @@ macro(parse_key key) parse_object(${object}) endmacro() + +# Analogous to GNU mktemp, with fallbacks +function(mktempdir out) + # shell out to system mktemp if available + cpm_find_program(MKTEMP_EXECUTABLE mktemp) + if (MKTEMP_EXECUTABLE) + execute_process(COMMAND mktemp -d + OUTPUT_VARIABLE dir + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE ret) + + if (ret EQUAL 0) + set(${out} "${dir}" PARENT_SCOPE) + return() + endif() + endif() + + string(RANDOM LENGTH 10 rand_str) + set(tmp_str "tmp.${rand_str}") + + # create something in /tmp if it exists + if(EXISTS "/tmp" AND IS_DIRECTORY "/tmp") + set(dir "/tmp/${tmp_str}") + file(MAKE_DIRECTORY "${dir}" RESULT res) + if (res EQUAL 0) + set(${out} "${dir}" PARENT_SCOPE) + return() + endif() + endif() + + # tmpdir does not exist, extremely legacy mode + set(dir "${CMAKE_CURRENT_LIST_DIR}/.tmp/${tmp_str}") + file(MAKE_DIRECTORY "${dir}" RESULT res) + if (res EQUAL 0) + set(${out} "${dir}" PARENT_SCOPE) + return() + endif() + + fatal("Fatal: Could not create temporary directory. " + "Check write permissions to the current directory") +endfunction() + # Get a package's effective URL, for an already parsed object function(get_package_url_object out) if (${url}) From 724da89d2bc0f852689300d7588c6bae8170b404 Mon Sep 17 00:00:00 2001 From: crueter Date: Thu, 9 Jul 2026 19:21:01 -0400 Subject: [PATCH 3/3] echo Signed-off-by: crueter --- CMakeModules/CPMUtil.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeModules/CPMUtil.cmake b/CMakeModules/CPMUtil.cmake index c710f6e8e1..c9ad5f1e2f 100644 --- a/CMakeModules/CPMUtil.cmake +++ b/CMakeModules/CPMUtil.cmake @@ -338,7 +338,7 @@ function(fetch_package) endif() # TODO: only echo this in script mode - message("Extracted to ${abs_path}") + echo("Extracted to ${abs_path}") # Apply patches apply_patches("${ARG_PATCHES}" "${abs_path}")