ExternalProject.cmake 52 KB

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