ExternalData.cmake 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. #[=======================================================================[.rst:
  2. ExternalData
  3. ------------
  4. .. only:: html
  5. .. contents::
  6. Manage data files stored outside source tree
  7. Introduction
  8. ^^^^^^^^^^^^
  9. Use this module to unambiguously reference data files stored outside
  10. the source tree and fetch them at build time from arbitrary local and
  11. remote content-addressed locations. Functions provided by this module
  12. recognize arguments with the syntax ``DATA{<name>}`` as references to
  13. external data, replace them with full paths to local copies of those
  14. data, and create build rules to fetch and update the local copies.
  15. The ``DATA{}`` syntax is literal and the ``<name>`` is a full or relative path
  16. within the source tree. The source tree must contain either a real
  17. data file at ``<name>`` or a "content link" at ``<name><ext>`` containing a
  18. hash of the real file using a hash algorithm corresponding to ``<ext>``.
  19. For example, the argument ``DATA{img.png}`` may be satisfied by either a
  20. real ``img.png`` file in the current source directory or a ``img.png.md5``
  21. file containing its MD5 sum.
  22. Module Functions
  23. ^^^^^^^^^^^^^^^^
  24. .. command:: ExternalData_Expand_Arguments
  25. The ``ExternalData_Expand_Arguments`` function evaluates ``DATA{}``
  26. references in its arguments and constructs a new list of arguments::
  27. ExternalData_Expand_Arguments(
  28. <target> # Name of data management target
  29. <outVar> # Output variable
  30. [args...] # Input arguments, DATA{} allowed
  31. )
  32. It replaces each ``DATA{}`` reference in an argument with the full path of
  33. a real data file on disk that will exist after the ``<target>`` builds.
  34. .. command:: ExternalData_Add_Test
  35. The ``ExternalData_Add_Test`` function wraps around the CMake
  36. :command:`add_test` command but supports ``DATA{}`` references in
  37. its arguments::
  38. ExternalData_Add_Test(
  39. <target> # Name of data management target
  40. ... # Arguments of add_test(), DATA{} allowed
  41. )
  42. It passes its arguments through ``ExternalData_Expand_Arguments`` and then
  43. invokes the :command:`add_test` command using the results.
  44. .. command:: ExternalData_Add_Target
  45. The ``ExternalData_Add_Target`` function creates a custom target to
  46. manage local instances of data files stored externally::
  47. ExternalData_Add_Target(
  48. <target> # Name of data management target
  49. )
  50. It creates custom commands in the target as necessary to make data
  51. files available for each ``DATA{}`` reference previously evaluated by
  52. other functions provided by this module. A list of URL templates may
  53. be provided in the variable ``ExternalData_URL_TEMPLATES`` using the
  54. placeholders ``%(algo)`` and ``%(hash)`` in each template. Data fetch
  55. rules try each URL template in order by substituting the hash
  56. algorithm name for ``%(algo)`` and the hash value for ``%(hash)``.
  57. Hash Algorithms
  58. ^^^^^^^^^^^^^^^
  59. The following hash algorithms are supported::
  60. %(algo) <ext> Description
  61. ------- ----- -----------
  62. MD5 .md5 Message-Digest Algorithm 5, RFC 1321
  63. SHA1 .sha1 US Secure Hash Algorithm 1, RFC 3174
  64. SHA224 .sha224 US Secure Hash Algorithms, RFC 4634
  65. SHA256 .sha256 US Secure Hash Algorithms, RFC 4634
  66. SHA384 .sha384 US Secure Hash Algorithms, RFC 4634
  67. SHA512 .sha512 US Secure Hash Algorithms, RFC 4634
  68. Note that the hashes are used only for unique data identification and
  69. download verification.
  70. Example Usage
  71. ^^^^^^^^^^^^^
  72. .. code-block:: cmake
  73. include(ExternalData)
  74. set(ExternalData_URL_TEMPLATES "file:///local/%(algo)/%(hash)"
  75. "file:////host/share/%(algo)/%(hash)"
  76. "http://data.org/%(algo)/%(hash)")
  77. ExternalData_Add_Test(MyData
  78. NAME MyTest
  79. COMMAND MyExe DATA{MyInput.png}
  80. )
  81. ExternalData_Add_Target(MyData)
  82. When test ``MyTest`` runs the ``DATA{MyInput.png}`` argument will be
  83. replaced by the full path to a real instance of the data file
  84. ``MyInput.png`` on disk. If the source tree contains a content link
  85. such as ``MyInput.png.md5`` then the ``MyData`` target creates a real
  86. ``MyInput.png`` in the build tree.
  87. Referencing File Series
  88. ^^^^^^^^^^^^^^^^^^^^^^^
  89. The ``DATA{}`` syntax can be told to fetch a file series using the form
  90. ``DATA{<name>,:}``, where the ``:`` is literal. If the source tree
  91. contains a group of files or content links named like a series then a
  92. reference to one member adds rules to fetch all of them. Although all
  93. members of a series are fetched, only the file originally named by the
  94. ``DATA{}`` argument is substituted for it. The default configuration
  95. recognizes file series names ending with ``#.ext``, ``_#.ext``, ``.#.ext``,
  96. or ``-#.ext`` where ``#`` is a sequence of decimal digits and ``.ext`` is
  97. any single extension. Configure it with a regex that parses ``<number>``
  98. and ``<suffix>`` parts from the end of ``<name>``::
  99. ExternalData_SERIES_PARSE = regex of the form (<number>)(<suffix>)$
  100. For more complicated cases set::
  101. ExternalData_SERIES_PARSE = regex with at least two () groups
  102. ExternalData_SERIES_PARSE_PREFIX = <prefix> regex group number, if any
  103. ExternalData_SERIES_PARSE_NUMBER = <number> regex group number
  104. ExternalData_SERIES_PARSE_SUFFIX = <suffix> regex group number
  105. Configure series number matching with a regex that matches the
  106. ``<number>`` part of series members named ``<prefix><number><suffix>``::
  107. ExternalData_SERIES_MATCH = regex matching <number> in all series members
  108. Note that the ``<suffix>`` of a series does not include a hash-algorithm
  109. extension.
  110. Referencing Associated Files
  111. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  112. The ``DATA{}`` syntax can alternatively match files associated with the
  113. named file and contained in the same directory. Associated files may
  114. be specified by options using the syntax
  115. ``DATA{<name>,<opt1>,<opt2>,...}``. Each option may specify one file by
  116. name or specify a regular expression to match file names using the
  117. syntax ``REGEX:<regex>``. For example, the arguments::
  118. DATA{MyData/MyInput.mhd,MyInput.img} # File pair
  119. DATA{MyData/MyFrames00.png,REGEX:MyFrames[0-9]+\\.png} # Series
  120. will pass ``MyInput.mha`` and ``MyFrames00.png`` on the command line but
  121. ensure that the associated files are present next to them.
  122. Referencing Directories
  123. ^^^^^^^^^^^^^^^^^^^^^^^
  124. The ``DATA{}`` syntax may reference a directory using a trailing slash and
  125. a list of associated files. The form ``DATA{<name>/,<opt1>,<opt2>,...}``
  126. adds rules to fetch any files in the directory that match one of the
  127. associated file options. For example, the argument
  128. ``DATA{MyDataDir/,REGEX:.*}`` will pass the full path to a ``MyDataDir``
  129. directory on the command line and ensure that the directory contains
  130. files corresponding to every file or content link in the ``MyDataDir``
  131. source directory.
  132. Module Variables
  133. ^^^^^^^^^^^^^^^^
  134. The following variables configure behavior. They should be set before
  135. calling any of the functions provided by this module.
  136. .. variable:: ExternalData_LINK_CONTENT
  137. The ``ExternalData_LINK_CONTENT`` variable may be set to the name of a
  138. supported hash algorithm to enable automatic conversion of real data
  139. files referenced by the ``DATA{}`` syntax into content links. For each
  140. such ``<file>`` a content link named ``<file><ext>`` is created. The
  141. original file is renamed to the form ``.ExternalData_<algo>_<hash>`` to
  142. stage it for future transmission to one of the locations in the list
  143. of URL templates (by means outside the scope of this module). The
  144. data fetch rule created for the content link will use the staged
  145. object if it cannot be found using any URL template.
  146. .. variable:: ExternalData_OBJECT_STORES
  147. The ``ExternalData_OBJECT_STORES`` variable may be set to a list of local
  148. directories that store objects using the layout ``<dir>/%(algo)/%(hash)``.
  149. These directories will be searched first for a needed object. If the
  150. object is not available in any store then it will be fetched remotely
  151. using the URL templates and added to the first local store listed. If
  152. no stores are specified the default is a location inside the build
  153. tree.
  154. .. variable:: ExternalData_SOURCE_ROOT
  155. The ``ExternalData_SOURCE_ROOT`` variable may be set to the highest source
  156. directory containing any path named by a ``DATA{}`` reference. The
  157. default is ``CMAKE_SOURCE_DIR``. ``ExternalData_SOURCE_ROOT`` and
  158. ``CMAKE_SOURCE_DIR`` must refer to directories within a single source
  159. distribution (e.g. they come together in one tarball).
  160. .. variable:: ExternalData_BINARY_ROOT
  161. The ``ExternalData_BINARY_ROOT`` variable may be set to the directory to
  162. hold the real data files named by expanded ``DATA{}`` references. The
  163. default is ``CMAKE_BINARY_DIR``. The directory layout will mirror that of
  164. content links under ``ExternalData_SOURCE_ROOT``.
  165. .. variable:: ExternalData_TIMEOUT_INACTIVITY
  166. The ``ExternalData_TIMEOUT_INACTIVITY`` variable sets the download
  167. inactivity timeout, in seconds, with a default of ``60`` seconds.
  168. Set to ``0`` to disable enforcement.
  169. .. variable:: ExternalData_TIMEOUT_ABSOLUTE
  170. The ``ExternalData_TIMEOUT_ABSOLUTE`` variable sets the download
  171. absolute timeout, in seconds, with a default of ``300`` seconds.
  172. Set to ``0`` to disable enforcement.
  173. #]=======================================================================]
  174. #=============================================================================
  175. # Copyright 2010-2015 Kitware, Inc.
  176. #
  177. # Distributed under the OSI-approved BSD License (the "License");
  178. # see accompanying file Copyright.txt for details.
  179. #
  180. # This software is distributed WITHOUT ANY WARRANTY; without even the
  181. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  182. # See the License for more information.
  183. #=============================================================================
  184. # (To distribute this file outside of CMake, substitute the full
  185. # License text for the above reference.)
  186. function(ExternalData_add_test target)
  187. # Expand all arguments as a single string to preserve escaped semicolons.
  188. ExternalData_expand_arguments("${target}" testArgs "${ARGN}")
  189. add_test(${testArgs})
  190. endfunction()
  191. function(ExternalData_add_target target)
  192. if(NOT ExternalData_URL_TEMPLATES AND NOT ExternalData_OBJECT_STORES)
  193. message(FATAL_ERROR
  194. "Neither ExternalData_URL_TEMPLATES nor ExternalData_OBJECT_STORES is set!")
  195. endif()
  196. if(NOT ExternalData_OBJECT_STORES)
  197. set(ExternalData_OBJECT_STORES ${CMAKE_BINARY_DIR}/ExternalData/Objects)
  198. endif()
  199. set(config ${CMAKE_CURRENT_BINARY_DIR}/${target}_config.cmake)
  200. configure_file(${_ExternalData_SELF_DIR}/ExternalData_config.cmake.in ${config} @ONLY)
  201. set(files "")
  202. # Set "_ExternalData_FILE_${file}" for each output file to avoid duplicate
  203. # rules. Use local data first to prefer real files over content links.
  204. # Custom commands to copy or link local data.
  205. get_property(data_local GLOBAL PROPERTY _ExternalData_${target}_LOCAL)
  206. foreach(entry IN LISTS data_local)
  207. string(REPLACE "|" ";" tuple "${entry}")
  208. list(GET tuple 0 file)
  209. list(GET tuple 1 name)
  210. if(NOT DEFINED "_ExternalData_FILE_${file}")
  211. set("_ExternalData_FILE_${file}" 1)
  212. add_custom_command(
  213. COMMENT "Generating ${file}"
  214. OUTPUT "${file}"
  215. COMMAND ${CMAKE_COMMAND} -Drelative_top=${CMAKE_BINARY_DIR}
  216. -Dfile=${file} -Dname=${name}
  217. -DExternalData_ACTION=local
  218. -DExternalData_CONFIG=${config}
  219. -P ${_ExternalData_SELF}
  220. MAIN_DEPENDENCY "${name}"
  221. )
  222. list(APPEND files "${file}")
  223. endif()
  224. endforeach()
  225. # Custom commands to fetch remote data.
  226. get_property(data_fetch GLOBAL PROPERTY _ExternalData_${target}_FETCH)
  227. foreach(entry IN LISTS data_fetch)
  228. string(REPLACE "|" ";" tuple "${entry}")
  229. list(GET tuple 0 file)
  230. list(GET tuple 1 name)
  231. list(GET tuple 2 ext)
  232. set(stamp "${ext}-stamp")
  233. if(NOT DEFINED "_ExternalData_FILE_${file}")
  234. set("_ExternalData_FILE_${file}" 1)
  235. add_custom_command(
  236. # Users care about the data file, so hide the hash/timestamp file.
  237. COMMENT "Generating ${file}"
  238. # The hash/timestamp file is the output from the build perspective.
  239. # List the real file as a second output in case it is a broken link.
  240. # The files must be listed in this order so CMake can hide from the
  241. # make tool that a symlink target may not be newer than the input.
  242. OUTPUT "${file}${stamp}" "${file}"
  243. # Run the data fetch/update script.
  244. COMMAND ${CMAKE_COMMAND} -Drelative_top=${CMAKE_BINARY_DIR}
  245. -Dfile=${file} -Dname=${name} -Dext=${ext}
  246. -DExternalData_ACTION=fetch
  247. -DExternalData_CONFIG=${config}
  248. -P ${_ExternalData_SELF}
  249. # Update whenever the object hash changes.
  250. MAIN_DEPENDENCY "${name}${ext}"
  251. )
  252. list(APPEND files "${file}${stamp}")
  253. endif()
  254. endforeach()
  255. # Custom target to drive all update commands.
  256. add_custom_target(${target} ALL DEPENDS ${files})
  257. endfunction()
  258. function(ExternalData_expand_arguments target outArgsVar)
  259. # Replace DATA{} references with real arguments.
  260. set(data_regex "DATA{([^;{}\r\n]*)}")
  261. set(other_regex "([^D]|D[^A]|DA[^T]|DAT[^A]|DATA[^{])+|.")
  262. set(outArgs "")
  263. # This list expansion un-escapes semicolons in list element values so we
  264. # must re-escape them below anywhere a new list expansion will occur.
  265. foreach(arg IN LISTS ARGN)
  266. if("x${arg}" MATCHES "${data_regex}")
  267. # Re-escape in-value semicolons before expansion in foreach below.
  268. string(REPLACE ";" "\\;" tmp "${arg}")
  269. # Split argument into DATA{}-pieces and other pieces.
  270. string(REGEX MATCHALL "${data_regex}|${other_regex}" pieces "${tmp}")
  271. # Compose output argument with DATA{}-pieces replaced.
  272. set(outArg "")
  273. foreach(piece IN LISTS pieces)
  274. if("x${piece}" MATCHES "^x${data_regex}$")
  275. # Replace this DATA{}-piece with a file path.
  276. _ExternalData_arg("${target}" "${piece}" "${CMAKE_MATCH_1}" file)
  277. set(outArg "${outArg}${file}")
  278. else()
  279. # No replacement needed for this piece.
  280. set(outArg "${outArg}${piece}")
  281. endif()
  282. endforeach()
  283. else()
  284. # No replacements needed in this argument.
  285. set(outArg "${arg}")
  286. endif()
  287. # Re-escape in-value semicolons in resulting list.
  288. string(REPLACE ";" "\\;" outArg "${outArg}")
  289. list(APPEND outArgs "${outArg}")
  290. endforeach()
  291. set("${outArgsVar}" "${outArgs}" PARENT_SCOPE)
  292. endfunction()
  293. #-----------------------------------------------------------------------------
  294. # Private helper interface
  295. set(_ExternalData_REGEX_ALGO "MD5|SHA1|SHA224|SHA256|SHA384|SHA512")
  296. set(_ExternalData_REGEX_EXT "md5|sha1|sha224|sha256|sha384|sha512")
  297. set(_ExternalData_SELF "${CMAKE_CURRENT_LIST_FILE}")
  298. get_filename_component(_ExternalData_SELF_DIR "${_ExternalData_SELF}" PATH)
  299. function(_ExternalData_compute_hash var_hash algo file)
  300. if("${algo}" MATCHES "^${_ExternalData_REGEX_ALGO}$")
  301. file("${algo}" "${file}" hash)
  302. set("${var_hash}" "${hash}" PARENT_SCOPE)
  303. else()
  304. message(FATAL_ERROR "Hash algorithm ${algo} unimplemented.")
  305. endif()
  306. endfunction()
  307. function(_ExternalData_random var)
  308. string(RANDOM LENGTH 6 random)
  309. set("${var}" "${random}" PARENT_SCOPE)
  310. endfunction()
  311. function(_ExternalData_exact_regex regex_var string)
  312. string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" regex "${string}")
  313. set("${regex_var}" "${regex}" PARENT_SCOPE)
  314. endfunction()
  315. function(_ExternalData_atomic_write file content)
  316. _ExternalData_random(random)
  317. set(tmp "${file}.tmp${random}")
  318. file(WRITE "${tmp}" "${content}")
  319. file(RENAME "${tmp}" "${file}")
  320. endfunction()
  321. function(_ExternalData_link_content name var_ext)
  322. if("${ExternalData_LINK_CONTENT}" MATCHES "^(${_ExternalData_REGEX_ALGO})$")
  323. set(algo "${ExternalData_LINK_CONTENT}")
  324. else()
  325. message(FATAL_ERROR
  326. "Unknown hash algorithm specified by ExternalData_LINK_CONTENT:\n"
  327. " ${ExternalData_LINK_CONTENT}")
  328. endif()
  329. _ExternalData_compute_hash(hash "${algo}" "${name}")
  330. get_filename_component(dir "${name}" PATH)
  331. set(staged "${dir}/.ExternalData_${algo}_${hash}")
  332. string(TOLOWER ".${algo}" ext)
  333. _ExternalData_atomic_write("${name}${ext}" "${hash}\n")
  334. file(RENAME "${name}" "${staged}")
  335. set("${var_ext}" "${ext}" PARENT_SCOPE)
  336. file(RELATIVE_PATH relname "${ExternalData_SOURCE_ROOT}" "${name}${ext}")
  337. message(STATUS "Linked ${relname} to ExternalData ${algo}/${hash}")
  338. endfunction()
  339. function(_ExternalData_arg target arg options var_file)
  340. # Separate data path from the options.
  341. string(REPLACE "," ";" options "${options}")
  342. list(GET options 0 data)
  343. list(REMOVE_AT options 0)
  344. # Interpret trailing slashes as directories.
  345. set(data_is_directory 0)
  346. if("x${data}" MATCHES "^x(.*)([/\\])$")
  347. set(data_is_directory 1)
  348. set(data "${CMAKE_MATCH_1}")
  349. endif()
  350. # Convert to full path.
  351. if(IS_ABSOLUTE "${data}")
  352. set(absdata "${data}")
  353. else()
  354. set(absdata "${CMAKE_CURRENT_SOURCE_DIR}/${data}")
  355. endif()
  356. get_filename_component(absdata "${absdata}" ABSOLUTE)
  357. # Convert to relative path under the source tree.
  358. if(NOT ExternalData_SOURCE_ROOT)
  359. set(ExternalData_SOURCE_ROOT "${CMAKE_SOURCE_DIR}")
  360. endif()
  361. set(top_src "${ExternalData_SOURCE_ROOT}")
  362. file(RELATIVE_PATH reldata "${top_src}" "${absdata}")
  363. if(IS_ABSOLUTE "${reldata}" OR "${reldata}" MATCHES "^\\.\\./")
  364. message(FATAL_ERROR "Data file referenced by argument\n"
  365. " ${arg}\n"
  366. "does not lie under the top-level source directory\n"
  367. " ${top_src}\n")
  368. endif()
  369. if(data_is_directory AND NOT IS_DIRECTORY "${top_src}/${reldata}")
  370. message(FATAL_ERROR "Data directory referenced by argument\n"
  371. " ${arg}\n"
  372. "corresponds to source tree path\n"
  373. " ${reldata}\n"
  374. "that does not exist as a directory!")
  375. endif()
  376. if(NOT ExternalData_BINARY_ROOT)
  377. set(ExternalData_BINARY_ROOT "${CMAKE_BINARY_DIR}")
  378. endif()
  379. set(top_bin "${ExternalData_BINARY_ROOT}")
  380. # Handle in-source builds gracefully.
  381. if("${top_src}" STREQUAL "${top_bin}")
  382. if(ExternalData_LINK_CONTENT)
  383. message(WARNING "ExternalData_LINK_CONTENT cannot be used in-source")
  384. set(ExternalData_LINK_CONTENT 0)
  385. endif()
  386. set(top_same 1)
  387. endif()
  388. set(external "") # Entries external to the source tree.
  389. set(internal "") # Entries internal to the source tree.
  390. set(have_original ${data_is_directory})
  391. set(have_original_as_dir 0)
  392. # Process options.
  393. set(series_option "")
  394. set(associated_files "")
  395. set(associated_regex "")
  396. foreach(opt ${options})
  397. # Regular expression to match associated files.
  398. if("x${opt}" MATCHES "^xREGEX:([^:/]+)$")
  399. list(APPEND associated_regex "${CMAKE_MATCH_1}")
  400. elseif(opt STREQUAL ":")
  401. # Activate series matching.
  402. set(series_option "${opt}")
  403. elseif("x${opt}" MATCHES "^[^][:/*?]+$")
  404. # Specific associated file.
  405. list(APPEND associated_files "${opt}")
  406. else()
  407. message(FATAL_ERROR "Unknown option \"${opt}\" in argument\n"
  408. " ${arg}\n")
  409. endif()
  410. endforeach()
  411. if(series_option)
  412. if(data_is_directory)
  413. message(FATAL_ERROR "Series option \"${series_option}\" not allowed with directories.")
  414. endif()
  415. if(associated_files OR associated_regex)
  416. message(FATAL_ERROR "Series option \"${series_option}\" not allowed with associated files.")
  417. endif()
  418. # Load a whole file series.
  419. _ExternalData_arg_series()
  420. elseif(data_is_directory)
  421. if(associated_files OR associated_regex)
  422. # Load listed/matching associated files in the directory.
  423. _ExternalData_arg_associated()
  424. else()
  425. message(FATAL_ERROR "Data directory referenced by argument\n"
  426. " ${arg}\n"
  427. "must list associated files.")
  428. endif()
  429. else()
  430. # Load the named data file.
  431. _ExternalData_arg_single()
  432. if(associated_files OR associated_regex)
  433. # Load listed/matching associated files.
  434. _ExternalData_arg_associated()
  435. endif()
  436. endif()
  437. if(NOT have_original)
  438. if(have_original_as_dir)
  439. set(msg_kind FATAL_ERROR)
  440. set(msg "that is directory instead of a file!")
  441. else()
  442. set(msg_kind AUTHOR_WARNING)
  443. set(msg "that does not exist as a file (with or without an extension)!")
  444. endif()
  445. message(${msg_kind} "Data file referenced by argument\n"
  446. " ${arg}\n"
  447. "corresponds to source tree path\n"
  448. " ${reldata}\n"
  449. "${msg}")
  450. endif()
  451. if(external)
  452. # Make the series available in the build tree.
  453. set_property(GLOBAL APPEND PROPERTY
  454. _ExternalData_${target}_FETCH "${external}")
  455. set_property(GLOBAL APPEND PROPERTY
  456. _ExternalData_${target}_LOCAL "${internal}")
  457. set("${var_file}" "${top_bin}/${reldata}" PARENT_SCOPE)
  458. else()
  459. # The whole series is in the source tree.
  460. set("${var_file}" "${top_src}/${reldata}" PARENT_SCOPE)
  461. endif()
  462. endfunction()
  463. macro(_ExternalData_arg_associated)
  464. # Associated files lie in the same directory.
  465. if(data_is_directory)
  466. set(reldir "${reldata}")
  467. else()
  468. get_filename_component(reldir "${reldata}" PATH)
  469. endif()
  470. if(reldir)
  471. set(reldir "${reldir}/")
  472. endif()
  473. _ExternalData_exact_regex(reldir_regex "${reldir}")
  474. # Find files named explicitly.
  475. foreach(file ${associated_files})
  476. _ExternalData_exact_regex(file_regex "${file}")
  477. _ExternalData_arg_find_files("${reldir}${file}" "${reldir_regex}${file_regex}")
  478. endforeach()
  479. # Find files matching the given regular expressions.
  480. set(all "")
  481. set(sep "")
  482. foreach(regex ${associated_regex})
  483. set(all "${all}${sep}${reldir_regex}${regex}")
  484. set(sep "|")
  485. endforeach()
  486. _ExternalData_arg_find_files("${reldir}" "${all}")
  487. endmacro()
  488. macro(_ExternalData_arg_single)
  489. # Match only the named data by itself.
  490. _ExternalData_exact_regex(data_regex "${reldata}")
  491. _ExternalData_arg_find_files("${reldata}" "${data_regex}")
  492. endmacro()
  493. macro(_ExternalData_arg_series)
  494. # Configure series parsing and matching.
  495. set(series_parse_prefix "")
  496. set(series_parse_number "\\1")
  497. set(series_parse_suffix "\\2")
  498. if(ExternalData_SERIES_PARSE)
  499. if(ExternalData_SERIES_PARSE_NUMBER AND ExternalData_SERIES_PARSE_SUFFIX)
  500. if(ExternalData_SERIES_PARSE_PREFIX)
  501. set(series_parse_prefix "\\${ExternalData_SERIES_PARSE_PREFIX}")
  502. endif()
  503. set(series_parse_number "\\${ExternalData_SERIES_PARSE_NUMBER}")
  504. set(series_parse_suffix "\\${ExternalData_SERIES_PARSE_SUFFIX}")
  505. elseif(NOT "x${ExternalData_SERIES_PARSE}" MATCHES "^x\\([^()]*\\)\\([^()]*\\)\\$$")
  506. message(FATAL_ERROR
  507. "ExternalData_SERIES_PARSE is set to\n"
  508. " ${ExternalData_SERIES_PARSE}\n"
  509. "which is not of the form\n"
  510. " (<number>)(<suffix>)$\n"
  511. "Fix the regular expression or set variables\n"
  512. " ExternalData_SERIES_PARSE_PREFIX = <prefix> regex group number, if any\n"
  513. " ExternalData_SERIES_PARSE_NUMBER = <number> regex group number\n"
  514. " ExternalData_SERIES_PARSE_SUFFIX = <suffix> regex group number\n"
  515. )
  516. endif()
  517. set(series_parse "${ExternalData_SERIES_PARSE}")
  518. else()
  519. set(series_parse "([0-9]*)(\\.[^./]*)$")
  520. endif()
  521. if(ExternalData_SERIES_MATCH)
  522. set(series_match "${ExternalData_SERIES_MATCH}")
  523. else()
  524. set(series_match "[_.-]?[0-9]*")
  525. endif()
  526. # Parse the base, number, and extension components of the series.
  527. string(REGEX REPLACE "${series_parse}" "${series_parse_prefix};${series_parse_number};${series_parse_suffix}" tuple "${reldata}")
  528. list(LENGTH tuple len)
  529. if(NOT "${len}" EQUAL 3)
  530. message(FATAL_ERROR "Data file referenced by argument\n"
  531. " ${arg}\n"
  532. "corresponds to path\n"
  533. " ${reldata}\n"
  534. "that does not match regular expression\n"
  535. " ${series_parse}")
  536. endif()
  537. list(GET tuple 0 relbase)
  538. list(GET tuple 2 ext)
  539. # Glob files that might match the series.
  540. # Then match base, number, and extension.
  541. _ExternalData_exact_regex(series_base "${relbase}")
  542. _ExternalData_exact_regex(series_ext "${ext}")
  543. _ExternalData_arg_find_files("${relbase}*${ext}"
  544. "${series_base}${series_match}${series_ext}")
  545. endmacro()
  546. function(_ExternalData_arg_find_files pattern regex)
  547. file(GLOB globbed RELATIVE "${top_src}" "${top_src}/${pattern}*")
  548. foreach(entry IN LISTS globbed)
  549. if("x${entry}" MATCHES "^x(.*)(\\.(${_ExternalData_REGEX_EXT}))$")
  550. set(relname "${CMAKE_MATCH_1}")
  551. set(alg "${CMAKE_MATCH_2}")
  552. else()
  553. set(relname "${entry}")
  554. set(alg "")
  555. endif()
  556. if("x${relname}" MATCHES "^x${regex}$" # matches
  557. AND NOT "x${relname}" MATCHES "(^x|/)\\.ExternalData_" # not staged obj
  558. )
  559. if(IS_DIRECTORY "${top_src}/${entry}")
  560. if("${relname}" STREQUAL "${reldata}")
  561. set(have_original_as_dir 1)
  562. endif()
  563. else()
  564. set(name "${top_src}/${relname}")
  565. set(file "${top_bin}/${relname}")
  566. if(alg)
  567. list(APPEND external "${file}|${name}|${alg}")
  568. elseif(ExternalData_LINK_CONTENT)
  569. _ExternalData_link_content("${name}" alg)
  570. list(APPEND external "${file}|${name}|${alg}")
  571. elseif(NOT top_same)
  572. list(APPEND internal "${file}|${name}")
  573. endif()
  574. if("${relname}" STREQUAL "${reldata}")
  575. set(have_original 1)
  576. endif()
  577. endif()
  578. endif()
  579. endforeach()
  580. set(external "${external}" PARENT_SCOPE)
  581. set(internal "${internal}" PARENT_SCOPE)
  582. set(have_original "${have_original}" PARENT_SCOPE)
  583. set(have_original_as_dir "${have_original_as_dir}" PARENT_SCOPE)
  584. endfunction()
  585. #-----------------------------------------------------------------------------
  586. # Private script mode interface
  587. if(CMAKE_GENERATOR OR NOT ExternalData_ACTION)
  588. return()
  589. endif()
  590. if(ExternalData_CONFIG)
  591. include(${ExternalData_CONFIG})
  592. endif()
  593. if(NOT ExternalData_URL_TEMPLATES AND NOT ExternalData_OBJECT_STORES)
  594. message(FATAL_ERROR
  595. "Neither ExternalData_URL_TEMPLATES nor ExternalData_OBJECT_STORES is set!")
  596. endif()
  597. function(_ExternalData_link_or_copy src dst)
  598. # Create a temporary file first.
  599. get_filename_component(dst_dir "${dst}" PATH)
  600. file(MAKE_DIRECTORY "${dst_dir}")
  601. _ExternalData_random(random)
  602. set(tmp "${dst}.tmp${random}")
  603. if(UNIX)
  604. # Create a symbolic link.
  605. set(tgt "${src}")
  606. if(relative_top)
  607. # Use relative path if files are close enough.
  608. file(RELATIVE_PATH relsrc "${relative_top}" "${src}")
  609. file(RELATIVE_PATH relfile "${relative_top}" "${dst}")
  610. if(NOT IS_ABSOLUTE "${relsrc}" AND NOT "${relsrc}" MATCHES "^\\.\\./" AND
  611. NOT IS_ABSOLUTE "${reldst}" AND NOT "${reldst}" MATCHES "^\\.\\./")
  612. file(RELATIVE_PATH tgt "${dst_dir}" "${src}")
  613. endif()
  614. endif()
  615. execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "${tgt}" "${tmp}" RESULT_VARIABLE result)
  616. else()
  617. # Create a copy.
  618. execute_process(COMMAND "${CMAKE_COMMAND}" -E copy "${src}" "${tmp}" RESULT_VARIABLE result)
  619. endif()
  620. if(result)
  621. file(REMOVE "${tmp}")
  622. message(FATAL_ERROR "Failed to create\n ${tmp}\nfrom\n ${obj}")
  623. endif()
  624. # Atomically create/replace the real destination.
  625. file(RENAME "${tmp}" "${dst}")
  626. endfunction()
  627. function(_ExternalData_download_file url file err_var msg_var)
  628. set(retry 3)
  629. while(retry)
  630. math(EXPR retry "${retry} - 1")
  631. if(ExternalData_TIMEOUT_INACTIVITY)
  632. set(inactivity_timeout INACTIVITY_TIMEOUT ${ExternalData_TIMEOUT_INACTIVITY})
  633. elseif(NOT "${ExternalData_TIMEOUT_INACTIVITY}" EQUAL 0)
  634. set(inactivity_timeout INACTIVITY_TIMEOUT 60)
  635. else()
  636. set(inactivity_timeout "")
  637. endif()
  638. if(ExternalData_TIMEOUT_ABSOLUTE)
  639. set(absolute_timeout TIMEOUT ${ExternalData_TIMEOUT_ABSOLUTE})
  640. elseif(NOT "${ExternalData_TIMEOUT_ABSOLUTE}" EQUAL 0)
  641. set(absolute_timeout TIMEOUT 300)
  642. else()
  643. set(absolute_timeout "")
  644. endif()
  645. file(DOWNLOAD "${url}" "${file}" STATUS status LOG log ${inactivity_timeout} ${absolute_timeout} SHOW_PROGRESS)
  646. list(GET status 0 err)
  647. list(GET status 1 msg)
  648. if(err)
  649. if("${msg}" MATCHES "HTTP response code said error" AND
  650. "${log}" MATCHES "error: 503")
  651. set(msg "temporarily unavailable")
  652. endif()
  653. elseif("${log}" MATCHES "\nHTTP[^\n]* 503")
  654. set(err TRUE)
  655. set(msg "temporarily unavailable")
  656. endif()
  657. if(NOT err OR NOT "${msg}" MATCHES "partial|timeout|temporarily")
  658. break()
  659. elseif(retry)
  660. message(STATUS "[download terminated: ${msg}, retries left: ${retry}]")
  661. endif()
  662. endwhile()
  663. set("${err_var}" "${err}" PARENT_SCOPE)
  664. set("${msg_var}" "${msg}" PARENT_SCOPE)
  665. endfunction()
  666. function(_ExternalData_download_object name hash algo var_obj)
  667. # Search all object stores for an existing object.
  668. foreach(dir ${ExternalData_OBJECT_STORES})
  669. set(obj "${dir}/${algo}/${hash}")
  670. if(EXISTS "${obj}")
  671. message(STATUS "Found object: \"${obj}\"")
  672. set("${var_obj}" "${obj}" PARENT_SCOPE)
  673. return()
  674. endif()
  675. endforeach()
  676. # Download object to the first store.
  677. list(GET ExternalData_OBJECT_STORES 0 store)
  678. set(obj "${store}/${algo}/${hash}")
  679. _ExternalData_random(random)
  680. set(tmp "${obj}.tmp${random}")
  681. set(found 0)
  682. set(tried "")
  683. foreach(url_template IN LISTS ExternalData_URL_TEMPLATES)
  684. string(REPLACE "%(hash)" "${hash}" url_tmp "${url_template}")
  685. string(REPLACE "%(algo)" "${algo}" url "${url_tmp}")
  686. message(STATUS "Fetching \"${url}\"")
  687. _ExternalData_download_file("${url}" "${tmp}" err errMsg)
  688. set(tried "${tried}\n ${url}")
  689. if(err)
  690. set(tried "${tried} (${errMsg})")
  691. else()
  692. # Verify downloaded object.
  693. _ExternalData_compute_hash(dl_hash "${algo}" "${tmp}")
  694. if("${dl_hash}" STREQUAL "${hash}")
  695. set(found 1)
  696. break()
  697. else()
  698. set(tried "${tried} (wrong hash ${algo}=${dl_hash})")
  699. if("$ENV{ExternalData_DEBUG_DOWNLOAD}" MATCHES ".")
  700. file(RENAME "${tmp}" "${store}/${algo}/${dl_hash}")
  701. endif()
  702. endif()
  703. endif()
  704. file(REMOVE "${tmp}")
  705. endforeach()
  706. get_filename_component(dir "${name}" PATH)
  707. set(staged "${dir}/.ExternalData_${algo}_${hash}")
  708. if(found)
  709. file(RENAME "${tmp}" "${obj}")
  710. message(STATUS "Downloaded object: \"${obj}\"")
  711. elseif(EXISTS "${staged}")
  712. set(obj "${staged}")
  713. message(STATUS "Staged object: \"${obj}\"")
  714. else()
  715. if(NOT tried)
  716. set(tried "\n (No ExternalData_URL_TEMPLATES given)")
  717. endif()
  718. message(FATAL_ERROR "Object ${algo}=${hash} not found at:${tried}")
  719. endif()
  720. set("${var_obj}" "${obj}" PARENT_SCOPE)
  721. endfunction()
  722. if("${ExternalData_ACTION}" STREQUAL "fetch")
  723. foreach(v ExternalData_OBJECT_STORES file name ext)
  724. if(NOT DEFINED "${v}")
  725. message(FATAL_ERROR "No \"-D${v}=\" value provided!")
  726. endif()
  727. endforeach()
  728. file(READ "${name}${ext}" hash)
  729. string(STRIP "${hash}" hash)
  730. if("${ext}" MATCHES "^\\.(${_ExternalData_REGEX_EXT})$")
  731. string(TOUPPER "${CMAKE_MATCH_1}" algo)
  732. else()
  733. message(FATAL_ERROR "Unknown hash algorithm extension \"${ext}\"")
  734. endif()
  735. _ExternalData_download_object("${name}" "${hash}" "${algo}" obj)
  736. # Check if file already corresponds to the object.
  737. set(stamp "${ext}-stamp")
  738. set(file_up_to_date 0)
  739. if(EXISTS "${file}" AND EXISTS "${file}${stamp}")
  740. file(READ "${file}${stamp}" f_hash)
  741. string(STRIP "${f_hash}" f_hash)
  742. if("${f_hash}" STREQUAL "${hash}")
  743. #message(STATUS "File already corresponds to object")
  744. set(file_up_to_date 1)
  745. endif()
  746. endif()
  747. if(file_up_to_date)
  748. # Touch the file to convince the build system it is up to date.
  749. execute_process(COMMAND "${CMAKE_COMMAND}" -E touch "${file}")
  750. else()
  751. _ExternalData_link_or_copy("${obj}" "${file}")
  752. endif()
  753. # Atomically update the hash/timestamp file to record the object referenced.
  754. _ExternalData_atomic_write("${file}${stamp}" "${hash}\n")
  755. elseif("${ExternalData_ACTION}" STREQUAL "local")
  756. foreach(v file name)
  757. if(NOT DEFINED "${v}")
  758. message(FATAL_ERROR "No \"-D${v}=\" value provided!")
  759. endif()
  760. endforeach()
  761. _ExternalData_link_or_copy("${name}" "${file}")
  762. else()
  763. message(FATAL_ERROR "Unknown ExternalData_ACTION=[${ExternalData_ACTION}]")
  764. endif()