ExternalProject.cmake 44 KB

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