ExternalProject.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 PREIFX 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_set_directories name)
  174. get_property(prefix TARGET ${name} PROPERTY _EP_PREFIX)
  175. if(NOT prefix)
  176. get_property(prefix DIRECTORY PROPERTY EP_PREFIX)
  177. if(NOT prefix)
  178. get_property(base DIRECTORY PROPERTY EP_BASE)
  179. if(NOT base)
  180. set(prefix "${name}-prefix")
  181. endif()
  182. endif()
  183. endif()
  184. if(prefix)
  185. set(tmp_default "${prefix}/tmp")
  186. set(download_default "${prefix}/src")
  187. set(source_default "${prefix}/src/${name}")
  188. set(binary_default "${prefix}/src/${name}-build")
  189. set(stamp_default "${prefix}/src/${name}-stamp")
  190. set(install_default "${prefix}")
  191. else() # assert(base)
  192. set(tmp_default "${base}/tmp/${name}")
  193. set(download_default "${base}/Download/${name}")
  194. set(source_default "${base}/Source/${name}")
  195. set(binary_default "${base}/Build/${name}")
  196. set(stamp_default "${base}/Stamp/${name}")
  197. set(install_default "${base}/Install/${name}")
  198. endif()
  199. get_property(build_in_source TARGET ${name} PROPERTY _EP_BUILD_IN_SOURCE)
  200. if(build_in_source)
  201. get_property(have_binary_dir TARGET ${name} PROPERTY _EP_BINARY_DIR SET)
  202. if(have_binary_dir)
  203. message(FATAL_ERROR
  204. "External project ${name} has both BINARY_DIR and BUILD_IN_SOURCE!")
  205. endif()
  206. endif()
  207. set(top "${CMAKE_CURRENT_BINARY_DIR}")
  208. set(places stamp download source binary install tmp)
  209. foreach(var ${places})
  210. string(TOUPPER "${var}" VAR)
  211. get_property(${var}_dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  212. if(NOT ${var}_dir)
  213. set(${var}_dir "${${var}_default}")
  214. endif()
  215. if(NOT IS_ABSOLUTE "${${var}_dir}")
  216. get_filename_component(${var}_dir "${top}/${${var}_dir}" ABSOLUTE)
  217. endif()
  218. set_property(TARGET ${name} PROPERTY _EP_${VAR}_DIR "${${var}_dir}")
  219. endforeach()
  220. if(build_in_source)
  221. get_property(source_dir TARGET ${name} PROPERTY _EP_SOURCE_DIR)
  222. set_property(TARGET ${name} PROPERTY _EP_BINARY_DIR "${source_dir}")
  223. endif()
  224. # Make the directories at CMake configure time *and* add a custom command
  225. # to make them at build time. They need to exist at makefile generation
  226. # time for Borland make and wmake so that CMake may generate makefiles
  227. # with "cd C:\short\paths\with\no\spaces" commands in them.
  228. #
  229. # Additionally, the add_custom_command is still used in case somebody
  230. # removes one of the necessary directories and tries to rebuild without
  231. # re-running cmake.
  232. foreach(var ${places})
  233. string(TOUPPER "${var}" VAR)
  234. get_property(dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  235. file(MAKE_DIRECTORY "${dir}")
  236. if(NOT EXISTS "${dir}")
  237. message(FATAL_ERROR "dir '${dir}' does not exist after file(MAKE_DIRECTORY)")
  238. endif()
  239. endforeach()
  240. endfunction(_ep_set_directories)
  241. function(ep_get name)
  242. foreach(var ${ARGN})
  243. string(TOUPPER "${var}" VAR)
  244. get_property(${var} TARGET ${name} PROPERTY _EP_${VAR})
  245. if(NOT ${var})
  246. message(FATAL_ERROR "External project \"${name}\" has no ${var}")
  247. endif()
  248. set(${var} "${${var}}" PARENT_SCOPE)
  249. endforeach()
  250. endfunction(ep_get)
  251. function(_ep_get_configure_command_id name cfg_cmd_id_var)
  252. get_target_property(cmd ${name} _EP_CONFIGURE_COMMAND)
  253. if(cmd STREQUAL "")
  254. # Explicit empty string means no configure step for this project
  255. set(${cfg_cmd_id_var} "none" PARENT_SCOPE)
  256. else()
  257. if(NOT cmd)
  258. # Default is "use cmake":
  259. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  260. else()
  261. # Otherwise we have to analyze the value:
  262. if(cmd MATCHES "^[^;]*/configure")
  263. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  264. elseif(cmd MATCHES "^[^;]*/cmake" AND NOT cmd MATCHES ";-[PE];")
  265. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  266. elseif(cmd MATCHES "config")
  267. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  268. else()
  269. set(${cfg_cmd_id_var} "unknown:${cmd}" PARENT_SCOPE)
  270. endif()
  271. endif()
  272. endif()
  273. endfunction(_ep_get_configure_command_id)
  274. function(_ep_get_build_command name step cmd_var)
  275. set(cmd "${${cmd_var}}")
  276. if(NOT cmd)
  277. set(args)
  278. _ep_get_configure_command_id(${name} cfg_cmd_id)
  279. if(cfg_cmd_id STREQUAL "cmake")
  280. # CMake project. Select build command based on generator.
  281. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  282. if("${cmake_generator}" MATCHES "Make" AND
  283. "${cmake_generator}" STREQUAL "${CMAKE_GENERATOR}")
  284. # The project uses the same Makefile generator. Use recursive make.
  285. set(cmd "$(MAKE)")
  286. if(step STREQUAL "INSTALL")
  287. set(args install)
  288. endif()
  289. else()
  290. # Drive the project with "cmake --build".
  291. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  292. if(cmake_command)
  293. set(cmd "${cmake_command}")
  294. else()
  295. set(cmd "${CMAKE_COMMAND}")
  296. endif()
  297. set(args --build ${binary_dir} --config ${CMAKE_CFG_INTDIR})
  298. if(step STREQUAL "INSTALL")
  299. list(APPEND args --target install)
  300. endif()
  301. endif()
  302. else() # if(cfg_cmd_id STREQUAL "configure")
  303. # Non-CMake project. Guess "make" and "make install".
  304. set(cmd "make")
  305. if(step STREQUAL "INSTALL")
  306. set(args install)
  307. endif()
  308. endif()
  309. # Use user-specified arguments instead of default arguments, if any.
  310. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  311. if(have_args)
  312. get_target_property(args ${name} _EP_${step}_ARGS)
  313. endif()
  314. list(APPEND cmd ${args})
  315. endif()
  316. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  317. endfunction(_ep_get_build_command)
  318. function(ep_add_step name step)
  319. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  320. ep_get(${name} stamp_dir)
  321. add_custom_command(APPEND
  322. OUTPUT ${cmf_dir}/${name}-complete
  323. DEPENDS ${stamp_dir}/${name}-${step}
  324. )
  325. _ep_parse_arguments(ep_add_step
  326. ${name} _EP_${step}_ "${ARGN}")
  327. # Steps depending on this step.
  328. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  329. foreach(depender IN LISTS dependers)
  330. add_custom_command(APPEND
  331. OUTPUT ${stamp_dir}/${name}-${depender}
  332. DEPENDS ${stamp_dir}/${name}-${step}
  333. )
  334. endforeach()
  335. # Dependencies on files.
  336. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  337. # Dependencies on steps.
  338. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  339. foreach(dependee IN LISTS dependees)
  340. list(APPEND depends ${stamp_dir}/${name}-${dependee})
  341. endforeach()
  342. # The command to run.
  343. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  344. if(command)
  345. set(comment "Performing ${step} step for '${name}'")
  346. else()
  347. set(comment "No ${step} step for '${name}'")
  348. endif()
  349. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  350. # Replace list separators.
  351. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  352. if(sep AND command)
  353. string(REPLACE "${sep}" "\\;" command "${command}")
  354. endif()
  355. # Replace location tags.
  356. foreach(var comment command work_dir)
  357. if(${var})
  358. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  359. get_property(val TARGET ${name} PROPERTY _EP_${dir})
  360. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  361. endforeach()
  362. endif()
  363. endforeach()
  364. # Custom comment?
  365. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  366. if(comment_set)
  367. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  368. endif()
  369. # Run every time?
  370. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  371. if(always)
  372. set_property(SOURCE ${stamp_dir}/${name}-${step} PROPERTY SYMBOLIC 1)
  373. set(touch)
  374. else()
  375. set(touch ${CMAKE_COMMAND} -E touch ${stamp_dir}/${name}-${step})
  376. endif()
  377. add_custom_command(
  378. OUTPUT ${stamp_dir}/${name}-${step}
  379. COMMENT ${comment}
  380. COMMAND ${command}
  381. COMMAND ${touch}
  382. DEPENDS ${depends}
  383. WORKING_DIRECTORY ${work_dir}
  384. VERBATIM
  385. )
  386. endfunction(ep_add_step)
  387. function(_ep_add_mkdir_command name)
  388. ep_get(${name}
  389. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  390. ep_add_step(${name} mkdir
  391. COMMENT "Creating directories for '${name}'"
  392. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  393. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  394. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  395. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  396. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}
  397. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  398. )
  399. endfunction(_ep_add_mkdir_command)
  400. function(_ep_add_download_command name)
  401. ep_get(${name} source_dir stamp_dir download_dir tmp_dir)
  402. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  403. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  404. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  405. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  406. get_property(url TARGET ${name} PROPERTY _EP_URL)
  407. # TODO: Perhaps file:// should be copied to download dir before extraction.
  408. string(REGEX REPLACE "^file://" "" url "${url}")
  409. set(depends)
  410. set(comment)
  411. set(work_dir)
  412. if(cmd_set)
  413. set(work_dir ${download_dir})
  414. elseif(cvs_repository)
  415. find_package(CVS)
  416. if(NOT CVS_EXECUTABLE)
  417. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  418. endif()
  419. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  420. if(NOT cvs_module)
  421. message(FATAL_ERROR "error: no CVS_MODULE")
  422. endif()
  423. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  424. set(repository ${cvs_repository})
  425. set(module ${cvs_module})
  426. set(tag ${cvs_tag})
  427. configure_file(
  428. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  429. "${stamp_dir}/${name}-cvsinfo.txt"
  430. @ONLY
  431. )
  432. get_filename_component(src_name "${source_dir}" NAME)
  433. get_filename_component(work_dir "${source_dir}" PATH)
  434. set(comment "Performing download step (CVS checkout) for '${name}'")
  435. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  436. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  437. elseif(svn_repository)
  438. find_package(Subversion)
  439. if(NOT Subversion_SVN_EXECUTABLE)
  440. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  441. endif()
  442. get_property(svn_tag TARGET ${name} PROPERTY _EP_SVN_TAG)
  443. set(repository ${svn_repository})
  444. set(module)
  445. set(tag ${svn_tag})
  446. configure_file(
  447. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  448. "${stamp_dir}/${name}-svninfo.txt"
  449. @ONLY
  450. )
  451. get_filename_component(src_name "${source_dir}" NAME)
  452. get_filename_component(work_dir "${source_dir}" PATH)
  453. set(comment "Performing download step (SVN checkout) for '${name}'")
  454. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_tag} ${src_name})
  455. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  456. elseif(url)
  457. get_filename_component(work_dir "${source_dir}" PATH)
  458. set(repository "external project URL")
  459. set(module "${url}")
  460. set(tag "")
  461. configure_file(
  462. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  463. "${stamp_dir}/${name}-urlinfo.txt"
  464. @ONLY
  465. )
  466. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  467. if(IS_DIRECTORY "${url}")
  468. get_filename_component(abs_dir "${url}" ABSOLUTE)
  469. set(comment "Performing download step (DIR copy) for '${name}'")
  470. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  471. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  472. else()
  473. if("${url}" MATCHES "^[a-z]+://")
  474. # TODO: Should download and extraction be different steps?
  475. string(REGEX MATCH "\\.(tar|tgz|tar\\.gz)" ext "${url}")
  476. set(file ${download_dir}/${name}${ext})
  477. set(cmd ${CMAKE_COMMAND} -Dremote=${url} -Dlocal=${file} -P ${CMAKE_ROOT}/Modules/DownloadFile.cmake
  478. COMMAND)
  479. set(comment "Performing download step (download and extract) for '${name}'")
  480. else()
  481. set(file "${url}")
  482. set(comment "Performing download step (extract) for '${name}'")
  483. endif()
  484. # TODO: Support other archive formats.
  485. list(APPEND cmd ${CMAKE_COMMAND} -Dfilename=${file} -Dtmp=${tmp_dir} -Ddirectory=${source_dir} -P ${CMAKE_ROOT}/Modules/UntarFile.cmake)
  486. endif()
  487. else()
  488. message(SEND_ERROR "error: no download info for '${name}'")
  489. endif()
  490. ep_add_step(${name} download
  491. COMMENT ${comment}
  492. COMMAND ${cmd}
  493. WORKING_DIRECTORY ${work_dir}
  494. DEPENDS ${depends}
  495. DEPENDEES mkdir
  496. )
  497. endfunction(_ep_add_download_command)
  498. function(_ep_add_update_command name)
  499. ep_get(${name} source_dir)
  500. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  501. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  502. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  503. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  504. set(work_dir)
  505. set(comment)
  506. set(always)
  507. if(cmd_set)
  508. set(work_dir ${source_dir})
  509. elseif(cvs_repository)
  510. if(NOT CVS_EXECUTABLE)
  511. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  512. endif()
  513. set(work_dir ${source_dir})
  514. set(comment "Performing update step (CVS update) for '${name}'")
  515. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  516. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  517. set(always 1)
  518. elseif(svn_repository)
  519. if(NOT Subversion_SVN_EXECUTABLE)
  520. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  521. endif()
  522. set(work_dir ${source_dir})
  523. set(comment "Performing update step (SVN update) for '${name}'")
  524. get_property(svn_tag TARGET ${name} PROPERTY _EP_SVN_TAG)
  525. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_tag})
  526. set(always 1)
  527. endif()
  528. ep_add_step(${name} update
  529. COMMENT ${comment}
  530. COMMAND ${cmd}
  531. ALWAYS ${always}
  532. WORKING_DIRECTORY ${work_dir}
  533. DEPENDEES download
  534. )
  535. endfunction(_ep_add_update_command)
  536. function(_ep_add_patch_command name)
  537. ep_get(${name} source_dir)
  538. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  539. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  540. set(work_dir)
  541. if(cmd_set)
  542. set(work_dir ${source_dir})
  543. endif()
  544. ep_add_step(${name} patch
  545. COMMAND ${cmd}
  546. WORKING_DIRECTORY ${work_dir}
  547. DEPENDEES download
  548. )
  549. endfunction(_ep_add_patch_command)
  550. # TODO: Make sure external projects use the proper compiler
  551. function(_ep_add_configure_command name)
  552. ep_get(${name} source_dir binary_dir)
  553. # Depend on other external projects (file-level).
  554. set(file_deps)
  555. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  556. foreach(dep IN LISTS deps)
  557. get_property(dep_stamp_dir TARGET ${dep} PROPERTY _EP_STAMP_DIR)
  558. list(APPEND file_deps ${dep_stamp_dir}/${dep}-done)
  559. endforeach()
  560. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  561. if(cmd_set)
  562. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  563. else()
  564. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  565. if(cmake_command)
  566. set(cmd "${cmake_command}")
  567. else()
  568. set(cmd "${CMAKE_COMMAND}")
  569. endif()
  570. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  571. list(APPEND cmd ${cmake_args})
  572. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  573. if(cmake_generator)
  574. list(APPEND cmd "-G${cmake_generator}" "${source_dir}")
  575. endif()
  576. endif()
  577. ep_add_step(${name} configure
  578. COMMAND ${cmd}
  579. WORKING_DIRECTORY ${binary_dir}
  580. DEPENDEES update patch
  581. DEPENDS ${file_deps}
  582. )
  583. endfunction(_ep_add_configure_command)
  584. function(_ep_add_build_command name)
  585. ep_get(${name} binary_dir)
  586. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  587. if(cmd_set)
  588. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  589. else()
  590. _ep_get_build_command(${name} BUILD cmd)
  591. endif()
  592. ep_add_step(${name} build
  593. COMMAND ${cmd}
  594. WORKING_DIRECTORY ${binary_dir}
  595. DEPENDEES configure
  596. )
  597. endfunction(_ep_add_build_command)
  598. function(_ep_add_install_command name)
  599. ep_get(${name} binary_dir)
  600. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  601. if(cmd_set)
  602. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  603. else()
  604. _ep_get_build_command(${name} INSTALL cmd)
  605. endif()
  606. ep_add_step(${name} install
  607. COMMAND ${cmd}
  608. WORKING_DIRECTORY ${binary_dir}
  609. DEPENDEES build
  610. )
  611. endfunction(_ep_add_install_command)
  612. function(ep_add name)
  613. # Add a custom target for the external project.
  614. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  615. add_custom_target(${name} ALL DEPENDS ${cmf_dir}/${name}-complete)
  616. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  617. _ep_parse_arguments(ep_add ${name} _EP_ "${ARGN}")
  618. _ep_set_directories(${name})
  619. ep_get(${name} stamp_dir)
  620. # The 'complete' step depends on all other steps and creates a
  621. # 'done' mark. A dependent external project's 'configure' step
  622. # depends on the 'done' mark so that it rebuilds when this project
  623. # rebuilds. It is important that 'done' is not the output of any
  624. # custom command so that CMake does not propagate build rules to
  625. # other external project targets.
  626. add_custom_command(
  627. OUTPUT ${cmf_dir}/${name}-complete
  628. COMMENT "Completed '${name}'"
  629. COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}/${name}-complete
  630. COMMAND ${CMAKE_COMMAND} -E touch ${stamp_dir}/${name}-done
  631. DEPENDS ${stamp_dir}/${name}-install
  632. VERBATIM
  633. )
  634. # Depend on other external projects (target-level).
  635. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  636. foreach(arg IN LISTS deps)
  637. add_dependencies(${name} ${arg})
  638. endforeach()
  639. # Set up custom build steps based on the target properties.
  640. # Each step depends on the previous one.
  641. #
  642. # The target depends on the output of the final step.
  643. # (Already set up above in the DEPENDS of the add_custom_target command.)
  644. #
  645. _ep_add_mkdir_command(${name})
  646. _ep_add_download_command(${name})
  647. _ep_add_update_command(${name})
  648. _ep_add_patch_command(${name})
  649. _ep_add_configure_command(${name})
  650. _ep_add_build_command(${name})
  651. _ep_add_install_command(${name})
  652. endfunction(ep_add)