shared_internal_commands.cmake 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. cmake_policy(VERSION 4.1)
  2. # Determine the remote URL of the project containing the working_directory.
  3. # This will leave output_variable unset if the URL can't be determined.
  4. function(_ep_get_git_remote_url output_variable working_directory)
  5. set("${output_variable}" "" PARENT_SCOPE)
  6. find_package(Git QUIET REQUIRED)
  7. execute_process(
  8. COMMAND ${GIT_EXECUTABLE} symbolic-ref --short HEAD
  9. WORKING_DIRECTORY "${working_directory}"
  10. OUTPUT_VARIABLE git_symbolic_ref
  11. OUTPUT_STRIP_TRAILING_WHITESPACE
  12. ERROR_QUIET
  13. )
  14. if(NOT git_symbolic_ref STREQUAL "")
  15. # We are potentially on a branch. See if that branch is associated with
  16. # an upstream remote (might be just a local one or not a branch at all).
  17. execute_process(
  18. COMMAND ${GIT_EXECUTABLE} config branch.${git_symbolic_ref}.remote
  19. WORKING_DIRECTORY "${working_directory}"
  20. OUTPUT_VARIABLE git_remote_name
  21. OUTPUT_STRIP_TRAILING_WHITESPACE
  22. ERROR_QUIET
  23. )
  24. endif()
  25. if(NOT git_remote_name)
  26. # Can't select a remote based on a branch. If there's only one remote,
  27. # or we have multiple remotes but one is called "origin", choose that.
  28. execute_process(
  29. COMMAND ${GIT_EXECUTABLE} remote
  30. WORKING_DIRECTORY "${working_directory}"
  31. OUTPUT_VARIABLE git_remote_list
  32. OUTPUT_STRIP_TRAILING_WHITESPACE
  33. ERROR_QUIET
  34. )
  35. string(REPLACE "\n" ";" git_remote_list "${git_remote_list}")
  36. list(LENGTH git_remote_list git_remote_list_length)
  37. if(git_remote_list_length EQUAL 0)
  38. message(FATAL_ERROR "Git remote not found in parent project.")
  39. elseif(git_remote_list_length EQUAL 1)
  40. list(GET git_remote_list 0 git_remote_name)
  41. else()
  42. set(base_warning_msg "Multiple git remotes found for parent project")
  43. if("origin" IN_LIST git_remote_list)
  44. message(WARNING "${base_warning_msg}, defaulting to origin.")
  45. set(git_remote_name "origin")
  46. else()
  47. message(FATAL_ERROR "${base_warning_msg}, none of which are origin.")
  48. endif()
  49. endif()
  50. endif()
  51. if(Git_VERSION VERSION_LESS 1.7.5)
  52. set(_git_remote_url_cmd_args config remote.${git_remote_name}.url)
  53. elseif(Git_VERSION VERSION_LESS 2.7)
  54. set(_git_remote_url_cmd_args ls-remote --get-url ${git_remote_name})
  55. else()
  56. set(_git_remote_url_cmd_args remote get-url ${git_remote_name})
  57. endif()
  58. execute_process(
  59. COMMAND ${GIT_EXECUTABLE} ${_git_remote_url_cmd_args}
  60. WORKING_DIRECTORY "${working_directory}"
  61. OUTPUT_VARIABLE git_remote_url
  62. OUTPUT_STRIP_TRAILING_WHITESPACE
  63. COMMAND_ERROR_IS_FATAL LAST
  64. ENCODING UTF-8 # Needed to handle non-ascii characters in local paths
  65. )
  66. set("${output_variable}" "${git_remote_url}" PARENT_SCOPE)
  67. endfunction()
  68. function(_ep_is_relative_git_remote output_variable remote_url)
  69. if(remote_url MATCHES "^\\.\\./")
  70. set("${output_variable}" TRUE PARENT_SCOPE)
  71. else()
  72. set("${output_variable}" FALSE PARENT_SCOPE)
  73. endif()
  74. endfunction()
  75. # Return an absolute remote URL given an existing remote URL and relative path.
  76. # The output_variable will be set to an empty string if an absolute URL
  77. # could not be computed (no error message is output).
  78. function(_ep_resolve_relative_git_remote
  79. output_variable
  80. parent_remote_url
  81. relative_remote_url
  82. )
  83. set("${output_variable}" "" PARENT_SCOPE)
  84. if(parent_remote_url STREQUAL "")
  85. return()
  86. endif()
  87. string(REGEX MATCH
  88. "^(([A-Za-z0-9][A-Za-z0-9+.-]*)://)?(([^/@]+)@)?(\\[[A-Za-z0-9:]+\\]|[^/:]+)?([/:]/?)(.+(\\.git)?/?)$"
  89. git_remote_url_components
  90. "${parent_remote_url}"
  91. )
  92. set(protocol "${CMAKE_MATCH_1}")
  93. set(auth "${CMAKE_MATCH_3}")
  94. set(host "${CMAKE_MATCH_5}")
  95. set(separator "${CMAKE_MATCH_6}")
  96. set(path "${CMAKE_MATCH_7}")
  97. string(REPLACE "/" ";" remote_path_components "${path}")
  98. string(REPLACE "/" ";" relative_path_components "${relative_remote_url}")
  99. foreach(relative_path_component IN LISTS relative_path_components)
  100. if(NOT relative_path_component STREQUAL "..")
  101. break()
  102. endif()
  103. list(LENGTH remote_path_components remote_path_component_count)
  104. if(remote_path_component_count LESS 1)
  105. return()
  106. endif()
  107. list(POP_BACK remote_path_components)
  108. list(POP_FRONT relative_path_components)
  109. endforeach()
  110. list(APPEND final_path_components ${remote_path_components} ${relative_path_components})
  111. list(JOIN final_path_components "/" path)
  112. set("${output_variable}" "${protocol}${auth}${host}${separator}${path}" PARENT_SCOPE)
  113. endfunction()
  114. # The output_variable will be set to the original git_repository if it
  115. # could not be resolved (no error message is output). The original value is
  116. # also returned if it doesn't need to be resolved.
  117. function(_ep_resolve_git_remote
  118. output_variable
  119. git_repository
  120. cmp0150
  121. cmp0150_old_base_dir
  122. )
  123. if(git_repository STREQUAL "")
  124. set("${output_variable}" "" PARENT_SCOPE)
  125. return()
  126. endif()
  127. _ep_is_relative_git_remote(_git_repository_is_relative "${git_repository}")
  128. if(NOT _git_repository_is_relative)
  129. set("${output_variable}" "${git_repository}" PARENT_SCOPE)
  130. return()
  131. endif()
  132. if(cmp0150 STREQUAL "NEW")
  133. _ep_get_git_remote_url(_parent_git_remote_url "${CMAKE_CURRENT_SOURCE_DIR}")
  134. _ep_resolve_relative_git_remote(_resolved_git_remote_url "${_parent_git_remote_url}" "${git_repository}")
  135. if(_resolved_git_remote_url STREQUAL "")
  136. message(FATAL_ERROR
  137. "Failed to resolve relative git remote URL:\n"
  138. " Relative URL: ${git_repository}\n"
  139. " Parent URL: ${_parent_git_remote_url}"
  140. )
  141. endif()
  142. set("${output_variable}" "${_resolved_git_remote_url}" PARENT_SCOPE)
  143. return()
  144. elseif(cmp0150 STREQUAL "")
  145. cmake_policy(GET_WARNING CMP0150 _cmp0150_warning)
  146. message(AUTHOR_WARNING
  147. "${_cmp0150_warning}\n"
  148. "A relative GIT_REPOSITORY path was detected. "
  149. "This will be interpreted as a local path to where the project is being cloned. "
  150. "Set GIT_REPOSITORY to an absolute path or set policy CMP0150 to NEW to avoid "
  151. "this warning."
  152. )
  153. endif()
  154. set("${output_variable}" "${cmp0150_old_base_dir}/${git_repository}" PARENT_SCOPE)
  155. endfunction()
  156. macro(_ep_get_hash_algos out_var)
  157. set(${out_var}
  158. MD5
  159. SHA1
  160. SHA224
  161. SHA256
  162. SHA384
  163. SHA512
  164. SHA3_224
  165. SHA3_256
  166. SHA3_384
  167. SHA3_512
  168. )
  169. endmacro()
  170. macro(_ep_get_hash_regex out_var)
  171. _ep_get_hash_algos(${out_var})
  172. list(JOIN ${out_var} "|" ${out_var})
  173. set(${out_var} "^(${${out_var}})=([0-9A-Fa-f]+)$")
  174. endmacro()
  175. function(_ep_parse_arguments_to_vars
  176. f
  177. keywords
  178. name
  179. ns
  180. args
  181. )
  182. # Transfer the arguments into variables in the calling scope.
  183. # Because some keywords can be repeated, we can't use cmake_parse_arguments().
  184. # Instead, we loop through the args and consider the namespace starting with
  185. # an upper-case letter followed by at least two more upper-case letters,
  186. # numbers or underscores to be keywords.
  187. foreach(key IN LISTS keywords)
  188. unset(${ns}${key})
  189. endforeach()
  190. set(key)
  191. foreach(arg IN LISTS args)
  192. set(is_value 1)
  193. if(arg MATCHES "^[A-Z][A-Z0-9_][A-Z0-9_]+$" AND
  194. NOT (("x${arg}x" STREQUAL "x${key}x") AND
  195. ("x${key}x" STREQUAL "xCOMMANDx")) AND
  196. NOT arg MATCHES "^(TRUE|FALSE|YES)$")
  197. if(arg IN_LIST keywords)
  198. set(is_value 0)
  199. endif()
  200. endif()
  201. if(is_value)
  202. if(key)
  203. # Value
  204. list(APPEND ${ns}${key} "${arg}")
  205. else()
  206. # Missing Keyword
  207. message(AUTHOR_WARNING
  208. "value '${arg}' with no previous keyword in ${f}"
  209. )
  210. endif()
  211. else()
  212. set(key "${arg}")
  213. endif()
  214. endforeach()
  215. foreach(key IN LISTS keywords)
  216. if(DEFINED ${ns}${key})
  217. set(${ns}${key} "${${ns}${key}}" PARENT_SCOPE)
  218. else()
  219. unset(${ns}${key} PARENT_SCOPE)
  220. endif()
  221. endforeach()
  222. endfunction()
  223. # NOTE: This cannot be a macro because that will evaluate anything that looks
  224. # like a CMake variable in any of the args.
  225. function(_ep_parse_arguments
  226. f
  227. keywords
  228. name
  229. ns
  230. args
  231. )
  232. _ep_parse_arguments_to_vars(
  233. "${f}"
  234. "${keywords}"
  235. ${name}
  236. ${ns}
  237. "${args}"
  238. )
  239. foreach(key IN LISTS keywords)
  240. if(DEFINED ${ns}${key})
  241. set(${ns}${key} "${${ns}${key}}" PARENT_SCOPE)
  242. else()
  243. unset(${ns}${key} PARENT_SCOPE)
  244. endif()
  245. endforeach()
  246. # Transfer the arguments to the target as target properties. These are
  247. # read by the various steps, potentially from different scopes.
  248. foreach(key IN LISTS keywords)
  249. if(DEFINED ${ns}${key})
  250. set_property(TARGET ${name} PROPERTY ${ns}${key} "${${ns}${key}}")
  251. endif()
  252. endforeach()
  253. endfunction()
  254. function(_ep_get_tls_version name tls_version_var)
  255. # Note that the arguments are assumed to have already been parsed and have
  256. # been translated into variables with the prefix _EP_... by a call to
  257. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  258. set(tls_version_regex "^1\\.[0-3]$")
  259. set(tls_version "${_EP_TLS_VERSION}")
  260. if(NOT "x${tls_version}" STREQUAL "x")
  261. if(NOT tls_version MATCHES "${tls_version_regex}")
  262. message(FATAL_ERROR "TLS_VERSION '${tls_version}' not known")
  263. endif()
  264. elseif(NOT "x${CMAKE_TLS_VERSION}" STREQUAL "x")
  265. set(tls_version "${CMAKE_TLS_VERSION}")
  266. if(NOT tls_version MATCHES "${tls_version_regex}")
  267. message(FATAL_ERROR "CMAKE_TLS_VERSION '${tls_version}' not known")
  268. endif()
  269. elseif(NOT "x$ENV{CMAKE_TLS_VERSION}" STREQUAL "x")
  270. set(tls_version "$ENV{CMAKE_TLS_VERSION}")
  271. if(NOT tls_version MATCHES "${tls_version_regex}")
  272. message(FATAL_ERROR "ENV{CMAKE_TLS_VERSION} '${tls_version}' not known")
  273. endif()
  274. endif()
  275. set("${tls_version_var}" "${tls_version}" PARENT_SCOPE)
  276. endfunction()
  277. function(_ep_get_tls_verify name tls_verify_var)
  278. # Note that the arguments are assumed to have already been parsed and have
  279. # been translated into variables with the prefix _EP_... by a call to
  280. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  281. set(tls_verify "${_EP_TLS_VERIFY}")
  282. if("x${tls_verify}" STREQUAL "x")
  283. if(NOT "x${CMAKE_TLS_VERIFY}" STREQUAL "x")
  284. set(tls_verify "${CMAKE_TLS_VERIFY}")
  285. elseif(NOT "x$ENV{CMAKE_TLS_VERIFY}" STREQUAL "x")
  286. set(tls_verify "$ENV{CMAKE_TLS_VERIFY}")
  287. endif()
  288. endif()
  289. set("${tls_verify_var}" "${tls_verify}" PARENT_SCOPE)
  290. endfunction()
  291. function(_ep_get_tls_cainfo name tls_cainfo_var)
  292. # Note that the arguments are assumed to have already been parsed and have
  293. # been translated into variables with the prefix _EP_... by a call to
  294. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  295. set(tls_cainfo "${_EP_TLS_CAINFO}")
  296. if("x${tls_cainfo}" STREQUAL "x" AND DEFINED CMAKE_TLS_CAINFO)
  297. set(tls_cainfo "${CMAKE_TLS_CAINFO}")
  298. endif()
  299. set("${tls_cainfo_var}" "${tls_cainfo}" PARENT_SCOPE)
  300. endfunction()
  301. function(_ep_get_netrc name netrc_var)
  302. # Note that the arguments are assumed to have already been parsed and have
  303. # been translated into variables with the prefix _EP_... by a call to
  304. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  305. set(netrc "${_EP_NETRC}")
  306. if("x${netrc}" STREQUAL "x" AND DEFINED CMAKE_NETRC)
  307. set(netrc "${CMAKE_NETRC}")
  308. endif()
  309. set("${netrc_var}" "${netrc}" PARENT_SCOPE)
  310. endfunction()
  311. function(_ep_get_netrc_file name netrc_file_var)
  312. # Note that the arguments are assumed to have already been parsed and have
  313. # been translated into variables with the prefix _EP_... by a call to
  314. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  315. set(netrc_file "${_EP_NETRC_FILE}")
  316. if("x${netrc_file}" STREQUAL "x" AND DEFINED CMAKE_NETRC_FILE)
  317. set(netrc_file "${CMAKE_NETRC_FILE}")
  318. endif()
  319. set("${netrc_file_var}" "${netrc_file}" PARENT_SCOPE)
  320. endfunction()
  321. function(_ep_write_gitclone_script
  322. script_filename
  323. source_dir
  324. git_EXECUTABLE
  325. git_repository
  326. git_tag
  327. git_remote_name
  328. init_submodules
  329. git_submodules_recurse
  330. git_submodules
  331. git_shallow
  332. git_progress
  333. git_config
  334. src_name
  335. work_dir
  336. gitclone_infofile
  337. gitclone_stampfile
  338. tls_version
  339. tls_verify
  340. )
  341. if(NOT Git_VERSION VERSION_LESS 1.8.5)
  342. # Use `git checkout <tree-ish> --` to avoid ambiguity with a local path.
  343. set(git_checkout_explicit-- "--")
  344. else()
  345. # Use `git checkout <branch>` even though this risks ambiguity with a
  346. # local path. Unfortunately we cannot use `git checkout <tree-ish> --`
  347. # because that will not search for remote branch names, a common use case.
  348. set(git_checkout_explicit-- "")
  349. endif()
  350. if("${git_tag}" STREQUAL "")
  351. message(FATAL_ERROR "Tag for git checkout should not be empty.")
  352. endif()
  353. if(Git_VERSION VERSION_LESS 2.20 OR
  354. 2.21 VERSION_LESS_EQUAL Git_VERSION)
  355. set(git_clone_options "--no-checkout")
  356. else()
  357. set(git_clone_options)
  358. endif()
  359. if(git_shallow)
  360. if(NOT Git_VERSION VERSION_LESS 1.7.10)
  361. list(APPEND git_clone_options "--depth 1 --no-single-branch")
  362. else()
  363. list(APPEND git_clone_options "--depth 1")
  364. endif()
  365. endif()
  366. if(git_progress)
  367. list(APPEND git_clone_options --progress)
  368. endif()
  369. foreach(config IN LISTS git_config)
  370. list(APPEND git_clone_options --config \"${config}\")
  371. endforeach()
  372. if(NOT ${git_remote_name} STREQUAL "origin")
  373. list(APPEND git_clone_options --origin \"${git_remote_name}\")
  374. endif()
  375. # The clone config option is sticky, it will apply to all subsequent git
  376. # update operations. The submodules config option is not sticky, because
  377. # git doesn't provide any way to do that. Thus, we will have to pass the
  378. # same config option in the update step too for submodules, but not for
  379. # the main git repo.
  380. set(git_submodules_config_options "")
  381. if(NOT "x${tls_version}" STREQUAL "x")
  382. list(APPEND git_clone_options -c http.sslVersion=tlsv${tls_version})
  383. list(APPEND git_submodules_config_options -c http.sslVersion=tlsv${tls_version})
  384. endif()
  385. if(NOT "x${tls_verify}" STREQUAL "x")
  386. if(tls_verify)
  387. # Default git behavior is "true", but the user might have changed the
  388. # global default to "false". Since TLS_VERIFY was given, ensure we honor
  389. # the specified setting regardless of what the global default might be.
  390. list(APPEND git_clone_options -c http.sslVerify=true)
  391. list(APPEND git_submodules_config_options -c http.sslVerify=true)
  392. else()
  393. list(APPEND git_clone_options -c http.sslVerify=false)
  394. list(APPEND git_submodules_config_options -c http.sslVerify=false)
  395. endif()
  396. endif()
  397. string (REPLACE ";" " " git_clone_options "${git_clone_options}")
  398. configure_file(
  399. ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/gitclone.cmake.in
  400. ${script_filename}
  401. @ONLY
  402. )
  403. endfunction()
  404. function(_ep_write_hgclone_script
  405. script_filename
  406. source_dir
  407. hg_EXECUTABLE
  408. hg_repository
  409. hg_tag
  410. src_name
  411. work_dir
  412. hgclone_infofile
  413. hgclone_stampfile
  414. )
  415. if("${hg_tag}" STREQUAL "")
  416. message(FATAL_ERROR "Tag for hg checkout should not be empty.")
  417. endif()
  418. configure_file(
  419. ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/hgclone.cmake.in
  420. ${script_filename}
  421. @ONLY
  422. )
  423. endfunction()
  424. function(_ep_write_gitupdate_script
  425. script_filename
  426. git_EXECUTABLE
  427. git_tag
  428. git_remote_name
  429. init_submodules
  430. git_submodules_recurse
  431. git_submodules
  432. git_repository
  433. work_dir
  434. git_update_strategy
  435. tls_version
  436. tls_verify
  437. )
  438. if("${git_tag}" STREQUAL "")
  439. message(FATAL_ERROR "Tag for git checkout should not be empty.")
  440. endif()
  441. set(git_stash_save_options --quiet)
  442. if(Git_VERSION VERSION_GREATER_EQUAL 1.7.7)
  443. # This avoids stashing files covered by .gitignore
  444. list(APPEND git_stash_save_options --include-untracked)
  445. elseif(Git_VERSION VERSION_GREATER_EQUAL 1.7.6)
  446. # Untracked files, but also ignored files, so potentially slower
  447. list(APPEND git_stash_save_options --all)
  448. endif()
  449. # The submodules config option is not sticky, git doesn't provide any way
  450. # to do that. We have to pass this config option for the update step too.
  451. # We don't need to set it for the non-submodule update because it gets
  452. # recorded as part of the clone operation in a sticky manner.
  453. set(git_submodules_config_options "")
  454. if(NOT "x${tls_version}" STREQUAL "x")
  455. list(APPEND git_submodules_config_options -c http.sslVersion=tlsv${tls_version})
  456. endif()
  457. if(NOT "x${tls_verify}" STREQUAL "x")
  458. if(tls_verify)
  459. # Default git behavior is "true", but the user might have changed the
  460. # global default to "false". Since TLS_VERIFY was given, ensure we honor
  461. # the specified setting regardless of what the global default might be.
  462. list(APPEND git_submodules_config_options -c http.sslVerify=true)
  463. else()
  464. list(APPEND git_submodules_config_options -c http.sslVerify=false)
  465. endif()
  466. endif()
  467. configure_file(
  468. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/gitupdate.cmake.in"
  469. "${script_filename}"
  470. @ONLY
  471. )
  472. endfunction()
  473. function(_ep_write_downloadfile_script
  474. script_filename
  475. REMOTE
  476. LOCAL
  477. timeout
  478. inactivity_timeout
  479. no_progress
  480. hash
  481. tls_version
  482. tls_verify
  483. tls_cainfo
  484. userpwd
  485. http_headers
  486. netrc
  487. netrc_file
  488. )
  489. if("x${REMOTE}" STREQUAL "x")
  490. message(FATAL_ERROR "REMOTE can't be empty")
  491. endif()
  492. if("x${LOCAL}" STREQUAL "x")
  493. message(FATAL_ERROR "LOCAL can't be empty")
  494. endif()
  495. # REMOTE could contain special characters that parse as separate arguments.
  496. # Things like parentheses are legitimate characters in a URL, but would be
  497. # seen as the start of a new unquoted argument by the cmake language parser.
  498. # Avoid those special cases by preparing quoted strings for direct inclusion
  499. # in the foreach() call that iterates over the set of URLs in REMOTE.
  500. set(REMOTE "[====[${REMOTE}]====]")
  501. string(REPLACE ";" "]====] [====[" REMOTE "${REMOTE}")
  502. if(timeout)
  503. set(TIMEOUT_ARGS TIMEOUT ${timeout})
  504. set(TIMEOUT_MSG "${timeout} seconds")
  505. else()
  506. set(TIMEOUT_ARGS "# no TIMEOUT")
  507. set(TIMEOUT_MSG "none")
  508. endif()
  509. if(inactivity_timeout)
  510. set(INACTIVITY_TIMEOUT_ARGS INACTIVITY_TIMEOUT ${inactivity_timeout})
  511. set(INACTIVITY_TIMEOUT_MSG "${inactivity_timeout} seconds")
  512. else()
  513. set(INACTIVITY_TIMEOUT_ARGS "# no INACTIVITY_TIMEOUT")
  514. set(INACTIVITY_TIMEOUT_MSG "none")
  515. endif()
  516. if(no_progress)
  517. set(SHOW_PROGRESS "")
  518. else()
  519. set(SHOW_PROGRESS "SHOW_PROGRESS")
  520. endif()
  521. _ep_get_hash_regex(_ep_hash_regex)
  522. if("${hash}" MATCHES "${_ep_hash_regex}")
  523. set(ALGO "${CMAKE_MATCH_1}")
  524. string(TOLOWER "${CMAKE_MATCH_2}" EXPECT_VALUE)
  525. else()
  526. set(ALGO "")
  527. set(EXPECT_VALUE "")
  528. endif()
  529. set(TLS_VERSION_CODE "")
  530. if(NOT "x${tls_version}" STREQUAL "x")
  531. set(TLS_VERSION_CODE "set(CMAKE_TLS_VERSION \"${tls_version}\")")
  532. endif()
  533. set(TLS_VERIFY_CODE "")
  534. if(NOT "x${tls_verify}" STREQUAL "x")
  535. set(TLS_VERIFY_CODE "set(CMAKE_TLS_VERIFY \"${tls_verify}\")")
  536. endif()
  537. set(TLS_CAINFO_CODE "")
  538. if(NOT "x${tls_cainfo}" STREQUAL "x")
  539. set(TLS_CAINFO_CODE "set(CMAKE_TLS_CAINFO \"${tls_cainfo}\")")
  540. endif()
  541. set(NETRC_CODE "")
  542. if(NOT "x${netrc}" STREQUAL "x")
  543. set(NETRC_CODE "set(CMAKE_NETRC \"${netrc}\")")
  544. endif()
  545. set(NETRC_FILE_CODE "")
  546. if(NOT "x${netrc_file}" STREQUAL "x")
  547. set(NETRC_FILE_CODE "set(CMAKE_NETRC_FILE \"${netrc_file}\")")
  548. endif()
  549. if(userpwd STREQUAL ":")
  550. set(USERPWD_ARGS)
  551. else()
  552. set(USERPWD_ARGS USERPWD "${userpwd}")
  553. endif()
  554. set(HTTP_HEADERS_ARGS "")
  555. if(NOT http_headers STREQUAL "")
  556. foreach(header IN LISTS http_headers)
  557. string(PREPEND HTTP_HEADERS_ARGS
  558. "HTTPHEADER \"${header}\"\n "
  559. )
  560. endforeach()
  561. endif()
  562. # Used variables:
  563. # * TLS_VERSION_CODE
  564. # * TLS_VERIFY_CODE
  565. # * TLS_CAINFO_CODE
  566. # * ALGO
  567. # * EXPECT_VALUE
  568. # * REMOTE
  569. # * LOCAL
  570. # * SHOW_PROGRESS
  571. # * TIMEOUT_ARGS
  572. # * TIMEOUT_MSG
  573. # * USERPWD_ARGS
  574. # * HTTP_HEADERS_ARGS
  575. configure_file(
  576. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/download.cmake.in"
  577. "${script_filename}"
  578. @ONLY
  579. )
  580. endfunction()
  581. function(_ep_write_verifyfile_script
  582. script_filename
  583. LOCAL
  584. hash
  585. )
  586. _ep_get_hash_regex(_ep_hash_regex)
  587. if("${hash}" MATCHES "${_ep_hash_regex}")
  588. set(ALGO "${CMAKE_MATCH_1}")
  589. string(TOLOWER "${CMAKE_MATCH_2}" EXPECT_VALUE)
  590. else()
  591. set(ALGO "")
  592. set(EXPECT_VALUE "")
  593. endif()
  594. # Used variables:
  595. # * ALGO
  596. # * EXPECT_VALUE
  597. # * LOCAL
  598. configure_file(
  599. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/verify.cmake.in"
  600. "${script_filename}"
  601. @ONLY
  602. )
  603. endfunction()
  604. function(_ep_write_extractfile_script
  605. script_filename
  606. name
  607. filename
  608. directory
  609. options
  610. )
  611. # cmake -E tar auto detects the type of archive being extracted
  612. set(args "xf")
  613. configure_file(
  614. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/extractfile.cmake.in"
  615. "${script_filename}"
  616. @ONLY
  617. )
  618. endfunction()
  619. function(_ep_is_dir_empty dir empty_var)
  620. file(GLOB gr "${dir}/*")
  621. if("${gr}" STREQUAL "")
  622. set(${empty_var} 1 PARENT_SCOPE)
  623. else()
  624. set(${empty_var} 0 PARENT_SCOPE)
  625. endif()
  626. endfunction()
  627. function(_ep_get_git_submodules_recurse git_submodules_recurse)
  628. # Checks for GIT_SUBMODULES_RECURSE argument. Default is ON, which sets
  629. # git_submodules_recurse output variable to "--recursive". Otherwise, the
  630. # output variable is set to an empty value "".
  631. # Note that the arguments are assumed to have already been parsed and have
  632. # been translated into variables with the prefix _EP_... by a call to
  633. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  634. if(NOT DEFINED _EP_GIT_SUBMODULES_RECURSE)
  635. set(recurseFlag "--recursive")
  636. else()
  637. if(_EP_GIT_SUBMODULES_RECURSE)
  638. set(recurseFlag "--recursive")
  639. else()
  640. set(recurseFlag "")
  641. endif()
  642. endif()
  643. set(${git_submodules_recurse} "${recurseFlag}" PARENT_SCOPE)
  644. # The git submodule update '--recursive' flag requires git >= v1.6.5
  645. if(recurseFlag AND Git_VERSION VERSION_LESS 1.6.5)
  646. message(FATAL_ERROR
  647. "git version 1.6.5 or later required for --recursive flag with "
  648. "'git submodule ...': Git_VERSION='${Git_VERSION}'"
  649. )
  650. endif()
  651. endfunction()
  652. function(_ep_add_script_commands script_var work_dir cmd)
  653. # We only support a subset of what ep_replace_location_tags() handles
  654. set(location_tags
  655. SOURCE_DIR
  656. SOURCE_SUBDIR
  657. BINARY_DIR
  658. TMP_DIR
  659. DOWNLOAD_DIR
  660. DOWNLOADED_FILE
  661. )
  662. # There can be multiple COMMANDs, but we have to split those up to
  663. # one command per call to execute_process()
  664. string(CONCAT execute_process_cmd
  665. "execute_process(\n"
  666. " WORKING_DIRECTORY \"${work_dir}\"\n"
  667. " COMMAND_ERROR_IS_FATAL LAST\n"
  668. )
  669. cmake_language(GET_MESSAGE_LOG_LEVEL active_log_level)
  670. if(active_log_level MATCHES "VERBOSE|DEBUG|TRACE")
  671. string(APPEND execute_process_cmd " COMMAND_ECHO STDOUT\n")
  672. endif()
  673. string(APPEND execute_process_cmd " COMMAND ")
  674. string(APPEND ${script_var} "${execute_process_cmd}")
  675. foreach(cmd_arg IN LISTS cmd)
  676. if(cmd_arg STREQUAL "COMMAND")
  677. string(APPEND ${script_var} "\n)\n${execute_process_cmd}")
  678. else()
  679. if(_EP_LIST_SEPARATOR)
  680. string(REPLACE "${_EP_LIST_SEPARATOR}" "\\;" cmd_arg "${cmd_arg}")
  681. endif()
  682. foreach(dir IN LISTS location_tags)
  683. string(REPLACE "<${dir}>" "${_EP_${dir}}" cmd_arg "${cmd_arg}")
  684. endforeach()
  685. string(APPEND ${script_var} " [====[${cmd_arg}]====]")
  686. endif()
  687. endforeach()
  688. string(APPEND ${script_var} "\n)")
  689. set(${script_var} "${${script_var}}" PARENT_SCOPE)
  690. endfunction()
  691. function(_ep_add_download_command name)
  692. set(noValueOptions )
  693. set(singleValueOptions
  694. SCRIPT_FILE # These should only be used by FetchContent
  695. DEPENDS_VARIABLE #
  696. )
  697. set(multiValueOptions )
  698. cmake_parse_arguments(PARSE_ARGV 1 arg
  699. "${noValueOptions}" "${singleValueOptions}" "${multiValueOptions}"
  700. )
  701. # The various _EP_... variables mentioned here and throughout this function
  702. # are expected to already have been set by the caller via a call to
  703. # _ep_parse_arguments() or ep_parse_arguments_to_vars(). Other variables
  704. # with different names are assigned to for historical reasons only to keep
  705. # the code more readable and minimize change.
  706. set(source_dir "${_EP_SOURCE_DIR}")
  707. set(stamp_dir "${_EP_STAMP_DIR}")
  708. set(download_dir "${_EP_DOWNLOAD_DIR}")
  709. set(tmp_dir "${_EP_TMP_DIR}")
  710. set(cmd "${_EP_DOWNLOAD_COMMAND}")
  711. set(cvs_repository "${_EP_CVS_REPOSITORY}")
  712. set(svn_repository "${_EP_SVN_REPOSITORY}")
  713. set(git_repository "${_EP_GIT_REPOSITORY}")
  714. set(hg_repository "${_EP_HG_REPOSITORY}")
  715. set(url "${_EP_URL}")
  716. set(fname "${_EP_DOWNLOAD_NAME}")
  717. # TODO: Perhaps file:// should be copied to download dir before extraction.
  718. string(REGEX REPLACE "file://" "" url "${url}")
  719. set(step_script_contents)
  720. set(depends)
  721. set(comment)
  722. set(work_dir)
  723. set(extra_repo_info)
  724. if(DEFINED _EP_DOWNLOAD_COMMAND)
  725. set(work_dir ${download_dir})
  726. set(method custom)
  727. if(NOT "x${cmd}" STREQUAL "x" AND arg_SCRIPT_FILE)
  728. _ep_add_script_commands(
  729. step_script_contents
  730. "${work_dir}"
  731. "${cmd}" # Must be a single quoted argument
  732. )
  733. endif()
  734. elseif(cvs_repository)
  735. set(method cvs)
  736. find_package(CVS QUIET)
  737. if(NOT CVS_EXECUTABLE)
  738. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  739. endif()
  740. set(cvs_module "${_EP_CVS_MODULE}")
  741. if(NOT cvs_module)
  742. message(FATAL_ERROR "error: no CVS_MODULE")
  743. endif()
  744. set(cvs_tag "${_EP_CVS_TAG}")
  745. get_filename_component(src_name "${source_dir}" NAME)
  746. get_filename_component(work_dir "${source_dir}" PATH)
  747. set(comment "Performing download step (CVS checkout) for '${name}'")
  748. set(cmd
  749. ${CVS_EXECUTABLE}
  750. -d ${cvs_repository}
  751. -q
  752. co ${cvs_tag}
  753. -d ${src_name}
  754. ${cvs_module}
  755. )
  756. if(arg_SCRIPT_FILE)
  757. _ep_add_script_commands(
  758. step_script_contents
  759. "${work_dir}"
  760. "${cmd}" # Must be a single quoted argument
  761. )
  762. endif()
  763. elseif(svn_repository)
  764. set(method svn)
  765. find_package(Subversion QUIET)
  766. if(NOT Subversion_SVN_EXECUTABLE)
  767. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  768. endif()
  769. set(svn_trust_cert "${_EP_SVN_TRUST_CERT}")
  770. set(uses_terminal "${_EP_USES_TERMINAL_DOWNLOAD}")
  771. get_filename_component(src_name "${source_dir}" NAME)
  772. get_filename_component(work_dir "${source_dir}" PATH)
  773. set(comment "Performing download step (SVN checkout) for '${name}'")
  774. set(cmd
  775. ${Subversion_SVN_EXECUTABLE}
  776. co
  777. ${svn_repository}
  778. ${_EP_SVN_REVISION}
  779. )
  780. # The --trust-server-cert option requires --non-interactive
  781. if(svn_trust_cert OR NOT uses_terminal)
  782. list(APPEND cmd "--non-interactive")
  783. endif()
  784. if(svn_trust_cert)
  785. list(APPEND cmd "--trust-server-cert")
  786. endif()
  787. if(DEFINED _EP_SVN_USERNAME)
  788. list(APPEND cmd "--username=${_EP_SVN_USERNAME}")
  789. endif()
  790. if(DEFINED _EP_SVN_PASSWORD)
  791. list(APPEND cmd "--password=${_EP_SVN_PASSWORD}")
  792. endif()
  793. list(APPEND cmd ${src_name})
  794. if(arg_SCRIPT_FILE)
  795. _ep_add_script_commands(
  796. step_script_contents
  797. "${work_dir}"
  798. "${cmd}" # Must be a single quoted argument
  799. )
  800. endif()
  801. elseif(git_repository)
  802. set(method git)
  803. # FetchContent gives us these directly, so don't try to recompute them
  804. if(NOT GIT_EXECUTABLE OR NOT Git_VERSION)
  805. unset(CMAKE_MODULE_PATH) # Use CMake builtin find module
  806. find_package(Git QUIET)
  807. if(NOT GIT_EXECUTABLE)
  808. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  809. endif()
  810. endif()
  811. _ep_get_git_submodules_recurse(git_submodules_recurse)
  812. set(git_tag "${_EP_GIT_TAG}")
  813. if(NOT git_tag)
  814. set(git_tag "master")
  815. endif()
  816. set(git_init_submodules TRUE)
  817. if(DEFINED _EP_GIT_SUBMODULES)
  818. set(git_submodules "${_EP_GIT_SUBMODULES}")
  819. if(git_submodules STREQUAL "" AND _EP_CMP0097 STREQUAL "NEW")
  820. set(git_init_submodules FALSE)
  821. endif()
  822. endif()
  823. set(git_remote_name "${_EP_GIT_REMOTE_NAME}")
  824. if(NOT git_remote_name)
  825. set(git_remote_name "origin")
  826. endif()
  827. _ep_get_tls_version(${name} tls_version)
  828. _ep_get_tls_verify(${name} tls_verify)
  829. set(git_shallow "${_EP_GIT_SHALLOW}")
  830. set(git_progress "${_EP_GIT_PROGRESS}")
  831. set(git_config "${_EP_GIT_CONFIG}")
  832. # If git supports it, make checkouts quiet when checking out a git hash.
  833. # This avoids the very noisy detached head message.
  834. if(Git_VERSION VERSION_GREATER_EQUAL 1.7.7)
  835. list(PREPEND git_config advice.detachedHead=false)
  836. endif()
  837. # The command doesn't expose any details, so we need to record additional
  838. # information in the RepositoryInfo.txt file. For the download step, only
  839. # the things specifically affecting the clone operation should be recorded.
  840. # If the repo changes, the clone script should be run again.
  841. # But if only the tag changes, avoid running the clone script again.
  842. # Let the 'always' running update step checkout the new tag.
  843. #
  844. set(extra_repo_info
  845. "repository=${git_repository}
  846. remote=${git_remote_name}
  847. init_submodules=${git_init_submodules}
  848. recurse_submodules=${git_submodules_recurse}
  849. submodules=${git_submodules}
  850. CMP0097=${_EP_CMP0097}
  851. ")
  852. get_filename_component(src_name "${source_dir}" NAME)
  853. get_filename_component(work_dir "${source_dir}" PATH)
  854. # Since git clone doesn't succeed if the non-empty source_dir exists,
  855. # create a cmake script to invoke as download command.
  856. # The script will delete the source directory and then call git clone.
  857. #
  858. set(clone_script ${tmp_dir}/${name}-gitclone.cmake)
  859. _ep_write_gitclone_script(
  860. ${clone_script}
  861. ${source_dir}
  862. ${GIT_EXECUTABLE}
  863. ${git_repository}
  864. ${git_tag}
  865. ${git_remote_name}
  866. ${git_init_submodules}
  867. "${git_submodules_recurse}"
  868. "${git_submodules}"
  869. "${git_shallow}"
  870. "${git_progress}"
  871. "${git_config}"
  872. ${src_name}
  873. ${work_dir}
  874. ${stamp_dir}/${name}-gitinfo.txt
  875. ${stamp_dir}/${name}-gitclone-lastrun.txt
  876. "${tls_version}"
  877. "${tls_verify}"
  878. )
  879. set(comment "Performing download step (git clone) for '${name}'")
  880. set(cmd ${CMAKE_COMMAND}
  881. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  882. -P ${clone_script}
  883. )
  884. if(arg_SCRIPT_FILE)
  885. set(step_script_contents "include(\"${clone_script}\")")
  886. list(APPEND depends ${clone_script})
  887. endif()
  888. elseif(hg_repository)
  889. set(method hg)
  890. find_package(Hg QUIET)
  891. if(NOT HG_EXECUTABLE)
  892. message(FATAL_ERROR "error: could not find hg for clone of ${name}")
  893. endif()
  894. set(hg_tag "${_EP_HG_TAG}")
  895. if(NOT hg_tag)
  896. set(hg_tag "tip")
  897. endif()
  898. # The command doesn't expose any details, so we need to record additional
  899. # information in the RepositoryInfo.txt file. For the download step, only
  900. # the things specifically affecting the clone operation should be recorded.
  901. # If the repo changes, the clone script should be run again.
  902. # But if only the tag changes, avoid running the clone script again.
  903. # Let the 'always' running update step checkout the new tag.
  904. #
  905. set(extra_repo_info "repository=${hg_repository}")
  906. get_filename_component(src_name "${source_dir}" NAME)
  907. get_filename_component(work_dir "${source_dir}" PATH)
  908. # Since hg clone doesn't succeed if the non-empty source_dir exists,
  909. # create a cmake script to invoke as download command.
  910. # The script will delete the source directory and then call hg clone.
  911. #
  912. set(clone_script ${tmp_dir}/${name}-hgclone.cmake)
  913. _ep_write_hgclone_script(
  914. ${clone_script}
  915. ${source_dir}
  916. ${HG_EXECUTABLE}
  917. ${hg_repository}
  918. ${hg_tag}
  919. ${src_name}
  920. ${work_dir}
  921. ${stamp_dir}/${name}-hginfo.txt
  922. ${stamp_dir}/${name}-hgclone-lastrun.txt
  923. )
  924. set(comment "Performing download step (hg clone) for '${name}'")
  925. set(cmd ${CMAKE_COMMAND}
  926. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  927. -P ${clone_script}
  928. )
  929. if(arg_SCRIPT_FILE)
  930. set(step_script_contents "include(\"${clone_script}\")")
  931. list(APPEND depends ${clone_script})
  932. endif()
  933. elseif(url)
  934. set(method url)
  935. get_filename_component(work_dir "${source_dir}" PATH)
  936. set(hash "${_EP_URL_HASH}")
  937. _ep_get_hash_regex(_ep_hash_regex)
  938. if(hash AND NOT "${hash}" MATCHES "${_ep_hash_regex}")
  939. _ep_get_hash_algos(_ep_hash_algos)
  940. list(JOIN _ep_hash_algos "|" _ep_hash_algos)
  941. message(FATAL_ERROR
  942. "URL_HASH is set to\n"
  943. " ${hash}\n"
  944. "but must be ALGO=value where ALGO is\n"
  945. " ${_ep_hash_algos}\n"
  946. "and value is a hex string."
  947. )
  948. endif()
  949. set(md5 "${_EP_URL_MD5}")
  950. if(md5 AND NOT "MD5=${md5}" MATCHES "${_ep_hash_regex}")
  951. message(FATAL_ERROR
  952. "URL_MD5 is set to\n"
  953. " ${md5}\n"
  954. "but must be a hex string."
  955. )
  956. endif()
  957. if(md5 AND NOT hash)
  958. set(hash "MD5=${md5}")
  959. endif()
  960. set(extra_repo_info
  961. "url(s)=${url}
  962. hash=${hash}
  963. ")
  964. list(LENGTH url url_list_length)
  965. if(NOT "${url_list_length}" STREQUAL "1")
  966. foreach(entry IN LISTS url)
  967. if(NOT "${entry}" MATCHES "^[a-z]+://")
  968. message(FATAL_ERROR
  969. "At least one entry of URL is a path (invalid in a list)"
  970. )
  971. endif()
  972. endforeach()
  973. if("x${fname}" STREQUAL "x")
  974. list(GET url 0 fname)
  975. endif()
  976. endif()
  977. if(IS_DIRECTORY "${url}")
  978. get_filename_component(abs_dir "${url}" ABSOLUTE)
  979. set(comment "Performing download step (DIR copy) for '${name}'")
  980. set(cmd
  981. ${CMAKE_COMMAND} -E rm -rf ${source_dir}
  982. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir}
  983. )
  984. if(arg_SCRIPT_FILE)
  985. # While it may be tempting to implement the two operations directly
  986. # with file(), the behavior is different. file(COPY) preserves input
  987. # file timestamps, which we don't want. Therefore, still use the same
  988. # external commands so that we get the same behavior.
  989. _ep_add_script_commands(
  990. step_script_contents
  991. "${work_dir}"
  992. "${cmd}" # Must be a single quoted argument
  993. )
  994. endif()
  995. else()
  996. set(no_extract "${_EP_DOWNLOAD_NO_EXTRACT}")
  997. string(APPEND extra_repo_info "no_extract=${no_extract}\n")
  998. set(verify_script "${stamp_dir}/verify-${name}.cmake")
  999. if("${url}" MATCHES "^[a-z]+://")
  1000. # TODO: Should download and extraction be different steps?
  1001. if("x${fname}" STREQUAL "x")
  1002. set(fname "${url}")
  1003. endif()
  1004. set(ext_regex [[7z|tar|tar\.bz2|tar\.gz|tar\.xz|tbz2|tgz|txz|zip]])
  1005. if("${fname}" MATCHES "([^/\\?#]+(\\.|=)(${ext_regex}))([/?#].*)?$")
  1006. set(fname "${CMAKE_MATCH_1}")
  1007. elseif(no_extract)
  1008. get_filename_component(fname "${fname}" NAME)
  1009. else()
  1010. # Fall back to a default file name. The actual file name does not
  1011. # matter because it is used only internally and our extraction tool
  1012. # inspects the file content directly. If it turns out the wrong URL
  1013. # was given that will be revealed during the build which is an easier
  1014. # place for users to diagnose than an error here anyway.
  1015. set(fname "archive.tar")
  1016. endif()
  1017. string(REPLACE ";" "-" fname "${fname}")
  1018. set(file ${download_dir}/${fname})
  1019. set(timeout "${_EP_TIMEOUT}")
  1020. set(inactivity_timeout "${_EP_INACTIVITY_TIMEOUT}")
  1021. set(no_progress "${_EP_DOWNLOAD_NO_PROGRESS}")
  1022. _ep_get_tls_version(${name} tls_version)
  1023. _ep_get_tls_verify(${name} tls_verify)
  1024. _ep_get_tls_cainfo(${name} tls_cainfo)
  1025. _ep_get_netrc(${name} netrc)
  1026. _ep_get_netrc_file(${name} netrc_file)
  1027. set(http_username "${_EP_HTTP_USERNAME}")
  1028. set(http_password "${_EP_HTTP_PASSWORD}")
  1029. set(http_headers "${_EP_HTTP_HEADER}")
  1030. set(download_script "${stamp_dir}/download-${name}.cmake")
  1031. _ep_write_downloadfile_script(
  1032. "${download_script}"
  1033. "${url}"
  1034. "${file}"
  1035. "${timeout}"
  1036. "${inactivity_timeout}"
  1037. "${no_progress}"
  1038. "${hash}"
  1039. "${tls_version}"
  1040. "${tls_verify}"
  1041. "${tls_cainfo}"
  1042. "${http_username}:${http_password}"
  1043. "${http_headers}"
  1044. "${netrc}"
  1045. "${netrc_file}"
  1046. )
  1047. set(cmd
  1048. ${CMAKE_COMMAND}
  1049. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  1050. -P "${download_script}"
  1051. COMMAND
  1052. )
  1053. if(arg_SCRIPT_FILE)
  1054. set(step_script_contents "include(\"${download_script}\")\n")
  1055. endif()
  1056. if (no_extract)
  1057. set(steps "download and verify")
  1058. else ()
  1059. set(steps "download, verify and extract")
  1060. endif ()
  1061. set(comment "Performing download step (${steps}) for '${name}'")
  1062. # already verified by 'download_script'
  1063. # We use file(CONFIGURE) instead of file(WRITE) to avoid updating the
  1064. # timestamp when the file already existed and was empty.
  1065. file(CONFIGURE OUTPUT "${verify_script}" CONTENT "")
  1066. # Rather than adding everything to the RepositoryInfo.txt file, it is
  1067. # more robust to just depend on the download script. That way, we will
  1068. # re-download if any aspect of the download changes.
  1069. list(APPEND depends "${download_script}")
  1070. else()
  1071. set(file "${url}")
  1072. if (no_extract)
  1073. set(steps "verify")
  1074. else ()
  1075. set(steps "verify and extract")
  1076. endif ()
  1077. set(comment "Performing download step (${steps}) for '${name}'")
  1078. _ep_write_verifyfile_script(
  1079. "${verify_script}"
  1080. "${file}"
  1081. "${hash}"
  1082. )
  1083. endif()
  1084. list(APPEND cmd ${CMAKE_COMMAND}
  1085. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  1086. -P ${verify_script}
  1087. )
  1088. if(arg_SCRIPT_FILE)
  1089. string(APPEND step_script_contents "include(\"${verify_script}\")\n")
  1090. list(APPEND depends ${verify_script})
  1091. endif()
  1092. set(extract_timestamp "${_EP_DOWNLOAD_EXTRACT_TIMESTAMP}")
  1093. if(no_extract)
  1094. if(DEFINED _EP_DOWNLOAD_EXTRACT_TIMESTAMP)
  1095. message(FATAL_ERROR
  1096. "Cannot specify DOWNLOAD_EXTRACT_TIMESTAMP when using "
  1097. "DOWNLOAD_NO_EXTRACT TRUE"
  1098. )
  1099. endif()
  1100. if(arg_SCRIPT_FILE)
  1101. # There's no target to record the location of the downloaded file.
  1102. # Instead, we copy it to the source directory within the script,
  1103. # which is what FetchContent always does in this situation.
  1104. cmake_path(SET safe_file NORMALIZE "${file}")
  1105. cmake_path(GET safe_file FILENAME filename)
  1106. string(APPEND step_script_contents
  1107. "file(COPY_FILE\n"
  1108. " \"${file}\"\n"
  1109. " \"${source_dir}/${filename}\"\n"
  1110. " ONLY_IF_DIFFERENT\n"
  1111. " INPUT_MAY_BE_RECENT\n"
  1112. ")"
  1113. )
  1114. list(APPEND depends ${source_dir}/${filename})
  1115. else()
  1116. set_property(TARGET ${name} PROPERTY _EP_DOWNLOADED_FILE ${file})
  1117. endif()
  1118. else()
  1119. if(NOT DEFINED _EP_DOWNLOAD_EXTRACT_TIMESTAMP)
  1120. # Default depends on policy CMP0135
  1121. if(_EP_CMP0135 STREQUAL "")
  1122. message(AUTHOR_WARNING
  1123. "The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy "
  1124. "CMP0135 is not set. The policy's OLD behavior will be used. "
  1125. "When using a URL download, the timestamps of extracted files "
  1126. "should preferably be that of the time of extraction, otherwise "
  1127. "code that depends on the extracted contents might not be "
  1128. "rebuilt if the URL changes. The OLD behavior preserves the "
  1129. "timestamps from the archive instead, but this is usually not "
  1130. "what you want. Update your project to the NEW behavior or "
  1131. "specify the DOWNLOAD_EXTRACT_TIMESTAMP option with a value of "
  1132. "true to avoid this robustness issue."
  1133. )
  1134. set(extract_timestamp TRUE)
  1135. elseif(_EP_CMP0135 STREQUAL "NEW")
  1136. set(extract_timestamp FALSE)
  1137. else()
  1138. set(extract_timestamp TRUE)
  1139. endif()
  1140. endif()
  1141. if(extract_timestamp)
  1142. set(options "")
  1143. else()
  1144. set(options "--touch")
  1145. endif()
  1146. set(extract_script "${stamp_dir}/extract-${name}.cmake")
  1147. _ep_write_extractfile_script(
  1148. "${extract_script}"
  1149. "${name}"
  1150. "${file}"
  1151. "${source_dir}"
  1152. "${options}"
  1153. )
  1154. list(APPEND cmd
  1155. COMMAND ${CMAKE_COMMAND}
  1156. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  1157. -P ${extract_script}
  1158. )
  1159. if(arg_SCRIPT_FILE)
  1160. string(APPEND step_script_contents "include(\"${extract_script}\")\n")
  1161. list(APPEND depends ${extract_script})
  1162. endif()
  1163. endif ()
  1164. endif()
  1165. else()
  1166. set(method source_dir)
  1167. _ep_is_dir_empty("${source_dir}" empty)
  1168. if(${empty})
  1169. message(FATAL_ERROR
  1170. "No download info given for '${name}' and its source directory:\n"
  1171. " ${source_dir}\n"
  1172. "is not an existing non-empty directory. Please specify one of:\n"
  1173. " * SOURCE_DIR with an existing non-empty directory\n"
  1174. " * DOWNLOAD_COMMAND\n"
  1175. " * URL\n"
  1176. " * GIT_REPOSITORY\n"
  1177. " * SVN_REPOSITORY\n"
  1178. " * HG_REPOSITORY\n"
  1179. " * CVS_REPOSITORY and CVS_MODULE"
  1180. )
  1181. endif()
  1182. if(arg_SCRIPT_FILE)
  1183. set(step_script_contents "message(VERBOSE [[Using SOURCE_DIR as is]])")
  1184. endif()
  1185. endif()
  1186. # We use configure_file() to write the repo_info_file so that the file's
  1187. # timestamp is not updated if we don't change the contents
  1188. set(repo_info_file ${stamp_dir}/${name}-${method}info.txt)
  1189. list(APPEND depends ${repo_info_file})
  1190. configure_file(
  1191. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/RepositoryInfo.txt.in"
  1192. "${repo_info_file}"
  1193. @ONLY
  1194. )
  1195. if(arg_SCRIPT_FILE)
  1196. set(step_name download)
  1197. configure_file(
  1198. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/stepscript.cmake.in"
  1199. "${arg_SCRIPT_FILE}"
  1200. @ONLY
  1201. )
  1202. set(${arg_DEPENDS_VARIABLE} "${depends}" PARENT_SCOPE)
  1203. return()
  1204. endif()
  1205. # Nothing below this point is applicable when we've been asked to put the
  1206. # download step in a script file (which is the FetchContent case).
  1207. if(_EP_LOG_DOWNLOAD)
  1208. set(log LOG 1)
  1209. else()
  1210. set(log "")
  1211. endif()
  1212. if(_EP_USES_TERMINAL_DOWNLOAD)
  1213. set(uses_terminal USES_TERMINAL 1)
  1214. else()
  1215. set(uses_terminal "")
  1216. endif()
  1217. set(__cmdQuoted)
  1218. foreach(__item IN LISTS cmd)
  1219. string(APPEND __cmdQuoted " [==[${__item}]==]")
  1220. endforeach()
  1221. cmake_language(EVAL CODE "
  1222. ExternalProject_Add_Step(\${name} download
  1223. INDEPENDENT TRUE
  1224. COMMENT \${comment}
  1225. COMMAND ${__cmdQuoted}
  1226. WORKING_DIRECTORY \${work_dir}
  1227. DEPENDS \${depends}
  1228. DEPENDEES mkdir
  1229. ${log}
  1230. ${uses_terminal}
  1231. )"
  1232. )
  1233. endfunction()
  1234. function(_ep_get_update_disconnected var name)
  1235. # Note that the arguments are assumed to have already been parsed and have
  1236. # been translated into variables with the prefix _EP_... by a call to
  1237. # ep_parse_arguments() or ep_parse_arguments_to_vars().
  1238. if(DEFINED _EP_UPDATE_DISCONNECTED)
  1239. set(update_disconnected "${_EP_UPDATE_DISCONNECTED}")
  1240. else()
  1241. get_property(update_disconnected
  1242. DIRECTORY
  1243. PROPERTY EP_UPDATE_DISCONNECTED
  1244. )
  1245. endif()
  1246. set(${var} "${update_disconnected}" PARENT_SCOPE)
  1247. endfunction()
  1248. function(_ep_add_update_command name)
  1249. set(noValueOptions )
  1250. set(singleValueOptions
  1251. SCRIPT_FILE # These should only be used by FetchContent
  1252. DEPEND_VARIABLE #
  1253. )
  1254. set(multiValueOptions )
  1255. cmake_parse_arguments(PARSE_ARGV 1 arg
  1256. "${noValueOptions}" "${singleValueOptions}" "${multiValueOptions}"
  1257. )
  1258. # The various _EP_... variables mentioned here and throughout this function
  1259. # are expected to already have been set by the caller via a call to
  1260. # _ep_parse_arguments() or ep_parse_arguments_to_vars(). Other variables
  1261. # with different names are assigned to for historical reasons only to keep
  1262. # the code more readable and minimize change.
  1263. set(source_dir "${_EP_SOURCE_DIR}")
  1264. set(stamp_dir "${_EP_STAMP_DIR}")
  1265. set(tmp_dir "${_EP_TMP_DIR}")
  1266. set(cmd "${_EP_UPDATE_COMMAND}")
  1267. set(cvs_repository "${_EP_CVS_REPOSITORY}")
  1268. set(svn_repository "${_EP_SVN_REPOSITORY}")
  1269. set(git_repository "${_EP_GIT_REPOSITORY}")
  1270. set(hg_repository "${_EP_HG_REPOSITORY}")
  1271. _ep_get_update_disconnected(update_disconnected ${name})
  1272. set(work_dir)
  1273. set(cmd_disconnected)
  1274. set(comment)
  1275. set(comment_disconnected)
  1276. set(always)
  1277. set(file_deps)
  1278. if(DEFINED _EP_UPDATE_COMMAND)
  1279. set(work_dir ${source_dir})
  1280. if(NOT "x${cmd}" STREQUAL "x")
  1281. set(always 1)
  1282. _ep_add_script_commands(
  1283. step_script_contents
  1284. "${work_dir}"
  1285. "${cmd}" # Must be a single quoted argument
  1286. )
  1287. endif()
  1288. elseif(cvs_repository)
  1289. if(NOT CVS_EXECUTABLE)
  1290. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  1291. endif()
  1292. set(work_dir ${source_dir})
  1293. set(comment "Performing update step (CVS update) for '${name}'")
  1294. set(cvs_tag "${_EP_CVS_TAG}")
  1295. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  1296. set(always 1)
  1297. if(arg_SCRIPT_FILE)
  1298. _ep_add_script_commands(
  1299. step_script_contents
  1300. "${work_dir}"
  1301. "${cmd}" # Must be a single quoted argument
  1302. )
  1303. endif()
  1304. elseif(svn_repository)
  1305. if(NOT Subversion_SVN_EXECUTABLE)
  1306. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  1307. endif()
  1308. set(work_dir ${source_dir})
  1309. set(comment "Performing update step (SVN update) for '${name}'")
  1310. set(svn_trust_cert "${_EP_SVN_TRUST_CERT}")
  1311. set(uses_terminal "${_EP_USES_TERMINAL_UPDATE}")
  1312. set(cmd
  1313. ${Subversion_SVN_EXECUTABLE}
  1314. up
  1315. ${_EP_SVN_REVISION}
  1316. )
  1317. # The --trust-server-cert option requires --non-interactive
  1318. if(svn_trust_cert OR NOT uses_terminal)
  1319. list(APPEND cmd "--non-interactive")
  1320. endif()
  1321. if(svn_trust_cert)
  1322. list(APPEND cmd --trust-server-cert)
  1323. endif()
  1324. if(DEFINED _EP_SVN_USERNAME)
  1325. list(APPEND cmd "--username=${_EP_SVN_USERNAME}")
  1326. endif()
  1327. if(DEFINED _EP_SVN_PASSWORD)
  1328. list(APPEND cmd "--password=${_EP_SVN_PASSWORD}")
  1329. endif()
  1330. set(always 1)
  1331. if(arg_SCRIPT_FILE)
  1332. _ep_add_script_commands(
  1333. step_script_contents
  1334. "${work_dir}"
  1335. "${cmd}" # Must be a single quoted argument
  1336. )
  1337. endif()
  1338. elseif(git_repository)
  1339. # FetchContent gives us these directly, so don't try to recompute them
  1340. if(NOT GIT_EXECUTABLE OR NOT Git_VERSION)
  1341. unset(CMAKE_MODULE_PATH) # Use CMake builtin find module
  1342. find_package(Git QUIET)
  1343. if(NOT GIT_EXECUTABLE)
  1344. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  1345. endif()
  1346. endif()
  1347. set(work_dir ${source_dir})
  1348. set(comment "Performing update step for '${name}'")
  1349. set(comment_disconnected "Performing disconnected update step for '${name}'")
  1350. if(update_disconnected)
  1351. set(can_fetch_default NO)
  1352. else()
  1353. set(can_fetch_default YES)
  1354. endif()
  1355. set(git_tag "${_EP_GIT_TAG}")
  1356. if(NOT git_tag)
  1357. set(git_tag "master")
  1358. endif()
  1359. set(git_remote_name "${_EP_GIT_REMOTE_NAME}")
  1360. if(NOT git_remote_name)
  1361. set(git_remote_name "origin")
  1362. endif()
  1363. set(git_init_submodules TRUE)
  1364. if(DEFINED _EP_GIT_SUBMODULES)
  1365. set(git_submodules "${_EP_GIT_SUBMODULES}")
  1366. if(git_submodules STREQUAL "" AND _EP_CMP0097 STREQUAL "NEW")
  1367. set(git_init_submodules FALSE)
  1368. endif()
  1369. endif()
  1370. set(git_update_strategy "${_EP_GIT_REMOTE_UPDATE_STRATEGY}")
  1371. if(NOT git_update_strategy)
  1372. set(git_update_strategy "${CMAKE_EP_GIT_REMOTE_UPDATE_STRATEGY}")
  1373. endif()
  1374. if(NOT git_update_strategy)
  1375. set(git_update_strategy REBASE)
  1376. endif()
  1377. set(strategies CHECKOUT REBASE REBASE_CHECKOUT)
  1378. if(NOT git_update_strategy IN_LIST strategies)
  1379. message(FATAL_ERROR
  1380. "'${git_update_strategy}' is not one of the supported strategies: "
  1381. "${strategies}"
  1382. )
  1383. endif()
  1384. _ep_get_git_submodules_recurse(git_submodules_recurse)
  1385. _ep_get_tls_version(${name} tls_version)
  1386. _ep_get_tls_verify(${name} tls_verify)
  1387. set(update_script "${tmp_dir}/${name}-gitupdate.cmake")
  1388. list(APPEND file_deps ${update_script})
  1389. _ep_write_gitupdate_script(
  1390. "${update_script}"
  1391. "${GIT_EXECUTABLE}"
  1392. "${git_tag}"
  1393. "${git_remote_name}"
  1394. "${git_init_submodules}"
  1395. "${git_submodules_recurse}"
  1396. "${git_submodules}"
  1397. "${git_repository}"
  1398. "${work_dir}"
  1399. "${git_update_strategy}"
  1400. "${tls_version}"
  1401. "${tls_verify}"
  1402. )
  1403. set(cmd ${CMAKE_COMMAND}
  1404. -Dcan_fetch=YES
  1405. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  1406. -P ${update_script}
  1407. )
  1408. set(cmd_disconnected ${CMAKE_COMMAND}
  1409. -Dcan_fetch=NO
  1410. -DCMAKE_MESSAGE_LOG_LEVEL=VERBOSE
  1411. -P ${update_script}
  1412. )
  1413. set(always 1)
  1414. if(arg_SCRIPT_FILE)
  1415. set(step_script_contents "include(\"${update_script}\")")
  1416. endif()
  1417. elseif(hg_repository)
  1418. if(NOT HG_EXECUTABLE)
  1419. message(FATAL_ERROR "error: could not find hg for pull of ${name}")
  1420. endif()
  1421. set(work_dir ${source_dir})
  1422. set(comment "Performing update step (hg pull) for '${name}'")
  1423. set(comment_disconnected "Performing disconnected update step for '${name}'")
  1424. set(hg_tag "${_EP_HG_TAG}")
  1425. if(NOT hg_tag)
  1426. set(hg_tag "tip")
  1427. endif()
  1428. if("${Hg_VERSION}" STREQUAL "2.1")
  1429. set(notesAnchor
  1430. "#A2.1.1:_revert_pull_return_code_change.2C_compile_issue_on_OS_X"
  1431. )
  1432. message(WARNING
  1433. "Mercurial 2.1 does not distinguish an empty pull from a failed pull:
  1434. http://mercurial.selenic.com/wiki/UpgradeNotes${notesAnchor}
  1435. http://thread.gmane.org/gmane.comp.version-control.mercurial.devel/47656
  1436. Update to Mercurial >= 2.1.1.
  1437. ")
  1438. endif()
  1439. set(cmd
  1440. ${HG_EXECUTABLE} pull
  1441. COMMAND ${HG_EXECUTABLE} update ${hg_tag}
  1442. )
  1443. set(cmd_disconnected ${HG_EXECUTABLE} update ${hg_tag})
  1444. set(always 1)
  1445. if(arg_SCRIPT_FILE)
  1446. # These commands are simple, and we know whether updates need to be
  1447. # disconnected or not for this case, so write them directly instead of
  1448. # forming them from "cmd" and "cmd_disconnected".
  1449. if(NOT update_disconnected)
  1450. string(APPEND step_script_contents
  1451. "execute_process(\n"
  1452. " WORKING_DIRECTORY \"${work_dir}\"\n"
  1453. " COMMAND_ERROR_IS_FATAL LAST\n"
  1454. " COMMAND \"${HG_EXECUTABLE}\" pull\n"
  1455. ")"
  1456. )
  1457. endif()
  1458. string(APPEND step_script_contents
  1459. "execute_process(\n"
  1460. " WORKING_DIRECTORY \"${work_dir}\"\n"
  1461. " COMMAND_ERROR_IS_FATAL LAST\n"
  1462. " COMMAND \"${HG_EXECUTABLE}\" update \"${hg_tag}\"\n"
  1463. ")"
  1464. )
  1465. endif()
  1466. endif()
  1467. # We use configure_file() to write the update_info_file so that the file's
  1468. # timestamp is not updated if we don't change the contents
  1469. if(NOT DEFINED cmd_disconnected)
  1470. set(cmd_disconnected "${cmd}")
  1471. endif()
  1472. set(update_info_file ${stamp_dir}/${name}-update-info.txt)
  1473. list(APPEND file_deps ${update_info_file})
  1474. configure_file(
  1475. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/UpdateInfo.txt.in"
  1476. "${update_info_file}"
  1477. @ONLY
  1478. )
  1479. if(arg_SCRIPT_FILE)
  1480. set(step_name update)
  1481. configure_file(
  1482. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/stepscript.cmake.in"
  1483. "${arg_SCRIPT_FILE}"
  1484. @ONLY
  1485. )
  1486. set(${arg_DEPENDS_VARIABLE} "${file_deps}" PARENT_SCOPE)
  1487. return()
  1488. endif()
  1489. # Nothing below this point is applicable when we've been asked to put the
  1490. # update step in a script file (which is the FetchContent case).
  1491. if(_EP_LOG_UPDATE)
  1492. set(log LOG 1)
  1493. else()
  1494. set(log "")
  1495. endif()
  1496. if(_EP_USES_TERMINAL_UPDATE)
  1497. set(uses_terminal USES_TERMINAL 1)
  1498. else()
  1499. set(uses_terminal "")
  1500. endif()
  1501. set(__cmdQuoted)
  1502. foreach(__item IN LISTS cmd)
  1503. string(APPEND __cmdQuoted " [==[${__item}]==]")
  1504. endforeach()
  1505. cmake_language(EVAL CODE "
  1506. ExternalProject_Add_Step(${name} update
  1507. INDEPENDENT TRUE
  1508. COMMENT \${comment}
  1509. COMMAND ${__cmdQuoted}
  1510. ALWAYS \${always}
  1511. EXCLUDE_FROM_MAIN \${update_disconnected}
  1512. WORKING_DIRECTORY \${work_dir}
  1513. DEPENDEES download
  1514. DEPENDS \${file_deps}
  1515. ${log}
  1516. ${uses_terminal}
  1517. )"
  1518. )
  1519. if(update_disconnected)
  1520. if(NOT DEFINED comment_disconnected)
  1521. set(comment_disconnected "${comment}")
  1522. endif()
  1523. set(__cmdQuoted)
  1524. foreach(__item IN LISTS cmd_disconnected)
  1525. string(APPEND __cmdQuoted " [==[${__item}]==]")
  1526. endforeach()
  1527. cmake_language(EVAL CODE "
  1528. ExternalProject_Add_Step(${name} update_disconnected
  1529. INDEPENDENT TRUE
  1530. COMMENT \${comment_disconnected}
  1531. COMMAND ${__cmdQuoted}
  1532. WORKING_DIRECTORY \${work_dir}
  1533. DEPENDEES download
  1534. DEPENDS \${file_deps}
  1535. ${log}
  1536. ${uses_terminal}
  1537. )"
  1538. )
  1539. endif()
  1540. endfunction()
  1541. function(_ep_add_patch_command name)
  1542. set(noValueOptions )
  1543. set(singleValueOptions
  1544. SCRIPT_FILE # These should only be used by FetchContent
  1545. )
  1546. set(multiValueOptions )
  1547. cmake_parse_arguments(PARSE_ARGV 1 arg
  1548. "${noValueOptions}" "${singleValueOptions}" "${multiValueOptions}"
  1549. )
  1550. # The various _EP_... variables mentioned here and throughout this function
  1551. # are expected to already have been set by the caller via a call to
  1552. # _ep_parse_arguments() or ep_parse_arguments_to_vars(). Other variables
  1553. # with different names are assigned to for historical reasons only to keep
  1554. # the code more readable and minimize change.
  1555. set(source_dir "${_EP_SOURCE_DIR}")
  1556. set(stamp_dir "${_EP_STAMP_DIR}")
  1557. set(cmd "${_EP_PATCH_COMMAND}")
  1558. set(step_script_contents "")
  1559. set(work_dir)
  1560. if(DEFINED _EP_PATCH_COMMAND)
  1561. set(work_dir ${source_dir})
  1562. if(arg_SCRIPT_FILE)
  1563. _ep_add_script_commands(
  1564. step_script_contents
  1565. "${work_dir}"
  1566. "${cmd}" # Must be a single quoted argument
  1567. )
  1568. endif()
  1569. endif()
  1570. # We use configure_file() to write the patch_info_file so that the file's
  1571. # timestamp is not updated if we don't change the contents
  1572. set(patch_info_file ${stamp_dir}/${name}-patch-info.txt)
  1573. configure_file(
  1574. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PatchInfo.txt.in"
  1575. "${patch_info_file}"
  1576. @ONLY
  1577. )
  1578. if(arg_SCRIPT_FILE)
  1579. set(step_name patch)
  1580. configure_file(
  1581. "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/stepscript.cmake.in"
  1582. "${arg_SCRIPT_FILE}"
  1583. @ONLY
  1584. )
  1585. return()
  1586. endif()
  1587. # Nothing below this point is applicable when we've been asked to put the
  1588. # patch step in a script file (which is the FetchContent case).
  1589. if(_EP_LOG_PATCH)
  1590. set(log LOG 1)
  1591. else()
  1592. set(log "")
  1593. endif()
  1594. if(_EP_USES_TERMINAL_PATCH)
  1595. set(uses_terminal USES_TERMINAL 1)
  1596. else()
  1597. set(uses_terminal "")
  1598. endif()
  1599. _ep_get_update_disconnected(update_disconnected ${name})
  1600. set(__cmdQuoted)
  1601. foreach(__item IN LISTS cmd)
  1602. string(APPEND __cmdQuoted " [==[${__item}]==]")
  1603. endforeach()
  1604. cmake_language(EVAL CODE "
  1605. ExternalProject_Add_Step(${name} patch
  1606. INDEPENDENT TRUE
  1607. COMMAND ${__cmdQuoted}
  1608. WORKING_DIRECTORY \${work_dir}
  1609. EXCLUDE_FROM_MAIN \${update_disconnected}
  1610. DEPENDEES update
  1611. DEPENDS \${patch_info_file}
  1612. ${log}
  1613. ${uses_terminal}
  1614. )"
  1615. )
  1616. if(update_disconnected)
  1617. cmake_language(EVAL CODE "
  1618. ExternalProject_Add_Step(${name} patch_disconnected
  1619. INDEPENDENT TRUE
  1620. COMMAND ${__cmdQuoted}
  1621. WORKING_DIRECTORY \${work_dir}
  1622. DEPENDEES update_disconnected
  1623. DEPENDS \${patch_info_file}
  1624. ${log}
  1625. ${uses_terminal}
  1626. )"
  1627. )
  1628. endif()
  1629. endfunction()
  1630. macro(_ep_get_add_keywords out_var)
  1631. set(${out_var}
  1632. #
  1633. # Directory options
  1634. #
  1635. PREFIX
  1636. TMP_DIR
  1637. STAMP_DIR
  1638. LOG_DIR
  1639. DOWNLOAD_DIR
  1640. SOURCE_DIR
  1641. BINARY_DIR
  1642. INSTALL_DIR
  1643. #
  1644. # Download step options
  1645. #
  1646. DOWNLOAD_COMMAND
  1647. #
  1648. URL
  1649. URL_HASH
  1650. URL_MD5
  1651. DOWNLOAD_NAME
  1652. DOWNLOAD_EXTRACT_TIMESTAMP
  1653. DOWNLOAD_NO_EXTRACT
  1654. DOWNLOAD_NO_PROGRESS
  1655. TIMEOUT
  1656. INACTIVITY_TIMEOUT
  1657. HTTP_USERNAME
  1658. HTTP_PASSWORD
  1659. HTTP_HEADER
  1660. TLS_VERSION # Also used for git clone operations
  1661. TLS_VERIFY # Also used for git clone operations
  1662. TLS_CAINFO
  1663. NETRC
  1664. NETRC_FILE
  1665. #
  1666. GIT_REPOSITORY
  1667. GIT_TAG
  1668. GIT_REMOTE_NAME
  1669. GIT_SUBMODULES
  1670. GIT_SUBMODULES_RECURSE
  1671. GIT_SHALLOW
  1672. GIT_PROGRESS
  1673. GIT_CONFIG
  1674. GIT_REMOTE_UPDATE_STRATEGY
  1675. #
  1676. SVN_REPOSITORY
  1677. SVN_REVISION
  1678. SVN_USERNAME
  1679. SVN_PASSWORD
  1680. SVN_TRUST_CERT
  1681. #
  1682. HG_REPOSITORY
  1683. HG_TAG
  1684. #
  1685. CVS_REPOSITORY
  1686. CVS_MODULE
  1687. CVS_TAG
  1688. #
  1689. # Update step options
  1690. #
  1691. UPDATE_COMMAND
  1692. UPDATE_DISCONNECTED
  1693. #
  1694. # Patch step options
  1695. #
  1696. PATCH_COMMAND
  1697. #
  1698. # Configure step options
  1699. #
  1700. CONFIGURE_COMMAND
  1701. CONFIGURE_ENVIRONMENT_MODIFICATION
  1702. CMAKE_COMMAND
  1703. CMAKE_GENERATOR
  1704. CMAKE_GENERATOR_PLATFORM
  1705. CMAKE_GENERATOR_TOOLSET
  1706. CMAKE_GENERATOR_INSTANCE
  1707. CMAKE_ARGS
  1708. CMAKE_CACHE_ARGS
  1709. CMAKE_CACHE_DEFAULT_ARGS
  1710. SOURCE_SUBDIR
  1711. CONFIGURE_HANDLED_BY_BUILD
  1712. #
  1713. # Build step options
  1714. #
  1715. BUILD_COMMAND
  1716. BUILD_ENVIRONMENT_MODIFICATION
  1717. BUILD_IN_SOURCE
  1718. BUILD_ALWAYS
  1719. BUILD_BYPRODUCTS
  1720. BUILD_JOB_SERVER_AWARE
  1721. #
  1722. # Install step options
  1723. #
  1724. INSTALL_COMMAND
  1725. INSTALL_ENVIRONMENT_MODIFICATION
  1726. INSTALL_BYPRODUCTS
  1727. INSTALL_JOB_SERVER_AWARE
  1728. #
  1729. # Test step options
  1730. #
  1731. TEST_COMMAND
  1732. TEST_ENVIRONMENT_MODIFICATION
  1733. TEST_BEFORE_INSTALL
  1734. TEST_AFTER_INSTALL
  1735. TEST_EXCLUDE_FROM_MAIN
  1736. #
  1737. # Logging options
  1738. #
  1739. LOG_DOWNLOAD
  1740. LOG_UPDATE
  1741. LOG_PATCH
  1742. LOG_CONFIGURE
  1743. LOG_BUILD
  1744. LOG_INSTALL
  1745. LOG_TEST
  1746. LOG_MERGED_STDOUTERR
  1747. LOG_OUTPUT_ON_FAILURE
  1748. #
  1749. # Terminal access options
  1750. #
  1751. USES_TERMINAL_DOWNLOAD
  1752. USES_TERMINAL_UPDATE
  1753. USES_TERMINAL_PATCH
  1754. USES_TERMINAL_CONFIGURE
  1755. USES_TERMINAL_BUILD
  1756. USES_TERMINAL_INSTALL
  1757. USES_TERMINAL_TEST
  1758. #
  1759. # Target options
  1760. #
  1761. DEPENDS
  1762. EXCLUDE_FROM_ALL
  1763. STEP_TARGETS
  1764. INDEPENDENT_STEP_TARGETS
  1765. #
  1766. # Miscellaneous options
  1767. #
  1768. LIST_SEPARATOR
  1769. #
  1770. # Internal options (undocumented)
  1771. #
  1772. EXTERNALPROJECT_INTERNAL_ARGUMENT_SEPARATOR
  1773. )
  1774. endmacro()