ExternalProject.cmake 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. #.rst:
  2. # ExternalProject
  3. # ---------------
  4. #
  5. # Create custom targets to build projects in external trees
  6. #
  7. # The ``ExternalProject_Add`` function creates a custom target to drive
  8. # download, update/patch, configure, build, install and test steps of an
  9. # external project:
  10. #
  11. # .. code-block:: cmake
  12. #
  13. # ExternalProject_Add(<name> # Name for custom target
  14. # [DEPENDS projects...] # Targets on which the project depends
  15. # [PREFIX dir] # Root dir for entire project
  16. # [LIST_SEPARATOR sep] # Sep to be replaced by ; in cmd lines
  17. # [TMP_DIR dir] # Directory to store temporary files
  18. # [STAMP_DIR dir] # Directory to store step timestamps
  19. # #--Download step--------------
  20. # [DOWNLOAD_NAME fname] # File name to store (if not end of URL)
  21. # [DOWNLOAD_DIR dir] # Directory to store downloaded files
  22. # [DOWNLOAD_COMMAND cmd...] # Command to download source tree
  23. # [CVS_REPOSITORY cvsroot] # CVSROOT of CVS repository
  24. # [CVS_MODULE mod] # Module to checkout from CVS repo
  25. # [CVS_TAG tag] # Tag to checkout from CVS repo
  26. # [SVN_REPOSITORY url] # URL of Subversion repo
  27. # [SVN_REVISION rev] # Revision to checkout from Subversion repo
  28. # [SVN_USERNAME john ] # Username for Subversion checkout and update
  29. # [SVN_PASSWORD doe ] # Password for Subversion checkout and update
  30. # [SVN_TRUST_CERT 1 ] # Trust the Subversion server site certificate
  31. # [GIT_REPOSITORY url] # URL of git repo
  32. # [GIT_TAG tag] # Git branch name, commit id or tag
  33. # [GIT_SUBMODULES modules...] # Git submodules that shall be updated, all if empty
  34. # [HG_REPOSITORY url] # URL of mercurial repo
  35. # [HG_TAG tag] # Mercurial branch name, commit id or tag
  36. # [URL /.../src.tgz] # Full path or URL of source
  37. # [URL_HASH ALGO=value] # Hash of file at URL
  38. # [URL_MD5 md5] # Equivalent to URL_HASH MD5=md5
  39. # [TLS_VERIFY bool] # Should certificate for https be checked
  40. # [TLS_CAINFO file] # Path to a certificate authority file
  41. # [TIMEOUT seconds] # Time allowed for file download operations
  42. # #--Update/Patch step----------
  43. # [UPDATE_COMMAND cmd...] # Source work-tree update command
  44. # [PATCH_COMMAND cmd...] # Command to patch downloaded source
  45. # #--Configure step-------------
  46. # [SOURCE_DIR dir] # Source dir to be used for build
  47. # [CONFIGURE_COMMAND cmd...] # Build tree configuration command
  48. # [CMAKE_COMMAND /.../cmake] # Specify alternative cmake executable
  49. # [CMAKE_GENERATOR gen] # Specify generator for native build
  50. # [CMAKE_GENERATOR_TOOLSET t] # Generator-specific toolset name
  51. # [CMAKE_ARGS args...] # Arguments to CMake command line
  52. # [CMAKE_CACHE_ARGS args...] # Initial cache arguments, of the form -Dvar:string=on
  53. # #--Build step-----------------
  54. # [BINARY_DIR dir] # Specify build dir location
  55. # [BUILD_COMMAND cmd...] # Command to drive the native build
  56. # [BUILD_IN_SOURCE 1] # Use source dir for build dir
  57. # [BUILD_ALWAYS 1] # No stamp file, build step always runs
  58. # #--Install step---------------
  59. # [INSTALL_DIR dir] # Installation prefix
  60. # [INSTALL_COMMAND cmd...] # Command to drive install after build
  61. # #--Test step------------------
  62. # [TEST_BEFORE_INSTALL 1] # Add test step executed before install step
  63. # [TEST_AFTER_INSTALL 1] # Add test step executed after install step
  64. # [TEST_COMMAND cmd...] # Command to drive test
  65. # #--Output logging-------------
  66. # [LOG_DOWNLOAD 1] # Wrap download in script to log output
  67. # [LOG_UPDATE 1] # Wrap update in script to log output
  68. # [LOG_CONFIGURE 1] # Wrap configure in script to log output
  69. # [LOG_BUILD 1] # Wrap build in script to log output
  70. # [LOG_TEST 1] # Wrap test in script to log output
  71. # [LOG_INSTALL 1] # Wrap install in script to log output
  72. # #--Custom targets-------------
  73. # [STEP_TARGETS st1 st2 ...] # Generate custom targets for these steps
  74. # )
  75. #
  76. # The ``*_DIR`` options specify directories for the project, with default
  77. # directories computed as follows. If the ``PREFIX`` option is given to
  78. # ``ExternalProject_Add()`` or the ``EP_PREFIX`` directory property is set,
  79. # then an external project is built and installed under the specified prefix::
  80. #
  81. # TMP_DIR = <prefix>/tmp
  82. # STAMP_DIR = <prefix>/src/<name>-stamp
  83. # DOWNLOAD_DIR = <prefix>/src
  84. # SOURCE_DIR = <prefix>/src/<name>
  85. # BINARY_DIR = <prefix>/src/<name>-build
  86. # INSTALL_DIR = <prefix>
  87. #
  88. # Otherwise, if the ``EP_BASE`` directory property is set then components
  89. # of an external project are stored under the specified base::
  90. #
  91. # TMP_DIR = <base>/tmp/<name>
  92. # STAMP_DIR = <base>/Stamp/<name>
  93. # DOWNLOAD_DIR = <base>/Download/<name>
  94. # SOURCE_DIR = <base>/Source/<name>
  95. # BINARY_DIR = <base>/Build/<name>
  96. # INSTALL_DIR = <base>/Install/<name>
  97. #
  98. # If no ``PREFIX``, ``EP_PREFIX``, or ``EP_BASE`` is specified then the
  99. # default is to set ``PREFIX`` to ``<name>-prefix``. Relative paths are
  100. # interpreted with respect to the build directory corresponding to the
  101. # source directory in which ``ExternalProject_Add`` is invoked.
  102. #
  103. # If ``SOURCE_DIR`` is explicitly set to an existing directory the project
  104. # will be built from it. Otherwise a download step must be specified
  105. # using one of the ``DOWNLOAD_COMMAND``, ``CVS_*``, ``SVN_*``, or ``URL``
  106. # options. The ``URL`` option may refer locally to a directory or source
  107. # tarball, or refer to a remote tarball (e.g. ``http://.../src.tgz``).
  108. #
  109. # The ``ExternalProject_Add_Step`` function adds a custom step to an
  110. # external project:
  111. #
  112. # .. code-block:: cmake
  113. #
  114. # ExternalProject_Add_Step(<name> <step> # Names of project and custom step
  115. # [COMMAND cmd...] # Command line invoked by this step
  116. # [COMMENT "text..."] # Text printed when step executes
  117. # [DEPENDEES steps...] # Steps on which this step depends
  118. # [DEPENDERS steps...] # Steps that depend on this step
  119. # [DEPENDS files...] # Files on which this step depends
  120. # [ALWAYS 1] # No stamp file, step always runs
  121. # [WORKING_DIRECTORY dir] # Working directory for command
  122. # [LOG 1] # Wrap step in script to log output
  123. # )
  124. #
  125. # The command line, comment, and working directory of every standard and
  126. # custom step is processed to replace tokens ``<SOURCE_DIR>``,
  127. # ``<BINARY_DIR>``, ``<INSTALL_DIR>``, and ``<TMP_DIR>`` with
  128. # corresponding property values.
  129. #
  130. # Any builtin step that specifies a ``<step>_COMMAND cmd...`` or custom
  131. # step that specifies a ``COMMAND cmd...`` may specify additional command
  132. # lines using the form ``COMMAND cmd...``. At build time the commands
  133. # will be executed in order and aborted if any one fails. For example::
  134. #
  135. # ... BUILD_COMMAND make COMMAND echo done ...
  136. #
  137. # specifies to run ``make`` and then ``echo done`` during the build step.
  138. # Whether the current working directory is preserved between commands is
  139. # not defined. Behavior of shell operators like ``&&`` is not defined.
  140. #
  141. # The ``ExternalProject_Get_Property`` function retrieves external project
  142. # target properties::
  143. #
  144. # ExternalProject_Get_Property(<name> [prop1 [prop2 [...]]])
  145. #
  146. # It stores property values in variables of the same name. Property
  147. # names correspond to the keyword argument names of
  148. # ``ExternalProject_Add``.
  149. #
  150. # The ``ExternalProject_Add_StepTargets`` function generates custom
  151. # targets for the steps listed::
  152. #
  153. # ExternalProject_Add_StepTargets(<name> [step1 [step2 [...]]])
  154. #
  155. # If ``STEP_TARGETS`` is set then ``ExternalProject_Add_StepTargets`` is
  156. # automatically called at the end of matching calls to
  157. # ``ExternalProject_Add_Step``. Pass ``STEP_TARGETS`` explicitly to
  158. # individual ``ExternalProject_Add`` calls, or implicitly to all
  159. # ``ExternalProject_Add`` calls by setting the directory property
  160. # ``EP_STEP_TARGETS``.
  161. #
  162. # If ``STEP_TARGETS`` is not set, clients may still manually call
  163. # ``ExternalProject_Add_StepTargets`` after calling
  164. # ``ExternalProject_Add`` or ``ExternalProject_Add_Step``.
  165. #
  166. # This functionality is provided to make it easy to drive the steps
  167. # independently of each other by specifying targets on build command
  168. # lines. For example, you may be submitting to a sub-project based
  169. # dashboard, where you want to drive the configure portion of the build,
  170. # then submit to the dashboard, followed by the build portion, followed
  171. # by tests. If you invoke a custom target that depends on a step
  172. # halfway through the step dependency chain, then all the previous steps
  173. # will also run to ensure everything is up to date.
  174. #
  175. # For example, to drive configure, build and test steps independently
  176. # for each ``ExternalProject_Add`` call in your project, write the following
  177. # line prior to any ``ExternalProject_Add`` calls in your ``CMakeLists.txt``
  178. # file::
  179. #
  180. # set_property(DIRECTORY PROPERTY EP_STEP_TARGETS configure build test)
  181. #=============================================================================
  182. # Copyright 2008-2013 Kitware, Inc.
  183. #
  184. # Distributed under the OSI-approved BSD License (the "License");
  185. # see accompanying file Copyright.txt for details.
  186. #
  187. # This software is distributed WITHOUT ANY WARRANTY; without even the
  188. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  189. # See the License for more information.
  190. #=============================================================================
  191. # (To distribute this file outside of CMake, substitute the full
  192. # License text for the above reference.)
  193. # Pre-compute a regex to match documented keywords for each command.
  194. math(EXPR _ep_documentation_line_count "${CMAKE_CURRENT_LIST_LINE} - 16")
  195. file(STRINGS "${CMAKE_CURRENT_LIST_FILE}" lines
  196. LIMIT_COUNT ${_ep_documentation_line_count}
  197. REGEX "^# ( \\[[A-Z0-9_]+ [^]]*\\] +#.*$|[A-Za-z0-9_]+\\()")
  198. foreach(line IN LISTS lines)
  199. if("${line}" MATCHES "^# [A-Za-z0-9_]+\\(")
  200. if(_ep_func)
  201. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  202. endif()
  203. string(REGEX REPLACE "^# ([A-Za-z0-9_]+)\\(.*" "\\1" _ep_func "${line}")
  204. #message("function [${_ep_func}]")
  205. set(_ep_keywords_${_ep_func} "^(")
  206. set(_ep_keyword_sep)
  207. else()
  208. string(REGEX REPLACE "^# \\[([A-Z0-9_]+) .*" "\\1" _ep_key "${line}")
  209. #message(" keyword [${_ep_key}]")
  210. set(_ep_keywords_${_ep_func}
  211. "${_ep_keywords_${_ep_func}}${_ep_keyword_sep}${_ep_key}")
  212. set(_ep_keyword_sep "|")
  213. endif()
  214. endforeach()
  215. if(_ep_func)
  216. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  217. endif()
  218. # Save regex matching supported hash algorithm names.
  219. set(_ep_hash_algos "MD5|SHA1|SHA224|SHA256|SHA384|SHA512")
  220. set(_ep_hash_regex "^(${_ep_hash_algos})=([0-9A-Fa-f]+)$")
  221. function(_ep_parse_arguments f name ns args)
  222. # Transfer the arguments to this function into target properties for the
  223. # new custom target we just added so that we can set up all the build steps
  224. # correctly based on target properties.
  225. #
  226. # We loop through ARGN and consider the namespace starting with an
  227. # upper-case letter followed by at least two more upper-case letters,
  228. # numbers or underscores to be keywords.
  229. set(key)
  230. foreach(arg IN LISTS args)
  231. set(is_value 1)
  232. if(arg MATCHES "^[A-Z][A-Z0-9_][A-Z0-9_]+$" AND
  233. NOT ((arg STREQUAL "${key}") AND (key STREQUAL "COMMAND")) AND
  234. NOT arg MATCHES "^(TRUE|FALSE)$")
  235. if(_ep_keywords_${f} AND arg MATCHES "${_ep_keywords_${f}}")
  236. set(is_value 0)
  237. endif()
  238. endif()
  239. if(is_value)
  240. if(key)
  241. # Value
  242. if(NOT arg STREQUAL "")
  243. set_property(TARGET ${name} APPEND PROPERTY ${ns}${key} "${arg}")
  244. else()
  245. get_property(have_key TARGET ${name} PROPERTY ${ns}${key} SET)
  246. if(have_key)
  247. get_property(value TARGET ${name} PROPERTY ${ns}${key})
  248. set_property(TARGET ${name} PROPERTY ${ns}${key} "${value};${arg}")
  249. else()
  250. set_property(TARGET ${name} PROPERTY ${ns}${key} "${arg}")
  251. endif()
  252. endif()
  253. else()
  254. # Missing Keyword
  255. message(AUTHOR_WARNING "value '${arg}' with no previous keyword in ${f}")
  256. endif()
  257. else()
  258. set(key "${arg}")
  259. endif()
  260. endforeach()
  261. endfunction()
  262. define_property(DIRECTORY PROPERTY "EP_BASE" INHERITED
  263. BRIEF_DOCS "Base directory for External Project storage."
  264. FULL_DOCS
  265. "See documentation of the ExternalProject_Add() function in the "
  266. "ExternalProject module."
  267. )
  268. define_property(DIRECTORY PROPERTY "EP_PREFIX" INHERITED
  269. BRIEF_DOCS "Top prefix for External Project storage."
  270. FULL_DOCS
  271. "See documentation of the ExternalProject_Add() function in the "
  272. "ExternalProject module."
  273. )
  274. define_property(DIRECTORY PROPERTY "EP_STEP_TARGETS" INHERITED
  275. BRIEF_DOCS
  276. "List of ExternalProject steps that automatically get corresponding targets"
  277. FULL_DOCS
  278. "See documentation of the ExternalProject_Add_StepTargets() function in the "
  279. "ExternalProject module."
  280. )
  281. function(_ep_write_gitclone_script script_filename source_dir git_EXECUTABLE git_repository git_tag git_submodules src_name work_dir gitclone_infofile gitclone_stampfile)
  282. file(WRITE ${script_filename}
  283. "if(\"${git_tag}\" STREQUAL \"\")
  284. message(FATAL_ERROR \"Tag for git checkout should not be empty.\")
  285. endif()
  286. set(run 0)
  287. if(\"${gitclone_infofile}\" IS_NEWER_THAN \"${gitclone_stampfile}\")
  288. set(run 1)
  289. endif()
  290. if(NOT run)
  291. message(STATUS \"Avoiding repeated git clone, stamp file is up to date: '${gitclone_stampfile}'\")
  292. return()
  293. endif()
  294. execute_process(
  295. COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
  296. RESULT_VARIABLE error_code
  297. )
  298. if(error_code)
  299. message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
  300. endif()
  301. # try the clone 3 times incase there is an odd git clone issue
  302. set(error_code 1)
  303. set(number_of_tries 0)
  304. while(error_code AND number_of_tries LESS 3)
  305. execute_process(
  306. COMMAND \"${git_EXECUTABLE}\" clone \"${git_repository}\" \"${src_name}\"
  307. WORKING_DIRECTORY \"${work_dir}\"
  308. RESULT_VARIABLE error_code
  309. )
  310. math(EXPR number_of_tries \"\${number_of_tries} + 1\")
  311. endwhile()
  312. if(number_of_tries GREATER 1)
  313. message(STATUS \"Had to git clone more than once:
  314. \${number_of_tries} times.\")
  315. endif()
  316. if(error_code)
  317. message(FATAL_ERROR \"Failed to clone repository: '${git_repository}'\")
  318. endif()
  319. execute_process(
  320. COMMAND \"${git_EXECUTABLE}\" checkout ${git_tag}
  321. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  322. RESULT_VARIABLE error_code
  323. )
  324. if(error_code)
  325. message(FATAL_ERROR \"Failed to checkout tag: '${git_tag}'\")
  326. endif()
  327. execute_process(
  328. COMMAND \"${git_EXECUTABLE}\" submodule init
  329. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  330. RESULT_VARIABLE error_code
  331. )
  332. if(error_code)
  333. message(FATAL_ERROR \"Failed to init submodules in: '${work_dir}/${src_name}'\")
  334. endif()
  335. execute_process(
  336. COMMAND \"${git_EXECUTABLE}\" submodule update --recursive ${git_submodules}
  337. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  338. RESULT_VARIABLE error_code
  339. )
  340. if(error_code)
  341. message(FATAL_ERROR \"Failed to update submodules in: '${work_dir}/${src_name}'\")
  342. endif()
  343. # Complete success, update the script-last-run stamp file:
  344. #
  345. execute_process(
  346. COMMAND \${CMAKE_COMMAND} -E copy
  347. \"${gitclone_infofile}\"
  348. \"${gitclone_stampfile}\"
  349. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  350. RESULT_VARIABLE error_code
  351. )
  352. if(error_code)
  353. message(FATAL_ERROR \"Failed to copy script-last-run stamp file: '${gitclone_stampfile}'\")
  354. endif()
  355. "
  356. )
  357. endfunction()
  358. function(_ep_write_hgclone_script script_filename source_dir hg_EXECUTABLE hg_repository hg_tag src_name work_dir hgclone_infofile hgclone_stampfile)
  359. file(WRITE ${script_filename}
  360. "if(\"${hg_tag}\" STREQUAL \"\")
  361. message(FATAL_ERROR \"Tag for hg checkout should not be empty.\")
  362. endif()
  363. set(run 0)
  364. if(\"${hgclone_infofile}\" IS_NEWER_THAN \"${hgclone_stampfile}\")
  365. set(run 1)
  366. endif()
  367. if(NOT run)
  368. message(STATUS \"Avoiding repeated hg clone, stamp file is up to date: '${hgclone_stampfile}'\")
  369. return()
  370. endif()
  371. execute_process(
  372. COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
  373. RESULT_VARIABLE error_code
  374. )
  375. if(error_code)
  376. message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
  377. endif()
  378. execute_process(
  379. COMMAND \"${hg_EXECUTABLE}\" clone \"${hg_repository}\" \"${src_name}\"
  380. WORKING_DIRECTORY \"${work_dir}\"
  381. RESULT_VARIABLE error_code
  382. )
  383. if(error_code)
  384. message(FATAL_ERROR \"Failed to clone repository: '${hg_repository}'\")
  385. endif()
  386. execute_process(
  387. COMMAND \"${hg_EXECUTABLE}\" update ${hg_tag}
  388. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  389. RESULT_VARIABLE error_code
  390. )
  391. if(error_code)
  392. message(FATAL_ERROR \"Failed to checkout tag: '${hg_tag}'\")
  393. endif()
  394. # Complete success, update the script-last-run stamp file:
  395. #
  396. execute_process(
  397. COMMAND \${CMAKE_COMMAND} -E copy
  398. \"${hgclone_infofile}\"
  399. \"${hgclone_stampfile}\"
  400. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  401. RESULT_VARIABLE error_code
  402. )
  403. if(error_code)
  404. message(FATAL_ERROR \"Failed to copy script-last-run stamp file: '${hgclone_stampfile}'\")
  405. endif()
  406. "
  407. )
  408. endfunction()
  409. function(_ep_write_gitupdate_script script_filename git_EXECUTABLE git_tag git_submodules git_repository work_dir)
  410. file(WRITE ${script_filename}
  411. "if(\"${git_tag}\" STREQUAL \"\")
  412. message(FATAL_ERROR \"Tag for git checkout should not be empty.\")
  413. endif()
  414. execute_process(
  415. COMMAND \"${git_EXECUTABLE}\" rev-list --max-count=1 HEAD
  416. WORKING_DIRECTORY \"${work_dir}\"
  417. RESULT_VARIABLE error_code
  418. OUTPUT_VARIABLE head_sha
  419. )
  420. if(error_code)
  421. message(FATAL_ERROR \"Failed to get the hash for HEAD\")
  422. endif()
  423. execute_process(
  424. COMMAND \"${git_EXECUTABLE}\" show-ref ${git_tag}
  425. WORKING_DIRECTORY \"${work_dir}\"
  426. OUTPUT_VARIABLE show_ref_output
  427. )
  428. # If a remote ref is asked for, which can possibly move around,
  429. # we must always do a fetch and checkout.
  430. if(\"\${show_ref_output}\" MATCHES \"remotes\")
  431. set(is_remote_ref 1)
  432. else()
  433. set(is_remote_ref 0)
  434. endif()
  435. # This will fail if the tag does not exist (it probably has not been fetched
  436. # yet).
  437. execute_process(
  438. COMMAND \"${git_EXECUTABLE}\" rev-list --max-count=1 ${git_tag}
  439. WORKING_DIRECTORY \"${work_dir}\"
  440. RESULT_VARIABLE error_code
  441. OUTPUT_VARIABLE tag_sha
  442. )
  443. # Is the hash checkout out that we want?
  444. if(error_code OR is_remote_ref OR NOT (\"\${tag_sha}\" STREQUAL \"\${head_sha}\"))
  445. execute_process(
  446. COMMAND \"${git_EXECUTABLE}\" fetch
  447. WORKING_DIRECTORY \"${work_dir}\"
  448. RESULT_VARIABLE error_code
  449. )
  450. if(error_code)
  451. message(FATAL_ERROR \"Failed to fetch repository '${git_repository}'\")
  452. endif()
  453. execute_process(
  454. COMMAND \"${git_EXECUTABLE}\" checkout ${git_tag}
  455. WORKING_DIRECTORY \"${work_dir}\"
  456. RESULT_VARIABLE error_code
  457. )
  458. if(error_code)
  459. message(FATAL_ERROR \"Failed to checkout tag: '${git_tag}'\")
  460. endif()
  461. execute_process(
  462. COMMAND \"${git_EXECUTABLE}\" submodule update --recursive ${git_submodules}
  463. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  464. RESULT_VARIABLE error_code
  465. )
  466. if(error_code)
  467. message(FATAL_ERROR \"Failed to update submodules in: '${work_dir}/${src_name}'\")
  468. endif()
  469. endif()
  470. "
  471. )
  472. endfunction(_ep_write_gitupdate_script)
  473. function(_ep_write_downloadfile_script script_filename remote local timeout hash tls_verify tls_cainfo)
  474. if(timeout)
  475. set(timeout_args TIMEOUT ${timeout})
  476. set(timeout_msg "${timeout} seconds")
  477. else()
  478. set(timeout_args "# no TIMEOUT")
  479. set(timeout_msg "none")
  480. endif()
  481. if("${hash}" MATCHES "${_ep_hash_regex}")
  482. set(hash_args EXPECTED_HASH ${CMAKE_MATCH_1}=${CMAKE_MATCH_2})
  483. else()
  484. set(hash_args "# no EXPECTED_HASH")
  485. endif()
  486. # check for curl globals in the project
  487. if(DEFINED CMAKE_TLS_VERIFY)
  488. set(tls_verify "set(CMAKE_TLS_VERIFY ${CMAKE_TLS_VERIFY})")
  489. endif()
  490. if(DEFINED CMAKE_TLS_CAINFO)
  491. set(tls_cainfo "set(CMAKE_TLS_CAINFO \"${CMAKE_TLS_CAINFO}\")")
  492. endif()
  493. # now check for curl locals so that the local values
  494. # will override the globals
  495. # check for tls_verify argument
  496. string(LENGTH "${tls_verify}" tls_verify_len)
  497. if(tls_verify_len GREATER 0)
  498. set(tls_verify "set(CMAKE_TLS_VERIFY ${tls_verify})")
  499. endif()
  500. # check for tls_cainfo argument
  501. string(LENGTH "${tls_cainfo}" tls_cainfo_len)
  502. if(tls_cainfo_len GREATER 0)
  503. set(tls_cainfo "set(CMAKE_TLS_CAINFO \"${tls_cainfo}\")")
  504. endif()
  505. file(WRITE ${script_filename}
  506. "message(STATUS \"downloading...
  507. src='${remote}'
  508. dst='${local}'
  509. timeout='${timeout_msg}'\")
  510. ${tls_verify}
  511. ${tls_cainfo}
  512. file(DOWNLOAD
  513. \"${remote}\"
  514. \"${local}\"
  515. SHOW_PROGRESS
  516. ${hash_args}
  517. ${timeout_args}
  518. STATUS status
  519. LOG log)
  520. list(GET status 0 status_code)
  521. list(GET status 1 status_string)
  522. if(NOT status_code EQUAL 0)
  523. message(FATAL_ERROR \"error: downloading '${remote}' failed
  524. status_code: \${status_code}
  525. status_string: \${status_string}
  526. log: \${log}
  527. \")
  528. endif()
  529. message(STATUS \"downloading... done\")
  530. "
  531. )
  532. endfunction()
  533. function(_ep_write_verifyfile_script script_filename local hash retries download_script)
  534. if("${hash}" MATCHES "${_ep_hash_regex}")
  535. set(algo "${CMAKE_MATCH_1}")
  536. string(TOLOWER "${CMAKE_MATCH_2}" expect_value)
  537. set(script_content "set(expect_value \"${expect_value}\")
  538. set(attempt 0)
  539. set(succeeded 0)
  540. while(\${attempt} LESS ${retries} OR \${attempt} EQUAL ${retries} AND NOT \${succeeded})
  541. file(${algo} \"\${file}\" actual_value)
  542. if(\"\${actual_value}\" STREQUAL \"\${expect_value}\")
  543. set(succeeded 1)
  544. elseif(\${attempt} LESS ${retries})
  545. message(STATUS \"${algo} hash of \${file}
  546. does not match expected value
  547. expected: \${expect_value}
  548. actual: \${actual_value}
  549. Retrying download.
  550. \")
  551. file(REMOVE \"\${file}\")
  552. execute_process(COMMAND ${CMAKE_COMMAND} -P \"${download_script}\")
  553. endif()
  554. endwhile()
  555. if(\${succeeded})
  556. message(STATUS \"verifying file... done\")
  557. else()
  558. message(FATAL_ERROR \"error: ${algo} hash of
  559. \${file}
  560. does not match expected value
  561. expected: \${expect_value}
  562. actual: \${actual_value}
  563. \")
  564. endif()")
  565. else()
  566. set(script_content "message(STATUS \"verifying file... warning: did not verify file - no URL_HASH specified?\")")
  567. endif()
  568. file(WRITE ${script_filename} "set(file \"${local}\")
  569. message(STATUS \"verifying file...
  570. file='\${file}'\")
  571. ${script_content}
  572. ")
  573. endfunction()
  574. function(_ep_write_extractfile_script script_filename name filename directory)
  575. set(args "")
  576. if(filename MATCHES "(\\.|=)(bz2|tar\\.gz|tgz|zip)$")
  577. set(args xfz)
  578. endif()
  579. if(filename MATCHES "(\\.|=)tar$")
  580. set(args xf)
  581. endif()
  582. if(args STREQUAL "")
  583. message(SEND_ERROR "error: do not know how to extract '${filename}' -- known types are .bz2, .tar, .tar.gz, .tgz and .zip")
  584. return()
  585. endif()
  586. file(WRITE ${script_filename}
  587. "# Make file names absolute:
  588. #
  589. get_filename_component(filename \"${filename}\" ABSOLUTE)
  590. get_filename_component(directory \"${directory}\" ABSOLUTE)
  591. message(STATUS \"extracting...
  592. src='\${filename}'
  593. dst='\${directory}'\")
  594. if(NOT EXISTS \"\${filename}\")
  595. message(FATAL_ERROR \"error: file to extract does not exist: '\${filename}'\")
  596. endif()
  597. # Prepare a space for extracting:
  598. #
  599. set(i 1234)
  600. while(EXISTS \"\${directory}/../ex-${name}\${i}\")
  601. math(EXPR i \"\${i} + 1\")
  602. endwhile()
  603. set(ut_dir \"\${directory}/../ex-${name}\${i}\")
  604. file(MAKE_DIRECTORY \"\${ut_dir}\")
  605. # Extract it:
  606. #
  607. message(STATUS \"extracting... [tar ${args}]\")
  608. execute_process(COMMAND \${CMAKE_COMMAND} -E tar ${args} \${filename}
  609. WORKING_DIRECTORY \${ut_dir}
  610. RESULT_VARIABLE rv)
  611. if(NOT rv EQUAL 0)
  612. message(STATUS \"extracting... [error clean up]\")
  613. file(REMOVE_RECURSE \"\${ut_dir}\")
  614. message(FATAL_ERROR \"error: extract of '\${filename}' failed\")
  615. endif()
  616. # Analyze what came out of the tar file:
  617. #
  618. message(STATUS \"extracting... [analysis]\")
  619. file(GLOB contents \"\${ut_dir}/*\")
  620. list(LENGTH contents n)
  621. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY \"\${contents}\")
  622. set(contents \"\${ut_dir}\")
  623. endif()
  624. # Move \"the one\" directory to the final directory:
  625. #
  626. message(STATUS \"extracting... [rename]\")
  627. file(REMOVE_RECURSE \${directory})
  628. get_filename_component(contents \${contents} ABSOLUTE)
  629. file(RENAME \${contents} \${directory})
  630. # Clean up:
  631. #
  632. message(STATUS \"extracting... [clean up]\")
  633. file(REMOVE_RECURSE \"\${ut_dir}\")
  634. message(STATUS \"extracting... done\")
  635. "
  636. )
  637. endfunction()
  638. function(_ep_set_directories name)
  639. get_property(prefix TARGET ${name} PROPERTY _EP_PREFIX)
  640. if(NOT prefix)
  641. get_property(prefix DIRECTORY PROPERTY EP_PREFIX)
  642. if(NOT prefix)
  643. get_property(base DIRECTORY PROPERTY EP_BASE)
  644. if(NOT base)
  645. set(prefix "${name}-prefix")
  646. endif()
  647. endif()
  648. endif()
  649. if(prefix)
  650. set(tmp_default "${prefix}/tmp")
  651. set(download_default "${prefix}/src")
  652. set(source_default "${prefix}/src/${name}")
  653. set(binary_default "${prefix}/src/${name}-build")
  654. set(stamp_default "${prefix}/src/${name}-stamp")
  655. set(install_default "${prefix}")
  656. else()
  657. set(tmp_default "${base}/tmp/${name}")
  658. set(download_default "${base}/Download/${name}")
  659. set(source_default "${base}/Source/${name}")
  660. set(binary_default "${base}/Build/${name}")
  661. set(stamp_default "${base}/Stamp/${name}")
  662. set(install_default "${base}/Install/${name}")
  663. endif()
  664. get_property(build_in_source TARGET ${name} PROPERTY _EP_BUILD_IN_SOURCE)
  665. if(build_in_source)
  666. get_property(have_binary_dir TARGET ${name} PROPERTY _EP_BINARY_DIR SET)
  667. if(have_binary_dir)
  668. message(FATAL_ERROR
  669. "External project ${name} has both BINARY_DIR and BUILD_IN_SOURCE!")
  670. endif()
  671. endif()
  672. set(top "${CMAKE_CURRENT_BINARY_DIR}")
  673. set(places stamp download source binary install tmp)
  674. foreach(var ${places})
  675. string(TOUPPER "${var}" VAR)
  676. get_property(${var}_dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  677. if(NOT ${var}_dir)
  678. set(${var}_dir "${${var}_default}")
  679. endif()
  680. if(NOT IS_ABSOLUTE "${${var}_dir}")
  681. get_filename_component(${var}_dir "${top}/${${var}_dir}" ABSOLUTE)
  682. endif()
  683. set_property(TARGET ${name} PROPERTY _EP_${VAR}_DIR "${${var}_dir}")
  684. endforeach()
  685. if(build_in_source)
  686. get_property(source_dir TARGET ${name} PROPERTY _EP_SOURCE_DIR)
  687. set_property(TARGET ${name} PROPERTY _EP_BINARY_DIR "${source_dir}")
  688. endif()
  689. # Make the directories at CMake configure time *and* add a custom command
  690. # to make them at build time. They need to exist at makefile generation
  691. # time for Borland make and wmake so that CMake may generate makefiles
  692. # with "cd C:\short\paths\with\no\spaces" commands in them.
  693. #
  694. # Additionally, the add_custom_command is still used in case somebody
  695. # removes one of the necessary directories and tries to rebuild without
  696. # re-running cmake.
  697. foreach(var ${places})
  698. string(TOUPPER "${var}" VAR)
  699. get_property(dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  700. file(MAKE_DIRECTORY "${dir}")
  701. if(NOT EXISTS "${dir}")
  702. message(FATAL_ERROR "dir '${dir}' does not exist after file(MAKE_DIRECTORY)")
  703. endif()
  704. endforeach()
  705. endfunction()
  706. # IMPORTANT: this MUST be a macro and not a function because of the
  707. # in-place replacements that occur in each ${var}
  708. #
  709. macro(_ep_replace_location_tags target_name)
  710. set(vars ${ARGN})
  711. foreach(var ${vars})
  712. if(${var})
  713. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  714. get_property(val TARGET ${target_name} PROPERTY _EP_${dir})
  715. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  716. endforeach()
  717. endif()
  718. endforeach()
  719. endmacro()
  720. function(_ep_write_initial_cache target_name script_filename args)
  721. # Write out values into an initial cache, that will be passed to CMake with -C
  722. set(script_initial_cache "")
  723. set(regex "^([^:]+):([^=]+)=(.*)$")
  724. set(setArg "")
  725. foreach(line ${args})
  726. if("${line}" MATCHES "^-D")
  727. if(setArg)
  728. # This is required to build up lists in variables, or complete an entry
  729. set(setArg "${setArg}${accumulator}\" CACHE ${type} \"Initial cache\" FORCE)")
  730. set(script_initial_cache "${script_initial_cache}\n${setArg}")
  731. set(accumulator "")
  732. set(setArg "")
  733. endif()
  734. string(REGEX REPLACE "^-D" "" line ${line})
  735. if("${line}" MATCHES "${regex}")
  736. string(REGEX MATCH "${regex}" match "${line}")
  737. set(name "${CMAKE_MATCH_1}")
  738. set(type "${CMAKE_MATCH_2}")
  739. set(value "${CMAKE_MATCH_3}")
  740. set(setArg "set(${name} \"${value}")
  741. else()
  742. message(WARNING "Line '${line}' does not match regex. Ignoring.")
  743. endif()
  744. else()
  745. # Assume this is a list to append to the last var
  746. set(accumulator "${accumulator};${line}")
  747. endif()
  748. endforeach()
  749. # Catch the final line of the args
  750. if(setArg)
  751. set(setArg "${setArg}${accumulator}\" CACHE ${type} \"Initial cache\" FORCE)")
  752. set(script_initial_cache "${script_initial_cache}\n${setArg}")
  753. endif()
  754. # Replace location tags.
  755. _ep_replace_location_tags(${target_name} script_initial_cache)
  756. # Write out the initial cache file to the location specified.
  757. if(NOT EXISTS "${script_filename}.in")
  758. file(WRITE "${script_filename}.in" "\@script_initial_cache\@\n")
  759. endif()
  760. configure_file("${script_filename}.in" "${script_filename}")
  761. endfunction()
  762. function(ExternalProject_Get_Property name)
  763. foreach(var ${ARGN})
  764. string(TOUPPER "${var}" VAR)
  765. get_property(${var} TARGET ${name} PROPERTY _EP_${VAR})
  766. if(NOT ${var})
  767. message(FATAL_ERROR "External project \"${name}\" has no ${var}")
  768. endif()
  769. set(${var} "${${var}}" PARENT_SCOPE)
  770. endforeach()
  771. endfunction()
  772. function(_ep_get_configure_command_id name cfg_cmd_id_var)
  773. get_target_property(cmd ${name} _EP_CONFIGURE_COMMAND)
  774. if(cmd STREQUAL "")
  775. # Explicit empty string means no configure step for this project
  776. set(${cfg_cmd_id_var} "none" PARENT_SCOPE)
  777. else()
  778. if(NOT cmd)
  779. # Default is "use cmake":
  780. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  781. else()
  782. # Otherwise we have to analyze the value:
  783. if(cmd MATCHES "^[^;]*/configure")
  784. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  785. elseif(cmd MATCHES "^[^;]*/cmake" AND NOT cmd MATCHES ";-[PE];")
  786. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  787. elseif(cmd MATCHES "config")
  788. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  789. else()
  790. set(${cfg_cmd_id_var} "unknown:${cmd}" PARENT_SCOPE)
  791. endif()
  792. endif()
  793. endif()
  794. endfunction()
  795. function(_ep_get_build_command name step cmd_var)
  796. set(cmd "${${cmd_var}}")
  797. if(NOT cmd)
  798. set(args)
  799. _ep_get_configure_command_id(${name} cfg_cmd_id)
  800. if(cfg_cmd_id STREQUAL "cmake")
  801. # CMake project. Select build command based on generator.
  802. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  803. if("${CMAKE_GENERATOR}" MATCHES "Make" AND
  804. ("${cmake_generator}" MATCHES "Make" OR NOT cmake_generator))
  805. # The project uses the same Makefile generator. Use recursive make.
  806. set(cmd "$(MAKE)")
  807. if(step STREQUAL "INSTALL")
  808. set(args install)
  809. endif()
  810. if(step STREQUAL "TEST")
  811. set(args test)
  812. endif()
  813. else()
  814. # Drive the project with "cmake --build".
  815. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  816. if(cmake_command)
  817. set(cmd "${cmake_command}")
  818. else()
  819. set(cmd "${CMAKE_COMMAND}")
  820. endif()
  821. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  822. if(step STREQUAL "INSTALL")
  823. list(APPEND args --target install)
  824. endif()
  825. # But for "TEST" drive the project with corresponding "ctest".
  826. if(step STREQUAL "TEST")
  827. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  828. set(args "")
  829. endif()
  830. endif()
  831. else()
  832. # Non-CMake project. Guess "make" and "make install" and "make test".
  833. if("${CMAKE_GENERATOR}" MATCHES "Makefiles")
  834. # Try to get the parallel arguments
  835. set(cmd "$(MAKE)")
  836. else()
  837. set(cmd "make")
  838. endif()
  839. if(step STREQUAL "INSTALL")
  840. set(args install)
  841. endif()
  842. if(step STREQUAL "TEST")
  843. set(args test)
  844. endif()
  845. endif()
  846. # Use user-specified arguments instead of default arguments, if any.
  847. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  848. if(have_args)
  849. get_target_property(args ${name} _EP_${step}_ARGS)
  850. endif()
  851. list(APPEND cmd ${args})
  852. endif()
  853. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  854. endfunction()
  855. function(_ep_write_log_script name step cmd_var)
  856. ExternalProject_Get_Property(${name} stamp_dir)
  857. set(command "${${cmd_var}}")
  858. set(make "")
  859. set(code_cygpath_make "")
  860. if("${command}" MATCHES "^\\$\\(MAKE\\)")
  861. # GNU make recognizes the string "$(MAKE)" as recursive make, so
  862. # ensure that it appears directly in the makefile.
  863. string(REGEX REPLACE "^\\$\\(MAKE\\)" "\${make}" command "${command}")
  864. set(make "-Dmake=$(MAKE)")
  865. if(WIN32 AND NOT CYGWIN)
  866. set(code_cygpath_make "
  867. if(\${make} MATCHES \"^/\")
  868. execute_process(
  869. COMMAND cygpath -w \${make}
  870. OUTPUT_VARIABLE cygpath_make
  871. ERROR_VARIABLE cygpath_make
  872. RESULT_VARIABLE cygpath_error
  873. OUTPUT_STRIP_TRAILING_WHITESPACE
  874. )
  875. if(NOT cygpath_error)
  876. set(make \${cygpath_make})
  877. endif()
  878. endif()
  879. ")
  880. endif()
  881. endif()
  882. set(config "")
  883. if("${CMAKE_CFG_INTDIR}" MATCHES "^\\$")
  884. string(REPLACE "${CMAKE_CFG_INTDIR}" "\${config}" command "${command}")
  885. set(config "-Dconfig=${CMAKE_CFG_INTDIR}")
  886. endif()
  887. # Wrap multiple 'COMMAND' lines up into a second-level wrapper
  888. # script so all output can be sent to one log file.
  889. if("${command}" MATCHES ";COMMAND;")
  890. set(code_execute_process "
  891. ${code_cygpath_make}
  892. execute_process(COMMAND \${command} RESULT_VARIABLE result)
  893. if(result)
  894. set(msg \"Command failed (\${result}):\\n\")
  895. foreach(arg IN LISTS command)
  896. set(msg \"\${msg} '\${arg}'\")
  897. endforeach()
  898. message(FATAL_ERROR \"\${msg}\")
  899. endif()
  900. ")
  901. set(code "")
  902. set(cmd "")
  903. set(sep "")
  904. foreach(arg IN LISTS command)
  905. if("x${arg}" STREQUAL "xCOMMAND")
  906. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  907. set(cmd "")
  908. set(sep "")
  909. else()
  910. set(cmd "${cmd}${sep}${arg}")
  911. set(sep ";")
  912. endif()
  913. endforeach()
  914. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  915. file(WRITE ${stamp_dir}/${name}-${step}-impl.cmake "${code}")
  916. set(command ${CMAKE_COMMAND} "-Dmake=\${make}" "-Dconfig=\${config}" -P ${stamp_dir}/${name}-${step}-impl.cmake)
  917. endif()
  918. # Wrap the command in a script to log output to files.
  919. set(script ${stamp_dir}/${name}-${step}.cmake)
  920. set(logbase ${stamp_dir}/${name}-${step})
  921. file(WRITE ${script} "
  922. ${code_cygpath_make}
  923. set(command \"${command}\")
  924. execute_process(
  925. COMMAND \${command}
  926. RESULT_VARIABLE result
  927. OUTPUT_FILE \"${logbase}-out.log\"
  928. ERROR_FILE \"${logbase}-err.log\"
  929. )
  930. if(result)
  931. set(msg \"Command failed: \${result}\\n\")
  932. foreach(arg IN LISTS command)
  933. set(msg \"\${msg} '\${arg}'\")
  934. endforeach()
  935. set(msg \"\${msg}\\nSee also\\n ${logbase}-*.log\\n\")
  936. message(FATAL_ERROR \"\${msg}\")
  937. else()
  938. set(msg \"${name} ${step} command succeeded. See also ${logbase}-*.log\\n\")
  939. message(STATUS \"\${msg}\")
  940. endif()
  941. ")
  942. set(command ${CMAKE_COMMAND} ${make} ${config} -P ${script})
  943. set(${cmd_var} "${command}" PARENT_SCOPE)
  944. endfunction()
  945. # This module used to use "/${CMAKE_CFG_INTDIR}" directly and produced
  946. # makefiles with "/./" in paths for custom command dependencies. Which
  947. # resulted in problems with parallel make -j invocations.
  948. #
  949. # This function was added so that the suffix (search below for ${cfgdir}) is
  950. # only set to "/${CMAKE_CFG_INTDIR}" when ${CMAKE_CFG_INTDIR} is not going to
  951. # be "." (multi-configuration build systems like Visual Studio and Xcode...)
  952. #
  953. function(_ep_get_configuration_subdir_suffix suffix_var)
  954. set(suffix "")
  955. if(CMAKE_CONFIGURATION_TYPES)
  956. set(suffix "/${CMAKE_CFG_INTDIR}")
  957. endif()
  958. set(${suffix_var} "${suffix}" PARENT_SCOPE)
  959. endfunction()
  960. function(_ep_get_step_stampfile name step stampfile_var)
  961. ExternalProject_Get_Property(${name} stamp_dir)
  962. _ep_get_configuration_subdir_suffix(cfgdir)
  963. set(stampfile "${stamp_dir}${cfgdir}/${name}-${step}")
  964. set(${stampfile_var} "${stampfile}" PARENT_SCOPE)
  965. endfunction()
  966. function(ExternalProject_Add_StepTargets name)
  967. set(steps ${ARGN})
  968. foreach(step ${steps})
  969. _ep_get_step_stampfile(${name} ${step} stamp_file)
  970. add_custom_target(${name}-${step}
  971. DEPENDS ${stamp_file})
  972. # Depend on other external projects (target-level).
  973. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  974. foreach(arg IN LISTS deps)
  975. add_dependencies(${name}-${step} ${arg})
  976. endforeach()
  977. endforeach()
  978. endfunction()
  979. function(ExternalProject_Add_Step name step)
  980. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  981. _ep_get_configuration_subdir_suffix(cfgdir)
  982. set(complete_stamp_file "${cmf_dir}${cfgdir}/${name}-complete")
  983. _ep_get_step_stampfile(${name} ${step} stamp_file)
  984. add_custom_command(APPEND
  985. OUTPUT ${complete_stamp_file}
  986. DEPENDS ${stamp_file}
  987. )
  988. _ep_parse_arguments(ExternalProject_Add_Step
  989. ${name} _EP_${step}_ "${ARGN}")
  990. # Steps depending on this step.
  991. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  992. foreach(depender IN LISTS dependers)
  993. _ep_get_step_stampfile(${name} ${depender} depender_stamp_file)
  994. add_custom_command(APPEND
  995. OUTPUT ${depender_stamp_file}
  996. DEPENDS ${stamp_file}
  997. )
  998. endforeach()
  999. # Dependencies on files.
  1000. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  1001. # Dependencies on steps.
  1002. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  1003. foreach(dependee IN LISTS dependees)
  1004. _ep_get_step_stampfile(${name} ${dependee} dependee_stamp_file)
  1005. list(APPEND depends ${dependee_stamp_file})
  1006. endforeach()
  1007. # The command to run.
  1008. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  1009. if(command)
  1010. set(comment "Performing ${step} step for '${name}'")
  1011. else()
  1012. set(comment "No ${step} step for '${name}'")
  1013. endif()
  1014. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  1015. # Replace list separators.
  1016. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  1017. if(sep AND command)
  1018. string(REPLACE "${sep}" "\\;" command "${command}")
  1019. endif()
  1020. # Replace location tags.
  1021. _ep_replace_location_tags(${name} comment command work_dir)
  1022. # Custom comment?
  1023. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  1024. if(comment_set)
  1025. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  1026. endif()
  1027. # Run every time?
  1028. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  1029. if(always)
  1030. set_property(SOURCE ${stamp_file} PROPERTY SYMBOLIC 1)
  1031. set(touch)
  1032. else()
  1033. set(touch ${CMAKE_COMMAND} -E touch ${stamp_file})
  1034. endif()
  1035. # Wrap with log script?
  1036. get_property(log TARGET ${name} PROPERTY _EP_${step}_LOG)
  1037. if(command AND log)
  1038. _ep_write_log_script(${name} ${step} command)
  1039. endif()
  1040. add_custom_command(
  1041. OUTPUT ${stamp_file}
  1042. COMMENT ${comment}
  1043. COMMAND ${command}
  1044. COMMAND ${touch}
  1045. DEPENDS ${depends}
  1046. WORKING_DIRECTORY ${work_dir}
  1047. VERBATIM
  1048. )
  1049. # Add custom "step target"?
  1050. get_property(step_targets TARGET ${name} PROPERTY _EP_STEP_TARGETS)
  1051. if(NOT step_targets)
  1052. get_property(step_targets DIRECTORY PROPERTY EP_STEP_TARGETS)
  1053. endif()
  1054. foreach(st ${step_targets})
  1055. if("${st}" STREQUAL "${step}")
  1056. ExternalProject_Add_StepTargets(${name} ${step})
  1057. break()
  1058. endif()
  1059. endforeach()
  1060. endfunction()
  1061. function(_ep_add_mkdir_command name)
  1062. ExternalProject_Get_Property(${name}
  1063. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  1064. _ep_get_configuration_subdir_suffix(cfgdir)
  1065. ExternalProject_Add_Step(${name} mkdir
  1066. COMMENT "Creating directories for '${name}'"
  1067. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  1068. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  1069. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  1070. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  1071. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}${cfgdir}
  1072. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  1073. )
  1074. endfunction()
  1075. function(_ep_get_git_version git_EXECUTABLE git_version_var)
  1076. if(git_EXECUTABLE)
  1077. execute_process(
  1078. COMMAND "${git_EXECUTABLE}" --version
  1079. OUTPUT_VARIABLE ov
  1080. ERROR_VARIABLE ev
  1081. OUTPUT_STRIP_TRAILING_WHITESPACE
  1082. )
  1083. string(REGEX REPLACE "^git version (.+)$" "\\1" version "${ov}")
  1084. set(${git_version_var} "${version}" PARENT_SCOPE)
  1085. endif()
  1086. endfunction()
  1087. function(_ep_is_dir_empty dir empty_var)
  1088. file(GLOB gr "${dir}/*")
  1089. if("${gr}" STREQUAL "")
  1090. set(${empty_var} 1 PARENT_SCOPE)
  1091. else()
  1092. set(${empty_var} 0 PARENT_SCOPE)
  1093. endif()
  1094. endfunction()
  1095. function(_ep_add_download_command name)
  1096. ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
  1097. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  1098. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  1099. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  1100. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  1101. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  1102. get_property(hg_repository TARGET ${name} PROPERTY _EP_HG_REPOSITORY )
  1103. get_property(url TARGET ${name} PROPERTY _EP_URL)
  1104. get_property(fname TARGET ${name} PROPERTY _EP_DOWNLOAD_NAME)
  1105. # TODO: Perhaps file:// should be copied to download dir before extraction.
  1106. string(REGEX REPLACE "^file://" "" url "${url}")
  1107. set(depends)
  1108. set(comment)
  1109. set(work_dir)
  1110. if(cmd_set)
  1111. set(work_dir ${download_dir})
  1112. elseif(cvs_repository)
  1113. find_package(CVS)
  1114. if(NOT CVS_EXECUTABLE)
  1115. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  1116. endif()
  1117. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  1118. if(NOT cvs_module)
  1119. message(FATAL_ERROR "error: no CVS_MODULE")
  1120. endif()
  1121. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  1122. set(repository ${cvs_repository})
  1123. set(module ${cvs_module})
  1124. set(tag ${cvs_tag})
  1125. configure_file(
  1126. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1127. "${stamp_dir}/${name}-cvsinfo.txt"
  1128. @ONLY
  1129. )
  1130. get_filename_component(src_name "${source_dir}" NAME)
  1131. get_filename_component(work_dir "${source_dir}" PATH)
  1132. set(comment "Performing download step (CVS checkout) for '${name}'")
  1133. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  1134. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  1135. elseif(svn_repository)
  1136. find_package(Subversion)
  1137. if(NOT Subversion_SVN_EXECUTABLE)
  1138. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  1139. endif()
  1140. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  1141. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  1142. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  1143. get_property(svn_trust_cert TARGET ${name} PROPERTY _EP_SVN_TRUST_CERT)
  1144. set(repository "${svn_repository} user=${svn_username} password=${svn_password}")
  1145. set(module)
  1146. set(tag ${svn_revision})
  1147. configure_file(
  1148. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1149. "${stamp_dir}/${name}-svninfo.txt"
  1150. @ONLY
  1151. )
  1152. get_filename_component(src_name "${source_dir}" NAME)
  1153. get_filename_component(work_dir "${source_dir}" PATH)
  1154. set(comment "Performing download step (SVN checkout) for '${name}'")
  1155. set(svn_user_pw_args "")
  1156. if(DEFINED svn_username)
  1157. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  1158. endif()
  1159. if(DEFINED svn_password)
  1160. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  1161. endif()
  1162. if(svn_trust_cert)
  1163. set(svn_trust_cert_args --trust-server-cert)
  1164. endif()
  1165. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision}
  1166. --non-interactive ${svn_trust_cert_args} ${svn_user_pw_args} ${src_name})
  1167. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  1168. elseif(git_repository)
  1169. find_package(Git)
  1170. if(NOT GIT_EXECUTABLE)
  1171. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  1172. endif()
  1173. # The git submodule update '--recursive' flag requires git >= v1.6.5
  1174. #
  1175. _ep_get_git_version("${GIT_EXECUTABLE}" git_version)
  1176. if(git_version VERSION_LESS 1.6.5)
  1177. message(FATAL_ERROR "error: git version 1.6.5 or later required for 'git submodule update --recursive': git_version='${git_version}'")
  1178. endif()
  1179. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1180. if(NOT git_tag)
  1181. set(git_tag "master")
  1182. endif()
  1183. get_property(git_submodules TARGET ${name} PROPERTY _EP_GIT_SUBMODULES)
  1184. # For the download step, and the git clone operation, only the repository
  1185. # should be recorded in a configured RepositoryInfo file. If the repo
  1186. # changes, the clone script should be run again. But if only the tag
  1187. # changes, avoid running the clone script again. Let the 'always' running
  1188. # update step checkout the new tag.
  1189. #
  1190. set(repository ${git_repository})
  1191. set(module)
  1192. set(tag)
  1193. configure_file(
  1194. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1195. "${stamp_dir}/${name}-gitinfo.txt"
  1196. @ONLY
  1197. )
  1198. get_filename_component(src_name "${source_dir}" NAME)
  1199. get_filename_component(work_dir "${source_dir}" PATH)
  1200. # Since git clone doesn't succeed if the non-empty source_dir exists,
  1201. # create a cmake script to invoke as download command.
  1202. # The script will delete the source directory and then call git clone.
  1203. #
  1204. _ep_write_gitclone_script(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
  1205. ${GIT_EXECUTABLE} ${git_repository} ${git_tag} "${git_submodules}" ${src_name} ${work_dir}
  1206. ${stamp_dir}/${name}-gitinfo.txt ${stamp_dir}/${name}-gitclone-lastrun.txt
  1207. )
  1208. set(comment "Performing download step (git clone) for '${name}'")
  1209. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
  1210. list(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
  1211. elseif(hg_repository)
  1212. find_package(Hg)
  1213. if(NOT HG_EXECUTABLE)
  1214. message(FATAL_ERROR "error: could not find hg for clone of ${name}")
  1215. endif()
  1216. get_property(hg_tag TARGET ${name} PROPERTY _EP_HG_TAG)
  1217. if(NOT hg_tag)
  1218. set(hg_tag "tip")
  1219. endif()
  1220. # For the download step, and the hg clone operation, only the repository
  1221. # should be recorded in a configured RepositoryInfo file. If the repo
  1222. # changes, the clone script should be run again. But if only the tag
  1223. # changes, avoid running the clone script again. Let the 'always' running
  1224. # update step checkout the new tag.
  1225. #
  1226. set(repository ${hg_repository})
  1227. set(module)
  1228. set(tag)
  1229. configure_file(
  1230. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1231. "${stamp_dir}/${name}-hginfo.txt"
  1232. @ONLY
  1233. )
  1234. get_filename_component(src_name "${source_dir}" NAME)
  1235. get_filename_component(work_dir "${source_dir}" PATH)
  1236. # Since hg clone doesn't succeed if the non-empty source_dir exists,
  1237. # create a cmake script to invoke as download command.
  1238. # The script will delete the source directory and then call hg clone.
  1239. #
  1240. _ep_write_hgclone_script(${tmp_dir}/${name}-hgclone.cmake ${source_dir}
  1241. ${HG_EXECUTABLE} ${hg_repository} ${hg_tag} ${src_name} ${work_dir}
  1242. ${stamp_dir}/${name}-hginfo.txt ${stamp_dir}/${name}-hgclone-lastrun.txt
  1243. )
  1244. set(comment "Performing download step (hg clone) for '${name}'")
  1245. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-hgclone.cmake)
  1246. list(APPEND depends ${stamp_dir}/${name}-hginfo.txt)
  1247. elseif(url)
  1248. get_filename_component(work_dir "${source_dir}" PATH)
  1249. get_property(hash TARGET ${name} PROPERTY _EP_URL_HASH)
  1250. if(hash AND NOT "${hash}" MATCHES "${_ep_hash_regex}")
  1251. message(FATAL_ERROR "URL_HASH is set to\n ${hash}\n"
  1252. "but must be ALGO=value where ALGO is\n ${_ep_hash_algos}\n"
  1253. "and value is a hex string.")
  1254. endif()
  1255. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  1256. if(md5 AND NOT "MD5=${md5}" MATCHES "${_ep_hash_regex}")
  1257. message(FATAL_ERROR "URL_MD5 is set to\n ${md5}\nbut must be a hex string.")
  1258. endif()
  1259. if(md5 AND NOT hash)
  1260. set(hash "MD5=${md5}")
  1261. endif()
  1262. set(repository "external project URL")
  1263. set(module "${url}")
  1264. set(tag "${hash}")
  1265. set(retries 0)
  1266. set(download_script "")
  1267. configure_file(
  1268. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1269. "${stamp_dir}/${name}-urlinfo.txt"
  1270. @ONLY
  1271. )
  1272. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  1273. if(IS_DIRECTORY "${url}")
  1274. get_filename_component(abs_dir "${url}" ABSOLUTE)
  1275. set(comment "Performing download step (DIR copy) for '${name}'")
  1276. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  1277. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  1278. else()
  1279. if("${url}" MATCHES "^[a-z]+://")
  1280. # TODO: Should download and extraction be different steps?
  1281. if("x${fname}" STREQUAL "x")
  1282. string(REGEX MATCH "[^/\\?]*$" fname "${url}")
  1283. endif()
  1284. if(NOT "${fname}" MATCHES "(\\.|=)(bz2|tar|tgz|tar\\.gz|zip)$")
  1285. string(REGEX MATCH "([^/\\?]+(\\.|=)(bz2|tar|tgz|tar\\.gz|zip))/.*$" match_result "${url}")
  1286. set(fname "${CMAKE_MATCH_1}")
  1287. endif()
  1288. if(NOT "${fname}" MATCHES "(\\.|=)(bz2|tar|tgz|tar\\.gz|zip)$")
  1289. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  1290. endif()
  1291. string(REPLACE ";" "-" fname "${fname}")
  1292. set(file ${download_dir}/${fname})
  1293. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  1294. get_property(tls_verify TARGET ${name} PROPERTY _EP_TLS_VERIFY)
  1295. get_property(tls_cainfo TARGET ${name} PROPERTY _EP_TLS_CAINFO)
  1296. set(download_script "${stamp_dir}/download-${name}.cmake")
  1297. _ep_write_downloadfile_script("${download_script}" "${url}" "${file}" "${timeout}" "${hash}" "${tls_verify}" "${tls_cainfo}")
  1298. set(cmd ${CMAKE_COMMAND} -P "${download_script}"
  1299. COMMAND)
  1300. set(retries 3)
  1301. set(comment "Performing download step (download, verify and extract) for '${name}'")
  1302. else()
  1303. set(file "${url}")
  1304. set(comment "Performing download step (verify and extract) for '${name}'")
  1305. endif()
  1306. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${hash}" "${retries}" "${download_script}")
  1307. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake
  1308. COMMAND)
  1309. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${name}" "${file}" "${source_dir}")
  1310. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  1311. endif()
  1312. else()
  1313. _ep_is_dir_empty("${source_dir}" empty)
  1314. if(${empty})
  1315. message(SEND_ERROR "error: no download info for '${name}' -- please specify existing/non-empty SOURCE_DIR or one of URL, CVS_REPOSITORY and CVS_MODULE, SVN_REPOSITORY, GIT_REPOSITORY, HG_REPOSITORY or DOWNLOAD_COMMAND")
  1316. endif()
  1317. endif()
  1318. get_property(log TARGET ${name} PROPERTY _EP_LOG_DOWNLOAD)
  1319. if(log)
  1320. set(log LOG 1)
  1321. else()
  1322. set(log "")
  1323. endif()
  1324. ExternalProject_Add_Step(${name} download
  1325. COMMENT ${comment}
  1326. COMMAND ${cmd}
  1327. WORKING_DIRECTORY ${work_dir}
  1328. DEPENDS ${depends}
  1329. DEPENDEES mkdir
  1330. ${log}
  1331. )
  1332. endfunction()
  1333. function(_ep_add_update_command name)
  1334. ExternalProject_Get_Property(${name} source_dir tmp_dir)
  1335. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  1336. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  1337. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  1338. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  1339. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  1340. get_property(hg_repository TARGET ${name} PROPERTY _EP_HG_REPOSITORY )
  1341. set(work_dir)
  1342. set(comment)
  1343. set(always)
  1344. if(cmd_set)
  1345. set(work_dir ${source_dir})
  1346. elseif(cvs_repository)
  1347. if(NOT CVS_EXECUTABLE)
  1348. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  1349. endif()
  1350. set(work_dir ${source_dir})
  1351. set(comment "Performing update step (CVS update) for '${name}'")
  1352. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  1353. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  1354. set(always 1)
  1355. elseif(svn_repository)
  1356. if(NOT Subversion_SVN_EXECUTABLE)
  1357. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  1358. endif()
  1359. set(work_dir ${source_dir})
  1360. set(comment "Performing update step (SVN update) for '${name}'")
  1361. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  1362. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  1363. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  1364. get_property(svn_trust_cert TARGET ${name} PROPERTY _EP_SVN_TRUST_CERT)
  1365. set(svn_user_pw_args "")
  1366. if(DEFINED svn_username)
  1367. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  1368. endif()
  1369. if(DEFINED svn_password)
  1370. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  1371. endif()
  1372. if(svn_trust_cert)
  1373. set(svn_trust_cert_args --trust-server-cert)
  1374. endif()
  1375. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision}
  1376. --non-interactive ${svn_trust_cert_args} ${svn_user_pw_args})
  1377. set(always 1)
  1378. elseif(git_repository)
  1379. if(NOT GIT_EXECUTABLE)
  1380. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  1381. endif()
  1382. set(work_dir ${source_dir})
  1383. set(comment "Performing update step for '${name}'")
  1384. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1385. if(NOT git_tag)
  1386. set(git_tag "master")
  1387. endif()
  1388. get_property(git_submodules TARGET ${name} PROPERTY _EP_GIT_SUBMODULES)
  1389. _ep_write_gitupdate_script(${tmp_dir}/${name}-gitupdate.cmake
  1390. ${GIT_EXECUTABLE} ${git_tag} "${git_submodules}" ${git_repository} ${work_dir}
  1391. )
  1392. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitupdate.cmake)
  1393. set(always 1)
  1394. elseif(hg_repository)
  1395. if(NOT HG_EXECUTABLE)
  1396. message(FATAL_ERROR "error: could not find hg for pull of ${name}")
  1397. endif()
  1398. set(work_dir ${source_dir})
  1399. set(comment "Performing update step (hg pull) for '${name}'")
  1400. get_property(hg_tag TARGET ${name} PROPERTY _EP_HG_TAG)
  1401. if(NOT hg_tag)
  1402. set(hg_tag "tip")
  1403. endif()
  1404. if("${HG_VERSION_STRING}" STREQUAL "2.1")
  1405. message(WARNING "Mercurial 2.1 does not distinguish an empty pull from a failed pull:
  1406. http://mercurial.selenic.com/wiki/UpgradeNotes#A2.1.1:_revert_pull_return_code_change.2C_compile_issue_on_OS_X
  1407. http://thread.gmane.org/gmane.comp.version-control.mercurial.devel/47656
  1408. Update to Mercurial >= 2.1.1.
  1409. ")
  1410. endif()
  1411. set(cmd ${HG_EXECUTABLE} pull
  1412. COMMAND ${HG_EXECUTABLE} update ${hg_tag}
  1413. )
  1414. set(always 1)
  1415. endif()
  1416. get_property(log TARGET ${name} PROPERTY _EP_LOG_UPDATE)
  1417. if(log)
  1418. set(log LOG 1)
  1419. else()
  1420. set(log "")
  1421. endif()
  1422. ExternalProject_Add_Step(${name} update
  1423. COMMENT ${comment}
  1424. COMMAND ${cmd}
  1425. ALWAYS ${always}
  1426. WORKING_DIRECTORY ${work_dir}
  1427. DEPENDEES download
  1428. ${log}
  1429. )
  1430. endfunction()
  1431. function(_ep_add_patch_command name)
  1432. ExternalProject_Get_Property(${name} source_dir)
  1433. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  1434. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  1435. set(work_dir)
  1436. if(cmd_set)
  1437. set(work_dir ${source_dir})
  1438. endif()
  1439. ExternalProject_Add_Step(${name} patch
  1440. COMMAND ${cmd}
  1441. WORKING_DIRECTORY ${work_dir}
  1442. DEPENDEES download
  1443. )
  1444. endfunction()
  1445. # TODO: Make sure external projects use the proper compiler
  1446. function(_ep_add_configure_command name)
  1447. ExternalProject_Get_Property(${name} source_dir binary_dir tmp_dir)
  1448. # Depend on other external projects (file-level).
  1449. set(file_deps)
  1450. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1451. foreach(dep IN LISTS deps)
  1452. get_property(is_ep TARGET ${dep} PROPERTY _EP_IS_EXTERNAL_PROJECT)
  1453. if(is_ep)
  1454. _ep_get_step_stampfile(${dep} "done" done_stamp_file)
  1455. list(APPEND file_deps ${done_stamp_file})
  1456. endif()
  1457. endforeach()
  1458. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  1459. if(cmd_set)
  1460. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  1461. else()
  1462. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1463. if(cmake_command)
  1464. set(cmd "${cmake_command}")
  1465. else()
  1466. set(cmd "${CMAKE_COMMAND}")
  1467. endif()
  1468. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  1469. list(APPEND cmd ${cmake_args})
  1470. # If there are any CMAKE_CACHE_ARGS, write an initial cache and use it
  1471. get_property(cmake_cache_args TARGET ${name} PROPERTY _EP_CMAKE_CACHE_ARGS)
  1472. if(cmake_cache_args)
  1473. set(_ep_cache_args_script "${tmp_dir}/${name}-cache.cmake")
  1474. _ep_write_initial_cache(${name} "${_ep_cache_args_script}" "${cmake_cache_args}")
  1475. list(APPEND cmd "-C${_ep_cache_args_script}")
  1476. endif()
  1477. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1478. get_target_property(cmake_generator_toolset ${name} _EP_CMAKE_GENERATOR_TOOLSET)
  1479. if(cmake_generator)
  1480. list(APPEND cmd "-G${cmake_generator}")
  1481. if(cmake_generator_toolset)
  1482. list(APPEND cmd "-T${cmake_generator_toolset}")
  1483. endif()
  1484. else()
  1485. if(CMAKE_EXTRA_GENERATOR)
  1486. list(APPEND cmd "-G${CMAKE_EXTRA_GENERATOR} - ${CMAKE_GENERATOR}")
  1487. else()
  1488. list(APPEND cmd "-G${CMAKE_GENERATOR}")
  1489. endif()
  1490. if(cmake_generator_toolset)
  1491. message(FATAL_ERROR "Option CMAKE_GENERATOR_TOOLSET not allowed without CMAKE_GENERATOR.")
  1492. endif()
  1493. if(CMAKE_GENERATOR_TOOLSET)
  1494. list(APPEND cmd "-T${CMAKE_GENERATOR_TOOLSET}")
  1495. endif()
  1496. endif()
  1497. list(APPEND cmd "${source_dir}")
  1498. endif()
  1499. # If anything about the configure command changes, (command itself, cmake
  1500. # used, cmake args or cmake generator) then re-run the configure step.
  1501. # Fixes issue http://public.kitware.com/Bug/view.php?id=10258
  1502. #
  1503. if(NOT EXISTS ${tmp_dir}/${name}-cfgcmd.txt.in)
  1504. file(WRITE ${tmp_dir}/${name}-cfgcmd.txt.in "cmd='\@cmd\@'\n")
  1505. endif()
  1506. configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt)
  1507. list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt)
  1508. list(APPEND file_deps ${_ep_cache_args_script})
  1509. get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE)
  1510. if(log)
  1511. set(log LOG 1)
  1512. else()
  1513. set(log "")
  1514. endif()
  1515. ExternalProject_Add_Step(${name} configure
  1516. COMMAND ${cmd}
  1517. WORKING_DIRECTORY ${binary_dir}
  1518. DEPENDEES update patch
  1519. DEPENDS ${file_deps}
  1520. ${log}
  1521. )
  1522. endfunction()
  1523. function(_ep_add_build_command name)
  1524. ExternalProject_Get_Property(${name} binary_dir)
  1525. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  1526. if(cmd_set)
  1527. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  1528. else()
  1529. _ep_get_build_command(${name} BUILD cmd)
  1530. endif()
  1531. get_property(log TARGET ${name} PROPERTY _EP_LOG_BUILD)
  1532. if(log)
  1533. set(log LOG 1)
  1534. else()
  1535. set(log "")
  1536. endif()
  1537. get_property(build_always TARGET ${name} PROPERTY _EP_BUILD_ALWAYS)
  1538. if(build_always)
  1539. set(always 1)
  1540. else()
  1541. set(always 0)
  1542. endif()
  1543. ExternalProject_Add_Step(${name} build
  1544. COMMAND ${cmd}
  1545. WORKING_DIRECTORY ${binary_dir}
  1546. DEPENDEES configure
  1547. ALWAYS ${always}
  1548. ${log}
  1549. )
  1550. endfunction()
  1551. function(_ep_add_install_command name)
  1552. ExternalProject_Get_Property(${name} binary_dir)
  1553. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  1554. if(cmd_set)
  1555. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  1556. else()
  1557. _ep_get_build_command(${name} INSTALL cmd)
  1558. endif()
  1559. get_property(log TARGET ${name} PROPERTY _EP_LOG_INSTALL)
  1560. if(log)
  1561. set(log LOG 1)
  1562. else()
  1563. set(log "")
  1564. endif()
  1565. ExternalProject_Add_Step(${name} install
  1566. COMMAND ${cmd}
  1567. WORKING_DIRECTORY ${binary_dir}
  1568. DEPENDEES build
  1569. ${log}
  1570. )
  1571. endfunction()
  1572. function(_ep_add_test_command name)
  1573. ExternalProject_Get_Property(${name} binary_dir)
  1574. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  1575. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  1576. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  1577. # Only actually add the test step if one of the test related properties is
  1578. # explicitly set. (i.e. the test step is omitted unless requested...)
  1579. #
  1580. if(cmd_set OR before OR after)
  1581. if(cmd_set)
  1582. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  1583. else()
  1584. _ep_get_build_command(${name} TEST cmd)
  1585. endif()
  1586. if(before)
  1587. set(dep_args DEPENDEES build DEPENDERS install)
  1588. else()
  1589. set(dep_args DEPENDEES install)
  1590. endif()
  1591. get_property(log TARGET ${name} PROPERTY _EP_LOG_TEST)
  1592. if(log)
  1593. set(log LOG 1)
  1594. else()
  1595. set(log "")
  1596. endif()
  1597. ExternalProject_Add_Step(${name} test
  1598. COMMAND ${cmd}
  1599. WORKING_DIRECTORY ${binary_dir}
  1600. ${dep_args}
  1601. ${log}
  1602. )
  1603. endif()
  1604. endfunction()
  1605. function(ExternalProject_Add name)
  1606. _ep_get_configuration_subdir_suffix(cfgdir)
  1607. # Add a custom target for the external project.
  1608. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  1609. set(complete_stamp_file "${cmf_dir}${cfgdir}/${name}-complete")
  1610. add_custom_target(${name} ALL DEPENDS ${complete_stamp_file})
  1611. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  1612. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  1613. _ep_set_directories(${name})
  1614. _ep_get_step_stampfile(${name} "done" done_stamp_file)
  1615. _ep_get_step_stampfile(${name} "install" install_stamp_file)
  1616. # The 'complete' step depends on all other steps and creates a
  1617. # 'done' mark. A dependent external project's 'configure' step
  1618. # depends on the 'done' mark so that it rebuilds when this project
  1619. # rebuilds. It is important that 'done' is not the output of any
  1620. # custom command so that CMake does not propagate build rules to
  1621. # other external project targets, which may cause problems during
  1622. # parallel builds. However, the Ninja generator needs to see the entire
  1623. # dependency graph, and can cope with custom commands belonging to
  1624. # multiple targets, so we add the 'done' mark as an output for Ninja only.
  1625. set(complete_outputs ${complete_stamp_file})
  1626. if(${CMAKE_GENERATOR} MATCHES "Ninja")
  1627. set(complete_outputs ${complete_outputs} ${done_stamp_file})
  1628. endif()
  1629. add_custom_command(
  1630. OUTPUT ${complete_outputs}
  1631. COMMENT "Completed '${name}'"
  1632. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  1633. COMMAND ${CMAKE_COMMAND} -E touch ${complete_stamp_file}
  1634. COMMAND ${CMAKE_COMMAND} -E touch ${done_stamp_file}
  1635. DEPENDS ${install_stamp_file}
  1636. VERBATIM
  1637. )
  1638. # Depend on other external projects (target-level).
  1639. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1640. foreach(arg IN LISTS deps)
  1641. add_dependencies(${name} ${arg})
  1642. endforeach()
  1643. # Set up custom build steps based on the target properties.
  1644. # Each step depends on the previous one.
  1645. #
  1646. # The target depends on the output of the final step.
  1647. # (Already set up above in the DEPENDS of the add_custom_target command.)
  1648. #
  1649. _ep_add_mkdir_command(${name})
  1650. _ep_add_download_command(${name})
  1651. _ep_add_update_command(${name})
  1652. _ep_add_patch_command(${name})
  1653. _ep_add_configure_command(${name})
  1654. _ep_add_build_command(${name})
  1655. _ep_add_install_command(${name})
  1656. # Test is special in that it might depend on build, or it might depend
  1657. # on install.
  1658. #
  1659. _ep_add_test_command(${name})
  1660. endfunction()