ExternalProject.cmake 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. # - Create custom targets to build projects in external trees
  2. # The 'ExternalProject_Add' function creates a custom target to drive
  3. # download, update/patch, configure, build, install and test steps of an
  4. # external project:
  5. # ExternalProject_Add(<name> # Name for custom target
  6. # [DEPENDS projects...] # Targets on which the project depends
  7. # [PREFIX dir] # Root dir for entire project
  8. # [LIST_SEPARATOR sep] # Sep to be replaced by ; in cmd lines
  9. # [TMP_DIR dir] # Directory to store temporary files
  10. # [STAMP_DIR dir] # Directory to store step timestamps
  11. # #--Download step--------------
  12. # [DOWNLOAD_DIR dir] # Directory to store downloaded files
  13. # [DOWNLOAD_COMMAND cmd...] # Command to download source tree
  14. # [CVS_REPOSITORY cvsroot] # CVSROOT of CVS repository
  15. # [CVS_MODULE mod] # Module to checkout from CVS repo
  16. # [CVS_TAG tag] # Tag to checkout from CVS repo
  17. # [SVN_REPOSITORY url] # URL of Subversion repo
  18. # [SVN_REVISION rev] # Revision to checkout from Subversion repo
  19. # [SVN_USERNAME john ] # Username for Subversion checkout and update
  20. # [SVN_PASSWORD doe ] # Password for Subversion checkout and update
  21. # [GIT_REPOSITORY url] # URL of git repo
  22. # [GIT_TAG tag] # Git branch name, commit id or tag
  23. # [URL /.../src.tgz] # Full path or URL of source
  24. # [URL_MD5 md5] # MD5 checksum of file at URL
  25. # [TIMEOUT seconds] # Time allowed for file download operations
  26. # #--Update/Patch step----------
  27. # [UPDATE_COMMAND cmd...] # Source work-tree update command
  28. # [PATCH_COMMAND cmd...] # Command to patch downloaded source
  29. # #--Configure step-------------
  30. # [SOURCE_DIR dir] # Source dir to be used for build
  31. # [CONFIGURE_COMMAND cmd...] # Build tree configuration command
  32. # [CMAKE_COMMAND /.../cmake] # Specify alternative cmake executable
  33. # [CMAKE_GENERATOR gen] # Specify generator for native build
  34. # [CMAKE_ARGS args...] # Arguments to CMake command line
  35. # #--Build step-----------------
  36. # [BINARY_DIR dir] # Specify build dir location
  37. # [BUILD_COMMAND cmd...] # Command to drive the native build
  38. # [BUILD_IN_SOURCE 1] # Use source dir for build dir
  39. # #--Install step---------------
  40. # [INSTALL_DIR dir] # Installation prefix
  41. # [INSTALL_COMMAND cmd...] # Command to drive install after build
  42. # #--Test step------------------
  43. # [TEST_BEFORE_INSTALL 1] # Add test step executed before install step
  44. # [TEST_AFTER_INSTALL 1] # Add test step executed after install step
  45. # [TEST_COMMAND cmd...] # Command to drive test
  46. # #--Output logging-------------
  47. # [LOG_DOWNLOAD 1] # Wrap download in script to log output
  48. # [LOG_UPDATE 1] # Wrap update in script to log output
  49. # [LOG_CONFIGURE 1] # Wrap configure in script to log output
  50. # [LOG_BUILD 1] # Wrap build in script to log output
  51. # [LOG_TEST 1] # Wrap test in script to log output
  52. # [LOG_INSTALL 1] # Wrap install in script to log output
  53. # #--Custom targets-------------
  54. # [STEP_TARGETS st1 st2 ...] # Generate custom targets for these steps
  55. # )
  56. # The *_DIR options specify directories for the project, with default
  57. # directories computed as follows.
  58. # If the PREFIX option is given to ExternalProject_Add() or the EP_PREFIX
  59. # directory property is set, then an external project is built and installed
  60. # under the specified prefix:
  61. # TMP_DIR = <prefix>/tmp
  62. # STAMP_DIR = <prefix>/src/<name>-stamp
  63. # DOWNLOAD_DIR = <prefix>/src
  64. # SOURCE_DIR = <prefix>/src/<name>
  65. # BINARY_DIR = <prefix>/src/<name>-build
  66. # INSTALL_DIR = <prefix>
  67. # Otherwise, if the EP_BASE directory property is set then components
  68. # of an external project are stored under the specified base:
  69. # TMP_DIR = <base>/tmp/<name>
  70. # STAMP_DIR = <base>/Stamp/<name>
  71. # DOWNLOAD_DIR = <base>/Download/<name>
  72. # SOURCE_DIR = <base>/Source/<name>
  73. # BINARY_DIR = <base>/Build/<name>
  74. # INSTALL_DIR = <base>/Install/<name>
  75. # If no PREFIX, EP_PREFIX, or EP_BASE is specified then the default
  76. # is to set PREFIX to "<name>-prefix".
  77. # Relative paths are interpreted with respect to the build directory
  78. # corresponding to the source directory in which ExternalProject_Add is
  79. # invoked.
  80. #
  81. # If SOURCE_DIR is explicitly set to an existing directory the project
  82. # will be built from it.
  83. # Otherwise a download step must be specified using one of the
  84. # DOWNLOAD_COMMAND, CVS_*, SVN_*, or URL options.
  85. # The URL option may refer locally to a directory or source tarball,
  86. # or refer to a remote tarball (e.g. http://.../src.tgz).
  87. #
  88. # The 'ExternalProject_Add_Step' function adds a custom step to an external
  89. # project:
  90. # ExternalProject_Add_Step(<name> <step> # Names of project and custom step
  91. # [COMMAND cmd...] # Command line invoked by this step
  92. # [COMMENT "text..."] # Text printed when step executes
  93. # [DEPENDEES steps...] # Steps on which this step depends
  94. # [DEPENDERS steps...] # Steps that depend on this step
  95. # [DEPENDS files...] # Files on which this step depends
  96. # [ALWAYS 1] # No stamp file, step always runs
  97. # [WORKING_DIRECTORY dir] # Working directory for command
  98. # [LOG 1] # Wrap step in script to log output
  99. # )
  100. # The command line, comment, and working directory of every standard
  101. # and custom step is processed to replace tokens
  102. # <SOURCE_DIR>,
  103. # <BINARY_DIR>,
  104. # <INSTALL_DIR>,
  105. # and <TMP_DIR>
  106. # with corresponding property values.
  107. #
  108. # The 'ExternalProject_Get_Property' function retrieves external project
  109. # target properties:
  110. # ExternalProject_Get_Property(<name> [prop1 [prop2 [...]]])
  111. # It stores property values in variables of the same name.
  112. # Property names correspond to the keyword argument names of
  113. # 'ExternalProject_Add'.
  114. #
  115. # The 'ExternalProject_Add_StepTargets' function generates custom targets for
  116. # the steps listed:
  117. # ExternalProject_Add_StepTargets(<name> [step1 [step2 [...]]])
  118. #
  119. # If STEP_TARGETS is set then ExternalProject_Add_StepTargets is automatically
  120. # called at the end of matching calls to ExternalProject_Add_Step. Pass
  121. # STEP_TARGETS explicitly to individual ExternalProject_Add calls, or
  122. # implicitly to all ExternalProject_Add calls by setting the directory property
  123. # EP_STEP_TARGETS.
  124. #
  125. # If STEP_TARGETS is not set, clients may still manually call
  126. # ExternalProject_Add_StepTargets after calling ExternalProject_Add or
  127. # ExternalProject_Add_Step.
  128. #
  129. # This functionality is provided to make it easy to drive the steps
  130. # independently of each other by specifying targets on build command lines.
  131. # For example, you may be submitting to a sub-project based dashboard, where
  132. # you want to drive the configure portion of the build, then submit to the
  133. # dashboard, followed by the build portion, followed by tests. If you invoke
  134. # a custom target that depends on a step halfway through the step dependency
  135. # chain, then all the previous steps will also run to ensure everything is
  136. # up to date.
  137. #
  138. # For example, to drive configure, build and test steps independently for each
  139. # ExternalProject_Add call in your project, write the following line prior to
  140. # any ExternalProject_Add calls in your CMakeLists file:
  141. #
  142. # set_property(DIRECTORY PROPERTY EP_STEP_TARGETS configure build test)
  143. #=============================================================================
  144. # Copyright 2008-2009 Kitware, Inc.
  145. #
  146. # Distributed under the OSI-approved BSD License (the "License");
  147. # see accompanying file Copyright.txt for details.
  148. #
  149. # This software is distributed WITHOUT ANY WARRANTY; without even the
  150. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  151. # See the License for more information.
  152. #=============================================================================
  153. # (To distribute this file outside of CMake, substitute the full
  154. # License text for the above reference.)
  155. # Pre-compute a regex to match documented keywords for each command.
  156. math(EXPR _ep_documentation_line_count "${CMAKE_CURRENT_LIST_LINE} - 16")
  157. file(STRINGS "${CMAKE_CURRENT_LIST_FILE}" lines
  158. LIMIT_COUNT ${_ep_documentation_line_count}
  159. REGEX "^# ( \\[[A-Z0-9_]+ [^]]*\\] +#.*$|[A-Za-z0-9_]+\\()")
  160. foreach(line IN LISTS lines)
  161. if("${line}" MATCHES "^# [A-Za-z0-9_]+\\(")
  162. if(_ep_func)
  163. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  164. endif()
  165. string(REGEX REPLACE "^# ([A-Za-z0-9_]+)\\(.*" "\\1" _ep_func "${line}")
  166. #message("function [${_ep_func}]")
  167. set(_ep_keywords_${_ep_func} "^(")
  168. set(_ep_keyword_sep)
  169. else()
  170. string(REGEX REPLACE "^# \\[([A-Z0-9_]+) .*" "\\1" _ep_key "${line}")
  171. #message(" keyword [${_ep_key}]")
  172. set(_ep_keywords_${_ep_func}
  173. "${_ep_keywords_${_ep_func}}${_ep_keyword_sep}${_ep_key}")
  174. set(_ep_keyword_sep "|")
  175. endif()
  176. endforeach()
  177. if(_ep_func)
  178. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  179. endif()
  180. function(_ep_parse_arguments f name ns args)
  181. # Transfer the arguments to this function into target properties for the
  182. # new custom target we just added so that we can set up all the build steps
  183. # correctly based on target properties.
  184. #
  185. # We loop through ARGN and consider the namespace starting with an
  186. # upper-case letter followed by at least two more upper-case letters,
  187. # numbers or underscores to be keywords.
  188. set(key)
  189. foreach(arg IN LISTS args)
  190. set(is_value 1)
  191. if(arg MATCHES "^[A-Z][A-Z0-9_][A-Z0-9_]+$" AND
  192. NOT ((arg STREQUAL "${key}") AND (key STREQUAL "COMMAND")) AND
  193. NOT arg MATCHES "^(TRUE|FALSE)$")
  194. if(_ep_keywords_${f} AND arg MATCHES "${_ep_keywords_${f}}")
  195. set(is_value 0)
  196. endif()
  197. endif()
  198. if(is_value)
  199. if(key)
  200. # Value
  201. if(NOT arg STREQUAL "")
  202. set_property(TARGET ${name} APPEND PROPERTY ${ns}${key} "${arg}")
  203. else()
  204. get_property(have_key TARGET ${name} PROPERTY ${ns}${key} SET)
  205. if(have_key)
  206. get_property(value TARGET ${name} PROPERTY ${ns}${key})
  207. set_property(TARGET ${name} PROPERTY ${ns}${key} "${value};${arg}")
  208. else()
  209. set_property(TARGET ${name} PROPERTY ${ns}${key} "${arg}")
  210. endif()
  211. endif()
  212. else()
  213. # Missing Keyword
  214. message(AUTHOR_WARNING "value '${arg}' with no previous keyword in ${f}")
  215. endif()
  216. else()
  217. set(key "${arg}")
  218. endif()
  219. endforeach()
  220. endfunction(_ep_parse_arguments)
  221. define_property(DIRECTORY PROPERTY "EP_BASE" INHERITED
  222. BRIEF_DOCS "Base directory for External Project storage."
  223. FULL_DOCS
  224. "See documentation of the ExternalProject_Add() function in the "
  225. "ExternalProject module."
  226. )
  227. define_property(DIRECTORY PROPERTY "EP_PREFIX" INHERITED
  228. BRIEF_DOCS "Top prefix for External Project storage."
  229. FULL_DOCS
  230. "See documentation of the ExternalProject_Add() function in the "
  231. "ExternalProject module."
  232. )
  233. define_property(DIRECTORY PROPERTY "EP_STEP_TARGETS" INHERITED
  234. BRIEF_DOCS
  235. "List of ExternalProject steps that automatically get corresponding targets"
  236. FULL_DOCS
  237. "See documentation of the ExternalProject_Add_StepTargets() function in the "
  238. "ExternalProject module."
  239. )
  240. function(_ep_write_gitclone_script script_filename source_dir git_EXECUTABLE git_repository git_tag src_name work_dir)
  241. file(WRITE ${script_filename}
  242. "if(\"${git_tag}\" STREQUAL \"\")
  243. message(FATAL_ERROR \"Tag for git checkout should not be empty.\")
  244. endif()
  245. execute_process(
  246. COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
  247. RESULT_VARIABLE error_code
  248. )
  249. if(error_code)
  250. message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
  251. endif()
  252. execute_process(
  253. COMMAND \"${git_EXECUTABLE}\" clone \"${git_repository}\" \"${src_name}\"
  254. WORKING_DIRECTORY \"${work_dir}\"
  255. RESULT_VARIABLE error_code
  256. )
  257. if(error_code)
  258. message(FATAL_ERROR \"Failed to clone repository: '${git_repository}'\")
  259. endif()
  260. execute_process(
  261. COMMAND \"${git_EXECUTABLE}\" checkout ${git_tag}
  262. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  263. RESULT_VARIABLE error_code
  264. )
  265. if(error_code)
  266. message(FATAL_ERROR \"Failed to checkout tag: '${git_tag}'\")
  267. endif()
  268. execute_process(
  269. COMMAND \"${git_EXECUTABLE}\" submodule init
  270. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  271. RESULT_VARIABLE error_code
  272. )
  273. if(error_code)
  274. message(FATAL_ERROR \"Failed to init submodules in: '${work_dir}/${src_name}'\")
  275. endif()
  276. execute_process(
  277. COMMAND \"${git_EXECUTABLE}\" submodule update --recursive
  278. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  279. RESULT_VARIABLE error_code
  280. )
  281. if(error_code)
  282. message(FATAL_ERROR \"Failed to update submodules in: '${work_dir}/${src_name}'\")
  283. endif()
  284. "
  285. )
  286. endfunction(_ep_write_gitclone_script)
  287. function(_ep_write_downloadfile_script script_filename remote local timeout md5)
  288. if(timeout)
  289. set(timeout_args TIMEOUT ${timeout})
  290. set(timeout_msg "${timeout} seconds")
  291. else()
  292. set(timeout_args "# no TIMEOUT")
  293. set(timeout_msg "none")
  294. endif()
  295. if(md5)
  296. set(md5_args EXPECTED_MD5 ${md5})
  297. else()
  298. set(md5_args "# no EXPECTED_MD5")
  299. endif()
  300. file(WRITE ${script_filename}
  301. "message(STATUS \"downloading...
  302. src='${remote}'
  303. dst='${local}'
  304. timeout='${timeout_msg}'\")
  305. file(DOWNLOAD
  306. \"${remote}\"
  307. \"${local}\"
  308. SHOW_PROGRESS
  309. ${md5_args}
  310. ${timeout_args}
  311. STATUS status
  312. LOG log)
  313. list(GET status 0 status_code)
  314. list(GET status 1 status_string)
  315. if(NOT status_code EQUAL 0)
  316. message(FATAL_ERROR \"error: downloading '${remote}' failed
  317. status_code: \${status_code}
  318. status_string: \${status_string}
  319. log: \${log}
  320. \")
  321. endif()
  322. message(STATUS \"downloading... done\")
  323. "
  324. )
  325. endfunction(_ep_write_downloadfile_script)
  326. function(_ep_write_verifyfile_script script_filename local md5)
  327. file(WRITE ${script_filename}
  328. "message(STATUS \"verifying file...
  329. file='${local}'\")
  330. set(verified 0)
  331. # If an expected md5 checksum exists, compare against it:
  332. #
  333. if(NOT \"${md5}\" STREQUAL \"\")
  334. execute_process(COMMAND \${CMAKE_COMMAND} -E md5sum \"${local}\"
  335. OUTPUT_VARIABLE ov
  336. OUTPUT_STRIP_TRAILING_WHITESPACE
  337. RESULT_VARIABLE rv)
  338. if(NOT rv EQUAL 0)
  339. message(FATAL_ERROR \"error: computing md5sum of '${local}' failed\")
  340. endif()
  341. string(REGEX MATCH \"^([0-9A-Fa-f]+)\" md5_actual \"\${ov}\")
  342. string(TOLOWER \"\${md5_actual}\" md5_actual)
  343. string(TOLOWER \"${md5}\" md5)
  344. if(NOT \"\${md5}\" STREQUAL \"\${md5_actual}\")
  345. message(FATAL_ERROR \"error: md5sum of '${local}' does not match expected value
  346. md5_expected: \${md5}
  347. md5_actual: \${md5_actual}
  348. \")
  349. endif()
  350. set(verified 1)
  351. endif()
  352. if(verified)
  353. message(STATUS \"verifying file... done\")
  354. else()
  355. message(STATUS \"verifying file... warning: did not verify file - no URL_MD5 checksum argument? corrupt file?\")
  356. endif()
  357. "
  358. )
  359. endfunction(_ep_write_verifyfile_script)
  360. function(_ep_write_extractfile_script script_filename name filename directory)
  361. set(args "")
  362. if(filename MATCHES "(\\.bz2|\\.tar\\.gz|\\.tgz|\\.zip)$")
  363. set(args xfz)
  364. endif()
  365. if(filename MATCHES "\\.tar$")
  366. set(args xf)
  367. endif()
  368. if(args STREQUAL "")
  369. message(SEND_ERROR "error: do not know how to extract '${filename}' -- known types are .bz2, .tar, .tar.gz, .tgz and .zip")
  370. return()
  371. endif()
  372. file(WRITE ${script_filename}
  373. "# Make file names absolute:
  374. #
  375. get_filename_component(filename \"${filename}\" ABSOLUTE)
  376. get_filename_component(directory \"${directory}\" ABSOLUTE)
  377. message(STATUS \"extracting...
  378. src='\${filename}'
  379. dst='\${directory}'\")
  380. if(NOT EXISTS \"\${filename}\")
  381. message(FATAL_ERROR \"error: file to extract does not exist: '\${filename}'\")
  382. endif()
  383. # Prepare a space for extracting:
  384. #
  385. set(i 1234)
  386. while(EXISTS \"\${directory}/../ex-${name}\${i}\")
  387. math(EXPR i \"\${i} + 1\")
  388. endwhile()
  389. set(ut_dir \"\${directory}/../ex-${name}\${i}\")
  390. file(MAKE_DIRECTORY \"\${ut_dir}\")
  391. # Extract it:
  392. #
  393. message(STATUS \"extracting... [tar ${args}]\")
  394. execute_process(COMMAND \${CMAKE_COMMAND} -E tar ${args} \${filename}
  395. WORKING_DIRECTORY \${ut_dir}
  396. RESULT_VARIABLE rv)
  397. if(NOT rv EQUAL 0)
  398. message(STATUS \"extracting... [error clean up]\")
  399. file(REMOVE_RECURSE \"\${ut_dir}\")
  400. message(FATAL_ERROR \"error: extract of '\${filename}' failed\")
  401. endif()
  402. # Analyze what came out of the tar file:
  403. #
  404. message(STATUS \"extracting... [analysis]\")
  405. file(GLOB contents \"\${ut_dir}/*\")
  406. list(LENGTH contents n)
  407. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY \"\${contents}\")
  408. set(contents \"\${ut_dir}\")
  409. endif()
  410. # Move \"the one\" directory to the final directory:
  411. #
  412. message(STATUS \"extracting... [rename]\")
  413. file(REMOVE_RECURSE \${directory})
  414. get_filename_component(contents \${contents} ABSOLUTE)
  415. file(RENAME \${contents} \${directory})
  416. # Clean up:
  417. #
  418. message(STATUS \"extracting... [clean up]\")
  419. file(REMOVE_RECURSE \"\${ut_dir}\")
  420. message(STATUS \"extracting... done\")
  421. "
  422. )
  423. endfunction(_ep_write_extractfile_script)
  424. function(_ep_set_directories name)
  425. get_property(prefix TARGET ${name} PROPERTY _EP_PREFIX)
  426. if(NOT prefix)
  427. get_property(prefix DIRECTORY PROPERTY EP_PREFIX)
  428. if(NOT prefix)
  429. get_property(base DIRECTORY PROPERTY EP_BASE)
  430. if(NOT base)
  431. set(prefix "${name}-prefix")
  432. endif()
  433. endif()
  434. endif()
  435. if(prefix)
  436. set(tmp_default "${prefix}/tmp")
  437. set(download_default "${prefix}/src")
  438. set(source_default "${prefix}/src/${name}")
  439. set(binary_default "${prefix}/src/${name}-build")
  440. set(stamp_default "${prefix}/src/${name}-stamp")
  441. set(install_default "${prefix}")
  442. else() # assert(base)
  443. set(tmp_default "${base}/tmp/${name}")
  444. set(download_default "${base}/Download/${name}")
  445. set(source_default "${base}/Source/${name}")
  446. set(binary_default "${base}/Build/${name}")
  447. set(stamp_default "${base}/Stamp/${name}")
  448. set(install_default "${base}/Install/${name}")
  449. endif()
  450. get_property(build_in_source TARGET ${name} PROPERTY _EP_BUILD_IN_SOURCE)
  451. if(build_in_source)
  452. get_property(have_binary_dir TARGET ${name} PROPERTY _EP_BINARY_DIR SET)
  453. if(have_binary_dir)
  454. message(FATAL_ERROR
  455. "External project ${name} has both BINARY_DIR and BUILD_IN_SOURCE!")
  456. endif()
  457. endif()
  458. set(top "${CMAKE_CURRENT_BINARY_DIR}")
  459. set(places stamp download source binary install tmp)
  460. foreach(var ${places})
  461. string(TOUPPER "${var}" VAR)
  462. get_property(${var}_dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  463. if(NOT ${var}_dir)
  464. set(${var}_dir "${${var}_default}")
  465. endif()
  466. if(NOT IS_ABSOLUTE "${${var}_dir}")
  467. get_filename_component(${var}_dir "${top}/${${var}_dir}" ABSOLUTE)
  468. endif()
  469. set_property(TARGET ${name} PROPERTY _EP_${VAR}_DIR "${${var}_dir}")
  470. endforeach()
  471. if(build_in_source)
  472. get_property(source_dir TARGET ${name} PROPERTY _EP_SOURCE_DIR)
  473. set_property(TARGET ${name} PROPERTY _EP_BINARY_DIR "${source_dir}")
  474. endif()
  475. # Make the directories at CMake configure time *and* add a custom command
  476. # to make them at build time. They need to exist at makefile generation
  477. # time for Borland make and wmake so that CMake may generate makefiles
  478. # with "cd C:\short\paths\with\no\spaces" commands in them.
  479. #
  480. # Additionally, the add_custom_command is still used in case somebody
  481. # removes one of the necessary directories and tries to rebuild without
  482. # re-running cmake.
  483. foreach(var ${places})
  484. string(TOUPPER "${var}" VAR)
  485. get_property(dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  486. file(MAKE_DIRECTORY "${dir}")
  487. if(NOT EXISTS "${dir}")
  488. message(FATAL_ERROR "dir '${dir}' does not exist after file(MAKE_DIRECTORY)")
  489. endif()
  490. endforeach()
  491. endfunction(_ep_set_directories)
  492. function(ExternalProject_Get_Property name)
  493. foreach(var ${ARGN})
  494. string(TOUPPER "${var}" VAR)
  495. get_property(${var} TARGET ${name} PROPERTY _EP_${VAR})
  496. if(NOT ${var})
  497. message(FATAL_ERROR "External project \"${name}\" has no ${var}")
  498. endif()
  499. set(${var} "${${var}}" PARENT_SCOPE)
  500. endforeach()
  501. endfunction(ExternalProject_Get_Property)
  502. function(_ep_get_configure_command_id name cfg_cmd_id_var)
  503. get_target_property(cmd ${name} _EP_CONFIGURE_COMMAND)
  504. if(cmd STREQUAL "")
  505. # Explicit empty string means no configure step for this project
  506. set(${cfg_cmd_id_var} "none" PARENT_SCOPE)
  507. else()
  508. if(NOT cmd)
  509. # Default is "use cmake":
  510. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  511. else()
  512. # Otherwise we have to analyze the value:
  513. if(cmd MATCHES "^[^;]*/configure")
  514. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  515. elseif(cmd MATCHES "^[^;]*/cmake" AND NOT cmd MATCHES ";-[PE];")
  516. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  517. elseif(cmd MATCHES "config")
  518. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  519. else()
  520. set(${cfg_cmd_id_var} "unknown:${cmd}" PARENT_SCOPE)
  521. endif()
  522. endif()
  523. endif()
  524. endfunction(_ep_get_configure_command_id)
  525. function(_ep_get_build_command name step cmd_var)
  526. set(cmd "${${cmd_var}}")
  527. if(NOT cmd)
  528. set(args)
  529. _ep_get_configure_command_id(${name} cfg_cmd_id)
  530. if(cfg_cmd_id STREQUAL "cmake")
  531. # CMake project. Select build command based on generator.
  532. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  533. if("${CMAKE_GENERATOR}" MATCHES "Make" AND
  534. ("${cmake_generator}" MATCHES "Make" OR NOT cmake_generator))
  535. # The project uses the same Makefile generator. Use recursive make.
  536. set(cmd "$(MAKE)")
  537. if(step STREQUAL "INSTALL")
  538. set(args install)
  539. endif()
  540. if(step STREQUAL "TEST")
  541. set(args test)
  542. endif()
  543. else()
  544. # Drive the project with "cmake --build".
  545. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  546. if(cmake_command)
  547. set(cmd "${cmake_command}")
  548. else()
  549. set(cmd "${CMAKE_COMMAND}")
  550. endif()
  551. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  552. if(step STREQUAL "INSTALL")
  553. list(APPEND args --target install)
  554. endif()
  555. # But for "TEST" drive the project with corresponding "ctest".
  556. if(step STREQUAL "TEST")
  557. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  558. set(args "")
  559. endif()
  560. endif()
  561. else() # if(cfg_cmd_id STREQUAL "configure")
  562. # Non-CMake project. Guess "make" and "make install" and "make test".
  563. # But use "$(MAKE)" to get recursive parallel make.
  564. set(cmd "$(MAKE)")
  565. if(step STREQUAL "INSTALL")
  566. set(args install)
  567. endif()
  568. if(step STREQUAL "TEST")
  569. set(args test)
  570. endif()
  571. endif()
  572. # Use user-specified arguments instead of default arguments, if any.
  573. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  574. if(have_args)
  575. get_target_property(args ${name} _EP_${step}_ARGS)
  576. endif()
  577. list(APPEND cmd ${args})
  578. endif()
  579. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  580. endfunction(_ep_get_build_command)
  581. function(_ep_write_log_script name step cmd_var)
  582. ExternalProject_Get_Property(${name} stamp_dir)
  583. set(command "${${cmd_var}}")
  584. set(make "")
  585. set(code_cygpath_make "")
  586. if("${command}" MATCHES "^\\$\\(MAKE\\)")
  587. # GNU make recognizes the string "$(MAKE)" as recursive make, so
  588. # ensure that it appears directly in the makefile.
  589. string(REGEX REPLACE "^\\$\\(MAKE\\)" "\${make}" command "${command}")
  590. set(make "-Dmake=$(MAKE)")
  591. if(WIN32 AND NOT CYGWIN)
  592. set(code_cygpath_make "
  593. if(\${make} MATCHES \"^/\")
  594. execute_process(
  595. COMMAND cygpath -w \${make}
  596. OUTPUT_VARIABLE cygpath_make
  597. ERROR_VARIABLE cygpath_make
  598. RESULT_VARIABLE cygpath_error
  599. OUTPUT_STRIP_TRAILING_WHITESPACE
  600. )
  601. if(NOT cygpath_error)
  602. set(make \${cygpath_make})
  603. endif()
  604. endif()
  605. ")
  606. endif()
  607. endif()
  608. set(config "")
  609. if("${CMAKE_CFG_INTDIR}" MATCHES "^\\$")
  610. string(REPLACE "${CMAKE_CFG_INTDIR}" "\${config}" command "${command}")
  611. set(config "-Dconfig=${CMAKE_CFG_INTDIR}")
  612. endif()
  613. # Wrap multiple 'COMMAND' lines up into a second-level wrapper
  614. # script so all output can be sent to one log file.
  615. if("${command}" MATCHES ";COMMAND;")
  616. set(code_execute_process "
  617. ${code_cygpath_make}
  618. execute_process(COMMAND \${command} RESULT_VARIABLE result)
  619. if(result)
  620. set(msg \"Command failed (\${result}):\\n\")
  621. foreach(arg IN LISTS command)
  622. set(msg \"\${msg} '\${arg}'\")
  623. endforeach(arg)
  624. message(FATAL_ERROR \"\${msg}\")
  625. endif()
  626. ")
  627. set(code "")
  628. set(cmd "")
  629. set(sep "")
  630. foreach(arg IN LISTS command)
  631. if("x${arg}" STREQUAL "xCOMMAND")
  632. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  633. set(cmd "")
  634. set(sep "")
  635. else()
  636. set(cmd "${cmd}${sep}${arg}")
  637. set(sep ";")
  638. endif()
  639. endforeach()
  640. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  641. file(WRITE ${stamp_dir}/${name}-${step}-impl.cmake "${code}")
  642. set(command ${CMAKE_COMMAND} "-Dmake=\${make}" "-Dconfig=\${config}" -P ${stamp_dir}/${name}-${step}-impl.cmake)
  643. endif()
  644. # Wrap the command in a script to log output to files.
  645. set(script ${stamp_dir}/${name}-${step}.cmake)
  646. set(logbase ${stamp_dir}/${name}-${step})
  647. file(WRITE ${script} "
  648. ${code_cygpath_make}
  649. set(command \"${command}\")
  650. execute_process(
  651. COMMAND \${command}
  652. RESULT_VARIABLE result
  653. OUTPUT_FILE \"${logbase}-out.log\"
  654. ERROR_FILE \"${logbase}-err.log\"
  655. )
  656. if(result)
  657. set(msg \"Command failed: \${result}\\n\")
  658. foreach(arg IN LISTS command)
  659. set(msg \"\${msg} '\${arg}'\")
  660. endforeach(arg)
  661. set(msg \"\${msg}\\nSee also\\n ${logbase}-*.log\\n\")
  662. message(FATAL_ERROR \"\${msg}\")
  663. else()
  664. set(msg \"${name} ${step} command succeeded. See also ${logbase}-*.log\\n\")
  665. message(STATUS \"\${msg}\")
  666. endif()
  667. ")
  668. set(command ${CMAKE_COMMAND} ${make} ${config} -P ${script})
  669. set(${cmd_var} "${command}" PARENT_SCOPE)
  670. endfunction(_ep_write_log_script)
  671. # This module used to use "/${CMAKE_CFG_INTDIR}" directly and produced
  672. # makefiles with "/./" in paths for custom command dependencies. Which
  673. # resulted in problems with parallel make -j invocations.
  674. #
  675. # This function was added so that the suffix (search below for ${cfgdir}) is
  676. # only set to "/${CMAKE_CFG_INTDIR}" when ${CMAKE_CFG_INTDIR} is not going to
  677. # be "." (multi-configuration build systems like Visual Studio and Xcode...)
  678. #
  679. function(_ep_get_configuration_subdir_suffix suffix_var)
  680. set(suffix "")
  681. if(CMAKE_CONFIGURATION_TYPES)
  682. set(suffix "/${CMAKE_CFG_INTDIR}")
  683. endif()
  684. set(${suffix_var} "${suffix}" PARENT_SCOPE)
  685. endfunction(_ep_get_configuration_subdir_suffix)
  686. function(ExternalProject_Add_StepTargets name)
  687. set(steps ${ARGN})
  688. _ep_get_configuration_subdir_suffix(cfgdir)
  689. ExternalProject_Get_Property(${name} stamp_dir)
  690. foreach(step ${steps})
  691. add_custom_target(${name}-${step}
  692. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step})
  693. endforeach()
  694. endfunction(ExternalProject_Add_StepTargets)
  695. function(ExternalProject_Add_Step name step)
  696. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  697. ExternalProject_Get_Property(${name} stamp_dir)
  698. _ep_get_configuration_subdir_suffix(cfgdir)
  699. add_custom_command(APPEND
  700. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  701. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  702. )
  703. _ep_parse_arguments(ExternalProject_Add_Step
  704. ${name} _EP_${step}_ "${ARGN}")
  705. # Steps depending on this step.
  706. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  707. foreach(depender IN LISTS dependers)
  708. add_custom_command(APPEND
  709. OUTPUT ${stamp_dir}${cfgdir}/${name}-${depender}
  710. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  711. )
  712. endforeach()
  713. # Dependencies on files.
  714. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  715. # Dependencies on steps.
  716. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  717. foreach(dependee IN LISTS dependees)
  718. list(APPEND depends ${stamp_dir}${cfgdir}/${name}-${dependee})
  719. endforeach()
  720. # The command to run.
  721. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  722. if(command)
  723. set(comment "Performing ${step} step for '${name}'")
  724. else()
  725. set(comment "No ${step} step for '${name}'")
  726. endif()
  727. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  728. # Replace list separators.
  729. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  730. if(sep AND command)
  731. string(REPLACE "${sep}" "\\;" command "${command}")
  732. endif()
  733. # Replace location tags.
  734. foreach(var comment command work_dir)
  735. if(${var})
  736. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  737. get_property(val TARGET ${name} PROPERTY _EP_${dir})
  738. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  739. endforeach()
  740. endif()
  741. endforeach()
  742. # Custom comment?
  743. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  744. if(comment_set)
  745. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  746. endif()
  747. # Run every time?
  748. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  749. if(always)
  750. set_property(SOURCE ${stamp_dir}${cfgdir}/${name}-${step} PROPERTY SYMBOLIC 1)
  751. set(touch)
  752. else()
  753. set(touch ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-${step})
  754. endif()
  755. # Wrap with log script?
  756. get_property(log TARGET ${name} PROPERTY _EP_${step}_LOG)
  757. if(command AND log)
  758. _ep_write_log_script(${name} ${step} command)
  759. endif()
  760. add_custom_command(
  761. OUTPUT ${stamp_dir}${cfgdir}/${name}-${step}
  762. COMMENT ${comment}
  763. COMMAND ${command}
  764. COMMAND ${touch}
  765. DEPENDS ${depends}
  766. WORKING_DIRECTORY ${work_dir}
  767. VERBATIM
  768. )
  769. # Add custom "step target"?
  770. get_property(step_targets TARGET ${name} PROPERTY _EP_STEP_TARGETS)
  771. if(NOT step_targets)
  772. get_property(step_targets DIRECTORY PROPERTY EP_STEP_TARGETS)
  773. endif()
  774. foreach(st ${step_targets})
  775. if("${st}" STREQUAL "${step}")
  776. ExternalProject_Add_StepTargets(${name} ${step})
  777. break()
  778. endif()
  779. endforeach()
  780. endfunction(ExternalProject_Add_Step)
  781. function(_ep_add_mkdir_command name)
  782. ExternalProject_Get_Property(${name}
  783. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  784. _ep_get_configuration_subdir_suffix(cfgdir)
  785. ExternalProject_Add_Step(${name} mkdir
  786. COMMENT "Creating directories for '${name}'"
  787. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  788. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  789. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  790. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  791. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}${cfgdir}
  792. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  793. )
  794. endfunction(_ep_add_mkdir_command)
  795. function(_ep_get_git_version git_EXECUTABLE git_version_var)
  796. if(git_EXECUTABLE)
  797. execute_process(
  798. COMMAND "${git_EXECUTABLE}" --version
  799. OUTPUT_VARIABLE ov
  800. OUTPUT_STRIP_TRAILING_WHITESPACE
  801. )
  802. string(REGEX REPLACE "^git version (.+)$" "\\1" version "${ov}")
  803. set(${git_version_var} "${version}" PARENT_SCOPE)
  804. endif()
  805. endfunction()
  806. function(_ep_is_dir_empty dir empty_var)
  807. file(GLOB gr "${dir}/*")
  808. if("${gr}" STREQUAL "")
  809. set(${empty_var} 1 PARENT_SCOPE)
  810. else()
  811. set(${empty_var} 0 PARENT_SCOPE)
  812. endif()
  813. endfunction()
  814. function(_ep_add_download_command name)
  815. ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
  816. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  817. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  818. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  819. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  820. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  821. get_property(url TARGET ${name} PROPERTY _EP_URL)
  822. # TODO: Perhaps file:// should be copied to download dir before extraction.
  823. string(REGEX REPLACE "^file://" "" url "${url}")
  824. set(depends)
  825. set(comment)
  826. set(work_dir)
  827. if(cmd_set)
  828. set(work_dir ${download_dir})
  829. elseif(cvs_repository)
  830. find_package(CVS)
  831. if(NOT CVS_EXECUTABLE)
  832. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  833. endif()
  834. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  835. if(NOT cvs_module)
  836. message(FATAL_ERROR "error: no CVS_MODULE")
  837. endif()
  838. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  839. set(repository ${cvs_repository})
  840. set(module ${cvs_module})
  841. set(tag ${cvs_tag})
  842. configure_file(
  843. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  844. "${stamp_dir}/${name}-cvsinfo.txt"
  845. @ONLY
  846. )
  847. get_filename_component(src_name "${source_dir}" NAME)
  848. get_filename_component(work_dir "${source_dir}" PATH)
  849. set(comment "Performing download step (CVS checkout) for '${name}'")
  850. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  851. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  852. elseif(svn_repository)
  853. find_package(Subversion)
  854. if(NOT Subversion_SVN_EXECUTABLE)
  855. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  856. endif()
  857. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  858. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  859. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  860. set(repository "${svn_repository} user=${svn_username} password=${svn_password}")
  861. set(module)
  862. set(tag ${svn_revision})
  863. configure_file(
  864. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  865. "${stamp_dir}/${name}-svninfo.txt"
  866. @ONLY
  867. )
  868. get_filename_component(src_name "${source_dir}" NAME)
  869. get_filename_component(work_dir "${source_dir}" PATH)
  870. set(comment "Performing download step (SVN checkout) for '${name}'")
  871. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision}
  872. --username=${svn_username} --password=${svn_password} ${src_name})
  873. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  874. elseif(git_repository)
  875. find_package(Git)
  876. if(NOT GIT_EXECUTABLE)
  877. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  878. endif()
  879. # The git submodule update '--recursive' flag requires git >= v1.6.5
  880. #
  881. _ep_get_git_version("${GIT_EXECUTABLE}" git_version)
  882. if(git_version VERSION_LESS 1.6.5)
  883. message(FATAL_ERROR "error: git version 1.6.5 or later required for 'git submodule update --recursive': git_version='${git_version}'")
  884. endif()
  885. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  886. if(NOT git_tag)
  887. set(git_tag "master")
  888. endif()
  889. set(repository ${git_repository})
  890. set(module)
  891. set(tag ${git_tag})
  892. configure_file(
  893. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  894. "${stamp_dir}/${name}-gitinfo.txt"
  895. @ONLY
  896. )
  897. get_filename_component(src_name "${source_dir}" NAME)
  898. get_filename_component(work_dir "${source_dir}" PATH)
  899. # Since git clone doesn't succeed if the non-empty source_dir exists,
  900. # create a cmake script to invoke as download command.
  901. # The script will delete the source directory and then call git clone.
  902. #
  903. _ep_write_gitclone_script(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
  904. ${GIT_EXECUTABLE} ${git_repository} ${git_tag} ${src_name} ${work_dir}
  905. )
  906. set(comment "Performing download step (git clone) for '${name}'")
  907. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
  908. list(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
  909. elseif(url)
  910. get_filename_component(work_dir "${source_dir}" PATH)
  911. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  912. set(repository "external project URL")
  913. set(module "${url}")
  914. set(tag "${md5}")
  915. configure_file(
  916. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  917. "${stamp_dir}/${name}-urlinfo.txt"
  918. @ONLY
  919. )
  920. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  921. if(IS_DIRECTORY "${url}")
  922. get_filename_component(abs_dir "${url}" ABSOLUTE)
  923. set(comment "Performing download step (DIR copy) for '${name}'")
  924. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  925. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  926. else()
  927. if("${url}" MATCHES "^[a-z]+://")
  928. # TODO: Should download and extraction be different steps?
  929. string(REGEX MATCH "[^/]*$" fname "${url}")
  930. if(NOT "${fname}" MATCHES "\\.(bz2|tar|tgz|tar\\.gz|zip)$")
  931. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  932. endif()
  933. set(file ${download_dir}/${fname})
  934. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  935. _ep_write_downloadfile_script("${stamp_dir}/download-${name}.cmake" "${url}" "${file}" "${timeout}" "${md5}")
  936. set(cmd ${CMAKE_COMMAND} -P ${stamp_dir}/download-${name}.cmake
  937. COMMAND)
  938. set(comment "Performing download step (download, verify and extract) for '${name}'")
  939. else()
  940. set(file "${url}")
  941. set(comment "Performing download step (verify and extract) for '${name}'")
  942. endif()
  943. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${md5}")
  944. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake)
  945. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${name}" "${file}" "${source_dir}")
  946. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  947. endif()
  948. else()
  949. _ep_is_dir_empty("${source_dir}" empty)
  950. if(${empty})
  951. 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 or DOWNLOAD_COMMAND")
  952. endif()
  953. endif()
  954. get_property(log TARGET ${name} PROPERTY _EP_LOG_DOWNLOAD)
  955. if(log)
  956. set(log LOG 1)
  957. else()
  958. set(log "")
  959. endif()
  960. ExternalProject_Add_Step(${name} download
  961. COMMENT ${comment}
  962. COMMAND ${cmd}
  963. WORKING_DIRECTORY ${work_dir}
  964. DEPENDS ${depends}
  965. DEPENDEES mkdir
  966. ${log}
  967. )
  968. endfunction(_ep_add_download_command)
  969. function(_ep_add_update_command name)
  970. ExternalProject_Get_Property(${name} source_dir)
  971. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  972. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  973. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  974. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  975. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  976. set(work_dir)
  977. set(comment)
  978. set(always)
  979. if(cmd_set)
  980. set(work_dir ${source_dir})
  981. elseif(cvs_repository)
  982. if(NOT CVS_EXECUTABLE)
  983. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  984. endif()
  985. set(work_dir ${source_dir})
  986. set(comment "Performing update step (CVS update) for '${name}'")
  987. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  988. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  989. set(always 1)
  990. elseif(svn_repository)
  991. if(NOT Subversion_SVN_EXECUTABLE)
  992. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  993. endif()
  994. set(work_dir ${source_dir})
  995. set(comment "Performing update step (SVN update) for '${name}'")
  996. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  997. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  998. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  999. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision}
  1000. --username=${svn_username} --password=${svn_password})
  1001. set(always 1)
  1002. elseif(git_repository)
  1003. if(NOT GIT_EXECUTABLE)
  1004. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  1005. endif()
  1006. set(work_dir ${source_dir})
  1007. set(comment "Performing update step (git fetch) for '${name}'")
  1008. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1009. if(NOT git_tag)
  1010. set(git_tag "master")
  1011. endif()
  1012. set(cmd ${GIT_EXECUTABLE} fetch
  1013. COMMAND ${GIT_EXECUTABLE} checkout ${git_tag}
  1014. COMMAND ${GIT_EXECUTABLE} submodule update --recursive
  1015. )
  1016. set(always 1)
  1017. endif()
  1018. get_property(log TARGET ${name} PROPERTY _EP_LOG_UPDATE)
  1019. if(log)
  1020. set(log LOG 1)
  1021. else()
  1022. set(log "")
  1023. endif()
  1024. ExternalProject_Add_Step(${name} update
  1025. COMMENT ${comment}
  1026. COMMAND ${cmd}
  1027. ALWAYS ${always}
  1028. WORKING_DIRECTORY ${work_dir}
  1029. DEPENDEES download
  1030. ${log}
  1031. )
  1032. endfunction(_ep_add_update_command)
  1033. function(_ep_add_patch_command name)
  1034. ExternalProject_Get_Property(${name} source_dir)
  1035. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  1036. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  1037. set(work_dir)
  1038. if(cmd_set)
  1039. set(work_dir ${source_dir})
  1040. endif()
  1041. ExternalProject_Add_Step(${name} patch
  1042. COMMAND ${cmd}
  1043. WORKING_DIRECTORY ${work_dir}
  1044. DEPENDEES download
  1045. )
  1046. endfunction(_ep_add_patch_command)
  1047. # TODO: Make sure external projects use the proper compiler
  1048. function(_ep_add_configure_command name)
  1049. ExternalProject_Get_Property(${name} source_dir binary_dir tmp_dir)
  1050. _ep_get_configuration_subdir_suffix(cfgdir)
  1051. # Depend on other external projects (file-level).
  1052. set(file_deps)
  1053. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1054. foreach(dep IN LISTS deps)
  1055. get_property(dep_stamp_dir TARGET ${dep} PROPERTY _EP_STAMP_DIR)
  1056. list(APPEND file_deps ${dep_stamp_dir}${cfgdir}/${dep}-done)
  1057. endforeach()
  1058. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  1059. if(cmd_set)
  1060. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  1061. else()
  1062. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1063. if(cmake_command)
  1064. set(cmd "${cmake_command}")
  1065. else()
  1066. set(cmd "${CMAKE_COMMAND}")
  1067. endif()
  1068. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  1069. list(APPEND cmd ${cmake_args})
  1070. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1071. if(cmake_generator)
  1072. list(APPEND cmd "-G${cmake_generator}" "${source_dir}")
  1073. else()
  1074. list(APPEND cmd "-G${CMAKE_GENERATOR}" "${source_dir}")
  1075. endif()
  1076. endif()
  1077. # If anything about the configure command changes, (command itself, cmake
  1078. # used, cmake args or cmake generator) then re-run the configure step.
  1079. # Fixes issue http://public.kitware.com/Bug/view.php?id=10258
  1080. #
  1081. if(NOT EXISTS ${tmp_dir}/${name}-cfgcmd.txt.in)
  1082. file(WRITE ${tmp_dir}/${name}-cfgcmd.txt.in "cmd='@cmd@'\n")
  1083. endif()
  1084. configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt)
  1085. list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt)
  1086. get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE)
  1087. if(log)
  1088. set(log LOG 1)
  1089. else()
  1090. set(log "")
  1091. endif()
  1092. ExternalProject_Add_Step(${name} configure
  1093. COMMAND ${cmd}
  1094. WORKING_DIRECTORY ${binary_dir}
  1095. DEPENDEES update patch
  1096. DEPENDS ${file_deps}
  1097. ${log}
  1098. )
  1099. endfunction(_ep_add_configure_command)
  1100. function(_ep_add_build_command name)
  1101. ExternalProject_Get_Property(${name} binary_dir)
  1102. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  1103. if(cmd_set)
  1104. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  1105. else()
  1106. _ep_get_build_command(${name} BUILD cmd)
  1107. endif()
  1108. get_property(log TARGET ${name} PROPERTY _EP_LOG_BUILD)
  1109. if(log)
  1110. set(log LOG 1)
  1111. else()
  1112. set(log "")
  1113. endif()
  1114. ExternalProject_Add_Step(${name} build
  1115. COMMAND ${cmd}
  1116. WORKING_DIRECTORY ${binary_dir}
  1117. DEPENDEES configure
  1118. ${log}
  1119. )
  1120. endfunction(_ep_add_build_command)
  1121. function(_ep_add_install_command name)
  1122. ExternalProject_Get_Property(${name} binary_dir)
  1123. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  1124. if(cmd_set)
  1125. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  1126. else()
  1127. _ep_get_build_command(${name} INSTALL cmd)
  1128. endif()
  1129. get_property(log TARGET ${name} PROPERTY _EP_LOG_INSTALL)
  1130. if(log)
  1131. set(log LOG 1)
  1132. else()
  1133. set(log "")
  1134. endif()
  1135. ExternalProject_Add_Step(${name} install
  1136. COMMAND ${cmd}
  1137. WORKING_DIRECTORY ${binary_dir}
  1138. DEPENDEES build
  1139. ${log}
  1140. )
  1141. endfunction(_ep_add_install_command)
  1142. function(_ep_add_test_command name)
  1143. ExternalProject_Get_Property(${name} binary_dir)
  1144. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  1145. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  1146. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  1147. # Only actually add the test step if one of the test related properties is
  1148. # explicitly set. (i.e. the test step is omitted unless requested...)
  1149. #
  1150. if(cmd_set OR before OR after)
  1151. if(cmd_set)
  1152. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  1153. else()
  1154. _ep_get_build_command(${name} TEST cmd)
  1155. endif()
  1156. if(before)
  1157. set(dep_args DEPENDEES build DEPENDERS install)
  1158. else()
  1159. set(dep_args DEPENDEES install)
  1160. endif()
  1161. get_property(log TARGET ${name} PROPERTY _EP_LOG_TEST)
  1162. if(log)
  1163. set(log LOG 1)
  1164. else()
  1165. set(log "")
  1166. endif()
  1167. ExternalProject_Add_Step(${name} test
  1168. COMMAND ${cmd}
  1169. WORKING_DIRECTORY ${binary_dir}
  1170. ${dep_args}
  1171. ${log}
  1172. )
  1173. endif()
  1174. endfunction(_ep_add_test_command)
  1175. function(ExternalProject_Add name)
  1176. _ep_get_configuration_subdir_suffix(cfgdir)
  1177. # Add a custom target for the external project.
  1178. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  1179. add_custom_target(${name} ALL DEPENDS ${cmf_dir}${cfgdir}/${name}-complete)
  1180. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  1181. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  1182. _ep_set_directories(${name})
  1183. ExternalProject_Get_Property(${name} stamp_dir)
  1184. # The 'complete' step depends on all other steps and creates a
  1185. # 'done' mark. A dependent external project's 'configure' step
  1186. # depends on the 'done' mark so that it rebuilds when this project
  1187. # rebuilds. It is important that 'done' is not the output of any
  1188. # custom command so that CMake does not propagate build rules to
  1189. # other external project targets.
  1190. add_custom_command(
  1191. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  1192. COMMENT "Completed '${name}'"
  1193. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  1194. COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}${cfgdir}/${name}-complete
  1195. COMMAND ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-done
  1196. DEPENDS ${stamp_dir}${cfgdir}/${name}-install
  1197. VERBATIM
  1198. )
  1199. # Depend on other external projects (target-level).
  1200. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1201. foreach(arg IN LISTS deps)
  1202. add_dependencies(${name} ${arg})
  1203. endforeach()
  1204. # Set up custom build steps based on the target properties.
  1205. # Each step depends on the previous one.
  1206. #
  1207. # The target depends on the output of the final step.
  1208. # (Already set up above in the DEPENDS of the add_custom_target command.)
  1209. #
  1210. _ep_add_mkdir_command(${name})
  1211. _ep_add_download_command(${name})
  1212. _ep_add_update_command(${name})
  1213. _ep_add_patch_command(${name})
  1214. _ep_add_configure_command(${name})
  1215. _ep_add_build_command(${name})
  1216. _ep_add_install_command(${name})
  1217. # Test is special in that it might depend on build, or it might depend
  1218. # on install.
  1219. #
  1220. _ep_add_test_command(${name})
  1221. endfunction(ExternalProject_Add)