ExternalProject.cmake 47 KB

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