ExternalProject.cmake 49 KB

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