ExternalProject.cmake 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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}" MATCHES "Make" OR NOT cmake_generator))
  535. # The project uses the same Makefile generator. Use recursive make.
  536. set(cmd "$(MAKE)")
  537. if(step STREQUAL "INSTALL")
  538. set(args install)
  539. endif()
  540. if(step STREQUAL "TEST")
  541. set(args test)
  542. endif()
  543. else()
  544. # Drive the project with "cmake --build".
  545. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  546. if(cmake_command)
  547. set(cmd "${cmake_command}")
  548. else()
  549. set(cmd "${CMAKE_COMMAND}")
  550. endif()
  551. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  552. if(step STREQUAL "INSTALL")
  553. list(APPEND args --target install)
  554. endif()
  555. # But for "TEST" drive the project with corresponding "ctest".
  556. if(step STREQUAL "TEST")
  557. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  558. set(args "")
  559. endif()
  560. endif()
  561. else() # if(cfg_cmd_id STREQUAL "configure")
  562. # Non-CMake project. Guess "make" and "make install" and "make test".
  563. # But use "$(MAKE)" to get recursive parallel make.
  564. set(cmd "$(MAKE)")
  565. if(step STREQUAL "INSTALL")
  566. set(args install)
  567. endif()
  568. if(step STREQUAL "TEST")
  569. set(args test)
  570. endif()
  571. endif()
  572. # Use user-specified arguments instead of default arguments, if any.
  573. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  574. if(have_args)
  575. get_target_property(args ${name} _EP_${step}_ARGS)
  576. endif()
  577. list(APPEND cmd ${args})
  578. endif()
  579. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  580. endfunction(_ep_get_build_command)
  581. function(_ep_write_log_script name step cmd_var)
  582. ExternalProject_Get_Property(${name} stamp_dir)
  583. set(command "${${cmd_var}}")
  584. set(make "")
  585. set(code_cygpath_make "")
  586. if("${command}" MATCHES "^\\$\\(MAKE\\)")
  587. # GNU make recognizes the string "$(MAKE)" as recursive make, so
  588. # ensure that it appears directly in the makefile.
  589. string(REGEX REPLACE "^\\$\\(MAKE\\)" "\${make}" command "${command}")
  590. set(make "-Dmake=$(MAKE)")
  591. if(WIN32 AND NOT CYGWIN)
  592. set(code_cygpath_make "
  593. if(\${make} MATCHES \"^/\")
  594. execute_process(
  595. COMMAND cygpath -w \${make}
  596. OUTPUT_VARIABLE cygpath_make
  597. ERROR_VARIABLE cygpath_make
  598. RESULT_VARIABLE cygpath_error
  599. OUTPUT_STRIP_TRAILING_WHITESPACE
  600. )
  601. if(NOT cygpath_error)
  602. set(make \${cygpath_make})
  603. endif()
  604. endif()
  605. ")
  606. endif()
  607. endif()
  608. set(config "")
  609. if("${CMAKE_CFG_INTDIR}" MATCHES "^\\$")
  610. string(REPLACE "${CMAKE_CFG_INTDIR}" "\${config}" command "${command}")
  611. set(config "-Dconfig=${CMAKE_CFG_INTDIR}")
  612. endif()
  613. # Wrap multiple 'COMMAND' lines up into a second-level wrapper
  614. # script so all output can be sent to one log file.
  615. if("${command}" MATCHES ";COMMAND;")
  616. set(code_execute_process "
  617. ${code_cygpath_make}
  618. execute_process(COMMAND \${command} RESULT_VARIABLE result)
  619. if(result)
  620. set(msg \"Command failed (\${result}):\\n\")
  621. foreach(arg IN LISTS command)
  622. set(msg \"\${msg} '\${arg}'\")
  623. endforeach(arg)
  624. message(FATAL_ERROR \"\${msg}\")
  625. endif()
  626. ")
  627. set(code "")
  628. set(cmd "")
  629. set(sep "")
  630. foreach(arg IN LISTS command)
  631. if("x${arg}" STREQUAL "xCOMMAND")
  632. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  633. set(cmd "")
  634. set(sep "")
  635. else()
  636. set(cmd "${cmd}${sep}${arg}")
  637. set(sep ";")
  638. endif()
  639. endforeach()
  640. set(code "set(ENV{VS_UNICODE_OUTPUT} \"\")\n${code}set(command \"${cmd}\")${code_execute_process}")
  641. file(WRITE ${stamp_dir}/${name}-${step}-impl.cmake "${code}")
  642. set(command ${CMAKE_COMMAND} "-Dmake=\${make}" "-Dconfig=\${config}" -P ${stamp_dir}/${name}-${step}-impl.cmake)
  643. endif()
  644. # Wrap the command in a script to log output to files.
  645. set(script ${stamp_dir}/${name}-${step}.cmake)
  646. set(logbase ${stamp_dir}/${name}-${step})
  647. file(WRITE ${script} "
  648. ${code_cygpath_make}
  649. set(ENV{VS_UNICODE_OUTPUT} \"\")
  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(svn_user_pw_args "")
  873. if(svn_username)
  874. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  875. endif()
  876. if(svn_password)
  877. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  878. endif()
  879. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision}
  880. ${svn_user_pw_args} ${src_name})
  881. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  882. elseif(git_repository)
  883. find_package(Git)
  884. if(NOT GIT_EXECUTABLE)
  885. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  886. endif()
  887. # The git submodule update '--recursive' flag requires git >= v1.6.5
  888. #
  889. _ep_get_git_version("${GIT_EXECUTABLE}" git_version)
  890. if(git_version VERSION_LESS 1.6.5)
  891. message(FATAL_ERROR "error: git version 1.6.5 or later required for 'git submodule update --recursive': git_version='${git_version}'")
  892. endif()
  893. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  894. if(NOT git_tag)
  895. set(git_tag "master")
  896. endif()
  897. set(repository ${git_repository})
  898. set(module)
  899. set(tag ${git_tag})
  900. configure_file(
  901. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  902. "${stamp_dir}/${name}-gitinfo.txt"
  903. @ONLY
  904. )
  905. get_filename_component(src_name "${source_dir}" NAME)
  906. get_filename_component(work_dir "${source_dir}" PATH)
  907. # Since git clone doesn't succeed if the non-empty source_dir exists,
  908. # create a cmake script to invoke as download command.
  909. # The script will delete the source directory and then call git clone.
  910. #
  911. _ep_write_gitclone_script(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
  912. ${GIT_EXECUTABLE} ${git_repository} ${git_tag} ${src_name} ${work_dir}
  913. )
  914. set(comment "Performing download step (git clone) for '${name}'")
  915. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
  916. list(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
  917. elseif(url)
  918. get_filename_component(work_dir "${source_dir}" PATH)
  919. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  920. set(repository "external project URL")
  921. set(module "${url}")
  922. set(tag "${md5}")
  923. configure_file(
  924. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  925. "${stamp_dir}/${name}-urlinfo.txt"
  926. @ONLY
  927. )
  928. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  929. if(IS_DIRECTORY "${url}")
  930. get_filename_component(abs_dir "${url}" ABSOLUTE)
  931. set(comment "Performing download step (DIR copy) for '${name}'")
  932. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  933. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  934. else()
  935. if("${url}" MATCHES "^[a-z]+://")
  936. # TODO: Should download and extraction be different steps?
  937. string(REGEX MATCH "[^/]*$" fname "${url}")
  938. if(NOT "${fname}" MATCHES "\\.(bz2|tar|tgz|tar\\.gz|zip)$")
  939. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  940. endif()
  941. set(file ${download_dir}/${fname})
  942. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  943. _ep_write_downloadfile_script("${stamp_dir}/download-${name}.cmake" "${url}" "${file}" "${timeout}" "${md5}")
  944. set(cmd ${CMAKE_COMMAND} -P ${stamp_dir}/download-${name}.cmake
  945. COMMAND)
  946. set(comment "Performing download step (download, verify and extract) for '${name}'")
  947. else()
  948. set(file "${url}")
  949. set(comment "Performing download step (verify and extract) for '${name}'")
  950. endif()
  951. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${md5}")
  952. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake)
  953. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${name}" "${file}" "${source_dir}")
  954. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  955. endif()
  956. else()
  957. _ep_is_dir_empty("${source_dir}" empty)
  958. if(${empty})
  959. 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")
  960. endif()
  961. endif()
  962. get_property(log TARGET ${name} PROPERTY _EP_LOG_DOWNLOAD)
  963. if(log)
  964. set(log LOG 1)
  965. else()
  966. set(log "")
  967. endif()
  968. ExternalProject_Add_Step(${name} download
  969. COMMENT ${comment}
  970. COMMAND ${cmd}
  971. WORKING_DIRECTORY ${work_dir}
  972. DEPENDS ${depends}
  973. DEPENDEES mkdir
  974. ${log}
  975. )
  976. endfunction(_ep_add_download_command)
  977. function(_ep_add_update_command name)
  978. ExternalProject_Get_Property(${name} source_dir)
  979. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  980. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  981. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  982. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  983. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  984. set(work_dir)
  985. set(comment)
  986. set(always)
  987. if(cmd_set)
  988. set(work_dir ${source_dir})
  989. elseif(cvs_repository)
  990. if(NOT CVS_EXECUTABLE)
  991. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  992. endif()
  993. set(work_dir ${source_dir})
  994. set(comment "Performing update step (CVS update) for '${name}'")
  995. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  996. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  997. set(always 1)
  998. elseif(svn_repository)
  999. if(NOT Subversion_SVN_EXECUTABLE)
  1000. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  1001. endif()
  1002. set(work_dir ${source_dir})
  1003. set(comment "Performing update step (SVN update) for '${name}'")
  1004. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  1005. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  1006. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  1007. set(svn_user_pw_args "")
  1008. if(svn_username)
  1009. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  1010. endif()
  1011. if(svn_password)
  1012. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  1013. endif()
  1014. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision}
  1015. ${svn_user_pw_args})
  1016. set(always 1)
  1017. elseif(git_repository)
  1018. if(NOT GIT_EXECUTABLE)
  1019. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  1020. endif()
  1021. set(work_dir ${source_dir})
  1022. set(comment "Performing update step (git fetch) for '${name}'")
  1023. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1024. if(NOT git_tag)
  1025. set(git_tag "master")
  1026. endif()
  1027. set(cmd ${GIT_EXECUTABLE} fetch
  1028. COMMAND ${GIT_EXECUTABLE} checkout ${git_tag}
  1029. COMMAND ${GIT_EXECUTABLE} submodule update --recursive
  1030. )
  1031. set(always 1)
  1032. endif()
  1033. get_property(log TARGET ${name} PROPERTY _EP_LOG_UPDATE)
  1034. if(log)
  1035. set(log LOG 1)
  1036. else()
  1037. set(log "")
  1038. endif()
  1039. ExternalProject_Add_Step(${name} update
  1040. COMMENT ${comment}
  1041. COMMAND ${cmd}
  1042. ALWAYS ${always}
  1043. WORKING_DIRECTORY ${work_dir}
  1044. DEPENDEES download
  1045. ${log}
  1046. )
  1047. endfunction(_ep_add_update_command)
  1048. function(_ep_add_patch_command name)
  1049. ExternalProject_Get_Property(${name} source_dir)
  1050. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  1051. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  1052. set(work_dir)
  1053. if(cmd_set)
  1054. set(work_dir ${source_dir})
  1055. endif()
  1056. ExternalProject_Add_Step(${name} patch
  1057. COMMAND ${cmd}
  1058. WORKING_DIRECTORY ${work_dir}
  1059. DEPENDEES download
  1060. )
  1061. endfunction(_ep_add_patch_command)
  1062. # TODO: Make sure external projects use the proper compiler
  1063. function(_ep_add_configure_command name)
  1064. ExternalProject_Get_Property(${name} source_dir binary_dir tmp_dir)
  1065. _ep_get_configuration_subdir_suffix(cfgdir)
  1066. # Depend on other external projects (file-level).
  1067. set(file_deps)
  1068. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1069. foreach(dep IN LISTS deps)
  1070. get_property(dep_stamp_dir TARGET ${dep} PROPERTY _EP_STAMP_DIR)
  1071. list(APPEND file_deps ${dep_stamp_dir}${cfgdir}/${dep}-done)
  1072. endforeach()
  1073. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  1074. if(cmd_set)
  1075. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  1076. else()
  1077. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1078. if(cmake_command)
  1079. set(cmd "${cmake_command}")
  1080. else()
  1081. set(cmd "${CMAKE_COMMAND}")
  1082. endif()
  1083. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  1084. list(APPEND cmd ${cmake_args})
  1085. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1086. if(cmake_generator)
  1087. list(APPEND cmd "-G${cmake_generator}" "${source_dir}")
  1088. else()
  1089. list(APPEND cmd "-G${CMAKE_GENERATOR}" "${source_dir}")
  1090. endif()
  1091. endif()
  1092. # If anything about the configure command changes, (command itself, cmake
  1093. # used, cmake args or cmake generator) then re-run the configure step.
  1094. # Fixes issue http://public.kitware.com/Bug/view.php?id=10258
  1095. #
  1096. if(NOT EXISTS ${tmp_dir}/${name}-cfgcmd.txt.in)
  1097. file(WRITE ${tmp_dir}/${name}-cfgcmd.txt.in "cmd='@cmd@'\n")
  1098. endif()
  1099. configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt)
  1100. list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt)
  1101. get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE)
  1102. if(log)
  1103. set(log LOG 1)
  1104. else()
  1105. set(log "")
  1106. endif()
  1107. ExternalProject_Add_Step(${name} configure
  1108. COMMAND ${cmd}
  1109. WORKING_DIRECTORY ${binary_dir}
  1110. DEPENDEES update patch
  1111. DEPENDS ${file_deps}
  1112. ${log}
  1113. )
  1114. endfunction(_ep_add_configure_command)
  1115. function(_ep_add_build_command name)
  1116. ExternalProject_Get_Property(${name} binary_dir)
  1117. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  1118. if(cmd_set)
  1119. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  1120. else()
  1121. _ep_get_build_command(${name} BUILD cmd)
  1122. endif()
  1123. get_property(log TARGET ${name} PROPERTY _EP_LOG_BUILD)
  1124. if(log)
  1125. set(log LOG 1)
  1126. else()
  1127. set(log "")
  1128. endif()
  1129. ExternalProject_Add_Step(${name} build
  1130. COMMAND ${cmd}
  1131. WORKING_DIRECTORY ${binary_dir}
  1132. DEPENDEES configure
  1133. ${log}
  1134. )
  1135. endfunction(_ep_add_build_command)
  1136. function(_ep_add_install_command name)
  1137. ExternalProject_Get_Property(${name} binary_dir)
  1138. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  1139. if(cmd_set)
  1140. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  1141. else()
  1142. _ep_get_build_command(${name} INSTALL cmd)
  1143. endif()
  1144. get_property(log TARGET ${name} PROPERTY _EP_LOG_INSTALL)
  1145. if(log)
  1146. set(log LOG 1)
  1147. else()
  1148. set(log "")
  1149. endif()
  1150. ExternalProject_Add_Step(${name} install
  1151. COMMAND ${cmd}
  1152. WORKING_DIRECTORY ${binary_dir}
  1153. DEPENDEES build
  1154. ${log}
  1155. )
  1156. endfunction(_ep_add_install_command)
  1157. function(_ep_add_test_command name)
  1158. ExternalProject_Get_Property(${name} binary_dir)
  1159. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  1160. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  1161. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  1162. # Only actually add the test step if one of the test related properties is
  1163. # explicitly set. (i.e. the test step is omitted unless requested...)
  1164. #
  1165. if(cmd_set OR before OR after)
  1166. if(cmd_set)
  1167. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  1168. else()
  1169. _ep_get_build_command(${name} TEST cmd)
  1170. endif()
  1171. if(before)
  1172. set(dep_args DEPENDEES build DEPENDERS install)
  1173. else()
  1174. set(dep_args DEPENDEES install)
  1175. endif()
  1176. get_property(log TARGET ${name} PROPERTY _EP_LOG_TEST)
  1177. if(log)
  1178. set(log LOG 1)
  1179. else()
  1180. set(log "")
  1181. endif()
  1182. ExternalProject_Add_Step(${name} test
  1183. COMMAND ${cmd}
  1184. WORKING_DIRECTORY ${binary_dir}
  1185. ${dep_args}
  1186. ${log}
  1187. )
  1188. endif()
  1189. endfunction(_ep_add_test_command)
  1190. function(ExternalProject_Add name)
  1191. _ep_get_configuration_subdir_suffix(cfgdir)
  1192. # Add a custom target for the external project.
  1193. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  1194. add_custom_target(${name} ALL DEPENDS ${cmf_dir}${cfgdir}/${name}-complete)
  1195. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  1196. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  1197. _ep_set_directories(${name})
  1198. ExternalProject_Get_Property(${name} stamp_dir)
  1199. # The 'complete' step depends on all other steps and creates a
  1200. # 'done' mark. A dependent external project's 'configure' step
  1201. # depends on the 'done' mark so that it rebuilds when this project
  1202. # rebuilds. It is important that 'done' is not the output of any
  1203. # custom command so that CMake does not propagate build rules to
  1204. # other external project targets.
  1205. add_custom_command(
  1206. OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
  1207. COMMENT "Completed '${name}'"
  1208. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  1209. COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}${cfgdir}/${name}-complete
  1210. COMMAND ${CMAKE_COMMAND} -E touch ${stamp_dir}${cfgdir}/${name}-done
  1211. DEPENDS ${stamp_dir}${cfgdir}/${name}-install
  1212. VERBATIM
  1213. )
  1214. # Depend on other external projects (target-level).
  1215. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1216. foreach(arg IN LISTS deps)
  1217. add_dependencies(${name} ${arg})
  1218. endforeach()
  1219. # Set up custom build steps based on the target properties.
  1220. # Each step depends on the previous one.
  1221. #
  1222. # The target depends on the output of the final step.
  1223. # (Already set up above in the DEPENDS of the add_custom_target command.)
  1224. #
  1225. _ep_add_mkdir_command(${name})
  1226. _ep_add_download_command(${name})
  1227. _ep_add_update_command(${name})
  1228. _ep_add_patch_command(${name})
  1229. _ep_add_configure_command(${name})
  1230. _ep_add_build_command(${name})
  1231. _ep_add_install_command(${name})
  1232. # Test is special in that it might depend on build, or it might depend
  1233. # on install.
  1234. #
  1235. _ep_add_test_command(${name})
  1236. endfunction(ExternalProject_Add)