ExternalProject.cmake 39 KB

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