FetchContent.cmake 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FetchContent
  5. ------------------
  6. .. only:: html
  7. .. contents::
  8. Overview
  9. ^^^^^^^^
  10. This module enables populating content at configure time via any method
  11. supported by the :module:`ExternalProject` module. Whereas
  12. :command:`ExternalProject_Add` downloads at build time, the
  13. ``FetchContent`` module makes content available immediately, allowing the
  14. configure step to use the content in commands like :command:`add_subdirectory`,
  15. :command:`include` or :command:`file` operations.
  16. Content population details would normally be defined separately from the
  17. command that performs the actual population. This separation ensures that
  18. all of the dependency details are defined before anything may try to use those
  19. details to populate content. This is particularly important in more complex
  20. project hierarchies where dependencies may be shared between multiple projects.
  21. The following shows a typical example of declaring content details:
  22. .. code-block:: cmake
  23. FetchContent_Declare(
  24. googletest
  25. GIT_REPOSITORY https://github.com/google/googletest.git
  26. GIT_TAG release-1.8.0
  27. )
  28. For most typical cases, populating the content can then be done with a single
  29. command like so:
  30. .. code-block:: cmake
  31. FetchContent_MakeAvailable(googletest)
  32. The above command not only populates the content, it also adds it to the main
  33. build (if possible) so that the main build can use the populated project's
  34. targets, etc. In some cases, the main project may need to have more precise
  35. control over the population or may be required to explicitly define the
  36. population steps (e.g. if CMake versions earlier than 3.14 need to be
  37. supported). The typical pattern of such custom steps looks like this:
  38. .. code-block:: cmake
  39. FetchContent_GetProperties(googletest)
  40. if(NOT googletest_POPULATED)
  41. FetchContent_Populate(googletest)
  42. add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
  43. endif()
  44. Regardless of which population method is used, when using the
  45. declare-populate pattern with a hierarchical project arrangement, projects at
  46. higher levels in the hierarchy are able to override the population details of
  47. content specified anywhere lower in the project hierarchy. The ability to
  48. detect whether content has already been populated ensures that even if
  49. multiple child projects want certain content to be available, the first one
  50. to populate it wins. The other child project can simply make use of the
  51. already available content instead of repeating the population for itself.
  52. See the :ref:`Examples <fetch-content-examples>` section which demonstrates
  53. this scenario.
  54. The ``FetchContent`` module also supports defining and populating
  55. content in a single call, with no check for whether the content has been
  56. populated elsewhere in the project already. This is a more low level
  57. operation and would not normally be the way the module is used, but it is
  58. sometimes useful as part of implementing some higher level feature or to
  59. populate some content in CMake's script mode.
  60. Declaring Content Details
  61. ^^^^^^^^^^^^^^^^^^^^^^^^^
  62. .. command:: FetchContent_Declare
  63. .. code-block:: cmake
  64. FetchContent_Declare(<name> <contentOptions>...)
  65. The ``FetchContent_Declare()`` function records the options that describe
  66. how to populate the specified content, but if such details have already
  67. been recorded earlier in this project (regardless of where in the project
  68. hierarchy), this and all later calls for the same content ``<name>`` are
  69. ignored. This "first to record, wins" approach is what allows hierarchical
  70. projects to have parent projects override content details of child projects.
  71. The content ``<name>`` can be any string without spaces, but good practice
  72. would be to use only letters, numbers and underscores. The name will be
  73. treated case-insensitively and it should be obvious for the content it
  74. represents, often being the name of the child project or the value given
  75. to its top level :command:`project` command (if it is a CMake project).
  76. For well-known public projects, the name should generally be the official
  77. name of the project. Choosing an unusual name makes it unlikely that other
  78. projects needing that same content will use the same name, leading to
  79. the content being populated multiple times.
  80. The ``<contentOptions>`` can be any of the download or update/patch options
  81. that the :command:`ExternalProject_Add` command understands. The configure,
  82. build, install and test steps are explicitly disabled and therefore options
  83. related to them will be ignored. In most cases, ``<contentOptions>`` will
  84. just be a couple of options defining the download method and method-specific
  85. details like a commit tag or archive hash. For example:
  86. .. code-block:: cmake
  87. FetchContent_Declare(
  88. googletest
  89. GIT_REPOSITORY https://github.com/google/googletest.git
  90. GIT_TAG release-1.8.0
  91. )
  92. FetchContent_Declare(
  93. myCompanyIcons
  94. URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
  95. URL_HASH 5588a7b18261c20068beabfb4f530b87
  96. )
  97. FetchContent_Declare(
  98. myCompanyCertificates
  99. SVN_REPOSITORY svn+ssh://svn.mycompany.com/srv/svn/trunk/certs
  100. SVN_REVISION -r12345
  101. )
  102. Populating The Content
  103. ^^^^^^^^^^^^^^^^^^^^^^
  104. For most common scenarios, population means making content available to the
  105. main build according to previously declared details for that dependency.
  106. There are two main patterns for populating content, one based on calling
  107. :command:`FetchContent_GetProperties` and
  108. :command:`FetchContent_Populate` for more precise control and the other on
  109. calling :command:`FetchContent_MakeAvailable` for a simpler, more automated
  110. approach. The former generally follows this canonical pattern:
  111. .. _`fetch-content-canonical-pattern`:
  112. .. code-block:: cmake
  113. # Check if population has already been performed
  114. FetchContent_GetProperties(<name>)
  115. string(TOLOWER "<name>" lcName)
  116. if(NOT ${lcName}_POPULATED)
  117. # Fetch the content using previously declared details
  118. FetchContent_Populate(<name>)
  119. # Set custom variables, policies, etc.
  120. # ...
  121. # Bring the populated content into the build
  122. add_subdirectory(${${lcName}_SOURCE_DIR} ${${lcName}_BINARY_DIR})
  123. endif()
  124. The above is such a common pattern that, where no custom steps are needed
  125. between the calls to :command:`FetchContent_Populate` and
  126. :command:`add_subdirectory`, equivalent logic can be obtained by calling
  127. :command:`FetchContent_MakeAvailable` instead (and should be preferred where
  128. it meets the needs of the project).
  129. .. command:: FetchContent_Populate
  130. .. code-block:: cmake
  131. FetchContent_Populate( <name> )
  132. In most cases, the only argument given to ``FetchContent_Populate()`` is the
  133. ``<name>``. When used this way, the command assumes the content details have
  134. been recorded by an earlier call to :command:`FetchContent_Declare`. The
  135. details are stored in a global property, so they are unaffected by things
  136. like variable or directory scope. Therefore, it doesn't matter where in the
  137. project the details were previously declared, as long as they have been
  138. declared before the call to ``FetchContent_Populate()``. Those saved details
  139. are then used to construct a call to :command:`ExternalProject_Add` in a
  140. private sub-build to perform the content population immediately. The
  141. implementation of ``ExternalProject_Add()`` ensures that if the content has
  142. already been populated in a previous CMake run, that content will be reused
  143. rather than repopulating them again. For the common case where population
  144. involves downloading content, the cost of the download is only paid once.
  145. An internal global property records when a particular content population
  146. request has been processed. If ``FetchContent_Populate()`` is called more
  147. than once for the same content name within a configure run, the second call
  148. will halt with an error. Projects can and should check whether content
  149. population has already been processed with the
  150. :command:`FetchContent_GetProperties` command before calling
  151. ``FetchContent_Populate()``.
  152. ``FetchContent_Populate()`` will set three variables in the scope of the
  153. caller; ``<lcName>_POPULATED``, ``<lcName>_SOURCE_DIR`` and
  154. ``<lcName>_BINARY_DIR``, where ``<lcName>`` is the lowercased ``<name>``.
  155. ``<lcName>_POPULATED`` will always be set to ``True`` by the call.
  156. ``<lcName>_SOURCE_DIR`` is the location where the
  157. content can be found upon return (it will have already been populated), while
  158. ``<lcName>_BINARY_DIR`` is a directory intended for use as a corresponding
  159. build directory. The main use case for the two directory variables is to
  160. call :command:`add_subdirectory` immediately after population, i.e.:
  161. .. code-block:: cmake
  162. FetchContent_Populate(FooBar ...)
  163. add_subdirectory(${foobar_SOURCE_DIR} ${foobar_BINARY_DIR})
  164. The values of the three variables can also be retrieved from anywhere in the
  165. project hierarchy using the :command:`FetchContent_GetProperties` command.
  166. A number of cache variables influence the behavior of all content population
  167. performed using details saved from a :command:`FetchContent_Declare` call:
  168. ``FETCHCONTENT_BASE_DIR``
  169. In most cases, the saved details do not specify any options relating to the
  170. directories to use for the internal sub-build, final source and build areas.
  171. It is generally best to leave these decisions up to the ``FetchContent``
  172. module to handle on the project's behalf. The ``FETCHCONTENT_BASE_DIR``
  173. cache variable controls the point under which all content population
  174. directories are collected, but in most cases developers would not need to
  175. change this. The default location is ``${CMAKE_BINARY_DIR}/_deps``, but if
  176. developers change this value, they should aim to keep the path short and
  177. just below the top level of the build tree to avoid running into path
  178. length problems on Windows.
  179. ``FETCHCONTENT_QUIET``
  180. The logging output during population can be quite verbose, making the
  181. configure stage quite noisy. This cache option (``ON`` by default) hides
  182. all population output unless an error is encountered. If experiencing
  183. problems with hung downloads, temporarily switching this option off may
  184. help diagnose which content population is causing the issue.
  185. ``FETCHCONTENT_FULLY_DISCONNECTED``
  186. When this option is enabled, no attempt is made to download or update
  187. any content. It is assumed that all content has already been populated in
  188. a previous run or the source directories have been pointed at existing
  189. contents the developer has provided manually (using options described
  190. further below). When the developer knows that no changes have been made to
  191. any content details, turning this option ``ON`` can significantly speed up
  192. the configure stage. It is ``OFF`` by default.
  193. ``FETCHCONTENT_UPDATES_DISCONNECTED``
  194. This is a less severe download/update control compared to
  195. ``FETCHCONTENT_FULLY_DISCONNECTED``. Instead of bypassing all download and
  196. update logic, the ``FETCHCONTENT_UPDATES_DISCONNECTED`` only disables the
  197. update stage. Therefore, if content has not been downloaded previously,
  198. it will still be downloaded when this option is enabled. This can speed up
  199. the configure stage, but not as much as
  200. ``FETCHCONTENT_FULLY_DISCONNECTED``. It is ``OFF`` by default.
  201. In addition to the above cache variables, the following cache variables are
  202. also defined for each content name (``<ucName>`` is the uppercased value of
  203. ``<name>``):
  204. ``FETCHCONTENT_SOURCE_DIR_<ucName>``
  205. If this is set, no download or update steps are performed for the specified
  206. content and the ``<lcName>_SOURCE_DIR`` variable returned to the caller is
  207. pointed at this location. This gives developers a way to have a separate
  208. checkout of the content that they can modify freely without interference
  209. from the build. The build simply uses that existing source, but it still
  210. defines ``<lcName>_BINARY_DIR`` to point inside its own build area.
  211. Developers are strongly encouraged to use this mechanism rather than
  212. editing the sources populated in the default location, as changes to
  213. sources in the default location can be lost when content population details
  214. are changed by the project.
  215. ``FETCHCONTENT_UPDATES_DISCONNECTED_<ucName>``
  216. This is the per-content equivalent of
  217. ``FETCHCONTENT_UPDATES_DISCONNECTED``. If the global option or this option
  218. is ``ON``, then updates will be disabled for the named content.
  219. Disabling updates for individual content can be useful for content whose
  220. details rarely change, while still leaving other frequently changing
  221. content with updates enabled.
  222. The ``FetchContent_Populate()`` command also supports a syntax allowing the
  223. content details to be specified directly rather than using any saved
  224. details. This is more low-level and use of this form is generally to be
  225. avoided in favour of using saved content details as outlined above.
  226. Nevertheless, in certain situations it can be useful to invoke the content
  227. population as an isolated operation (typically as part of implementing some
  228. other higher level feature or when using CMake in script mode):
  229. .. code-block:: cmake
  230. FetchContent_Populate( <name>
  231. [QUIET]
  232. [SUBBUILD_DIR <subBuildDir>]
  233. [SOURCE_DIR <srcDir>]
  234. [BINARY_DIR <binDir>]
  235. ...
  236. )
  237. This form has a number of key differences to that where only ``<name>`` is
  238. provided:
  239. - All required population details are assumed to have been provided directly
  240. in the call to ``FetchContent_Populate()``. Any saved details for
  241. ``<name>`` are ignored.
  242. - No check is made for whether content for ``<name>`` has already been
  243. populated.
  244. - No global property is set to record that the population has occurred.
  245. - No global properties record the source or binary directories used for the
  246. populated content.
  247. - The ``FETCHCONTENT_FULLY_DISCONNECTED`` and
  248. ``FETCHCONTENT_UPDATES_DISCONNECTED`` cache variables are ignored.
  249. The ``<lcName>_SOURCE_DIR`` and ``<lcName>_BINARY_DIR`` variables are still
  250. returned to the caller, but since these locations are not stored as global
  251. properties when this form is used, they are only available to the calling
  252. scope and below rather than the entire project hierarchy. No
  253. ``<lcName>_POPULATED`` variable is set in the caller's scope with this form.
  254. The supported options for ``FetchContent_Populate()`` are the same as those
  255. for :command:`FetchContent_Declare()`. Those few options shown just
  256. above are either specific to ``FetchContent_Populate()`` or their behavior is
  257. slightly modified from how :command:`ExternalProject_Add` treats them.
  258. ``QUIET``
  259. The ``QUIET`` option can be given to hide the output associated with
  260. populating the specified content. If the population fails, the output will
  261. be shown regardless of whether this option was given or not so that the
  262. cause of the failure can be diagnosed. The global ``FETCHCONTENT_QUIET``
  263. cache variable has no effect on ``FetchContent_Populate()`` calls where the
  264. content details are provided directly.
  265. ``SUBBUILD_DIR``
  266. The ``SUBBUILD_DIR`` argument can be provided to change the location of the
  267. sub-build created to perform the population. The default value is
  268. ``${CMAKE_CURRENT_BINARY_DIR}/<lcName>-subbuild`` and it would be unusual
  269. to need to override this default. If a relative path is specified, it will
  270. be interpreted as relative to :variable:`CMAKE_CURRENT_BINARY_DIR`.
  271. ``SOURCE_DIR``, ``BINARY_DIR``
  272. The ``SOURCE_DIR`` and ``BINARY_DIR`` arguments are supported by
  273. :command:`ExternalProject_Add`, but different default values are used by
  274. ``FetchContent_Populate()``. ``SOURCE_DIR`` defaults to
  275. ``${CMAKE_CURRENT_BINARY_DIR}/<lcName>-src`` and ``BINARY_DIR`` defaults to
  276. ``${CMAKE_CURRENT_BINARY_DIR}/<lcName>-build``. If a relative path is
  277. specified, it will be interpreted as relative to
  278. :variable:`CMAKE_CURRENT_BINARY_DIR`.
  279. In addition to the above explicit options, any other unrecognized options are
  280. passed through unmodified to :command:`ExternalProject_Add` to perform the
  281. download, patch and update steps. The following options are explicitly
  282. prohibited (they are disabled by the ``FetchContent_Populate()`` command):
  283. - ``CONFIGURE_COMMAND``
  284. - ``BUILD_COMMAND``
  285. - ``INSTALL_COMMAND``
  286. - ``TEST_COMMAND``
  287. If using ``FetchContent_Populate()`` within CMake's script mode, be aware
  288. that the implementation sets up a sub-build which therefore requires a CMake
  289. generator and build tool to be available. If these cannot be found by
  290. default, then the :variable:`CMAKE_GENERATOR` and/or
  291. :variable:`CMAKE_MAKE_PROGRAM` variables will need to be set appropriately
  292. on the command line invoking the script.
  293. .. command:: FetchContent_GetProperties
  294. When using saved content details, a call to :command:`FetchContent_Populate`
  295. records information in global properties which can be queried at any time.
  296. This information includes the source and binary directories associated with
  297. the content and also whether or not the content population has been processed
  298. during the current configure run.
  299. .. code-block:: cmake
  300. FetchContent_GetProperties( <name>
  301. [SOURCE_DIR <srcDirVar>]
  302. [BINARY_DIR <binDirVar>]
  303. [POPULATED <doneVar>]
  304. )
  305. The ``SOURCE_DIR``, ``BINARY_DIR`` and ``POPULATED`` options can be used to
  306. specify which properties should be retrieved. Each option accepts a value
  307. which is the name of the variable in which to store that property. Most of
  308. the time though, only ``<name>`` is given, in which case the call will then
  309. set the same variables as a call to
  310. :command:`FetchContent_Populate(name) <FetchContent_Populate>`. This allows
  311. the following canonical pattern to be used, which ensures that the relevant
  312. variables will always be defined regardless of whether or not the population
  313. has been performed elsewhere in the project already:
  314. .. code-block:: cmake
  315. FetchContent_GetProperties(foobar)
  316. if(NOT foobar_POPULATED)
  317. FetchContent_Populate(foobar)
  318. ...
  319. endif()
  320. The above pattern allows other parts of the overall project hierarchy to
  321. re-use the same content and ensure that it is only populated once.
  322. .. command:: FetchContent_MakeAvailable
  323. .. code-block:: cmake
  324. FetchContent_MakeAvailable( <name1> [<name2>...] )
  325. This command implements the common pattern typically needed for most
  326. dependencies. It iterates over each of the named dependencies in turn
  327. and for each one it loosely follows the same
  328. :ref:`canonical pattern <fetch-content-canonical-pattern>` as
  329. presented at the beginning of this section. One small difference to
  330. that pattern is that it will only call :command:`add_subdirectory` on the
  331. populated content if there is a ``CMakeLists.txt`` file in its top level
  332. source directory. This allows the command to be used for dependencies
  333. that make downloaded content available at a known location but which do
  334. not need or support being added directly to the build.
  335. .. _`fetch-content-examples`:
  336. Examples
  337. ^^^^^^^^
  338. This first fairly straightforward example ensures that some popular testing
  339. frameworks are available to the main build:
  340. .. code-block:: cmake
  341. include(FetchContent)
  342. FetchContent_Declare(
  343. googletest
  344. GIT_REPOSITORY https://github.com/google/googletest.git
  345. GIT_TAG release-1.8.0
  346. )
  347. FetchContent_Declare(
  348. Catch2
  349. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  350. GIT_TAG v2.5.0
  351. )
  352. # After the following call, the CMake targets defined by googletest and
  353. # Catch2 will be defined and available to the rest of the build
  354. FetchContent_MakeAvailable(googletest Catch2)
  355. In more complex project hierarchies, the dependency relationships can be more
  356. complicated. Consider a hierarchy where ``projA`` is the top level project and
  357. it depends directly on projects ``projB`` and ``projC``. Both ``projB`` and
  358. ``projC`` can be built standalone and they also both depend on another project
  359. ``projD``. ``projB`` additionally depends on ``projE``. This example assumes
  360. that all five projects are available on a company git server. The
  361. ``CMakeLists.txt`` of each project might have sections like the following:
  362. *projA*:
  363. .. code-block:: cmake
  364. include(FetchContent)
  365. FetchContent_Declare(
  366. projB
  367. GIT_REPOSITORY git@mycompany.com:git/projB.git
  368. GIT_TAG 4a89dc7e24ff212a7b5167bef7ab079d
  369. )
  370. FetchContent_Declare(
  371. projC
  372. GIT_REPOSITORY git@mycompany.com:git/projC.git
  373. GIT_TAG 4ad4016bd1d8d5412d135cf8ceea1bb9
  374. )
  375. FetchContent_Declare(
  376. projD
  377. GIT_REPOSITORY git@mycompany.com:git/projD.git
  378. GIT_TAG origin/integrationBranch
  379. )
  380. FetchContent_Declare(
  381. projE
  382. GIT_REPOSITORY git@mycompany.com:git/projE.git
  383. GIT_TAG origin/release/2.3-rc1
  384. )
  385. # Order is important, see notes in the discussion further below
  386. FetchContent_MakeAvailable(projD projB projC)
  387. *projB*:
  388. .. code-block:: cmake
  389. include(FetchContent)
  390. FetchContent_Declare(
  391. projD
  392. GIT_REPOSITORY git@mycompany.com:git/projD.git
  393. GIT_TAG 20b415f9034bbd2a2e8216e9a5c9e632
  394. )
  395. FetchContent_Declare(
  396. projE
  397. GIT_REPOSITORY git@mycompany.com:git/projE.git
  398. GIT_TAG 68e20f674a48be38d60e129f600faf7d
  399. )
  400. FetchContent_MakeAvailable(projD projE)
  401. *projC*:
  402. .. code-block:: cmake
  403. include(FetchContent)
  404. FetchContent_Declare(
  405. projD
  406. GIT_REPOSITORY git@mycompany.com:git/projD.git
  407. GIT_TAG 7d9a17ad2c962aa13e2fbb8043fb6b8a
  408. )
  409. # This particular version of projD requires workarounds
  410. FetchContent_GetProperties(projD)
  411. if(NOT projd_POPULATED)
  412. FetchContent_Populate(projD)
  413. # Copy an additional/replacement file into the populated source
  414. file(COPY someFile.c DESTINATION ${projd_SOURCE_DIR}/src)
  415. add_subdirectory(${projd_SOURCE_DIR} ${projd_BINARY_DIR})
  416. endif()
  417. A few key points should be noted in the above:
  418. - ``projB`` and ``projC`` define different content details for ``projD``,
  419. but ``projA`` also defines a set of content details for ``projD``.
  420. Because ``projA`` will define them first, the details from ``projB`` and
  421. ``projC`` will not be used. The override details defined by ``projA``
  422. are not required to match either of those from ``projB`` or ``projC``, but
  423. it is up to the higher level project to ensure that the details it does
  424. define still make sense for the child projects.
  425. - In the ``projA`` call to :command:`FetchContent_MakeAvailable`, ``projD``
  426. is listed ahead of ``projB`` and ``projC`` to ensure that ``projA`` is in
  427. control of how ``projD`` is populated.
  428. - While ``projA`` defines content details for ``projE``, it does not need
  429. to explicitly call ``FetchContent_MakeAvailable(projE)`` or
  430. ``FetchContent_Populate(projD)`` itself. Instead, it leaves that to the
  431. child ``projB``. For higher level projects, it is often enough to just
  432. define the override content details and leave the actual population to the
  433. child projects. This saves repeating the same thing at each level of the
  434. project hierarchy unnecessarily.
  435. Projects don't always need to add the populated content to the build.
  436. Sometimes the project just wants to make the downloaded content available at
  437. a predictable location. The next example ensures that a set of standard
  438. company toolchain files (and potentially even the toolchain binaries
  439. themselves) is available early enough to be used for that same build.
  440. .. code-block:: cmake
  441. cmake_minimum_required(VERSION 3.14)
  442. include(FetchContent)
  443. FetchContent_Declare(
  444. mycom_toolchains
  445. URL https://intranet.mycompany.com//toolchains_1.3.2.tar.gz
  446. )
  447. FetchContent_MakeAvailable(mycom_toolchains)
  448. project(CrossCompileExample)
  449. The project could be configured to use one of the downloaded toolchains like
  450. so:
  451. .. code-block:: shell
  452. cmake -DCMAKE_TOOLCHAIN_FILE=_deps/mycom_toolchains-src/toolchain_arm.cmake /path/to/src
  453. When CMake processes the ``CMakeLists.txt`` file, it will download and unpack
  454. the tarball into ``_deps/mycompany_toolchains-src`` relative to the build
  455. directory. The :variable:`CMAKE_TOOLCHAIN_FILE` variable is not used until
  456. the :command:`project` command is reached, at which point CMake looks for the
  457. named toolchain file relative to the build directory. Because the tarball has
  458. already been downloaded and unpacked by then, the toolchain file will be in
  459. place, even the very first time that ``cmake`` is run in the build directory.
  460. Lastly, the following example demonstrates how one might download and unpack a
  461. firmware tarball using CMake's :manual:`script mode <cmake(1)>`. The call to
  462. :command:`FetchContent_Populate` specifies all the content details and the
  463. unpacked firmware will be placed in a ``firmware`` directory below the
  464. current working directory.
  465. *getFirmware.cmake*:
  466. .. code-block:: cmake
  467. # NOTE: Intended to be run in script mode with cmake -P
  468. include(FetchContent)
  469. FetchContent_Populate(
  470. firmware
  471. URL https://mycompany.com/assets/firmware-1.23-arm.tar.gz
  472. URL_HASH MD5=68247684da89b608d466253762b0ff11
  473. SOURCE_DIR firmware
  474. )
  475. #]=======================================================================]
  476. set(__FetchContent_privateDir "${CMAKE_CURRENT_LIST_DIR}/FetchContent")
  477. #=======================================================================
  478. # Recording and retrieving content details for later population
  479. #=======================================================================
  480. # Internal use, projects must not call this directly. It is
  481. # intended for use by FetchContent_Declare() only.
  482. #
  483. # Sets a content-specific global property (not meant for use
  484. # outside of functions defined here in this file) which can later
  485. # be retrieved using __FetchContent_getSavedDetails() with just the
  486. # same content name. If there is already a value stored in the
  487. # property, it is left unchanged and this call has no effect.
  488. # This allows parent projects to define the content details,
  489. # overriding anything a child project may try to set (properties
  490. # are not cached between runs, so the first thing to set it in a
  491. # build will be in control).
  492. function(__FetchContent_declareDetails contentName)
  493. string(TOLOWER ${contentName} contentNameLower)
  494. set(propertyName "_FetchContent_${contentNameLower}_savedDetails")
  495. get_property(alreadyDefined GLOBAL PROPERTY ${propertyName} DEFINED)
  496. if(NOT alreadyDefined)
  497. define_property(GLOBAL PROPERTY ${propertyName}
  498. BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()"
  499. FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}"
  500. )
  501. set_property(GLOBAL PROPERTY ${propertyName} ${ARGN})
  502. endif()
  503. endfunction()
  504. # Internal use, projects must not call this directly. It is
  505. # intended for use by the FetchContent_Declare() function.
  506. #
  507. # Retrieves details saved for the specified content in an
  508. # earlier call to __FetchContent_declareDetails().
  509. function(__FetchContent_getSavedDetails contentName outVar)
  510. string(TOLOWER ${contentName} contentNameLower)
  511. set(propertyName "_FetchContent_${contentNameLower}_savedDetails")
  512. get_property(alreadyDefined GLOBAL PROPERTY ${propertyName} DEFINED)
  513. if(NOT alreadyDefined)
  514. message(FATAL_ERROR "No content details recorded for ${contentName}")
  515. endif()
  516. get_property(propertyValue GLOBAL PROPERTY ${propertyName})
  517. set(${outVar} "${propertyValue}" PARENT_SCOPE)
  518. endfunction()
  519. # Saves population details of the content, sets defaults for the
  520. # SOURCE_DIR and BUILD_DIR.
  521. function(FetchContent_Declare contentName)
  522. set(options "")
  523. set(oneValueArgs SVN_REPOSITORY)
  524. set(multiValueArgs "")
  525. cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  526. unset(srcDirSuffix)
  527. unset(svnRepoArgs)
  528. if(ARG_SVN_REPOSITORY)
  529. # Add a hash of the svn repository URL to the source dir. This works
  530. # around the problem where if the URL changes, the download would
  531. # fail because it tries to checkout/update rather than switch the
  532. # old URL to the new one. We limit the hash to the first 7 characters
  533. # so that the source path doesn't get overly long (which can be a
  534. # problem on windows due to path length limits).
  535. string(SHA1 urlSHA ${ARG_SVN_REPOSITORY})
  536. string(SUBSTRING ${urlSHA} 0 7 urlSHA)
  537. set(srcDirSuffix "-${urlSHA}")
  538. set(svnRepoArgs SVN_REPOSITORY ${ARG_SVN_REPOSITORY})
  539. endif()
  540. string(TOLOWER ${contentName} contentNameLower)
  541. __FetchContent_declareDetails(
  542. ${contentNameLower}
  543. SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src${srcDirSuffix}"
  544. BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build"
  545. ${svnRepoArgs}
  546. # List these last so they can override things we set above
  547. ${ARG_UNPARSED_ARGUMENTS}
  548. )
  549. endfunction()
  550. #=======================================================================
  551. # Set/get whether the specified content has been populated yet.
  552. # The setter also records the source and binary dirs used.
  553. #=======================================================================
  554. # Internal use, projects must not call this directly. It is
  555. # intended for use by the FetchContent_Populate() function to
  556. # record when FetchContent_Populate() is called for a particular
  557. # content name.
  558. function(__FetchContent_setPopulated contentName sourceDir binaryDir)
  559. string(TOLOWER ${contentName} contentNameLower)
  560. set(prefix "_FetchContent_${contentNameLower}")
  561. set(propertyName "${prefix}_sourceDir")
  562. define_property(GLOBAL PROPERTY ${propertyName}
  563. BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()"
  564. FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}"
  565. )
  566. set_property(GLOBAL PROPERTY ${propertyName} ${sourceDir})
  567. set(propertyName "${prefix}_binaryDir")
  568. define_property(GLOBAL PROPERTY ${propertyName}
  569. BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()"
  570. FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}"
  571. )
  572. set_property(GLOBAL PROPERTY ${propertyName} ${binaryDir})
  573. set(propertyName "${prefix}_populated")
  574. define_property(GLOBAL PROPERTY ${propertyName}
  575. BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()"
  576. FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}"
  577. )
  578. set_property(GLOBAL PROPERTY ${propertyName} True)
  579. endfunction()
  580. # Set variables in the calling scope for any of the retrievable
  581. # properties. If no specific properties are requested, variables
  582. # will be set for all retrievable properties.
  583. #
  584. # This function is intended to also be used by projects as the canonical
  585. # way to detect whether they should call FetchContent_Populate()
  586. # and pull the populated source into the build with add_subdirectory(),
  587. # if they are using the populated content in that way.
  588. function(FetchContent_GetProperties contentName)
  589. string(TOLOWER ${contentName} contentNameLower)
  590. set(options "")
  591. set(oneValueArgs SOURCE_DIR BINARY_DIR POPULATED)
  592. set(multiValueArgs "")
  593. cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  594. if(NOT ARG_SOURCE_DIR AND
  595. NOT ARG_BINARY_DIR AND
  596. NOT ARG_POPULATED)
  597. # No specific properties requested, provide them all
  598. set(ARG_SOURCE_DIR ${contentNameLower}_SOURCE_DIR)
  599. set(ARG_BINARY_DIR ${contentNameLower}_BINARY_DIR)
  600. set(ARG_POPULATED ${contentNameLower}_POPULATED)
  601. endif()
  602. set(prefix "_FetchContent_${contentNameLower}")
  603. if(ARG_SOURCE_DIR)
  604. set(propertyName "${prefix}_sourceDir")
  605. get_property(value GLOBAL PROPERTY ${propertyName})
  606. if(value)
  607. set(${ARG_SOURCE_DIR} ${value} PARENT_SCOPE)
  608. endif()
  609. endif()
  610. if(ARG_BINARY_DIR)
  611. set(propertyName "${prefix}_binaryDir")
  612. get_property(value GLOBAL PROPERTY ${propertyName})
  613. if(value)
  614. set(${ARG_BINARY_DIR} ${value} PARENT_SCOPE)
  615. endif()
  616. endif()
  617. if(ARG_POPULATED)
  618. set(propertyName "${prefix}_populated")
  619. get_property(value GLOBAL PROPERTY ${propertyName} DEFINED)
  620. set(${ARG_POPULATED} ${value} PARENT_SCOPE)
  621. endif()
  622. endfunction()
  623. #=======================================================================
  624. # Performing the population
  625. #=======================================================================
  626. # The value of contentName will always have been lowercased by the caller.
  627. # All other arguments are assumed to be options that are understood by
  628. # ExternalProject_Add(), except for QUIET and SUBBUILD_DIR.
  629. function(__FetchContent_directPopulate contentName)
  630. set(options
  631. QUIET
  632. )
  633. set(oneValueArgs
  634. SUBBUILD_DIR
  635. SOURCE_DIR
  636. BINARY_DIR
  637. # Prevent the following from being passed through
  638. CONFIGURE_COMMAND
  639. BUILD_COMMAND
  640. INSTALL_COMMAND
  641. TEST_COMMAND
  642. # We force both of these to be ON since we are always executing serially
  643. # and we want all steps to have access to the terminal in case they
  644. # need input from the command line (e.g. ask for a private key password)
  645. # or they want to provide timely progress. We silently absorb and
  646. # discard these if they are set by the caller.
  647. USES_TERMINAL_DOWNLOAD
  648. USES_TERMINAL_UPDATE
  649. )
  650. set(multiValueArgs "")
  651. cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  652. if(NOT ARG_SUBBUILD_DIR)
  653. message(FATAL_ERROR "Internal error: SUBBUILD_DIR not set")
  654. elseif(NOT IS_ABSOLUTE "${ARG_SUBBUILD_DIR}")
  655. set(ARG_SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SUBBUILD_DIR}")
  656. endif()
  657. if(NOT ARG_SOURCE_DIR)
  658. message(FATAL_ERROR "Internal error: SOURCE_DIR not set")
  659. elseif(NOT IS_ABSOLUTE "${ARG_SOURCE_DIR}")
  660. set(ARG_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SOURCE_DIR}")
  661. endif()
  662. if(NOT ARG_BINARY_DIR)
  663. message(FATAL_ERROR "Internal error: BINARY_DIR not set")
  664. elseif(NOT IS_ABSOLUTE "${ARG_BINARY_DIR}")
  665. set(ARG_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_BINARY_DIR}")
  666. endif()
  667. # Ensure the caller can know where to find the source and build directories
  668. # with some convenient variables. Doing this here ensures the caller sees
  669. # the correct result in the case where the default values are overridden by
  670. # the content details set by the project.
  671. set(${contentName}_SOURCE_DIR "${ARG_SOURCE_DIR}" PARENT_SCOPE)
  672. set(${contentName}_BINARY_DIR "${ARG_BINARY_DIR}" PARENT_SCOPE)
  673. # The unparsed arguments may contain spaces, so build up ARG_EXTRA
  674. # in such a way that it correctly substitutes into the generated
  675. # CMakeLists.txt file with each argument quoted.
  676. unset(ARG_EXTRA)
  677. foreach(arg IN LISTS ARG_UNPARSED_ARGUMENTS)
  678. set(ARG_EXTRA "${ARG_EXTRA} \"${arg}\"")
  679. endforeach()
  680. # Hide output if requested, but save it to a variable in case there's an
  681. # error so we can show the output upon failure. When not quiet, don't
  682. # capture the output to a variable because the user may want to see the
  683. # output as it happens (e.g. progress during long downloads). Combine both
  684. # stdout and stderr in the one capture variable so the output stays in order.
  685. if (ARG_QUIET)
  686. set(outputOptions
  687. OUTPUT_VARIABLE capturedOutput
  688. ERROR_VARIABLE capturedOutput
  689. )
  690. else()
  691. set(capturedOutput)
  692. set(outputOptions)
  693. message(STATUS "Populating ${contentName}")
  694. endif()
  695. if(CMAKE_GENERATOR)
  696. set(generatorOpts "-G${CMAKE_GENERATOR}")
  697. if(CMAKE_GENERATOR_PLATFORM)
  698. list(APPEND generatorOpts "-A${CMAKE_GENERATOR_PLATFORM}")
  699. endif()
  700. if(CMAKE_GENERATOR_TOOLSET)
  701. list(APPEND generatorOpts "-T${CMAKE_GENERATOR_TOOLSET}")
  702. endif()
  703. if(CMAKE_MAKE_PROGRAM)
  704. list(APPEND generatorOpts "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
  705. endif()
  706. else()
  707. # Likely we've been invoked via CMake's script mode where no
  708. # generator is set (and hence CMAKE_MAKE_PROGRAM could not be
  709. # trusted even if provided). We will have to rely on being
  710. # able to find the default generator and build tool.
  711. unset(generatorOpts)
  712. endif()
  713. # Create and build a separate CMake project to carry out the population.
  714. # If we've already previously done these steps, they will not cause
  715. # anything to be updated, so extra rebuilds of the project won't occur.
  716. # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
  717. # has this set to something not findable on the PATH.
  718. configure_file("${__FetchContent_privateDir}/CMakeLists.cmake.in"
  719. "${ARG_SUBBUILD_DIR}/CMakeLists.txt")
  720. execute_process(
  721. COMMAND ${CMAKE_COMMAND} ${generatorOpts} .
  722. RESULT_VARIABLE result
  723. ${outputOptions}
  724. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  725. )
  726. if(result)
  727. if(capturedOutput)
  728. message("${capturedOutput}")
  729. endif()
  730. message(FATAL_ERROR "CMake step for ${contentName} failed: ${result}")
  731. endif()
  732. execute_process(
  733. COMMAND ${CMAKE_COMMAND} --build .
  734. RESULT_VARIABLE result
  735. ${outputOptions}
  736. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  737. )
  738. if(result)
  739. if(capturedOutput)
  740. message("${capturedOutput}")
  741. endif()
  742. message(FATAL_ERROR "Build step for ${contentName} failed: ${result}")
  743. endif()
  744. endfunction()
  745. option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist")
  746. option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population")
  747. option(FETCHCONTENT_QUIET "Enables QUIET option for all content population" ON)
  748. set(FETCHCONTENT_BASE_DIR "${CMAKE_BINARY_DIR}/_deps" CACHE PATH "Directory under which to collect all populated content")
  749. # Populate the specified content using details stored from
  750. # an earlier call to FetchContent_Declare().
  751. function(FetchContent_Populate contentName)
  752. if(NOT contentName)
  753. message(FATAL_ERROR "Empty contentName not allowed for FetchContent_Populate()")
  754. endif()
  755. string(TOLOWER ${contentName} contentNameLower)
  756. if(ARGN)
  757. # This is the direct population form with details fully specified
  758. # as part of the call, so we already have everything we need
  759. __FetchContent_directPopulate(
  760. ${contentNameLower}
  761. SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-subbuild"
  762. SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-src"
  763. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-build"
  764. ${ARGN} # Could override any of the above ..._DIR variables
  765. )
  766. # Pass source and binary dir variables back to the caller
  767. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  768. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  769. # Don't set global properties, or record that we did this population, since
  770. # this was a direct call outside of the normal declared details form.
  771. # We only want to save values in the global properties for content that
  772. # honours the hierarchical details mechanism so that projects are not
  773. # robbed of the ability to override details set in nested projects.
  774. return()
  775. endif()
  776. # No details provided, so assume they were saved from an earlier call
  777. # to FetchContent_Declare(). Do a check that we haven't already
  778. # populated this content before in case the caller forgot to check.
  779. FetchContent_GetProperties(${contentName})
  780. if(${contentNameLower}_POPULATED)
  781. message(FATAL_ERROR "Content ${contentName} already populated in ${${contentNameLower}_SOURCE_DIR}")
  782. endif()
  783. string(TOUPPER ${contentName} contentNameUpper)
  784. set(FETCHCONTENT_SOURCE_DIR_${contentNameUpper}
  785. "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}"
  786. CACHE PATH "When not empty, overrides where to find pre-populated content for ${contentName}")
  787. if(FETCHCONTENT_SOURCE_DIR_${contentNameUpper})
  788. # The source directory has been explicitly provided in the cache,
  789. # so no population is required
  790. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  791. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  792. elseif(FETCHCONTENT_FULLY_DISCONNECTED)
  793. # Bypass population and assume source is already there from a previous run
  794. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
  795. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  796. else()
  797. # Support both a global "disconnect all updates" and a per-content
  798. # update test (either one being set disables updates for this content).
  799. option(FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper}
  800. "Enables UPDATE_DISCONNECTED behavior just for population of ${contentName}")
  801. if(FETCHCONTENT_UPDATES_DISCONNECTED OR
  802. FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper})
  803. set(disconnectUpdates True)
  804. else()
  805. set(disconnectUpdates False)
  806. endif()
  807. if(FETCHCONTENT_QUIET)
  808. set(quietFlag QUIET)
  809. else()
  810. unset(quietFlag)
  811. endif()
  812. __FetchContent_getSavedDetails(${contentName} contentDetails)
  813. if("${contentDetails}" STREQUAL "")
  814. message(FATAL_ERROR "No details have been set for content: ${contentName}")
  815. endif()
  816. __FetchContent_directPopulate(
  817. ${contentNameLower}
  818. ${quietFlag}
  819. UPDATE_DISCONNECTED ${disconnectUpdates}
  820. SUBBUILD_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-subbuild"
  821. SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src"
  822. BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build"
  823. # Put the saved details last so they can override any of the
  824. # the options we set above (this can include SOURCE_DIR or
  825. # BUILD_DIR)
  826. ${contentDetails}
  827. )
  828. endif()
  829. __FetchContent_setPopulated(
  830. ${contentName}
  831. ${${contentNameLower}_SOURCE_DIR}
  832. ${${contentNameLower}_BINARY_DIR}
  833. )
  834. # Pass variables back to the caller. The variables passed back here
  835. # must match what FetchContent_GetProperties() sets when it is called
  836. # with just the content name.
  837. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  838. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  839. set(${contentNameLower}_POPULATED True PARENT_SCOPE)
  840. endfunction()
  841. # Arguments are assumed to be the names of dependencies that have been
  842. # declared previously and should be populated. It is not an error if
  843. # any of them have already been populated (they will just be skipped in
  844. # that case). The command is implemented as a macro so that the variables
  845. # defined by the FetchContent_GetProperties() and FetchContent_Populate()
  846. # calls will be available to the caller.
  847. macro(FetchContent_MakeAvailable)
  848. foreach(contentName IN ITEMS ${ARGV})
  849. string(TOLOWER ${contentName} contentNameLower)
  850. FetchContent_GetProperties(${contentName})
  851. if(NOT ${contentNameLower}_POPULATED)
  852. FetchContent_Populate(${contentName})
  853. # Only try to call add_subdirectory() if the populated content
  854. # can be treated that way. Protecting the call with the check
  855. # allows this function to be used for projects that just want
  856. # to ensure the content exists, such as to provide content at
  857. # a known location.
  858. if(EXISTS ${${contentNameLower}_SOURCE_DIR}/CMakeLists.txt)
  859. add_subdirectory(${${contentNameLower}_SOURCE_DIR}
  860. ${${contentNameLower}_BINARY_DIR})
  861. endif()
  862. endif()
  863. endforeach()
  864. endmacro()