ExternalProject.cmake 67 KB

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