1
0

ExternalProject.cmake 44 KB

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