ExternalProject.cmake 28 KB

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