ExternalProject.cmake 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. # [URL /.../src.tgz] # Full path or URL of source
  20. # [URL_MD5 md5] # MD5 checksum of file at URL
  21. # [TIMEOUT seconds] # Time allowed for file download operations
  22. # #--Update/Patch step----------
  23. # [UPDATE_COMMAND cmd...] # Source work-tree update command
  24. # [PATCH_COMMAND cmd...] # Command to patch downloaded source
  25. # #--Configure step-------------
  26. # [SOURCE_DIR dir] # Source dir to be used for build
  27. # [CONFIGURE_COMMAND cmd...] # Build tree configuration command
  28. # [CMAKE_COMMAND /.../cmake] # Specify alternative cmake executable
  29. # [CMAKE_GENERATOR gen] # Specify generator for native build
  30. # [CMAKE_ARGS args...] # Arguments to CMake command line
  31. # #--Build step-----------------
  32. # [BINARY_DIR dir] # Specify build dir location
  33. # [BUILD_COMMAND cmd...] # Command to drive the native build
  34. # [BUILD_IN_SOURCE 1] # Use source dir for build dir
  35. # #--Install step---------------
  36. # [INSTALL_DIR dir] # Installation prefix
  37. # [INSTALL_COMMAND cmd...] # Command to drive install after build
  38. # #--Test step---------------
  39. # [TEST_BEFORE_INSTALL 1] # Add test step executed before install step
  40. # [TEST_AFTER_INSTALL 1] # Add test step executed after install step
  41. # [TEST_COMMAND cmd...] # Command to drive test
  42. # )
  43. # The *_DIR options specify directories for the project, with default
  44. # directories computed as follows.
  45. # If the PREFIX option is given to ExternalProject_Add() or the EP_PREFIX
  46. # directory property is set, then an external project is built and installed
  47. # under the specified prefix:
  48. # TMP_DIR = <prefix>/tmp
  49. # STAMP_DIR = <prefix>/src/<name>-stamp
  50. # DOWNLOAD_DIR = <prefix>/src
  51. # SOURCE_DIR = <prefix>/src/<name>
  52. # BINARY_DIR = <prefix>/src/<name>-build
  53. # INSTALL_DIR = <prefix>
  54. # Otherwise, if the EP_BASE directory property is set then components
  55. # of an external project are stored under the specified base:
  56. # TMP_DIR = <base>/tmp/<name>
  57. # STAMP_DIR = <base>/Stamp/<name>
  58. # DOWNLOAD_DIR = <base>/Download/<name>
  59. # SOURCE_DIR = <base>/Source/<name>
  60. # BINARY_DIR = <base>/Build/<name>
  61. # INSTALL_DIR = <base>/Install/<name>
  62. # If no PREFIX, EP_PREFIX, or EP_BASE is specified then the default
  63. # is to set PREFIX to "<name>-prefix".
  64. # Relative paths are interpreted with respect to the build directory
  65. # corresponding to the source directory in which ExternalProject_Add is
  66. # invoked.
  67. #
  68. # If SOURCE_DIR is explicitly set to an existing directory the project
  69. # will be built from it.
  70. # Otherwise a download step must be specified using one of the
  71. # DOWNLOAD_COMMAND, CVS_*, SVN_*, or URL options.
  72. # The URL option may refer locally to a directory or source tarball,
  73. # or refer to a remote tarball (e.g. http://.../src.tgz).
  74. #
  75. # The 'ExternalProject_Add_Step' function adds a custom step to an external
  76. # project:
  77. # ExternalProject_Add_Step(<name> <step> # Names of project and custom step
  78. # [COMMAND cmd...] # Command line invoked by this step
  79. # [COMMENT "text..."] # Text printed when step executes
  80. # [DEPENDEES steps...] # Steps on which this step depends
  81. # [DEPENDERS steps...] # Steps that depend on this step
  82. # [DEPENDS files...] # Files on which this step depends
  83. # [ALWAYS 1] # No stamp file, step always runs
  84. # [WORKING_DIRECTORY dir] # Working directory for command
  85. # )
  86. # The command line, comment, and working directory of every standard
  87. # and custom step is processed to replace tokens
  88. # <SOURCE_DIR>,
  89. # <BINARY_DIR>,
  90. # <INSTALL_DIR>,
  91. # and <TMP_DIR>
  92. # with corresponding property values.
  93. #
  94. # The 'ExternalProject_Get_Property' function retrieves external project
  95. # target properties:
  96. # ExternalProject_Get_Property(<name> [prop1 [prop2 [...]]])
  97. # It stores property values in variables of the same name.
  98. # Property names correspond to the keyword argument names of
  99. # 'ExternalProject_Add'.
  100. #=============================================================================
  101. # Copyright 2008-2009 Kitware, Inc.
  102. #
  103. # Distributed under the OSI-approved BSD License (the "License");
  104. # see accompanying file Copyright.txt for details.
  105. #
  106. # This software is distributed WITHOUT ANY WARRANTY; without even the
  107. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  108. # See the License for more information.
  109. #=============================================================================
  110. # (To distributed this file outside of CMake, substitute the full
  111. # License text for the above reference.)
  112. # Pre-compute a regex to match documented keywords for each command.
  113. file(STRINGS "${CMAKE_CURRENT_LIST_FILE}" lines LIMIT_COUNT 103
  114. REGEX "^# ( \\[[A-Z0-9_]+ [^]]*\\] +#.*$|[A-Za-z0-9_]+\\()")
  115. foreach(line IN LISTS lines)
  116. if("${line}" MATCHES "^# [A-Za-z0-9_]+\\(")
  117. if(_ep_func)
  118. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  119. endif()
  120. string(REGEX REPLACE "^# ([A-Za-z0-9_]+)\\(.*" "\\1" _ep_func "${line}")
  121. #message("function [${_ep_func}]")
  122. set(_ep_keywords_${_ep_func} "^(")
  123. set(_ep_keyword_sep)
  124. else()
  125. string(REGEX REPLACE "^# \\[([A-Z0-9_]+) .*" "\\1" _ep_key "${line}")
  126. #message(" keyword [${_ep_key}]")
  127. set(_ep_keywords_${_ep_func}
  128. "${_ep_keywords_${_ep_func}}${_ep_keyword_sep}${_ep_key}")
  129. set(_ep_keyword_sep "|")
  130. endif()
  131. endforeach()
  132. if(_ep_func)
  133. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  134. endif()
  135. function(_ep_parse_arguments f name ns args)
  136. # Transfer the arguments to this function into target properties for the
  137. # new custom target we just added so that we can set up all the build steps
  138. # correctly based on target properties.
  139. #
  140. # We loop through ARGN and consider the namespace starting with an
  141. # upper-case letter followed by at least two more upper-case letters
  142. # or underscores to be keywords.
  143. set(key)
  144. foreach(arg IN LISTS args)
  145. set(is_value 1)
  146. if(arg MATCHES "^[A-Z][A-Z0-9_][A-Z0-9_]+$" AND
  147. NOT ((arg STREQUAL "${key}") AND (key STREQUAL "COMMAND")) AND
  148. NOT arg MATCHES "^(TRUE|FALSE)$")
  149. if(_ep_keywords_${f} AND arg MATCHES "${_ep_keywords_${f}}")
  150. set(is_value 0)
  151. else()
  152. if(NOT (key STREQUAL "COMMAND")
  153. AND NOT (key STREQUAL "CVS_MODULE")
  154. AND NOT (key STREQUAL "DEPENDS")
  155. )
  156. message(AUTHOR_WARNING "unknown ${f} keyword: ${arg}")
  157. endif()
  158. endif()
  159. endif()
  160. if(is_value)
  161. if(key)
  162. # Value
  163. if(NOT arg STREQUAL "")
  164. set_property(TARGET ${name} APPEND PROPERTY ${ns}${key} "${arg}")
  165. else()
  166. get_property(have_key TARGET ${name} PROPERTY ${ns}${key} SET)
  167. if(have_key)
  168. get_property(value TARGET ${name} PROPERTY ${ns}${key})
  169. set_property(TARGET ${name} PROPERTY ${ns}${key} "${value};${arg}")
  170. else()
  171. set_property(TARGET ${name} PROPERTY ${ns}${key} "${arg}")
  172. endif()
  173. endif()
  174. else()
  175. # Missing Keyword
  176. message(AUTHOR_WARNING "value '${arg}' with no previous keyword in ${f}")
  177. endif()
  178. else()
  179. set(key "${arg}")
  180. endif()
  181. endforeach()
  182. endfunction(_ep_parse_arguments)
  183. define_property(DIRECTORY PROPERTY "EP_BASE" INHERITED
  184. BRIEF_DOCS "Base directory for External Project storage."
  185. FULL_DOCS
  186. "See documentation of the ExternalProject_Add() function in the "
  187. "ExternalProject module."
  188. )
  189. define_property(DIRECTORY PROPERTY "EP_PREFIX" INHERITED
  190. BRIEF_DOCS "Top prefix for External Project storage."
  191. FULL_DOCS
  192. "See documentation of the ExternalProject_Add() function in the "
  193. "ExternalProject module."
  194. )
  195. function(_ep_write_downloadfile_script script_filename remote local timeout md5)
  196. if(timeout)
  197. set(timeout_args TIMEOUT ${timeout})
  198. set(timeout_msg "${timeout} seconds")
  199. else()
  200. set(timeout_args "# no TIMEOUT")
  201. set(timeout_msg "none")
  202. endif()
  203. if(md5)
  204. set(md5_args EXPECTED_MD5 ${md5})
  205. else()
  206. set(md5_args "# no EXPECTED_MD5")
  207. endif()
  208. file(WRITE ${script_filename}
  209. "message(STATUS \"downloading...
  210. src='${remote}'
  211. dst='${local}'
  212. timeout='${timeout_msg}'\")
  213. file(DOWNLOAD
  214. \"${remote}\"
  215. \"${local}\"
  216. SHOW_PROGRESS
  217. ${md5_args}
  218. ${timeout_args}
  219. STATUS status
  220. LOG log)
  221. list(GET status 0 status_code)
  222. list(GET status 1 status_string)
  223. if(NOT status_code EQUAL 0)
  224. message(FATAL_ERROR \"error: downloading '${remote}' failed
  225. status_code: \${status_code}
  226. status_string: \${status_string}
  227. log: \${log}
  228. \")
  229. endif()
  230. message(STATUS \"downloading... done\")
  231. "
  232. )
  233. endfunction(_ep_write_downloadfile_script)
  234. function(_ep_write_verifyfile_script script_filename local md5)
  235. file(WRITE ${script_filename}
  236. "message(STATUS \"verifying file...
  237. file='${local}'\")
  238. set(verified 0)
  239. # If an expected md5 checksum exists, compare against it:
  240. #
  241. if(NOT \"${md5}\" STREQUAL \"\")
  242. execute_process(COMMAND \${CMAKE_COMMAND} -E md5sum \"${local}\"
  243. OUTPUT_VARIABLE ov
  244. OUTPUT_STRIP_TRAILING_WHITESPACE
  245. RESULT_VARIABLE rv)
  246. if(NOT rv EQUAL 0)
  247. message(FATAL_ERROR \"error: computing md5sum of '${local}' failed\")
  248. endif()
  249. string(REGEX MATCH \"^([0-9A-Fa-f]+)\" md5_actual \"\${ov}\")
  250. string(TOLOWER \"\${md5_actual}\" md5_actual)
  251. string(TOLOWER \"${md5}\" md5)
  252. if(NOT \"\${md5}\" STREQUAL \"\${md5_actual}\")
  253. message(FATAL_ERROR \"error: md5sum of '${local}' does not match expected value
  254. md5_expected: \${md5}
  255. md5_actual: \${md5_actual}
  256. \")
  257. endif()
  258. set(verified 1)
  259. endif()
  260. if(verified)
  261. message(STATUS \"verifying file... done\")
  262. else()
  263. message(STATUS \"verifying file... warning: did not verify file - no URL_MD5 checksum argument? corrupt file?\")
  264. endif()
  265. "
  266. )
  267. endfunction(_ep_write_verifyfile_script)
  268. function(_ep_write_extractfile_script script_filename filename tmp directory)
  269. set(args "")
  270. if(filename MATCHES ".tar$")
  271. set(args xf)
  272. endif()
  273. if(filename MATCHES ".tgz$")
  274. set(args xfz)
  275. endif()
  276. if(filename MATCHES ".tar.gz$")
  277. set(args xfz)
  278. endif()
  279. if(args STREQUAL "")
  280. message(SEND_ERROR "error: do not know how to extract '${filename}' -- known types are .tar, .tgz and .tar.gz")
  281. return()
  282. endif()
  283. file(WRITE ${script_filename}
  284. "# Make file names absolute:
  285. #
  286. get_filename_component(filename \"${filename}\" ABSOLUTE)
  287. get_filename_component(tmp \"${tmp}\" ABSOLUTE)
  288. get_filename_component(directory \"${directory}\" ABSOLUTE)
  289. message(STATUS \"extracting...
  290. src='\${filename}'
  291. dst='\${directory}'\")
  292. # Prepare a space for extracting:
  293. #
  294. set(i 1)
  295. while(EXISTS \"\${tmp}/extract\${i}\")
  296. math(EXPR i \"\${i} + 1\")
  297. endwhile()
  298. set(ut_dir \"\${tmp}/extract\${i}\")
  299. file(MAKE_DIRECTORY \"\${ut_dir}\")
  300. # Extract it:
  301. #
  302. message(STATUS \"extracting... [tar ${args}]\")
  303. execute_process(COMMAND \${CMAKE_COMMAND} -E tar ${args} \${filename}
  304. WORKING_DIRECTORY \${ut_dir}
  305. RESULT_VARIABLE rv)
  306. if(NOT rv EQUAL 0)
  307. message(STATUS \"extracting... [error clean up]\")
  308. file(REMOVE_RECURSE \"\${ut_dir}\")
  309. message(FATAL_ERROR \"error: extract of '\${filename}' failed\")
  310. endif()
  311. # Analyze what came out of the tar file:
  312. #
  313. message(STATUS \"extracting... [analysis]\")
  314. file(GLOB contents \"\${ut_dir}/*\")
  315. list(LENGTH contents n)
  316. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY \"\${contents}\")
  317. set(contents \"\${ut_dir}\")
  318. endif()
  319. # Copy \"the one\" directory to the final directory:
  320. #
  321. message(STATUS \"extracting... [copy]\")
  322. file(COPY \"\${contents}/\" DESTINATION \${directory})
  323. # Clean up:
  324. #
  325. message(STATUS \"extracting... [clean up]\")
  326. file(REMOVE_RECURSE \"\${ut_dir}\")
  327. message(STATUS \"extracting... done\")
  328. "
  329. )
  330. endfunction(_ep_write_extractfile_script)
  331. function(_ep_set_directories name)
  332. get_property(prefix TARGET ${name} PROPERTY _EP_PREFIX)
  333. if(NOT prefix)
  334. get_property(prefix DIRECTORY PROPERTY EP_PREFIX)
  335. if(NOT prefix)
  336. get_property(base DIRECTORY PROPERTY EP_BASE)
  337. if(NOT base)
  338. set(prefix "${name}-prefix")
  339. endif()
  340. endif()
  341. endif()
  342. if(prefix)
  343. set(tmp_default "${prefix}/tmp")
  344. set(download_default "${prefix}/src")
  345. set(source_default "${prefix}/src/${name}")
  346. set(binary_default "${prefix}/src/${name}-build")
  347. set(stamp_default "${prefix}/src/${name}-stamp")
  348. set(install_default "${prefix}")
  349. else() # assert(base)
  350. set(tmp_default "${base}/tmp/${name}")
  351. set(download_default "${base}/Download/${name}")
  352. set(source_default "${base}/Source/${name}")
  353. set(binary_default "${base}/Build/${name}")
  354. set(stamp_default "${base}/Stamp/${name}")
  355. set(install_default "${base}/Install/${name}")
  356. endif()
  357. get_property(build_in_source TARGET ${name} PROPERTY _EP_BUILD_IN_SOURCE)
  358. if(build_in_source)
  359. get_property(have_binary_dir TARGET ${name} PROPERTY _EP_BINARY_DIR SET)
  360. if(have_binary_dir)
  361. message(FATAL_ERROR
  362. "External project ${name} has both BINARY_DIR and BUILD_IN_SOURCE!")
  363. endif()
  364. endif()
  365. set(top "${CMAKE_CURRENT_BINARY_DIR}")
  366. set(places stamp download source binary install tmp)
  367. foreach(var ${places})
  368. string(TOUPPER "${var}" VAR)
  369. get_property(${var}_dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  370. if(NOT ${var}_dir)
  371. set(${var}_dir "${${var}_default}")
  372. endif()
  373. if(NOT IS_ABSOLUTE "${${var}_dir}")
  374. get_filename_component(${var}_dir "${top}/${${var}_dir}" ABSOLUTE)
  375. endif()
  376. set_property(TARGET ${name} PROPERTY _EP_${VAR}_DIR "${${var}_dir}")
  377. endforeach()
  378. if(build_in_source)
  379. get_property(source_dir TARGET ${name} PROPERTY _EP_SOURCE_DIR)
  380. set_property(TARGET ${name} PROPERTY _EP_BINARY_DIR "${source_dir}")
  381. endif()
  382. # Make the directories at CMake configure time *and* add a custom command
  383. # to make them at build time. They need to exist at makefile generation
  384. # time for Borland make and wmake so that CMake may generate makefiles
  385. # with "cd C:\short\paths\with\no\spaces" commands in them.
  386. #
  387. # Additionally, the add_custom_command is still used in case somebody
  388. # removes one of the necessary directories and tries to rebuild without
  389. # re-running cmake.
  390. foreach(var ${places})
  391. string(TOUPPER "${var}" VAR)
  392. get_property(dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  393. file(MAKE_DIRECTORY "${dir}")
  394. if(NOT EXISTS "${dir}")
  395. message(FATAL_ERROR "dir '${dir}' does not exist after file(MAKE_DIRECTORY)")
  396. endif()
  397. endforeach()
  398. endfunction(_ep_set_directories)
  399. function(ExternalProject_Get_Property name)
  400. foreach(var ${ARGN})
  401. string(TOUPPER "${var}" VAR)
  402. get_property(${var} TARGET ${name} PROPERTY _EP_${VAR})
  403. if(NOT ${var})
  404. message(FATAL_ERROR "External project \"${name}\" has no ${var}")
  405. endif()
  406. set(${var} "${${var}}" PARENT_SCOPE)
  407. endforeach()
  408. endfunction(ExternalProject_Get_Property)
  409. function(_ep_get_configure_command_id name cfg_cmd_id_var)
  410. get_target_property(cmd ${name} _EP_CONFIGURE_COMMAND)
  411. if(cmd STREQUAL "")
  412. # Explicit empty string means no configure step for this project
  413. set(${cfg_cmd_id_var} "none" PARENT_SCOPE)
  414. else()
  415. if(NOT cmd)
  416. # Default is "use cmake":
  417. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  418. else()
  419. # Otherwise we have to analyze the value:
  420. if(cmd MATCHES "^[^;]*/configure")
  421. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  422. elseif(cmd MATCHES "^[^;]*/cmake" AND NOT cmd MATCHES ";-[PE];")
  423. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  424. elseif(cmd MATCHES "config")
  425. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  426. else()
  427. set(${cfg_cmd_id_var} "unknown:${cmd}" PARENT_SCOPE)
  428. endif()
  429. endif()
  430. endif()
  431. endfunction(_ep_get_configure_command_id)
  432. function(_ep_get_build_command name step cmd_var)
  433. set(cmd "${${cmd_var}}")
  434. if(NOT cmd)
  435. set(args)
  436. _ep_get_configure_command_id(${name} cfg_cmd_id)
  437. if(cfg_cmd_id STREQUAL "cmake")
  438. # CMake project. Select build command based on generator.
  439. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  440. if("${cmake_generator}" MATCHES "Make" AND
  441. "${cmake_generator}" STREQUAL "${CMAKE_GENERATOR}")
  442. # The project uses the same Makefile generator. Use recursive make.
  443. set(cmd "$(MAKE)")
  444. if(step STREQUAL "INSTALL")
  445. set(args install)
  446. endif()
  447. if(step STREQUAL "TEST")
  448. set(args test)
  449. endif()
  450. else()
  451. # Drive the project with "cmake --build".
  452. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  453. if(cmake_command)
  454. set(cmd "${cmake_command}")
  455. else()
  456. set(cmd "${CMAKE_COMMAND}")
  457. endif()
  458. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  459. if(step STREQUAL "INSTALL")
  460. list(APPEND args --target install)
  461. endif()
  462. # But for "TEST" drive the project with corresponding "ctest".
  463. if(step STREQUAL "TEST")
  464. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  465. set(args "")
  466. endif()
  467. endif()
  468. else() # if(cfg_cmd_id STREQUAL "configure")
  469. # Non-CMake project. Guess "make" and "make install" and "make test".
  470. set(cmd "make")
  471. if(step STREQUAL "INSTALL")
  472. set(args install)
  473. endif()
  474. if(step STREQUAL "TEST")
  475. set(args test)
  476. endif()
  477. endif()
  478. # Use user-specified arguments instead of default arguments, if any.
  479. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  480. if(have_args)
  481. get_target_property(args ${name} _EP_${step}_ARGS)
  482. endif()
  483. list(APPEND cmd ${args})
  484. endif()
  485. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  486. endfunction(_ep_get_build_command)
  487. # This module used to use "/${CMAKE_CFG_INTDIR}" directly and produced
  488. # makefiles with "/./" in paths for custom command dependencies. Which
  489. # resulted in problems with parallel make -j invocations.
  490. #
  491. # This function was added so that the suffix (search below for ${cfgdir}) is
  492. # only set to "/${CMAKE_CFG_INTDIR}" when ${CMAKE_CFG_INTDIR} is not going to
  493. # be "." (multi-configuration build systems like Visual Studio and Xcode...)
  494. #
  495. function(_ep_get_configuration_subdir_suffix suffix_var)
  496. set(suffix "")
  497. if(CMAKE_CONFIGURATION_TYPES)
  498. set(suffix "/${CMAKE_CFG_INTDIR}")
  499. endif()
  500. set(${suffix_var} "${suffix}" PARENT_SCOPE)
  501. endfunction(_ep_get_configuration_subdir_suffix)
  502. function(ExternalProject_Add_Step name step)
  503. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  504. ExternalProject_Get_Property(${name} stamp_dir)
  505. _ep_get_configuration_subdir_suffix(cfgdir)
  506. add_custom_command(APPEND
  507. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  508. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  509. )
  510. _ep_parse_arguments(ExternalProject_Add_Step
  511. ${name} _EP_${step}_ "${ARGN}")
  512. # Steps depending on this step.
  513. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  514. foreach(depender IN LISTS dependers)
  515. add_custom_command(APPEND
  516. OUTPUT ${stamp_dir}${cfgdir}/${name}-${depender}
  517. DEPENDS ${stamp_dir}${cfgdir}/${name}-${step}
  518. )
  519. endforeach()
  520. # Dependencies on files.
  521. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  522. # Dependencies on steps.
  523. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  524. foreach(dependee IN LISTS dependees)
  525. list(APPEND depends ${stamp_dir}${cfgdir}/${name}-${dependee})
  526. endforeach()
  527. # The command to run.
  528. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  529. if(command)
  530. set(comment "Performing ${step} step for '${name}'")
  531. else()
  532. set(comment "No ${step} step for '${name}'")
  533. endif()
  534. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  535. # Replace list separators.
  536. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  537. if(sep AND command)
  538. string(REPLACE "${sep}" "\\;" command "${command}")
  539. endif()
  540. # Replace location tags.
  541. foreach(var comment command work_dir)
  542. if(${var})
  543. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  544. get_property(val TARGET ${name} PROPERTY _EP_${dir})
  545. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  546. endforeach()
  547. endif()
  548. endforeach()
  549. # Custom comment?
  550. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  551. if(comment_set)
  552. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  553. endif()
  554. # Run every time?
  555. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  556. if(always)
  557. set_property(SOURCE ${stamp_dir}${cfgdir}/${name}-${step} PROPERTY SYMBOLIC 1)
  558. set(touch)
  559. else()
  560. set(touch ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-${step})
  561. endif()
  562. add_custom_command(
  563. OUTPUT ${stamp_dir}${cfgdir}/${name}-${step}
  564. COMMENT ${comment}
  565. COMMAND ${command}
  566. COMMAND ${touch}
  567. DEPENDS ${depends}
  568. WORKING_DIRECTORY ${work_dir}
  569. VERBATIM
  570. )
  571. endfunction(ExternalProject_Add_Step)
  572. function(_ep_add_mkdir_command name)
  573. ExternalProject_Get_Property(${name}
  574. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  575. _ep_get_configuration_subdir_suffix(cfgdir)
  576. ExternalProject_Add_Step(${name} mkdir
  577. COMMENT "Creating directories for '${name}'"
  578. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  579. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  580. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  581. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  582. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}${cfgdir}
  583. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  584. )
  585. endfunction(_ep_add_mkdir_command)
  586. function(_ep_add_download_command name)
  587. ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
  588. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  589. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  590. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  591. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  592. get_property(url TARGET ${name} PROPERTY _EP_URL)
  593. # TODO: Perhaps file:// should be copied to download dir before extraction.
  594. string(REGEX REPLACE "^file://" "" url "${url}")
  595. set(depends)
  596. set(comment)
  597. set(work_dir)
  598. if(cmd_set)
  599. set(work_dir ${download_dir})
  600. elseif(cvs_repository)
  601. find_package(CVS)
  602. if(NOT CVS_EXECUTABLE)
  603. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  604. endif()
  605. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  606. if(NOT cvs_module)
  607. message(FATAL_ERROR "error: no CVS_MODULE")
  608. endif()
  609. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  610. set(repository ${cvs_repository})
  611. set(module ${cvs_module})
  612. set(tag ${cvs_tag})
  613. configure_file(
  614. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  615. "${stamp_dir}/${name}-cvsinfo.txt"
  616. @ONLY
  617. )
  618. get_filename_component(src_name "${source_dir}" NAME)
  619. get_filename_component(work_dir "${source_dir}" PATH)
  620. set(comment "Performing download step (CVS checkout) for '${name}'")
  621. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  622. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  623. elseif(svn_repository)
  624. find_package(Subversion)
  625. if(NOT Subversion_SVN_EXECUTABLE)
  626. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  627. endif()
  628. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  629. set(repository ${svn_repository})
  630. set(module)
  631. set(tag ${svn_revision})
  632. configure_file(
  633. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  634. "${stamp_dir}/${name}-svninfo.txt"
  635. @ONLY
  636. )
  637. get_filename_component(src_name "${source_dir}" NAME)
  638. get_filename_component(work_dir "${source_dir}" PATH)
  639. set(comment "Performing download step (SVN checkout) for '${name}'")
  640. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision} ${src_name})
  641. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  642. elseif(url)
  643. get_filename_component(work_dir "${source_dir}" PATH)
  644. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  645. set(repository "external project URL")
  646. set(module "${url}")
  647. set(tag "${md5}")
  648. configure_file(
  649. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  650. "${stamp_dir}/${name}-urlinfo.txt"
  651. @ONLY
  652. )
  653. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  654. if(IS_DIRECTORY "${url}")
  655. get_filename_component(abs_dir "${url}" ABSOLUTE)
  656. set(comment "Performing download step (DIR copy) for '${name}'")
  657. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  658. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  659. else()
  660. if("${url}" MATCHES "^[a-z]+://")
  661. # TODO: Should download and extraction be different steps?
  662. string(REGEX MATCH "[^/]*$" fname "${url}")
  663. if(NOT "${fname}" MATCHES "\\.(tar|tgz|tar\\.gz)$")
  664. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  665. endif()
  666. set(file ${download_dir}/${fname})
  667. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  668. _ep_write_downloadfile_script("${stamp_dir}/download-${name}.cmake" "${url}" "${file}" "${timeout}" "${md5}")
  669. set(cmd ${CMAKE_COMMAND} -P ${stamp_dir}/download-${name}.cmake
  670. COMMAND)
  671. set(comment "Performing download step (download, verify and extract) for '${name}'")
  672. else()
  673. set(file "${url}")
  674. set(comment "Performing download step (verify and extract) for '${name}'")
  675. endif()
  676. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${md5}")
  677. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake)
  678. # TODO: Support other archive formats.
  679. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${file}" "${tmp_dir}" "${source_dir}")
  680. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  681. endif()
  682. else()
  683. message(SEND_ERROR "error: no download info for '${name}' -- please specify existing SOURCE_DIR or one of URL, CVS_REPOSITORY and CVS_MODULE, SVN_REPOSITORY or DOWNLOAD_COMMAND")
  684. endif()
  685. ExternalProject_Add_Step(${name} download
  686. COMMENT ${comment}
  687. COMMAND ${cmd}
  688. WORKING_DIRECTORY ${work_dir}
  689. DEPENDS ${depends}
  690. DEPENDEES mkdir
  691. )
  692. endfunction(_ep_add_download_command)
  693. function(_ep_add_update_command name)
  694. ExternalProject_Get_Property(${name} source_dir)
  695. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  696. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  697. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  698. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  699. set(work_dir)
  700. set(comment)
  701. set(always)
  702. if(cmd_set)
  703. set(work_dir ${source_dir})
  704. elseif(cvs_repository)
  705. if(NOT CVS_EXECUTABLE)
  706. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  707. endif()
  708. set(work_dir ${source_dir})
  709. set(comment "Performing update step (CVS update) for '${name}'")
  710. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  711. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  712. set(always 1)
  713. elseif(svn_repository)
  714. if(NOT Subversion_SVN_EXECUTABLE)
  715. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  716. endif()
  717. set(work_dir ${source_dir})
  718. set(comment "Performing update step (SVN update) for '${name}'")
  719. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  720. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision})
  721. set(always 1)
  722. endif()
  723. ExternalProject_Add_Step(${name} update
  724. COMMENT ${comment}
  725. COMMAND ${cmd}
  726. ALWAYS ${always}
  727. WORKING_DIRECTORY ${work_dir}
  728. DEPENDEES download
  729. )
  730. endfunction(_ep_add_update_command)
  731. function(_ep_add_patch_command name)
  732. ExternalProject_Get_Property(${name} source_dir)
  733. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  734. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  735. set(work_dir)
  736. if(cmd_set)
  737. set(work_dir ${source_dir})
  738. endif()
  739. ExternalProject_Add_Step(${name} patch
  740. COMMAND ${cmd}
  741. WORKING_DIRECTORY ${work_dir}
  742. DEPENDEES download
  743. )
  744. endfunction(_ep_add_patch_command)
  745. # TODO: Make sure external projects use the proper compiler
  746. function(_ep_add_configure_command name)
  747. ExternalProject_Get_Property(${name} source_dir binary_dir)
  748. _ep_get_configuration_subdir_suffix(cfgdir)
  749. # Depend on other external projects (file-level).
  750. set(file_deps)
  751. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  752. foreach(dep IN LISTS deps)
  753. get_property(dep_stamp_dir TARGET ${dep} PROPERTY _EP_STAMP_DIR)
  754. list(APPEND file_deps ${dep_stamp_dir}${cfgdir}/${dep}-done)
  755. endforeach()
  756. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  757. if(cmd_set)
  758. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  759. else()
  760. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  761. if(cmake_command)
  762. set(cmd "${cmake_command}")
  763. else()
  764. set(cmd "${CMAKE_COMMAND}")
  765. endif()
  766. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  767. list(APPEND cmd ${cmake_args})
  768. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  769. if(cmake_generator)
  770. list(APPEND cmd "-G${cmake_generator}" "${source_dir}")
  771. else()
  772. list(APPEND cmd "-G${CMAKE_GENERATOR}" "${source_dir}")
  773. endif()
  774. endif()
  775. ExternalProject_Add_Step(${name} configure
  776. COMMAND ${cmd}
  777. WORKING_DIRECTORY ${binary_dir}
  778. DEPENDEES update patch
  779. DEPENDS ${file_deps}
  780. )
  781. endfunction(_ep_add_configure_command)
  782. function(_ep_add_build_command name)
  783. ExternalProject_Get_Property(${name} binary_dir)
  784. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  785. if(cmd_set)
  786. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  787. else()
  788. _ep_get_build_command(${name} BUILD cmd)
  789. endif()
  790. ExternalProject_Add_Step(${name} build
  791. COMMAND ${cmd}
  792. WORKING_DIRECTORY ${binary_dir}
  793. DEPENDEES configure
  794. )
  795. endfunction(_ep_add_build_command)
  796. function(_ep_add_install_command name)
  797. ExternalProject_Get_Property(${name} binary_dir)
  798. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  799. if(cmd_set)
  800. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  801. else()
  802. _ep_get_build_command(${name} INSTALL cmd)
  803. endif()
  804. ExternalProject_Add_Step(${name} install
  805. COMMAND ${cmd}
  806. WORKING_DIRECTORY ${binary_dir}
  807. DEPENDEES build
  808. )
  809. endfunction(_ep_add_install_command)
  810. function(_ep_add_test_command name)
  811. ExternalProject_Get_Property(${name} binary_dir)
  812. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  813. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  814. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  815. # Only actually add the test step if one of the test related properties is
  816. # explicitly set. (i.e. the test step is omitted unless requested...)
  817. #
  818. if(cmd_set OR before OR after)
  819. if(cmd_set)
  820. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  821. else()
  822. _ep_get_build_command(${name} TEST cmd)
  823. endif()
  824. if(before)
  825. set(dep_args DEPENDEES build DEPENDERS install)
  826. else()
  827. set(dep_args DEPENDEES install)
  828. endif()
  829. ExternalProject_Add_Step(${name} test
  830. COMMAND ${cmd}
  831. WORKING_DIRECTORY ${binary_dir}
  832. ${dep_args}
  833. )
  834. endif()
  835. endfunction(_ep_add_test_command)
  836. function(ExternalProject_Add name)
  837. _ep_get_configuration_subdir_suffix(cfgdir)
  838. # Add a custom target for the external project.
  839. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  840. add_custom_target(${name} ALL DEPENDS ${cmf_dir}${cfgdir}/${name}-complete)
  841. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  842. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  843. _ep_set_directories(${name})
  844. ExternalProject_Get_Property(${name} stamp_dir)
  845. # The 'complete' step depends on all other steps and creates a
  846. # 'done' mark. A dependent external project's 'configure' step
  847. # depends on the 'done' mark so that it rebuilds when this project
  848. # rebuilds. It is important that 'done' is not the output of any
  849. # custom command so that CMake does not propagate build rules to
  850. # other external project targets.
  851. add_custom_command(
  852. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  853. COMMENT "Completed '${name}'"
  854. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  855. COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}${cfgdir}/${name}-complete
  856. COMMAND ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-done
  857. DEPENDS ${stamp_dir}${cfgdir}/${name}-install
  858. VERBATIM
  859. )
  860. # Depend on other external projects (target-level).
  861. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  862. foreach(arg IN LISTS deps)
  863. add_dependencies(${name} ${arg})
  864. endforeach()
  865. # Set up custom build steps based on the target properties.
  866. # Each step depends on the previous one.
  867. #
  868. # The target depends on the output of the final step.
  869. # (Already set up above in the DEPENDS of the add_custom_target command.)
  870. #
  871. _ep_add_mkdir_command(${name})
  872. _ep_add_download_command(${name})
  873. _ep_add_update_command(${name})
  874. _ep_add_patch_command(${name})
  875. _ep_add_configure_command(${name})
  876. _ep_add_build_command(${name})
  877. _ep_add_install_command(${name})
  878. # Test is special in that it might depend on build, or it might depend
  879. # on install.
  880. #
  881. _ep_add_test_command(${name})
  882. endfunction(ExternalProject_Add)