ExternalProject.cmake 49 KB

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