ExternalData: Generalize hash algo/ext handling

Use private global variables _ExternalData_REGEX_(ALGO|EXT) to match the
possible hash algorithm names and extensions in regular expressions.
Use "file(<algo>)" instead of "cmake -E md5sum" to compute hashes
without a child process and to support more hash algorithms.
This commit is contained in:
Brad King 2013-01-24 14:09:39 -05:00
parent 9e518a8169
commit aa8b2288d3
1 changed files with 9 additions and 11 deletions

View File

@ -261,18 +261,16 @@ endfunction()
#-----------------------------------------------------------------------------
# Private helper interface
set(_ExternalData_REGEX_ALGO "MD5")
set(_ExternalData_REGEX_EXT "md5")
set(_ExternalData_SELF "${CMAKE_CURRENT_LIST_FILE}")
get_filename_component(_ExternalData_SELF_DIR "${_ExternalData_SELF}" PATH)
function(_ExternalData_compute_hash var_hash algo file)
if("${algo}" STREQUAL "MD5")
# TODO: Errors
execute_process(COMMAND "${CMAKE_COMMAND}" -E md5sum "${file}"
OUTPUT_VARIABLE output)
string(SUBSTRING "${output}" 0 32 hash)
if("${algo}" MATCHES "^${_ExternalData_REGEX_ALGO}$")
file("${algo}" "${file}" hash)
set("${var_hash}" "${hash}" PARENT_SCOPE)
else()
# TODO: Other hashes.
message(FATAL_ERROR "Hash algorithm ${algo} unimplemented.")
endif()
endfunction()
@ -295,7 +293,7 @@ function(_ExternalData_atomic_write file content)
endfunction()
function(_ExternalData_link_content name var_ext)
if("${ExternalData_LINK_CONTENT}" MATCHES "^(MD5)$")
if("${ExternalData_LINK_CONTENT}" MATCHES "^(${_ExternalData_REGEX_ALGO})$")
set(algo "${ExternalData_LINK_CONTENT}")
else()
message(FATAL_ERROR
@ -305,7 +303,7 @@ function(_ExternalData_link_content name var_ext)
_ExternalData_compute_hash(hash "${algo}" "${name}")
get_filename_component(dir "${name}" PATH)
set(staged "${dir}/.ExternalData_${algo}_${hash}")
set(ext ".md5")
string(TOLOWER ".${algo}" ext)
_ExternalData_atomic_write("${name}${ext}" "${hash}\n")
file(RENAME "${name}" "${staged}")
set("${var_ext}" "${ext}" PARENT_SCOPE)
@ -533,7 +531,7 @@ endmacro()
function(_ExternalData_arg_find_files pattern regex)
file(GLOB globbed RELATIVE "${top_src}" "${top_src}/${pattern}*")
foreach(entry IN LISTS globbed)
if("x${entry}" MATCHES "^x(.*)(\\.md5)$")
if("x${entry}" MATCHES "^x(.*)(\\.(${_ExternalData_REGEX_EXT}))$")
set(relname "${CMAKE_MATCH_1}")
set(alg "${CMAKE_MATCH_2}")
else()
@ -716,8 +714,8 @@ if("${ExternalData_ACTION}" STREQUAL "fetch")
file(READ "${name}${ext}" hash)
string(STRIP "${hash}" hash)
if("${ext}" STREQUAL ".md5")
set(algo "MD5")
if("${ext}" MATCHES "^\\.(${_ExternalData_REGEX_EXT})$")
string(TOUPPER "${CMAKE_MATCH_1}" algo)
else()
message(FATAL_ERROR "Unknown hash algorithm extension \"${ext}\"")
endif()