ExternalProject.cmake 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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}" OR
  495. NOT cmake_generator))
  496. # The project uses the same Makefile generator. Use recursive make.
  497. set(cmd "$(MAKE)")
  498. if(step STREQUAL "INSTALL")
  499. set(args install)
  500. endif()
  501. if(step STREQUAL "TEST")
  502. set(args test)
  503. endif()
  504. else()
  505. # Drive the project with "cmake --build".
  506. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  507. if(cmake_command)
  508. set(cmd "${cmake_command}")
  509. else()
  510. set(cmd "${CMAKE_COMMAND}")
  511. endif()
  512. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  513. if(step STREQUAL "INSTALL")
  514. list(APPEND args --target install)
  515. endif()
  516. # But for "TEST" drive the project with corresponding "ctest".
  517. if(step STREQUAL "TEST")
  518. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  519. set(args "")
  520. endif()
  521. endif()
  522. else() # if(cfg_cmd_id STREQUAL "configure")
  523. # Non-CMake project. Guess "make" and "make install" and "make test".
  524. # But use "$(MAKE)" to get recursive parallel make.
  525. set(cmd "$(MAKE)")
  526. if(step STREQUAL "INSTALL")
  527. set(args install)
  528. endif()
  529. if(step STREQUAL "TEST")
  530. set(args test)
  531. endif()
  532. endif()
  533. # Use user-specified arguments instead of default arguments, if any.
  534. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  535. if(have_args)
  536. get_target_property(args ${name} _EP_${step}_ARGS)
  537. endif()
  538. list(APPEND cmd ${args})
  539. endif()
  540. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  541. endfunction(_ep_get_build_command)
  542. function(_ep_write_log_script name step cmd_var)
  543. ExternalProject_Get_Property(${name} stamp_dir)
  544. set(command "${${cmd_var}}")
  545. set(make "")
  546. set(code_cygpath_make "")
  547. if("${command}" MATCHES "^\\$\\(MAKE\\)")
  548. # GNU make recognizes the string "$(MAKE)" as recursive make, so
  549. # ensure that it appears directly in the makefile.
  550. string(REGEX REPLACE "^\\$\\(MAKE\\)" "\${make}" command "${command}")
  551. set(make "-Dmake=$(MAKE)")
  552. if(WIN32 AND NOT CYGWIN)
  553. set(code_cygpath_make "
  554. if(\${make} MATCHES \"^/\")
  555. execute_process(
  556. COMMAND cygpath -w \${make}
  557. OUTPUT_VARIABLE cygpath_make
  558. ERROR_VARIABLE cygpath_make
  559. RESULT_VARIABLE cygpath_error
  560. OUTPUT_STRIP_TRAILING_WHITESPACE
  561. )
  562. if(NOT cygpath_error)
  563. set(make \${cygpath_make})
  564. endif()
  565. endif()
  566. ")
  567. endif()
  568. endif()
  569. set(config "")
  570. if("${CMAKE_CFG_INTDIR}" MATCHES "^\\$")
  571. string(REPLACE "${CMAKE_CFG_INTDIR}" "\${config}" command "${command}")
  572. set(config "-Dconfig=${CMAKE_CFG_INTDIR}")
  573. endif()
  574. # Wrap multiple 'COMMAND' lines up into a second-level wrapper
  575. # script so all output can be sent to one log file.
  576. if("${command}" MATCHES ";COMMAND;")
  577. set(code_execute_process "
  578. ${code_cygpath_make}
  579. execute_process(COMMAND \${command} RESULT_VARIABLE result)
  580. if(result)
  581. set(msg \"Command failed (\${result}):\\n\")
  582. foreach(arg IN LISTS command)
  583. set(msg \"\${msg} '\${arg}'\")
  584. endforeach(arg)
  585. message(FATAL_ERROR \"\${msg}\")
  586. endif()
  587. ")
  588. set(code "")
  589. set(cmd "")
  590. set(sep "")
  591. foreach(arg IN LISTS command)
  592. if("x${arg}" STREQUAL "xCOMMAND")
  593. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  594. set(cmd "")
  595. set(sep "")
  596. else()
  597. set(cmd "${cmd}${sep}${arg}")
  598. set(sep ";")
  599. endif()
  600. endforeach()
  601. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  602. file(WRITE ${stamp_dir}/${name}-${step}-impl.cmake "${code}")
  603. set(command ${CMAKE_COMMAND} "-Dmake=\${make}" "-Dconfig=\${config}" -P ${stamp_dir}/${name}-${step}-impl.cmake)
  604. endif()
  605. # Wrap the command in a script to log output to files.
  606. set(script ${stamp_dir}/${name}-${step}.cmake)
  607. set(logbase ${stamp_dir}/${name}-${step})
  608. file(WRITE ${script} "
  609. ${code_cygpath_make}
  610. set(command \"${command}\")
  611. execute_process(
  612. COMMAND \${command}
  613. RESULT_VARIABLE result
  614. OUTPUT_FILE \"${logbase}-out.log\"
  615. ERROR_FILE \"${logbase}-err.log\"
  616. )
  617. if(result)
  618. set(msg \"Command failed: \${result}\\n\")
  619. foreach(arg IN LISTS command)
  620. set(msg \"\${msg} '\${arg}'\")
  621. endforeach(arg)
  622. set(msg \"\${msg}\\nSee also\\n ${logbase}-*.log\\n\")
  623. message(FATAL_ERROR \"\${msg}\")
  624. else()
  625. set(msg \"${name} ${step} command succeeded. See also ${logbase}-*.log\\n\")
  626. message(STATUS \"\${msg}\")
  627. endif()
  628. ")
  629. set(command ${CMAKE_COMMAND} ${make} ${config} -P ${script})
  630. set(${cmd_var} "${command}" PARENT_SCOPE)
  631. endfunction(_ep_write_log_script)
  632. # This module used to use "/${CMAKE_CFG_INTDIR}" directly and produced
  633. # makefiles with "/./" in paths for custom command dependencies. Which
  634. # resulted in problems with parallel make -j invocations.
  635. #
  636. # This function was added so that the suffix (search below for ${cfgdir}) is
  637. # only set to "/${CMAKE_CFG_INTDIR}" when ${CMAKE_CFG_INTDIR} is not going to
  638. # be "." (multi-configuration build systems like Visual Studio and Xcode...)
  639. #
  640. function(_ep_get_configuration_subdir_suffix suffix_var)
  641. set(suffix "")
  642. if(CMAKE_CONFIGURATION_TYPES)
  643. set(suffix "/${CMAKE_CFG_INTDIR}")
  644. endif()
  645. set(${suffix_var} "${suffix}" PARENT_SCOPE)
  646. endfunction(_ep_get_configuration_subdir_suffix)
  647. function(ExternalProject_Add_Step name step)
  648. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  649. ExternalProject_Get_Property(${name} stamp_dir)
  650. _ep_get_configuration_subdir_suffix(cfgdir)
  651. add_custom_command(APPEND
  652. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  653. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  654. )
  655. _ep_parse_arguments(ExternalProject_Add_Step
  656. ${name} _EP_${step}_ "${ARGN}")
  657. # Steps depending on this step.
  658. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  659. foreach(depender IN LISTS dependers)
  660. add_custom_command(APPEND
  661. OUTPUT ${stamp_dir}${cfgdir}/${name}-${depender}
  662. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  663. )
  664. endforeach()
  665. # Dependencies on files.
  666. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  667. # Dependencies on steps.
  668. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  669. foreach(dependee IN LISTS dependees)
  670. list(APPEND depends ${stamp_dir}${cfgdir}/${name}-${dependee})
  671. endforeach()
  672. # The command to run.
  673. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  674. if(command)
  675. set(comment "Performing ${step} step for '${name}'")
  676. else()
  677. set(comment "No ${step} step for '${name}'")
  678. endif()
  679. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  680. # Replace list separators.
  681. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  682. if(sep AND command)
  683. string(REPLACE "${sep}" "\\;" command "${command}")
  684. endif()
  685. # Replace location tags.
  686. foreach(var comment command work_dir)
  687. if(${var})
  688. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  689. get_property(val TARGET ${name} PROPERTY _EP_${dir})
  690. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  691. endforeach()
  692. endif()
  693. endforeach()
  694. # Custom comment?
  695. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  696. if(comment_set)
  697. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  698. endif()
  699. # Run every time?
  700. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  701. if(always)
  702. set_property(SOURCE ${stamp_dir}${cfgdir}/${name}-${step} PROPERTY SYMBOLIC 1)
  703. set(touch)
  704. else()
  705. set(touch ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-${step})
  706. endif()
  707. # Wrap with log script?
  708. get_property(log TARGET ${name} PROPERTY _EP_${step}_LOG)
  709. if(command AND log)
  710. _ep_write_log_script(${name} ${step} command)
  711. endif()
  712. add_custom_command(
  713. OUTPUT ${stamp_dir}${cfgdir}/${name}-${step}
  714. COMMENT ${comment}
  715. COMMAND ${command}
  716. COMMAND ${touch}
  717. DEPENDS ${depends}
  718. WORKING_DIRECTORY ${work_dir}
  719. VERBATIM
  720. )
  721. endfunction(ExternalProject_Add_Step)
  722. function(_ep_add_mkdir_command name)
  723. ExternalProject_Get_Property(${name}
  724. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  725. _ep_get_configuration_subdir_suffix(cfgdir)
  726. ExternalProject_Add_Step(${name} mkdir
  727. COMMENT "Creating directories for '${name}'"
  728. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  729. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  730. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  731. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  732. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}${cfgdir}
  733. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  734. )
  735. endfunction(_ep_add_mkdir_command)
  736. function(_ep_get_git_version git_EXECUTABLE git_version_var)
  737. if(git_EXECUTABLE)
  738. execute_process(
  739. COMMAND "${git_EXECUTABLE}" --version
  740. OUTPUT_VARIABLE ov
  741. OUTPUT_STRIP_TRAILING_WHITESPACE
  742. )
  743. string(REGEX REPLACE "^git version (.+)$" "\\1" version "${ov}")
  744. set(${git_version_var} "${version}" PARENT_SCOPE)
  745. endif()
  746. endfunction()
  747. function(_ep_is_dir_empty dir empty_var)
  748. file(GLOB gr "${dir}/*")
  749. if("${gr}" STREQUAL "")
  750. set(${empty_var} 1 PARENT_SCOPE)
  751. else()
  752. set(${empty_var} 0 PARENT_SCOPE)
  753. endif()
  754. endfunction()
  755. function(_ep_add_download_command name)
  756. ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
  757. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  758. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  759. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  760. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  761. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  762. get_property(url TARGET ${name} PROPERTY _EP_URL)
  763. # TODO: Perhaps file:// should be copied to download dir before extraction.
  764. string(REGEX REPLACE "^file://" "" url "${url}")
  765. set(depends)
  766. set(comment)
  767. set(work_dir)
  768. if(cmd_set)
  769. set(work_dir ${download_dir})
  770. elseif(cvs_repository)
  771. find_package(CVS)
  772. if(NOT CVS_EXECUTABLE)
  773. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  774. endif()
  775. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  776. if(NOT cvs_module)
  777. message(FATAL_ERROR "error: no CVS_MODULE")
  778. endif()
  779. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  780. set(repository ${cvs_repository})
  781. set(module ${cvs_module})
  782. set(tag ${cvs_tag})
  783. configure_file(
  784. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  785. "${stamp_dir}/${name}-cvsinfo.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 (CVS checkout) for '${name}'")
  791. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  792. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  793. elseif(svn_repository)
  794. find_package(Subversion)
  795. if(NOT Subversion_SVN_EXECUTABLE)
  796. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  797. endif()
  798. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  799. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  800. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  801. set(repository "${svn_repository} user=${svn_username} password=${svn_password}")
  802. set(module)
  803. set(tag ${svn_revision})
  804. configure_file(
  805. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  806. "${stamp_dir}/${name}-svninfo.txt"
  807. @ONLY
  808. )
  809. get_filename_component(src_name "${source_dir}" NAME)
  810. get_filename_component(work_dir "${source_dir}" PATH)
  811. set(comment "Performing download step (SVN checkout) for '${name}'")
  812. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision}
  813. --username=${svn_username} --password=${svn_password} ${src_name})
  814. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  815. elseif(git_repository)
  816. find_package(Git)
  817. if(NOT GIT_EXECUTABLE)
  818. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  819. endif()
  820. # The git submodule update '--recursive' flag requires git >= v1.6.5
  821. #
  822. _ep_get_git_version("${GIT_EXECUTABLE}" git_version)
  823. if(git_version VERSION_LESS 1.6.5)
  824. message(FATAL_ERROR "error: git version 1.6.5 or later required for 'git submodule update --recursive': git_version='${git_version}'")
  825. endif()
  826. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  827. if(NOT git_tag)
  828. set(git_tag "master")
  829. endif()
  830. set(repository ${git_repository})
  831. set(module)
  832. set(tag ${git_tag})
  833. configure_file(
  834. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  835. "${stamp_dir}/${name}-gitinfo.txt"
  836. @ONLY
  837. )
  838. get_filename_component(src_name "${source_dir}" NAME)
  839. get_filename_component(work_dir "${source_dir}" PATH)
  840. # Since git clone doesn't succeed if the non-empty source_dir exists,
  841. # create a cmake script to invoke as download command.
  842. # The script will delete the source directory and then call git clone.
  843. #
  844. _ep_write_gitclone_script(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
  845. ${GIT_EXECUTABLE} ${git_repository} ${git_tag} ${src_name} ${work_dir}
  846. )
  847. set(comment "Performing download step (git clone) for '${name}'")
  848. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
  849. list(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
  850. elseif(url)
  851. get_filename_component(work_dir "${source_dir}" PATH)
  852. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  853. set(repository "external project URL")
  854. set(module "${url}")
  855. set(tag "${md5}")
  856. configure_file(
  857. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  858. "${stamp_dir}/${name}-urlinfo.txt"
  859. @ONLY
  860. )
  861. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  862. if(IS_DIRECTORY "${url}")
  863. get_filename_component(abs_dir "${url}" ABSOLUTE)
  864. set(comment "Performing download step (DIR copy) for '${name}'")
  865. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  866. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  867. else()
  868. if("${url}" MATCHES "^[a-z]+://")
  869. # TODO: Should download and extraction be different steps?
  870. string(REGEX MATCH "[^/]*$" fname "${url}")
  871. if(NOT "${fname}" MATCHES "\\.(bz2|tar|tgz|tar\\.gz|zip)$")
  872. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  873. endif()
  874. set(file ${download_dir}/${fname})
  875. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  876. _ep_write_downloadfile_script("${stamp_dir}/download-${name}.cmake" "${url}" "${file}" "${timeout}" "${md5}")
  877. set(cmd ${CMAKE_COMMAND} -P ${stamp_dir}/download-${name}.cmake
  878. COMMAND)
  879. set(comment "Performing download step (download, verify and extract) for '${name}'")
  880. else()
  881. set(file "${url}")
  882. set(comment "Performing download step (verify and extract) for '${name}'")
  883. endif()
  884. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${md5}")
  885. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake)
  886. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${name}" "${file}" "${source_dir}")
  887. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  888. endif()
  889. else()
  890. _ep_is_dir_empty("${source_dir}" empty)
  891. if(${empty})
  892. 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")
  893. endif()
  894. endif()
  895. get_property(log TARGET ${name} PROPERTY _EP_LOG_DOWNLOAD)
  896. if(log)
  897. set(log LOG 1)
  898. else()
  899. set(log "")
  900. endif()
  901. ExternalProject_Add_Step(${name} download
  902. COMMENT ${comment}
  903. COMMAND ${cmd}
  904. WORKING_DIRECTORY ${work_dir}
  905. DEPENDS ${depends}
  906. DEPENDEES mkdir
  907. ${log}
  908. )
  909. endfunction(_ep_add_download_command)
  910. function(_ep_add_update_command name)
  911. ExternalProject_Get_Property(${name} source_dir)
  912. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  913. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  914. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  915. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  916. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  917. set(work_dir)
  918. set(comment)
  919. set(always)
  920. if(cmd_set)
  921. set(work_dir ${source_dir})
  922. elseif(cvs_repository)
  923. if(NOT CVS_EXECUTABLE)
  924. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  925. endif()
  926. set(work_dir ${source_dir})
  927. set(comment "Performing update step (CVS update) for '${name}'")
  928. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  929. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  930. set(always 1)
  931. elseif(svn_repository)
  932. if(NOT Subversion_SVN_EXECUTABLE)
  933. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  934. endif()
  935. set(work_dir ${source_dir})
  936. set(comment "Performing update step (SVN update) for '${name}'")
  937. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  938. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  939. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  940. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision}
  941. --username=${svn_username} --password=${svn_password})
  942. set(always 1)
  943. elseif(git_repository)
  944. if(NOT GIT_EXECUTABLE)
  945. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  946. endif()
  947. set(work_dir ${source_dir})
  948. set(comment "Performing update step (git fetch) for '${name}'")
  949. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  950. if(NOT git_tag)
  951. set(git_tag "master")
  952. endif()
  953. set(cmd ${GIT_EXECUTABLE} fetch
  954. COMMAND ${GIT_EXECUTABLE} checkout ${git_tag}
  955. COMMAND ${GIT_EXECUTABLE} submodule update --recursive
  956. )
  957. set(always 1)
  958. endif()
  959. get_property(log TARGET ${name} PROPERTY _EP_LOG_UPDATE)
  960. if(log)
  961. set(log LOG 1)
  962. else()
  963. set(log "")
  964. endif()
  965. ExternalProject_Add_Step(${name} update
  966. COMMENT ${comment}
  967. COMMAND ${cmd}
  968. ALWAYS ${always}
  969. WORKING_DIRECTORY ${work_dir}
  970. DEPENDEES download
  971. ${log}
  972. )
  973. endfunction(_ep_add_update_command)
  974. function(_ep_add_patch_command name)
  975. ExternalProject_Get_Property(${name} source_dir)
  976. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  977. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  978. set(work_dir)
  979. if(cmd_set)
  980. set(work_dir ${source_dir})
  981. endif()
  982. ExternalProject_Add_Step(${name} patch
  983. COMMAND ${cmd}
  984. WORKING_DIRECTORY ${work_dir}
  985. DEPENDEES download
  986. )
  987. endfunction(_ep_add_patch_command)
  988. # TODO: Make sure external projects use the proper compiler
  989. function(_ep_add_configure_command name)
  990. ExternalProject_Get_Property(${name} source_dir binary_dir tmp_dir)
  991. _ep_get_configuration_subdir_suffix(cfgdir)
  992. # Depend on other external projects (file-level).
  993. set(file_deps)
  994. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  995. foreach(dep IN LISTS deps)
  996. get_property(dep_stamp_dir TARGET ${dep} PROPERTY _EP_STAMP_DIR)
  997. list(APPEND file_deps ${dep_stamp_dir}${cfgdir}/${dep}-done)
  998. endforeach()
  999. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  1000. if(cmd_set)
  1001. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  1002. else()
  1003. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1004. if(cmake_command)
  1005. set(cmd "${cmake_command}")
  1006. else()
  1007. set(cmd "${CMAKE_COMMAND}")
  1008. endif()
  1009. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  1010. list(APPEND cmd ${cmake_args})
  1011. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1012. if(cmake_generator)
  1013. list(APPEND cmd "-G${cmake_generator}" "${source_dir}")
  1014. else()
  1015. list(APPEND cmd "-G${CMAKE_GENERATOR}" "${source_dir}")
  1016. endif()
  1017. endif()
  1018. # If anything about the configure command changes, (command itself, cmake
  1019. # used, cmake args or cmake generator) then re-run the configure step.
  1020. # Fixes issue http://public.kitware.com/Bug/view.php?id=10258
  1021. #
  1022. if(NOT EXISTS ${tmp_dir}/${name}-cfgcmd.txt.in)
  1023. file(WRITE ${tmp_dir}/${name}-cfgcmd.txt.in "cmd='@cmd@'\n")
  1024. endif()
  1025. configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt)
  1026. list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt)
  1027. get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE)
  1028. if(log)
  1029. set(log LOG 1)
  1030. else()
  1031. set(log "")
  1032. endif()
  1033. ExternalProject_Add_Step(${name} configure
  1034. COMMAND ${cmd}
  1035. WORKING_DIRECTORY ${binary_dir}
  1036. DEPENDEES update patch
  1037. DEPENDS ${file_deps}
  1038. ${log}
  1039. )
  1040. endfunction(_ep_add_configure_command)
  1041. function(_ep_add_build_command name)
  1042. ExternalProject_Get_Property(${name} binary_dir)
  1043. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  1044. if(cmd_set)
  1045. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  1046. else()
  1047. _ep_get_build_command(${name} BUILD cmd)
  1048. endif()
  1049. get_property(log TARGET ${name} PROPERTY _EP_LOG_BUILD)
  1050. if(log)
  1051. set(log LOG 1)
  1052. else()
  1053. set(log "")
  1054. endif()
  1055. ExternalProject_Add_Step(${name} build
  1056. COMMAND ${cmd}
  1057. WORKING_DIRECTORY ${binary_dir}
  1058. DEPENDEES configure
  1059. ${log}
  1060. )
  1061. endfunction(_ep_add_build_command)
  1062. function(_ep_add_install_command name)
  1063. ExternalProject_Get_Property(${name} binary_dir)
  1064. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  1065. if(cmd_set)
  1066. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  1067. else()
  1068. _ep_get_build_command(${name} INSTALL cmd)
  1069. endif()
  1070. get_property(log TARGET ${name} PROPERTY _EP_LOG_INSTALL)
  1071. if(log)
  1072. set(log LOG 1)
  1073. else()
  1074. set(log "")
  1075. endif()
  1076. ExternalProject_Add_Step(${name} install
  1077. COMMAND ${cmd}
  1078. WORKING_DIRECTORY ${binary_dir}
  1079. DEPENDEES build
  1080. ${log}
  1081. )
  1082. endfunction(_ep_add_install_command)
  1083. function(_ep_add_test_command name)
  1084. ExternalProject_Get_Property(${name} binary_dir)
  1085. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  1086. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  1087. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  1088. # Only actually add the test step if one of the test related properties is
  1089. # explicitly set. (i.e. the test step is omitted unless requested...)
  1090. #
  1091. if(cmd_set OR before OR after)
  1092. if(cmd_set)
  1093. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  1094. else()
  1095. _ep_get_build_command(${name} TEST cmd)
  1096. endif()
  1097. if(before)
  1098. set(dep_args DEPENDEES build DEPENDERS install)
  1099. else()
  1100. set(dep_args DEPENDEES install)
  1101. endif()
  1102. get_property(log TARGET ${name} PROPERTY _EP_LOG_TEST)
  1103. if(log)
  1104. set(log LOG 1)
  1105. else()
  1106. set(log "")
  1107. endif()
  1108. ExternalProject_Add_Step(${name} test
  1109. COMMAND ${cmd}
  1110. WORKING_DIRECTORY ${binary_dir}
  1111. ${dep_args}
  1112. ${log}
  1113. )
  1114. endif()
  1115. endfunction(_ep_add_test_command)
  1116. function(ExternalProject_Add name)
  1117. _ep_get_configuration_subdir_suffix(cfgdir)
  1118. # Add a custom target for the external project.
  1119. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  1120. add_custom_target(${name} ALL DEPENDS ${cmf_dir}${cfgdir}/${name}-complete)
  1121. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  1122. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  1123. _ep_set_directories(${name})
  1124. ExternalProject_Get_Property(${name} stamp_dir)
  1125. # The 'complete' step depends on all other steps and creates a
  1126. # 'done' mark. A dependent external project's 'configure' step
  1127. # depends on the 'done' mark so that it rebuilds when this project
  1128. # rebuilds. It is important that 'done' is not the output of any
  1129. # custom command so that CMake does not propagate build rules to
  1130. # other external project targets.
  1131. add_custom_command(
  1132. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  1133. COMMENT "Completed '${name}'"
  1134. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  1135. COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}${cfgdir}/${name}-complete
  1136. COMMAND ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-done
  1137. DEPENDS ${stamp_dir}${cfgdir}/${name}-install
  1138. VERBATIM
  1139. )
  1140. # Depend on other external projects (target-level).
  1141. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1142. foreach(arg IN LISTS deps)
  1143. add_dependencies(${name} ${arg})
  1144. endforeach()
  1145. # Set up custom build steps based on the target properties.
  1146. # Each step depends on the previous one.
  1147. #
  1148. # The target depends on the output of the final step.
  1149. # (Already set up above in the DEPENDS of the add_custom_target command.)
  1150. #
  1151. _ep_add_mkdir_command(${name})
  1152. _ep_add_download_command(${name})
  1153. _ep_add_update_command(${name})
  1154. _ep_add_patch_command(${name})
  1155. _ep_add_configure_command(${name})
  1156. _ep_add_build_command(${name})
  1157. _ep_add_install_command(${name})
  1158. # Test is special in that it might depend on build, or it might depend
  1159. # on install.
  1160. #
  1161. _ep_add_test_command(${name})
  1162. endfunction(ExternalProject_Add)