ExternalProject.cmake 47 KB

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