ExternalProject.cmake 44 KB

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