FetchContent.cmake 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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. .. versionadded:: 3.11
  7. .. only:: html
  8. .. contents::
  9. .. note:: The :guide:`Using Dependencies Guide` provides a high-level
  10. introduction to this general topic. It provides a broader overview of
  11. where the ``FetchContent`` module fits into the bigger picture,
  12. including its relationship to the :command:`find_package` command.
  13. The guide is recommended pre-reading before moving on to the details below.
  14. Overview
  15. ^^^^^^^^
  16. This module enables populating content at configure time via any method
  17. supported by the :module:`ExternalProject` module. Whereas
  18. :command:`ExternalProject_Add` downloads at build time, the
  19. ``FetchContent`` module makes content available immediately, allowing the
  20. configure step to use the content in commands like :command:`add_subdirectory`,
  21. :command:`include` or :command:`file` operations.
  22. Content population details should be defined separately from the command that
  23. performs the actual population. This separation ensures that all the
  24. dependency details are defined before anything might try to use them to
  25. populate content. This is particularly important in more complex project
  26. hierarchies where dependencies may be shared between multiple projects.
  27. The following shows a typical example of declaring content details for some
  28. dependencies and then ensuring they are populated with a separate call:
  29. .. code-block:: cmake
  30. FetchContent_Declare(
  31. googletest
  32. GIT_REPOSITORY https://github.com/google/googletest.git
  33. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  34. )
  35. FetchContent_Declare(
  36. myCompanyIcons
  37. URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
  38. URL_HASH MD5=5588a7b18261c20068beabfb4f530b87
  39. )
  40. FetchContent_MakeAvailable(googletest myCompanyIcons)
  41. The :command:`FetchContent_MakeAvailable` command ensures the named
  42. dependencies have been populated, either by an earlier call or by populating
  43. them itself. When performing the population, it will also add them to the
  44. main build, if possible, so that the main build can use the populated
  45. projects' targets, etc. See the command's documentation for how these steps
  46. are performed.
  47. When using a hierarchical project arrangement, projects at higher levels in
  48. the hierarchy are able to override the declared details of content specified
  49. anywhere lower in the project hierarchy. The first details to be declared
  50. for a given dependency take precedence, regardless of where in the project
  51. hierarchy that occurs. Similarly, the first call that tries to populate a
  52. dependency "wins", with subsequent populations reusing the result of the
  53. first instead of repeating the population again.
  54. See the :ref:`Examples <fetch-content-examples>` which demonstrate
  55. this scenario.
  56. In some cases, the main project may need to have more precise control over
  57. the population, or it may be required to explicitly define the population
  58. steps in a way that cannot be captured by the declared details alone.
  59. For such situations, the lower level :command:`FetchContent_GetProperties` and
  60. :command:`FetchContent_Populate` commands can be used. These lack the richer
  61. features provided by :command:`FetchContent_MakeAvailable` though, so their
  62. direct use should be considered a last resort. The typical pattern of such
  63. custom steps looks like this:
  64. .. code-block:: cmake
  65. # NOTE: Where possible, prefer to use FetchContent_MakeAvailable()
  66. # instead of custom logic like this
  67. # Check if population has already been performed
  68. FetchContent_GetProperties(depname)
  69. if(NOT depname_POPULATED)
  70. # Fetch the content using previously declared details
  71. FetchContent_Populate(depname)
  72. # Set custom variables, policies, etc.
  73. # ...
  74. # Bring the populated content into the build
  75. add_subdirectory(${depname_SOURCE_DIR} ${depname_BINARY_DIR})
  76. endif()
  77. The ``FetchContent`` module also supports defining and populating
  78. content in a single call, with no check for whether the content has been
  79. populated elsewhere already. This should not be done in projects, but may
  80. be appropriate for populating content in CMake's script mode.
  81. See :command:`FetchContent_Populate` for details.
  82. Commands
  83. ^^^^^^^^
  84. .. command:: FetchContent_Declare
  85. .. code-block:: cmake
  86. FetchContent_Declare(
  87. <name>
  88. <contentOptions>...
  89. [OVERRIDE_FIND_PACKAGE |
  90. FIND_PACKAGE_ARGS args...]
  91. )
  92. The ``FetchContent_Declare()`` function records the options that describe
  93. how to populate the specified content. If such details have already
  94. been recorded earlier in this project (regardless of where in the project
  95. hierarchy), this and all later calls for the same content ``<name>`` are
  96. ignored. This "first to record, wins" approach is what allows hierarchical
  97. projects to have parent projects override content details of child projects.
  98. The content ``<name>`` can be any string without spaces, but good practice
  99. would be to use only letters, numbers and underscores. The name will be
  100. treated case-insensitively and it should be obvious for the content it
  101. represents, often being the name of the child project or the value given
  102. to its top level :command:`project` command (if it is a CMake project).
  103. For well-known public projects, the name should generally be the official
  104. name of the project. Choosing an unusual name makes it unlikely that other
  105. projects needing that same content will use the same name, leading to
  106. the content being populated multiple times.
  107. The ``<contentOptions>`` can be any of the download, update or patch options
  108. that the :command:`ExternalProject_Add` command understands. The configure,
  109. build, install and test steps are explicitly disabled and therefore options
  110. related to them will be ignored. The ``SOURCE_SUBDIR`` option is an
  111. exception, see :command:`FetchContent_MakeAvailable` for details on how that
  112. affects behavior.
  113. In most cases, ``<contentOptions>`` will just be a couple of options defining
  114. the download method and method-specific details like a commit tag or archive
  115. hash. For example:
  116. .. code-block:: cmake
  117. FetchContent_Declare(
  118. googletest
  119. GIT_REPOSITORY https://github.com/google/googletest.git
  120. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  121. )
  122. FetchContent_Declare(
  123. myCompanyIcons
  124. URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
  125. URL_HASH MD5=5588a7b18261c20068beabfb4f530b87
  126. )
  127. FetchContent_Declare(
  128. myCompanyCertificates
  129. SVN_REPOSITORY svn+ssh://svn.mycompany.com/srv/svn/trunk/certs
  130. SVN_REVISION -r12345
  131. )
  132. Where contents are being fetched from a remote location and you do not
  133. control that server, it is advisable to use a hash for ``GIT_TAG`` rather
  134. than a branch or tag name. A commit hash is more secure and helps to
  135. confirm that the downloaded contents are what you expected.
  136. .. versionchanged:: 3.14
  137. Commands for the download, update or patch steps can access the terminal.
  138. This may be needed for things like password prompts or real-time display
  139. of command progress.
  140. .. versionadded:: 3.22
  141. The :variable:`CMAKE_TLS_VERIFY`, :variable:`CMAKE_TLS_CAINFO`,
  142. :variable:`CMAKE_NETRC` and :variable:`CMAKE_NETRC_FILE` variables now
  143. provide the defaults for their corresponding content options, just like
  144. they do for :command:`ExternalProject_Add`. Previously, these variables
  145. were ignored by the ``FetchContent`` module.
  146. .. versionadded:: 3.24
  147. ``FIND_PACKAGE_ARGS``
  148. This option is for scenarios where the
  149. :command:`FetchContent_MakeAvailable` command may first try a call to
  150. :command:`find_package` to satisfy the dependency for ``<name>``.
  151. By default, such a call would be simply ``find_package(<name>)``, but
  152. ``FIND_PACKAGE_ARGS`` can be used to provide additional arguments to be
  153. appended after the ``<name>``. ``FIND_PACKAGE_ARGS`` can also be given
  154. with nothing after it, which indicates that :command:`find_package` can
  155. still be called if :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` is
  156. set to ``OPT_IN`` or is not set.
  157. Everything after the ``FIND_PACKAGE_ARGS`` keyword is appended to the
  158. :command:`find_package` call, so all other ``<contentOptions>`` must
  159. come before the ``FIND_PACKAGE_ARGS`` keyword. If the
  160. :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to true
  161. at the time ``FetchContent_Declare()`` is called, a ``GLOBAL`` keyword
  162. will be appended to the :command:`find_package` arguments if it was
  163. not already specified. It will also be appended if
  164. ``FIND_PACKAGE_ARGS`` was not given, but
  165. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``ALWAYS``.
  166. ``OVERRIDE_FIND_PACKAGE`` cannot be used when ``FIND_PACKAGE_ARGS`` is
  167. given.
  168. :ref:`dependency_providers` discusses another way that
  169. :command:`FetchContent_MakeAvailable` calls can be redirected.
  170. ``FIND_PACKAGE_ARGS`` is intended for project control, whereas
  171. dependency providers allow users to override project behavior.
  172. ``OVERRIDE_FIND_PACKAGE``
  173. When a ``FetchContent_Declare(<name> ...)`` call includes this option,
  174. subsequent calls to ``find_package(<name> ...)`` will ensure that
  175. ``FetchContent_MakeAvailable(<name>)`` has been called, then use the
  176. config package files in the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
  177. directory (which are usually created by ``FetchContent_MakeAvailable()``).
  178. This effectively makes :command:`FetchContent_MakeAvailable` override
  179. :command:`find_package` for the named dependency, allowing the former to
  180. satisfy the package requirements of the latter. ``FIND_PACKAGE_ARGS``
  181. cannot be used when ``OVERRIDE_FIND_PACKAGE`` is given.
  182. If a :ref:`dependency provider <dependency_providers>` has been set
  183. and the project calls :command:`find_package` for the ``<name>``
  184. dependency, ``OVERRIDE_FIND_PACKAGE`` will not prevent the provider
  185. from seeing that call. Dependency providers always have the opportunity
  186. to intercept any direct call to :command:`find_package`, except if that
  187. call contains the ``BYPASS_PROVIDER`` option.
  188. .. command:: FetchContent_MakeAvailable
  189. .. versionadded:: 3.14
  190. .. code-block:: cmake
  191. FetchContent_MakeAvailable(<name1> [<name2>...])
  192. This command ensures that each of the named dependencies are made available
  193. to the project by the time it returns. There must have been a call to
  194. :command:`FetchContent_Declare` for each dependency, and the first such call
  195. will control how that dependency will be made available, as described below.
  196. If ``<lowercaseName>_SOURCE_DIR`` is not set:
  197. * .. versionadded:: 3.24
  198. If a :ref:`dependency provider <dependency_providers>` is set, call the
  199. provider's command with ``FETCHCONTENT_MAKEAVAILABLE_SERIAL`` as the
  200. first argument, followed by the arguments of the first call to
  201. :command:`FetchContent_Declare` for ``<name>``. If ``SOURCE_DIR`` or
  202. ``BINARY_DIR`` were not part of the original declared arguments, they
  203. will be added with their default values.
  204. If :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``NEVER``
  205. when the details were declared, any ``FIND_PACKAGE_ARGS`` will be
  206. omitted. The ``OVERRIDE_FIND_PACKAGE`` keyword is also always omitted.
  207. If the provider fulfilled the request, ``FetchContent_MakeAvailable()``
  208. will consider that dependency handled, skip the remaining steps below
  209. and move on to the next dependency in the list.
  210. * .. versionadded:: 3.24
  211. If permitted, :command:`find_package(<name> [<args>...]) <find_package>`
  212. will be called, where ``<args>...`` may be provided by the
  213. ``FIND_PACKAGE_ARGS`` option in :command:`FetchContent_Declare`.
  214. The value of the :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable
  215. at the time :command:`FetchContent_Declare` was called determines whether
  216. ``FetchContent_MakeAvailable()`` can call :command:`find_package`.
  217. If the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to
  218. true when ``FetchContent_MakeAvailable()`` is called, it still affects
  219. any imported targets created when that in turn calls
  220. :command:`find_package`, even if that variable was false when the
  221. corresponding details were declared.
  222. If the dependency was not satisfied by a provider or a
  223. :command:`find_package` call, ``FetchContent_MakeAvailable()`` then uses
  224. the following logic to make the dependency available:
  225. * If the dependency has already been populated earlier in this run, set
  226. the ``<lowercaseName>_POPULATED``, ``<lowercaseName>_SOURCE_DIR`` and
  227. ``<lowercaseName>_BINARY_DIR`` variables in the same way as a call to
  228. :command:`FetchContent_GetProperties`, then skip the remaining steps
  229. below and move on to the next dependency in the list.
  230. * Call :command:`FetchContent_Populate` to populate the dependency using
  231. the details recorded by an earlier call to :command:`FetchContent_Declare`.
  232. Halt with a fatal error if no such details have been recorded.
  233. :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>` can be used to override
  234. the declared details and use content provided at the specified location
  235. instead.
  236. * .. versionadded:: 3.24
  237. Ensure the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` directory
  238. contains a ``<lowercaseName>-config.cmake`` and a
  239. ``<lowercaseName>-config-version.cmake`` file (or equivalently
  240. ``<name>Config.cmake`` and ``<name>ConfigVersion.cmake``).
  241. The directory that the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
  242. variable points to is cleared at the start of every CMake run.
  243. If no config file exists when :command:`FetchContent_Populate` returns,
  244. a minimal one will be written which :command:`includes <include>` any
  245. ``<lowercaseName>-extra.cmake`` or ``<name>Extra.cmake`` file with the
  246. ``OPTIONAL`` flag (so the files can be missing and won't generate a
  247. warning). Similarly, if no config version file exists, a very simple
  248. one will be written which sets ``PACKAGE_VERSION_COMPATIBLE`` to true.
  249. CMake cannot automatically determine an arbitrary dependency's version,
  250. so it cannot set ``PACKAGE_VERSION`` or ``PACKAGE_VERSION_EXACT``.
  251. When a dependency is pulled in via :command:`add_subdirectory` in the
  252. next step, it may choose to overwrite the generated config version file
  253. in :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` with one that also sets
  254. ``PACKAGE_VERSION``, and if appropriate, ``PACKAGE_VERSION_EXACT``.
  255. The dependency may also write a ``<lowercaseName>-extra.cmake`` or
  256. ``<name>Extra.cmake`` file to perform custom processing or define any
  257. variables that their normal (installed) package config file would
  258. otherwise usually define (many projects don't do any custom processing
  259. or set any variables and therefore have no need to do this).
  260. If required, the main project can write these files instead if the
  261. dependency project doesn't do so. This allows the main project to
  262. add missing details from older dependencies that haven't or can't be
  263. updated to support this functionality.
  264. See `Integrating With find_package()`_ for examples.
  265. * If the top directory of the populated content contains a ``CMakeLists.txt``
  266. file, call :command:`add_subdirectory` to add it to the main build.
  267. It is not an error for there to be no ``CMakeLists.txt`` file, which
  268. allows the command to be used for dependencies that make downloaded
  269. content available at a known location, but which do not need or support
  270. being added directly to the build.
  271. .. versionadded:: 3.18
  272. The ``SOURCE_SUBDIR`` option can be given in the declared details to
  273. look somewhere below the top directory instead (i.e. the same way that
  274. ``SOURCE_SUBDIR`` is used by the :command:`ExternalProject_Add`
  275. command). The path provided with ``SOURCE_SUBDIR`` must be relative
  276. and will be treated as relative to the top directory. It can also
  277. point to a directory that does not contain a ``CMakeLists.txt`` file
  278. or even to a directory that doesn't exist. This can be used to avoid
  279. adding a project that contains a ``CMakeLists.txt`` file in its top
  280. directory.
  281. Projects should aim to declare the details of all dependencies they might
  282. use before they call ``FetchContent_MakeAvailable()`` for any of them.
  283. This ensures that if any of the dependencies are also sub-dependencies of
  284. one or more of the others, the main project still controls the details
  285. that will be used (because it will declare them first before the
  286. dependencies get a chance to). In the following code samples, assume that
  287. the ``uses_other`` dependency also uses ``FetchContent`` to add the ``other``
  288. dependency internally:
  289. .. code-block:: cmake
  290. # WRONG: Should declare all details first
  291. FetchContent_Declare(uses_other ...)
  292. FetchContent_MakeAvailable(uses_other)
  293. FetchContent_Declare(other ...) # Will be ignored, uses_other beat us to it
  294. FetchContent_MakeAvailable(other) # Would use details declared by uses_other
  295. .. code-block:: cmake
  296. # CORRECT: All details declared first, so they will take priority
  297. FetchContent_Declare(uses_other ...)
  298. FetchContent_Declare(other ...)
  299. FetchContent_MakeAvailable(uses_other other)
  300. Note that :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` is explicitly set
  301. to false upon entry to ``FetchContent_MakeAvailable()``, and is restored to
  302. its original value before the command returns. Developers typically only
  303. want to verify header sets from the main project, not those from any
  304. dependencies. This local manipulation of the
  305. :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` variable provides that
  306. intuitive behavior. You can use variables like
  307. :variable:`CMAKE_PROJECT_INCLUDE` or
  308. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE` to turn verification back
  309. on for all or some dependencies. You can also set the
  310. :prop_tgt:`VERIFY_INTERFACE_HEADER_SETS` property of individual targets.
  311. .. command:: FetchContent_Populate
  312. .. note::
  313. Where possible, prefer to use :command:`FetchContent_MakeAvailable`
  314. instead of implementing population manually with this command.
  315. .. code-block:: cmake
  316. FetchContent_Populate(<name>)
  317. In most cases, the only argument given to ``FetchContent_Populate()`` is the
  318. ``<name>``. When used this way, the command assumes the content details have
  319. been recorded by an earlier call to :command:`FetchContent_Declare`. The
  320. details are stored in a global property, so they are unaffected by things
  321. like variable or directory scope. Therefore, it doesn't matter where in the
  322. project the details were previously declared, as long as they have been
  323. declared before the call to ``FetchContent_Populate()``. Those saved details
  324. are then used to construct a call to :command:`ExternalProject_Add` in a
  325. private sub-build to perform the content population immediately. The
  326. implementation of ``ExternalProject_Add()`` ensures that if the content has
  327. already been populated in a previous CMake run, that content will be reused
  328. rather than repopulating them again. For the common case where population
  329. involves downloading content, the cost of the download is only paid once.
  330. An internal global property records when a particular content population
  331. request has been processed. If ``FetchContent_Populate()`` is called more
  332. than once for the same content name within a configure run, the second call
  333. will halt with an error. Projects can and should check whether content
  334. population has already been processed with the
  335. :command:`FetchContent_GetProperties` command before calling
  336. ``FetchContent_Populate()``.
  337. ``FetchContent_Populate()`` will set three variables in the scope of the
  338. caller:
  339. ``<lowercaseName>_POPULATED``
  340. This will always be set to ``TRUE`` by the call.
  341. ``<lowercaseName>_SOURCE_DIR``
  342. The location where the populated content can be found upon return.
  343. ``<lowercaseName>_BINARY_DIR``
  344. A directory intended for use as a corresponding build directory.
  345. The main use case for the ``<lowercaseName>_SOURCE_DIR`` and
  346. ``<lowercaseName>_BINARY_DIR`` variables is to call
  347. :command:`add_subdirectory` immediately after population:
  348. .. code-block:: cmake
  349. FetchContent_Populate(FooBar)
  350. add_subdirectory(${foobar_SOURCE_DIR} ${foobar_BINARY_DIR})
  351. The values of the three variables can also be retrieved from anywhere in the
  352. project hierarchy using the :command:`FetchContent_GetProperties` command.
  353. The ``FetchContent_Populate()`` command also supports a syntax allowing the
  354. content details to be specified directly rather than using any saved
  355. details. This is more low-level and use of this form is generally to be
  356. avoided in favor of using saved content details as outlined above.
  357. Nevertheless, in certain situations it can be useful to invoke the content
  358. population as an isolated operation (typically as part of implementing some
  359. other higher level feature or when using CMake in script mode):
  360. .. code-block:: cmake
  361. FetchContent_Populate(
  362. <name>
  363. [QUIET]
  364. [SUBBUILD_DIR <subBuildDir>]
  365. [SOURCE_DIR <srcDir>]
  366. [BINARY_DIR <binDir>]
  367. ...
  368. )
  369. This form has a number of key differences to that where only ``<name>`` is
  370. provided:
  371. - All required population details are assumed to have been provided directly
  372. in the call to ``FetchContent_Populate()``. Any saved details for
  373. ``<name>`` are ignored.
  374. - No check is made for whether content for ``<name>`` has already been
  375. populated.
  376. - No global property is set to record that the population has occurred.
  377. - No global properties record the source or binary directories used for the
  378. populated content.
  379. - The ``FETCHCONTENT_FULLY_DISCONNECTED`` and
  380. ``FETCHCONTENT_UPDATES_DISCONNECTED`` cache variables are ignored.
  381. The ``<lowercaseName>_SOURCE_DIR`` and ``<lowercaseName>_BINARY_DIR``
  382. variables are still returned to the caller, but since these locations are
  383. not stored as global properties when this form is used, they are only
  384. available to the calling scope and below rather than the entire project
  385. hierarchy. No ``<lowercaseName>_POPULATED`` variable is set in the caller's
  386. scope with this form.
  387. The supported options for ``FetchContent_Populate()`` are the same as those
  388. for :command:`FetchContent_Declare()`. Those few options shown just
  389. above are either specific to ``FetchContent_Populate()`` or their behavior is
  390. slightly modified from how :command:`ExternalProject_Add` treats them:
  391. ``QUIET``
  392. The ``QUIET`` option can be given to hide the output associated with
  393. populating the specified content. If the population fails, the output will
  394. be shown regardless of whether this option was given or not so that the
  395. cause of the failure can be diagnosed. The global ``FETCHCONTENT_QUIET``
  396. cache variable has no effect on ``FetchContent_Populate()`` calls where the
  397. content details are provided directly.
  398. ``SUBBUILD_DIR``
  399. The ``SUBBUILD_DIR`` argument can be provided to change the location of the
  400. sub-build created to perform the population. The default value is
  401. ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-subbuild`` and it would be
  402. unusual to need to override this default. If a relative path is specified,
  403. it will be interpreted as relative to :variable:`CMAKE_CURRENT_BINARY_DIR`.
  404. This option should not be confused with the ``SOURCE_SUBDIR`` option which
  405. only affects the :command:`FetchContent_MakeAvailable` command.
  406. ``SOURCE_DIR``, ``BINARY_DIR``
  407. The ``SOURCE_DIR`` and ``BINARY_DIR`` arguments are supported by
  408. :command:`ExternalProject_Add`, but different default values are used by
  409. ``FetchContent_Populate()``. ``SOURCE_DIR`` defaults to
  410. ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-src`` and ``BINARY_DIR``
  411. defaults to ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-build``.
  412. If a relative path is specified, it will be interpreted as relative to
  413. :variable:`CMAKE_CURRENT_BINARY_DIR`.
  414. In addition to the above explicit options, any other unrecognized options are
  415. passed through unmodified to :command:`ExternalProject_Add` to perform the
  416. download, patch and update steps. The following options are explicitly
  417. prohibited (they are disabled by the ``FetchContent_Populate()`` command):
  418. - ``CONFIGURE_COMMAND``
  419. - ``BUILD_COMMAND``
  420. - ``INSTALL_COMMAND``
  421. - ``TEST_COMMAND``
  422. If using ``FetchContent_Populate()`` within CMake's script mode, be aware
  423. that the implementation sets up a sub-build which therefore requires a CMake
  424. generator and build tool to be available. If these cannot be found by
  425. default, then the :variable:`CMAKE_GENERATOR` and/or
  426. :variable:`CMAKE_MAKE_PROGRAM` variables will need to be set appropriately
  427. on the command line invoking the script.
  428. .. versionadded:: 3.18
  429. Added support for the ``DOWNLOAD_NO_EXTRACT`` option.
  430. .. command:: FetchContent_GetProperties
  431. When using saved content details, a call to
  432. :command:`FetchContent_MakeAvailable` or :command:`FetchContent_Populate`
  433. records information in global properties which can be queried at any time.
  434. This information may include the source and binary directories associated with
  435. the content and also whether or not the content population has been processed
  436. during the current configure run.
  437. .. code-block:: cmake
  438. FetchContent_GetProperties(
  439. <name>
  440. [SOURCE_DIR <srcDirVar>]
  441. [BINARY_DIR <binDirVar>]
  442. [POPULATED <doneVar>]
  443. )
  444. The ``SOURCE_DIR``, ``BINARY_DIR`` and ``POPULATED`` options can be used to
  445. specify which properties should be retrieved. Each option accepts a value
  446. which is the name of the variable in which to store that property. Most of
  447. the time though, only ``<name>`` is given, in which case the call will then
  448. set the same variables as a call to
  449. :command:`FetchContent_MakeAvailable(name) <FetchContent_MakeAvailable>` or
  450. :command:`FetchContent_Populate(name) <FetchContent_Populate>`.
  451. Note that the ``SOURCE_DIR`` and ``BINARY_DIR`` values can be empty if the
  452. call is fulfilled by a :ref:`dependency provider <dependency_providers>`.
  453. This command is rarely needed when using
  454. :command:`FetchContent_MakeAvailable`. It is more commonly used as part of
  455. implementing the following pattern with :command:`FetchContent_Populate`,
  456. which ensures that the relevant variables will always be defined regardless
  457. of whether or not the population has been performed elsewhere in the project
  458. already:
  459. .. code-block:: cmake
  460. # Check if population has already been performed
  461. FetchContent_GetProperties(depname)
  462. if(NOT depname_POPULATED)
  463. # Fetch the content using previously declared details
  464. FetchContent_Populate(depname)
  465. # Set custom variables, policies, etc.
  466. # ...
  467. # Bring the populated content into the build
  468. add_subdirectory(${depname_SOURCE_DIR} ${depname_BINARY_DIR})
  469. endif()
  470. .. command:: FetchContent_SetPopulated
  471. .. versionadded:: 3.24
  472. .. note::
  473. This command should only be called by
  474. :ref:`dependency providers <dependency_providers>`. Calling it in any
  475. other context is unsupported and future CMake versions may halt with a
  476. fatal error in such cases.
  477. .. code-block:: cmake
  478. FetchContent_SetPopulated(
  479. <name>
  480. [SOURCE_DIR <srcDir>]
  481. [BINARY_DIR <binDir>]
  482. )
  483. If a provider command fulfills a ``FETCHCONTENT_MAKEAVAILABLE_SERIAL``
  484. request, it must call this function before returning. The ``SOURCE_DIR``
  485. and ``BINARY_DIR`` arguments can be used to specify the values that
  486. :command:`FetchContent_GetProperties` should return for its corresponding
  487. arguments. Only provide ``SOURCE_DIR`` and ``BINARY_DIR`` if they have
  488. the same meaning as if they had been populated by the built-in
  489. :command:`FetchContent_MakeAvailable` implementation.
  490. Variables
  491. ^^^^^^^^^
  492. A number of cache variables can influence the behavior where details from a
  493. :command:`FetchContent_Declare` call are used to populate content.
  494. .. note::
  495. All of these variables are intended for the developer to customize behavior.
  496. They should not normally be set by the project.
  497. .. variable:: FETCHCONTENT_BASE_DIR
  498. In most cases, the saved details do not specify any options relating to the
  499. directories to use for the internal sub-build, final source and build areas.
  500. It is generally best to leave these decisions up to the ``FetchContent``
  501. module to handle on the project's behalf. The ``FETCHCONTENT_BASE_DIR``
  502. cache variable controls the point under which all content population
  503. directories are collected, but in most cases, developers would not need to
  504. change this. The default location is ``${CMAKE_BINARY_DIR}/_deps``, but if
  505. developers change this value, they should aim to keep the path short and
  506. just below the top level of the build tree to avoid running into path
  507. length problems on Windows.
  508. .. variable:: FETCHCONTENT_QUIET
  509. The logging output during population can be quite verbose, making the
  510. configure stage quite noisy. This cache option (``ON`` by default) hides
  511. all population output unless an error is encountered. If experiencing
  512. problems with hung downloads, temporarily switching this option off may
  513. help diagnose which content population is causing the issue.
  514. .. variable:: FETCHCONTENT_FULLY_DISCONNECTED
  515. When this option is enabled, no attempt is made to download or update
  516. any content. It is assumed that all content has already been populated in
  517. a previous run or the source directories have been pointed at existing
  518. contents the developer has provided manually (using options described
  519. further below). When the developer knows that no changes have been made to
  520. any content details, turning this option ``ON`` can significantly speed up
  521. the configure stage. It is ``OFF`` by default.
  522. .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED
  523. This is a less severe download/update control compared to
  524. :variable:`FETCHCONTENT_FULLY_DISCONNECTED`. Instead of bypassing all
  525. download and update logic, ``FETCHCONTENT_UPDATES_DISCONNECTED`` only
  526. disables the update stage. Therefore, if content has not been downloaded
  527. previously, it will still be downloaded when this option is enabled.
  528. This can speed up the configure stage, but not as much as
  529. :variable:`FETCHCONTENT_FULLY_DISCONNECTED`. It is ``OFF`` by default.
  530. .. variable:: FETCHCONTENT_TRY_FIND_PACKAGE_MODE
  531. .. versionadded:: 3.24
  532. This variable modifies the details that :command:`FetchContent_Declare`
  533. records for a given dependency. While it ultimately controls the behavior
  534. of :command:`FetchContent_MakeAvailable`, it is the variable's value when
  535. :command:`FetchContent_Declare` is called that gets used. It makes no
  536. difference what the variable is set to when
  537. :command:`FetchContent_MakeAvailable` is called. Since the variable should
  538. only be set by the user and not by projects directly, it will typically have
  539. the same value throughout anyway, so this distinction is not usually
  540. noticeable.
  541. ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` ultimately controls whether
  542. :command:`FetchContent_MakeAvailable` is allowed to call
  543. :command:`find_package` to satisfy a dependency. The variable can be set
  544. to one of the following values:
  545. ``OPT_IN``
  546. :command:`FetchContent_MakeAvailable` will only call
  547. :command:`find_package` if the :command:`FetchContent_Declare` call
  548. included a ``FIND_PACKAGE_ARGS`` keyword. This is also the default
  549. behavior if ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` is not set.
  550. ``ALWAYS``
  551. :command:`find_package` can be called by
  552. :command:`FetchContent_MakeAvailable` regardless of whether the
  553. :command:`FetchContent_Declare` call included a ``FIND_PACKAGE_ARGS``
  554. keyword or not. If no ``FIND_PACKAGE_ARGS`` keyword was given, the
  555. behavior will be as though ``FIND_PACKAGE_ARGS`` had been provided,
  556. with no additional arguments after it.
  557. ``NEVER``
  558. :command:`FetchContent_MakeAvailable` will not call
  559. :command:`find_package`. Any ``FIND_PACKAGE_ARGS`` given to the
  560. :command:`FetchContent_Declare` call will be ignored.
  561. As a special case, if the :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>`
  562. variable has a non-empty value for a dependency, it is assumed that the
  563. user is overriding all other methods of making that dependency available.
  564. ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` will have no effect on that
  565. dependency and :command:`FetchContent_MakeAvailable` will not try to call
  566. :command:`find_package` for it.
  567. In addition to the above, the following variables are also defined for each
  568. content name:
  569. .. variable:: FETCHCONTENT_SOURCE_DIR_<uppercaseName>
  570. If this is set, no download or update steps are performed for the specified
  571. content and the ``<lowercaseName>_SOURCE_DIR`` variable returned to the
  572. caller is pointed at this location. This gives developers a way to have a
  573. separate checkout of the content that they can modify freely without
  574. interference from the build. The build simply uses that existing source,
  575. but it still defines ``<lowercaseName>_BINARY_DIR`` to point inside its own
  576. build area. Developers are strongly encouraged to use this mechanism rather
  577. than editing the sources populated in the default location, as changes to
  578. sources in the default location can be lost when content population details
  579. are changed by the project.
  580. .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED_<uppercaseName>
  581. This is the per-content equivalent of
  582. :variable:`FETCHCONTENT_UPDATES_DISCONNECTED`. If the global option or
  583. this option is ``ON``, then updates will be disabled for the named content.
  584. Disabling updates for individual content can be useful for content whose
  585. details rarely change, while still leaving other frequently changing content
  586. with updates enabled.
  587. .. _`fetch-content-examples`:
  588. Examples
  589. ^^^^^^^^
  590. Typical Case
  591. """"""""""""
  592. This first fairly straightforward example ensures that some popular testing
  593. frameworks are available to the main build:
  594. .. code-block:: cmake
  595. include(FetchContent)
  596. FetchContent_Declare(
  597. googletest
  598. GIT_REPOSITORY https://github.com/google/googletest.git
  599. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  600. )
  601. FetchContent_Declare(
  602. Catch2
  603. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  604. GIT_TAG de6fe184a9ac1a06895cdd1c9b437f0a0bdf14ad # v2.13.4
  605. )
  606. # After the following call, the CMake targets defined by googletest and
  607. # Catch2 will be available to the rest of the build
  608. FetchContent_MakeAvailable(googletest Catch2)
  609. .. _FetchContent-find_package-integration-examples:
  610. Integrating With find_package()
  611. """""""""""""""""""""""""""""""
  612. For the previous example, if the user wanted to try to find ``googletest``
  613. and ``Catch2`` via :command:`find_package` first before trying to download
  614. and build them from source, they could set the
  615. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable to ``ALWAYS``.
  616. This would also affect any other calls to :command:`FetchContent_Declare`
  617. throughout the project, which might not be acceptable. The behavior can be
  618. enabled for just these two dependencies instead by adding ``FIND_PACKAGE_ARGS``
  619. to the declared details and leaving
  620. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` unset, or set to ``OPT_IN``:
  621. .. code-block:: cmake
  622. include(FetchContent)
  623. FetchContent_Declare(
  624. googletest
  625. GIT_REPOSITORY https://github.com/google/googletest.git
  626. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  627. FIND_PACKAGE_ARGS NAMES GTest
  628. )
  629. FetchContent_Declare(
  630. Catch2
  631. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  632. GIT_TAG de6fe184a9ac1a06895cdd1c9b437f0a0bdf14ad # v2.13.4
  633. FIND_PACKAGE_ARGS
  634. )
  635. # This will try calling find_package() first for both dependencies
  636. FetchContent_MakeAvailable(googletest Catch2)
  637. For ``Catch2``, no additional arguments to :command:`find_package` are needed,
  638. so no additional arguments are provided after the ``FIND_PACKAGE_ARGS``
  639. keyword. For ``googletest``, its package is more commonly called ``GTest``,
  640. so arguments are added to support it being found by that name.
  641. If the user wanted to disable :command:`FetchContent_MakeAvailable` from
  642. calling :command:`find_package` for any dependency, even if it provided
  643. ``FIND_PACKAGE_ARGS`` in its declared details, they could set
  644. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` to ``NEVER``.
  645. If the project wanted to indicate that these two dependencies should be
  646. downloaded and built from source and that :command:`find_package` calls
  647. should be redirected to use the built dependencies, the
  648. ``OVERRIDE_FIND_PACKAGE`` option should be used when declaring the content
  649. details:
  650. .. code-block:: cmake
  651. include(FetchContent)
  652. FetchContent_Declare(
  653. googletest
  654. GIT_REPOSITORY https://github.com/google/googletest.git
  655. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  656. OVERRIDE_FIND_PACKAGE
  657. )
  658. FetchContent_Declare(
  659. Catch2
  660. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  661. GIT_TAG de6fe184a9ac1a06895cdd1c9b437f0a0bdf14ad # v2.13.4
  662. OVERRIDE_FIND_PACKAGE
  663. )
  664. # The following will automatically forward through to FetchContent_MakeAvailable()
  665. find_package(googletest)
  666. find_package(Catch2)
  667. CMake provides a FindGTest module which defines some variables that older
  668. projects may use instead of linking to the imported targets. To support
  669. those cases, we can provide an extras file. In keeping with the
  670. "first to define, wins" philosophy of ``FetchContent``, we only write out
  671. that file if something else hasn't already done so.
  672. .. code-block:: cmake
  673. FetchContent_MakeAvailable(googletest)
  674. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extras.cmake AND
  675. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestExtras.cmake)
  676. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extras.cmake
  677. [=[
  678. if("${GTEST_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest)
  679. set(GTEST_LIBRARIES GTest::gtest)
  680. endif()
  681. if("${GTEST_MAIN_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest_main)
  682. set(GTEST_MAIN_LIBRARIES GTest::gtest_main)
  683. endif()
  684. if("${GTEST_BOTH_LIBRARIES}" STREQUAL "")
  685. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  686. endif()
  687. ]=])
  688. endif()
  689. Projects will also likely be using ``find_package(GTest)`` rather than
  690. ``find_package(googletest)``, but it is possible to make use of the
  691. :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` area to pull in the latter as
  692. a dependency of the former. This is likely to be sufficient to satisfy
  693. a typical ``find_package(GTest)`` call.
  694. .. code-block:: cmake
  695. FetchContent_MakeAvailable(googletest)
  696. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake AND
  697. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfig.cmake)
  698. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
  699. [=[
  700. include(CMakeFindDependencyMacro)
  701. find_dependency(googletest)
  702. ]=])
  703. endif()
  704. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake AND
  705. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfigVersion.cmake)
  706. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
  707. [=[
  708. include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-config-version.cmake OPTIONAL)
  709. if(NOT PACKAGE_VERSION_COMPATIBLE)
  710. include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestConfigVersion.cmake OPTIONAL)
  711. endif()
  712. ]=])
  713. endif()
  714. Overriding Where To Find CMakeLists.txt
  715. """""""""""""""""""""""""""""""""""""""
  716. If the sub-project's ``CMakeLists.txt`` file is not at the top level of its
  717. source tree, the ``SOURCE_SUBDIR`` option can be used to tell ``FetchContent``
  718. where to find it. The following example shows how to use that option, and
  719. it also sets a variable which is meaningful to the subproject before pulling
  720. it into the main build (set as an ``INTERNAL`` cache variable to avoid
  721. problems with policy :policy:`CMP0077`):
  722. .. code-block:: cmake
  723. include(FetchContent)
  724. FetchContent_Declare(
  725. protobuf
  726. GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
  727. GIT_TAG ae50d9b9902526efd6c7a1907d09739f959c6297 # v3.15.0
  728. SOURCE_SUBDIR cmake
  729. )
  730. set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
  731. FetchContent_MakeAvailable(protobuf)
  732. Complex Dependency Hierarchies
  733. """"""""""""""""""""""""""""""
  734. In more complex project hierarchies, the dependency relationships can be more
  735. complicated. Consider a hierarchy where ``projA`` is the top level project and
  736. it depends directly on projects ``projB`` and ``projC``. Both ``projB`` and
  737. ``projC`` can be built standalone and they also both depend on another project
  738. ``projD``. ``projB`` additionally depends on ``projE``. This example assumes
  739. that all five projects are available on a company git server. The
  740. ``CMakeLists.txt`` of each project might have sections like the following:
  741. *projA*:
  742. .. code-block:: cmake
  743. include(FetchContent)
  744. FetchContent_Declare(
  745. projB
  746. GIT_REPOSITORY [email protected]:git/projB.git
  747. GIT_TAG 4a89dc7e24ff212a7b5167bef7ab079d
  748. )
  749. FetchContent_Declare(
  750. projC
  751. GIT_REPOSITORY [email protected]:git/projC.git
  752. GIT_TAG 4ad4016bd1d8d5412d135cf8ceea1bb9
  753. )
  754. FetchContent_Declare(
  755. projD
  756. GIT_REPOSITORY [email protected]:git/projD.git
  757. GIT_TAG origin/integrationBranch
  758. )
  759. FetchContent_Declare(
  760. projE
  761. GIT_REPOSITORY [email protected]:git/projE.git
  762. GIT_TAG v2.3-rc1
  763. )
  764. # Order is important, see notes in the discussion further below
  765. FetchContent_MakeAvailable(projD projB projC)
  766. *projB*:
  767. .. code-block:: cmake
  768. include(FetchContent)
  769. FetchContent_Declare(
  770. projD
  771. GIT_REPOSITORY [email protected]:git/projD.git
  772. GIT_TAG 20b415f9034bbd2a2e8216e9a5c9e632
  773. )
  774. FetchContent_Declare(
  775. projE
  776. GIT_REPOSITORY [email protected]:git/projE.git
  777. GIT_TAG 68e20f674a48be38d60e129f600faf7d
  778. )
  779. FetchContent_MakeAvailable(projD projE)
  780. *projC*:
  781. .. code-block:: cmake
  782. include(FetchContent)
  783. FetchContent_Declare(
  784. projD
  785. GIT_REPOSITORY [email protected]:git/projD.git
  786. GIT_TAG 7d9a17ad2c962aa13e2fbb8043fb6b8a
  787. )
  788. # This particular version of projD requires workarounds
  789. FetchContent_GetProperties(projD)
  790. if(NOT projd_POPULATED)
  791. FetchContent_Populate(projD)
  792. # Copy an additional/replacement file into the populated source
  793. file(COPY someFile.c DESTINATION ${projd_SOURCE_DIR}/src)
  794. add_subdirectory(${projd_SOURCE_DIR} ${projd_BINARY_DIR})
  795. endif()
  796. A few key points should be noted in the above:
  797. - ``projB`` and ``projC`` define different content details for ``projD``,
  798. but ``projA`` also defines a set of content details for ``projD``.
  799. Because ``projA`` will define them first, the details from ``projB`` and
  800. ``projC`` will not be used. The override details defined by ``projA``
  801. are not required to match either of those from ``projB`` or ``projC``, but
  802. it is up to the higher level project to ensure that the details it does
  803. define still make sense for the child projects.
  804. - In the ``projA`` call to :command:`FetchContent_MakeAvailable`, ``projD``
  805. is listed ahead of ``projB`` and ``projC`` to ensure that ``projA`` is in
  806. control of how ``projD`` is populated.
  807. - While ``projA`` defines content details for ``projE``, it does not need
  808. to explicitly call ``FetchContent_MakeAvailable(projE)`` or
  809. ``FetchContent_Populate(projD)`` itself. Instead, it leaves that to the
  810. child ``projB``. For higher level projects, it is often enough to just
  811. define the override content details and leave the actual population to the
  812. child projects. This saves repeating the same thing at each level of the
  813. project hierarchy unnecessarily.
  814. Populating Content Without Adding It To The Build
  815. """""""""""""""""""""""""""""""""""""""""""""""""
  816. Projects don't always need to add the populated content to the build.
  817. Sometimes the project just wants to make the downloaded content available at
  818. a predictable location. The next example ensures that a set of standard
  819. company toolchain files (and potentially even the toolchain binaries
  820. themselves) is available early enough to be used for that same build.
  821. .. code-block:: cmake
  822. cmake_minimum_required(VERSION 3.14)
  823. include(FetchContent)
  824. FetchContent_Declare(
  825. mycom_toolchains
  826. URL https://intranet.mycompany.com//toolchains_1.3.2.tar.gz
  827. )
  828. FetchContent_MakeAvailable(mycom_toolchains)
  829. project(CrossCompileExample)
  830. The project could be configured to use one of the downloaded toolchains like
  831. so:
  832. .. code-block:: shell
  833. cmake -DCMAKE_TOOLCHAIN_FILE=_deps/mycom_toolchains-src/toolchain_arm.cmake /path/to/src
  834. When CMake processes the ``CMakeLists.txt`` file, it will download and unpack
  835. the tarball into ``_deps/mycompany_toolchains-src`` relative to the build
  836. directory. The :variable:`CMAKE_TOOLCHAIN_FILE` variable is not used until
  837. the :command:`project` command is reached, at which point CMake looks for the
  838. named toolchain file relative to the build directory. Because the tarball has
  839. already been downloaded and unpacked by then, the toolchain file will be in
  840. place, even the very first time that ``cmake`` is run in the build directory.
  841. Populating Content In CMake Script Mode
  842. """""""""""""""""""""""""""""""""""""""
  843. This last example demonstrates how one might download and unpack a
  844. firmware tarball using CMake's :manual:`script mode <cmake(1)>`. The call to
  845. :command:`FetchContent_Populate` specifies all the content details and the
  846. unpacked firmware will be placed in a ``firmware`` directory below the
  847. current working directory.
  848. *getFirmware.cmake*:
  849. .. code-block:: cmake
  850. # NOTE: Intended to be run in script mode with cmake -P
  851. include(FetchContent)
  852. FetchContent_Populate(
  853. firmware
  854. URL https://mycompany.com/assets/firmware-1.23-arm.tar.gz
  855. URL_HASH MD5=68247684da89b608d466253762b0ff11
  856. SOURCE_DIR firmware
  857. )
  858. #]=======================================================================]
  859. #=======================================================================
  860. # Recording and retrieving content details for later population
  861. #=======================================================================
  862. # Internal use, projects must not call this directly. It is
  863. # intended for use by FetchContent_Declare() only.
  864. #
  865. # Sets a content-specific global property (not meant for use
  866. # outside of functions defined here in this file) which can later
  867. # be retrieved using __FetchContent_getSavedDetails() with just the
  868. # same content name. If there is already a value stored in the
  869. # property, it is left unchanged and this call has no effect.
  870. # This allows parent projects to define the content details,
  871. # overriding anything a child project may try to set (properties
  872. # are not cached between runs, so the first thing to set it in a
  873. # build will be in control).
  874. function(__FetchContent_declareDetails contentName)
  875. string(TOLOWER ${contentName} contentNameLower)
  876. set(savedDetailsPropertyName "_FetchContent_${contentNameLower}_savedDetails")
  877. get_property(alreadyDefined GLOBAL PROPERTY ${savedDetailsPropertyName} DEFINED)
  878. if(alreadyDefined)
  879. return()
  880. endif()
  881. if("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "ALWAYS")
  882. set(__tryFindPackage TRUE)
  883. set(__tryFindPackageAllowed TRUE)
  884. elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "NEVER")
  885. set(__tryFindPackage FALSE)
  886. set(__tryFindPackageAllowed FALSE)
  887. elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "OPT_IN" OR
  888. NOT DEFINED FETCHCONTENT_TRY_FIND_PACKAGE_MODE)
  889. set(__tryFindPackage FALSE)
  890. set(__tryFindPackageAllowed TRUE)
  891. else()
  892. message(FATAL_ERROR
  893. "Unsupported value for FETCHCONTENT_TRY_FIND_PACKAGE_MODE: "
  894. "${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}"
  895. )
  896. endif()
  897. set(__cmdArgs)
  898. set(__findPackageArgs)
  899. set(__sawQuietKeyword NO)
  900. set(__sawGlobalKeyword NO)
  901. foreach(__item IN LISTS ARGN)
  902. if(DEFINED __findPackageArgs)
  903. # All remaining args are for find_package()
  904. string(APPEND __findPackageArgs " [==[${__item}]==]")
  905. if(__item STREQUAL "QUIET")
  906. set(__sawQuietKeyword YES)
  907. elseif(__item STREQUAL "GLOBAL")
  908. set(__sawGlobalKeyword YES)
  909. endif()
  910. continue()
  911. endif()
  912. # Still processing non-find_package() args
  913. if(__item STREQUAL "FIND_PACKAGE_ARGS")
  914. if(__tryFindPackageAllowed)
  915. set(__tryFindPackage TRUE)
  916. endif()
  917. # All arguments after this keyword are for find_package(). Define the
  918. # variable but with an empty value initially. This allows us to check
  919. # at the start of the loop whether to store remaining items in this
  920. # variable or not. Note that there could be no more args, which is still
  921. # a valid case because we automatically provide ${contentName} as the
  922. # package name and there may not need to be any further arguments.
  923. set(__findPackageArgs "")
  924. continue() # Don't store this item
  925. elseif(__item STREQUAL "OVERRIDE_FIND_PACKAGE")
  926. set(__tryFindPackageAllowed FALSE)
  927. # Define a separate dedicated property for find_package() to check
  928. # in its implementation. This will be a placeholder until FetchContent
  929. # actually does the population. After that, we will have created a
  930. # stand-in config file that find_package() will pick up instead.
  931. set(propertyName "_FetchContent_${contentNameLower}_override_find_package")
  932. define_property(GLOBAL PROPERTY ${propertyName})
  933. set_property(GLOBAL PROPERTY ${propertyName} TRUE)
  934. endif()
  935. string(APPEND __cmdArgs " [==[${__item}]==]")
  936. endforeach()
  937. define_property(GLOBAL PROPERTY ${savedDetailsPropertyName})
  938. cmake_language(EVAL CODE
  939. "set_property(GLOBAL PROPERTY ${savedDetailsPropertyName} ${__cmdArgs})"
  940. )
  941. if(__tryFindPackage AND __tryFindPackageAllowed)
  942. set(propertyName "_FetchContent_${contentNameLower}_find_package_args")
  943. define_property(GLOBAL PROPERTY ${propertyName})
  944. if(NOT __sawQuietKeyword)
  945. list(INSERT __findPackageArgs 0 QUIET)
  946. endif()
  947. if(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL AND NOT __sawGlobalKeyword)
  948. list(APPEND __findPackageArgs GLOBAL)
  949. endif()
  950. cmake_language(EVAL CODE
  951. "set_property(GLOBAL PROPERTY ${propertyName} ${__findPackageArgs})"
  952. )
  953. endif()
  954. endfunction()
  955. # Internal use, projects must not call this directly. It is
  956. # intended for use by the FetchContent_Declare() function.
  957. #
  958. # Retrieves details saved for the specified content in an
  959. # earlier call to __FetchContent_declareDetails().
  960. function(__FetchContent_getSavedDetails contentName outVar)
  961. string(TOLOWER ${contentName} contentNameLower)
  962. set(propertyName "_FetchContent_${contentNameLower}_savedDetails")
  963. get_property(alreadyDefined GLOBAL PROPERTY ${propertyName} DEFINED)
  964. if(NOT alreadyDefined)
  965. message(FATAL_ERROR "No content details recorded for ${contentName}")
  966. endif()
  967. get_property(propertyValue GLOBAL PROPERTY ${propertyName})
  968. set(${outVar} "${propertyValue}" PARENT_SCOPE)
  969. endfunction()
  970. # Saves population details of the content, sets defaults for the
  971. # SOURCE_DIR and BUILD_DIR.
  972. function(FetchContent_Declare contentName)
  973. # Always check this even if we won't save these details.
  974. # This helps projects catch errors earlier.
  975. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  976. list(FIND ARGN OVERRIDE_FIND_PACKAGE index_OVERRIDE_FIND_PACKAGE)
  977. list(FIND ARGN FIND_PACKAGE_ARGS index_FIND_PACKAGE_ARGS)
  978. if(index_OVERRIDE_FIND_PACKAGE GREATER_EQUAL 0 AND
  979. index_FIND_PACKAGE_ARGS GREATER_EQUAL 0)
  980. message(FATAL_ERROR
  981. "Cannot specify both OVERRIDE_FIND_PACKAGE and FIND_PACKAGE_ARGS "
  982. "when declaring details for ${contentName}"
  983. )
  984. endif()
  985. # Because we are only looking for a subset of the supported keywords, we
  986. # cannot check for multi-value arguments with this method. We will have to
  987. # handle the URL keyword differently.
  988. set(oneValueArgs
  989. SVN_REPOSITORY
  990. DOWNLOAD_NO_EXTRACT
  991. DOWNLOAD_EXTRACT_TIMESTAMP
  992. BINARY_DIR
  993. SOURCE_DIR
  994. )
  995. cmake_parse_arguments(PARSE_ARGV 1 ARG "" "${oneValueArgs}" "")
  996. string(TOLOWER ${contentName} contentNameLower)
  997. if(NOT ARG_BINARY_DIR)
  998. set(ARG_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  999. endif()
  1000. if(NOT ARG_SOURCE_DIR)
  1001. set(ARG_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
  1002. endif()
  1003. if(ARG_SVN_REPOSITORY)
  1004. # Add a hash of the svn repository URL to the source dir. This works
  1005. # around the problem where if the URL changes, the download would
  1006. # fail because it tries to checkout/update rather than switch the
  1007. # old URL to the new one. We limit the hash to the first 7 characters
  1008. # so that the source path doesn't get overly long (which can be a
  1009. # problem on windows due to path length limits).
  1010. string(SHA1 urlSHA ${ARG_SVN_REPOSITORY})
  1011. string(SUBSTRING ${urlSHA} 0 7 urlSHA)
  1012. string(APPEND ARG_SOURCE_DIR "-${urlSHA}")
  1013. endif()
  1014. # The ExternalProject_Add() call in the sub-build won't see the CMP0135
  1015. # policy setting of our caller. Work out if that policy will be needed and
  1016. # explicitly set the relevant option if not already provided. The condition
  1017. # here is essentially an abbreviated version of the logic in
  1018. # ExternalProject's _ep_add_download_command() function.
  1019. if(NOT ARG_DOWNLOAD_NO_EXTRACT AND
  1020. NOT DEFINED ARG_DOWNLOAD_EXTRACT_TIMESTAMP)
  1021. list(FIND ARGN URL urlIndex)
  1022. if(urlIndex GREATER_EQUAL 0)
  1023. math(EXPR urlIndex "${urlIndex} + 1")
  1024. list(LENGTH ARGN numArgs)
  1025. if(urlIndex GREATER_EQUAL numArgs)
  1026. message(FATAL_ERROR
  1027. "URL keyword needs to be followed by at least one URL"
  1028. )
  1029. endif()
  1030. # If we have multiple URLs, none of them are allowed to be local paths.
  1031. # Therefore, we can test just the first URL, and if it is non-local, so
  1032. # will be the others if there are more.
  1033. list(GET ARGN ${urlIndex} firstUrl)
  1034. if(NOT IS_DIRECTORY "${firstUrl}")
  1035. cmake_policy(GET CMP0135 _FETCHCONTENT_CMP0135
  1036. PARENT_SCOPE # undocumented, do not use outside of CMake
  1037. )
  1038. if(_FETCHCONTENT_CMP0135 STREQUAL "")
  1039. message(AUTHOR_WARNING
  1040. "The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy "
  1041. "CMP0135 is not set. The policy's OLD behavior will be used. "
  1042. "When using a URL download, the timestamps of extracted files "
  1043. "should preferably be that of the time of extraction, otherwise "
  1044. "code that depends on the extracted contents might not be "
  1045. "rebuilt if the URL changes. The OLD behavior preserves the "
  1046. "timestamps from the archive instead, but this is usually not "
  1047. "what you want. Update your project to the NEW behavior or "
  1048. "specify the DOWNLOAD_EXTRACT_TIMESTAMP option with a value of "
  1049. "true to avoid this robustness issue."
  1050. )
  1051. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  1052. elseif(_FETCHCONTENT_CMP0135 STREQUAL "NEW")
  1053. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP FALSE)
  1054. else()
  1055. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  1056. endif()
  1057. endif()
  1058. endif()
  1059. endif()
  1060. # Add back in the keyword args we pulled out and potentially tweaked/added
  1061. foreach(key IN LISTS oneValueArgs)
  1062. if(DEFINED ARG_${key})
  1063. list(PREPEND ARG_UNPARSED_ARGUMENTS ${key} "${ARG_${key}}")
  1064. endif()
  1065. endforeach()
  1066. set(__argsQuoted)
  1067. foreach(__item IN LISTS ARG_UNPARSED_ARGUMENTS)
  1068. string(APPEND __argsQuoted " [==[${__item}]==]")
  1069. endforeach()
  1070. cmake_language(EVAL CODE
  1071. "__FetchContent_declareDetails(${contentNameLower} ${__argsQuoted})"
  1072. )
  1073. endfunction()
  1074. #=======================================================================
  1075. # Set/get whether the specified content has been populated yet.
  1076. # The setter also records the source and binary dirs used.
  1077. #=======================================================================
  1078. # Semi-internal use. Projects must not call this directly. Dependency
  1079. # providers must call it if they satisfy a request made with the
  1080. # FETCHCONTENT_MAKEAVAILABLE_SERIAL method (that is the only permitted
  1081. # place to call it outside of the FetchContent module).
  1082. function(FetchContent_SetPopulated contentName)
  1083. cmake_parse_arguments(PARSE_ARGV 1 arg
  1084. ""
  1085. "SOURCE_DIR;BINARY_DIR"
  1086. ""
  1087. )
  1088. if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "")
  1089. message(FATAL_ERROR "Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}")
  1090. endif()
  1091. string(TOLOWER ${contentName} contentNameLower)
  1092. set(prefix "_FetchContent_${contentNameLower}")
  1093. set(propertyName "${prefix}_sourceDir")
  1094. define_property(GLOBAL PROPERTY ${propertyName})
  1095. if("${arg_SOURCE_DIR}" STREQUAL "")
  1096. # Don't discard a previously provided SOURCE_DIR
  1097. get_property(arg_SOURCE_DIR GLOBAL PROPERTY ${propertyName})
  1098. endif()
  1099. set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}")
  1100. set(propertyName "${prefix}_binaryDir")
  1101. define_property(GLOBAL PROPERTY ${propertyName})
  1102. if("${arg_BINARY_DIR}" STREQUAL "")
  1103. # Don't discard a previously provided BINARY_DIR
  1104. get_property(arg_BINARY_DIR GLOBAL PROPERTY ${propertyName})
  1105. endif()
  1106. set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}")
  1107. set(propertyName "${prefix}_populated")
  1108. define_property(GLOBAL PROPERTY ${propertyName})
  1109. set_property(GLOBAL PROPERTY ${propertyName} TRUE)
  1110. endfunction()
  1111. # Set variables in the calling scope for any of the retrievable
  1112. # properties. If no specific properties are requested, variables
  1113. # will be set for all retrievable properties.
  1114. #
  1115. # This function is intended to also be used by projects as the canonical
  1116. # way to detect whether they should call FetchContent_Populate()
  1117. # and pull the populated source into the build with add_subdirectory(),
  1118. # if they are using the populated content in that way.
  1119. function(FetchContent_GetProperties contentName)
  1120. string(TOLOWER ${contentName} contentNameLower)
  1121. set(options "")
  1122. set(oneValueArgs SOURCE_DIR BINARY_DIR POPULATED)
  1123. set(multiValueArgs "")
  1124. cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  1125. if(NOT ARG_SOURCE_DIR AND
  1126. NOT ARG_BINARY_DIR AND
  1127. NOT ARG_POPULATED)
  1128. # No specific properties requested, provide them all
  1129. set(ARG_SOURCE_DIR ${contentNameLower}_SOURCE_DIR)
  1130. set(ARG_BINARY_DIR ${contentNameLower}_BINARY_DIR)
  1131. set(ARG_POPULATED ${contentNameLower}_POPULATED)
  1132. endif()
  1133. set(prefix "_FetchContent_${contentNameLower}")
  1134. if(ARG_SOURCE_DIR)
  1135. set(propertyName "${prefix}_sourceDir")
  1136. get_property(value GLOBAL PROPERTY ${propertyName})
  1137. if(value)
  1138. set(${ARG_SOURCE_DIR} ${value} PARENT_SCOPE)
  1139. endif()
  1140. endif()
  1141. if(ARG_BINARY_DIR)
  1142. set(propertyName "${prefix}_binaryDir")
  1143. get_property(value GLOBAL PROPERTY ${propertyName})
  1144. if(value)
  1145. set(${ARG_BINARY_DIR} ${value} PARENT_SCOPE)
  1146. endif()
  1147. endif()
  1148. if(ARG_POPULATED)
  1149. set(propertyName "${prefix}_populated")
  1150. get_property(value GLOBAL PROPERTY ${propertyName} DEFINED)
  1151. set(${ARG_POPULATED} ${value} PARENT_SCOPE)
  1152. endif()
  1153. endfunction()
  1154. #=======================================================================
  1155. # Performing the population
  1156. #=======================================================================
  1157. # The value of contentName will always have been lowercased by the caller.
  1158. # All other arguments are assumed to be options that are understood by
  1159. # ExternalProject_Add(), except for QUIET and SUBBUILD_DIR.
  1160. function(__FetchContent_directPopulate contentName)
  1161. set(options
  1162. QUIET
  1163. )
  1164. set(oneValueArgs
  1165. SUBBUILD_DIR
  1166. SOURCE_DIR
  1167. BINARY_DIR
  1168. # We need special processing if DOWNLOAD_NO_EXTRACT is true
  1169. DOWNLOAD_NO_EXTRACT
  1170. # Prevent the following from being passed through
  1171. CONFIGURE_COMMAND
  1172. BUILD_COMMAND
  1173. INSTALL_COMMAND
  1174. TEST_COMMAND
  1175. # We force these to be ON since we are always executing serially
  1176. # and we want all steps to have access to the terminal in case they
  1177. # need input from the command line (e.g. ask for a private key password)
  1178. # or they want to provide timely progress. We silently absorb and
  1179. # discard these if they are set by the caller.
  1180. USES_TERMINAL_DOWNLOAD
  1181. USES_TERMINAL_UPDATE
  1182. USES_TERMINAL_PATCH
  1183. )
  1184. set(multiValueArgs "")
  1185. cmake_parse_arguments(PARSE_ARGV 1 ARG
  1186. "${options}" "${oneValueArgs}" "${multiValueArgs}")
  1187. if(NOT ARG_SUBBUILD_DIR)
  1188. message(FATAL_ERROR "Internal error: SUBBUILD_DIR not set")
  1189. elseif(NOT IS_ABSOLUTE "${ARG_SUBBUILD_DIR}")
  1190. set(ARG_SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SUBBUILD_DIR}")
  1191. endif()
  1192. if(NOT ARG_SOURCE_DIR)
  1193. message(FATAL_ERROR "Internal error: SOURCE_DIR not set")
  1194. elseif(NOT IS_ABSOLUTE "${ARG_SOURCE_DIR}")
  1195. set(ARG_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SOURCE_DIR}")
  1196. endif()
  1197. if(NOT ARG_BINARY_DIR)
  1198. message(FATAL_ERROR "Internal error: BINARY_DIR not set")
  1199. elseif(NOT IS_ABSOLUTE "${ARG_BINARY_DIR}")
  1200. set(ARG_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_BINARY_DIR}")
  1201. endif()
  1202. # Ensure the caller can know where to find the source and build directories
  1203. # with some convenient variables. Doing this here ensures the caller sees
  1204. # the correct result in the case where the default values are overridden by
  1205. # the content details set by the project.
  1206. set(${contentName}_SOURCE_DIR "${ARG_SOURCE_DIR}" PARENT_SCOPE)
  1207. set(${contentName}_BINARY_DIR "${ARG_BINARY_DIR}" PARENT_SCOPE)
  1208. # The unparsed arguments may contain spaces, so build up ARG_EXTRA
  1209. # in such a way that it correctly substitutes into the generated
  1210. # CMakeLists.txt file with each argument quoted.
  1211. unset(ARG_EXTRA)
  1212. foreach(arg IN LISTS ARG_UNPARSED_ARGUMENTS)
  1213. set(ARG_EXTRA "${ARG_EXTRA} \"${arg}\"")
  1214. endforeach()
  1215. if(ARG_DOWNLOAD_NO_EXTRACT)
  1216. set(ARG_EXTRA "${ARG_EXTRA} DOWNLOAD_NO_EXTRACT YES")
  1217. set(__FETCHCONTENT_COPY_FILE
  1218. "
  1219. ExternalProject_Get_Property(${contentName}-populate DOWNLOADED_FILE)
  1220. get_filename_component(dlFileName \"\${DOWNLOADED_FILE}\" NAME)
  1221. ExternalProject_Add_Step(${contentName}-populate copyfile
  1222. COMMAND \"${CMAKE_COMMAND}\" -E copy_if_different
  1223. \"<DOWNLOADED_FILE>\" \"${ARG_SOURCE_DIR}\"
  1224. DEPENDEES patch
  1225. DEPENDERS configure
  1226. BYPRODUCTS \"${ARG_SOURCE_DIR}/\${dlFileName}\"
  1227. COMMENT \"Copying file to SOURCE_DIR\"
  1228. )
  1229. ")
  1230. else()
  1231. unset(__FETCHCONTENT_COPY_FILE)
  1232. endif()
  1233. # Hide output if requested, but save it to a variable in case there's an
  1234. # error so we can show the output upon failure. When not quiet, don't
  1235. # capture the output to a variable because the user may want to see the
  1236. # output as it happens (e.g. progress during long downloads). Combine both
  1237. # stdout and stderr in the one capture variable so the output stays in order.
  1238. if (ARG_QUIET)
  1239. set(outputOptions
  1240. OUTPUT_VARIABLE capturedOutput
  1241. ERROR_VARIABLE capturedOutput
  1242. )
  1243. else()
  1244. set(capturedOutput)
  1245. set(outputOptions)
  1246. message(STATUS "Populating ${contentName}")
  1247. endif()
  1248. if(CMAKE_GENERATOR)
  1249. set(subCMakeOpts "-G${CMAKE_GENERATOR}")
  1250. if(CMAKE_GENERATOR_PLATFORM)
  1251. list(APPEND subCMakeOpts "-A${CMAKE_GENERATOR_PLATFORM}")
  1252. endif()
  1253. if(CMAKE_GENERATOR_TOOLSET)
  1254. list(APPEND subCMakeOpts "-T${CMAKE_GENERATOR_TOOLSET}")
  1255. endif()
  1256. if(CMAKE_MAKE_PROGRAM)
  1257. list(APPEND subCMakeOpts "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
  1258. endif()
  1259. # Override the sub-build's configuration types for multi-config generators.
  1260. # This ensures we are not affected by any custom setting from the project
  1261. # and can always request a known configuration further below.
  1262. get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  1263. if(is_multi_config)
  1264. list(APPEND subCMakeOpts "-DCMAKE_CONFIGURATION_TYPES:STRING=Debug")
  1265. endif()
  1266. else()
  1267. # Likely we've been invoked via CMake's script mode where no
  1268. # generator is set (and hence CMAKE_MAKE_PROGRAM could not be
  1269. # trusted even if provided). We will have to rely on being
  1270. # able to find the default generator and build tool.
  1271. unset(subCMakeOpts)
  1272. endif()
  1273. set(__FETCHCONTENT_CACHED_INFO "")
  1274. set(__passthrough_vars
  1275. CMAKE_EP_GIT_REMOTE_UPDATE_STRATEGY
  1276. CMAKE_TLS_VERIFY
  1277. CMAKE_TLS_CAINFO
  1278. CMAKE_NETRC
  1279. CMAKE_NETRC_FILE
  1280. )
  1281. foreach(var IN LISTS __passthrough_vars)
  1282. if(DEFINED ${var})
  1283. # Embed directly in the generated CMakeLists.txt file to avoid making
  1284. # the cmake command line excessively long. It also makes debugging and
  1285. # testing easier.
  1286. string(APPEND __FETCHCONTENT_CACHED_INFO "set(${var} [==[${${var}}]==])\n")
  1287. endif()
  1288. endforeach()
  1289. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  1290. list(FIND ARG_UNPARSED_ARGUMENTS GIT_REPOSITORY indexResult)
  1291. if(indexResult GREATER_EQUAL 0)
  1292. find_package(Git QUIET)
  1293. string(APPEND __FETCHCONTENT_CACHED_INFO "
  1294. # Pass through things we've already detected in the main project to avoid
  1295. # paying the cost of redetecting them again in ExternalProject_Add()
  1296. set(GIT_EXECUTABLE [==[${GIT_EXECUTABLE}]==])
  1297. set(GIT_VERSION_STRING [==[${GIT_VERSION_STRING}]==])
  1298. set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  1299. [==[${GIT_EXECUTABLE};${GIT_VERSION_STRING}]==]
  1300. )
  1301. ")
  1302. endif()
  1303. # Create and build a separate CMake project to carry out the population.
  1304. # If we've already previously done these steps, they will not cause
  1305. # anything to be updated, so extra rebuilds of the project won't occur.
  1306. # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
  1307. # has this set to something not findable on the PATH. We also ensured above
  1308. # that the Debug config will be defined for multi-config generators.
  1309. configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent/CMakeLists.cmake.in"
  1310. "${ARG_SUBBUILD_DIR}/CMakeLists.txt")
  1311. execute_process(
  1312. COMMAND ${CMAKE_COMMAND} ${subCMakeOpts} .
  1313. RESULT_VARIABLE result
  1314. ${outputOptions}
  1315. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  1316. )
  1317. if(result)
  1318. if(capturedOutput)
  1319. message("${capturedOutput}")
  1320. endif()
  1321. message(FATAL_ERROR "CMake step for ${contentName} failed: ${result}")
  1322. endif()
  1323. execute_process(
  1324. COMMAND ${CMAKE_COMMAND} --build . --config Debug
  1325. RESULT_VARIABLE result
  1326. ${outputOptions}
  1327. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  1328. )
  1329. if(result)
  1330. if(capturedOutput)
  1331. message("${capturedOutput}")
  1332. endif()
  1333. message(FATAL_ERROR "Build step for ${contentName} failed: ${result}")
  1334. endif()
  1335. endfunction()
  1336. option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist")
  1337. option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population")
  1338. option(FETCHCONTENT_QUIET "Enables QUIET option for all content population" ON)
  1339. set(FETCHCONTENT_BASE_DIR "${CMAKE_BINARY_DIR}/_deps" CACHE PATH "Directory under which to collect all populated content")
  1340. # Populate the specified content using details stored from
  1341. # an earlier call to FetchContent_Declare().
  1342. function(FetchContent_Populate contentName)
  1343. if(NOT contentName)
  1344. message(FATAL_ERROR "Empty contentName not allowed for FetchContent_Populate()")
  1345. endif()
  1346. string(TOLOWER ${contentName} contentNameLower)
  1347. if(ARGN)
  1348. # This is the direct population form with details fully specified
  1349. # as part of the call, so we already have everything we need
  1350. __FetchContent_directPopulate(
  1351. ${contentNameLower}
  1352. SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-subbuild"
  1353. SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-src"
  1354. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-build"
  1355. ${ARGN} # Could override any of the above ..._DIR variables
  1356. )
  1357. # Pass source and binary dir variables back to the caller
  1358. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  1359. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  1360. # Don't set global properties, or record that we did this population, since
  1361. # this was a direct call outside of the normal declared details form.
  1362. # We only want to save values in the global properties for content that
  1363. # honors the hierarchical details mechanism so that projects are not
  1364. # robbed of the ability to override details set in nested projects.
  1365. return()
  1366. endif()
  1367. # No details provided, so assume they were saved from an earlier call
  1368. # to FetchContent_Declare(). Do a check that we haven't already
  1369. # populated this content before in case the caller forgot to check.
  1370. FetchContent_GetProperties(${contentName})
  1371. if(${contentNameLower}_POPULATED)
  1372. if("${${contentNameLower}_SOURCE_DIR}" STREQUAL "")
  1373. message(FATAL_ERROR
  1374. "Content ${contentName} already populated by find_package() or a "
  1375. "dependency provider"
  1376. )
  1377. else()
  1378. message(FATAL_ERROR
  1379. "Content ${contentName} already populated in ${${contentNameLower}_SOURCE_DIR}"
  1380. )
  1381. endif()
  1382. endif()
  1383. __FetchContent_getSavedDetails(${contentName} contentDetails)
  1384. if("${contentDetails}" STREQUAL "")
  1385. message(FATAL_ERROR "No details have been set for content: ${contentName}")
  1386. endif()
  1387. string(TOUPPER ${contentName} contentNameUpper)
  1388. set(FETCHCONTENT_SOURCE_DIR_${contentNameUpper}
  1389. "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}"
  1390. CACHE PATH "When not empty, overrides where to find pre-populated content for ${contentName}")
  1391. if(FETCHCONTENT_SOURCE_DIR_${contentNameUpper})
  1392. # The source directory has been explicitly provided in the cache,
  1393. # so no population is required. The build directory may still be specified
  1394. # by the declared details though.
  1395. if(NOT IS_ABSOLUTE "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1396. # Don't check this directory because we don't know what location it is
  1397. # expected to be relative to. We can't make this a hard error for backward
  1398. # compatibility reasons.
  1399. message(WARNING "Relative source directory specified. This is not safe, "
  1400. "as it depends on the calling directory scope.\n"
  1401. " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1402. elseif(NOT EXISTS "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1403. message(FATAL_ERROR "Manually specified source directory is missing:\n"
  1404. " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1405. endif()
  1406. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1407. cmake_parse_arguments(savedDetails "" "BINARY_DIR" "" ${contentDetails})
  1408. if(savedDetails_BINARY_DIR)
  1409. set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
  1410. else()
  1411. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  1412. endif()
  1413. elseif(FETCHCONTENT_FULLY_DISCONNECTED)
  1414. # Bypass population and assume source is already there from a previous run.
  1415. # Declared details may override the default source or build directories.
  1416. cmake_parse_arguments(savedDetails "" "SOURCE_DIR;BINARY_DIR" "" ${contentDetails})
  1417. if(savedDetails_SOURCE_DIR)
  1418. set(${contentNameLower}_SOURCE_DIR ${savedDetails_SOURCE_DIR})
  1419. else()
  1420. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
  1421. endif()
  1422. if(savedDetails_BINARY_DIR)
  1423. set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
  1424. else()
  1425. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  1426. endif()
  1427. else()
  1428. # Support both a global "disconnect all updates" and a per-content
  1429. # update test (either one being set disables updates for this content).
  1430. option(FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper}
  1431. "Enables UPDATE_DISCONNECTED behavior just for population of ${contentName}")
  1432. if(FETCHCONTENT_UPDATES_DISCONNECTED OR
  1433. FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper})
  1434. set(disconnectUpdates True)
  1435. else()
  1436. set(disconnectUpdates False)
  1437. endif()
  1438. if(FETCHCONTENT_QUIET)
  1439. set(quietFlag QUIET)
  1440. else()
  1441. unset(quietFlag)
  1442. endif()
  1443. set(__detailsQuoted)
  1444. foreach(__item IN LISTS contentDetails)
  1445. if(NOT __item STREQUAL "OVERRIDE_FIND_PACKAGE")
  1446. string(APPEND __detailsQuoted " [==[${__item}]==]")
  1447. endif()
  1448. endforeach()
  1449. cmake_language(EVAL CODE "
  1450. __FetchContent_directPopulate(
  1451. ${contentNameLower}
  1452. ${quietFlag}
  1453. UPDATE_DISCONNECTED ${disconnectUpdates}
  1454. SUBBUILD_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-subbuild\"
  1455. SOURCE_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src\"
  1456. BINARY_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build\"
  1457. # Put the saved details last so they can override any of the
  1458. # the options we set above (this can include SOURCE_DIR or
  1459. # BUILD_DIR)
  1460. ${__detailsQuoted}
  1461. )"
  1462. )
  1463. endif()
  1464. FetchContent_SetPopulated(
  1465. ${contentName}
  1466. SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}"
  1467. BINARY_DIR "${${contentNameLower}_BINARY_DIR}"
  1468. )
  1469. # Pass variables back to the caller. The variables passed back here
  1470. # must match what FetchContent_GetProperties() sets when it is called
  1471. # with just the content name.
  1472. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  1473. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  1474. set(${contentNameLower}_POPULATED True PARENT_SCOPE)
  1475. endfunction()
  1476. function(__FetchContent_setupFindPackageRedirection contentName)
  1477. __FetchContent_getSavedDetails(${contentName} contentDetails)
  1478. string(TOLOWER ${contentName} contentNameLower)
  1479. get_property(wantFindPackage GLOBAL PROPERTY
  1480. _FetchContent_${contentNameLower}_find_package_args
  1481. DEFINED
  1482. )
  1483. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  1484. list(FIND contentDetails OVERRIDE_FIND_PACKAGE indexResult)
  1485. if(NOT wantFindPackage AND indexResult EQUAL -1)
  1486. # No find_package() redirection allowed
  1487. return()
  1488. endif()
  1489. # We write out dep-config.cmake and dep-config-version.cmake file name
  1490. # forms here because they are forced to lowercase. FetchContent
  1491. # dependency names are case-insensitive, but find_package() config files
  1492. # are only case-insensitive for the -config and -config-version forms,
  1493. # not the Config and ConfigVersion forms.
  1494. set(inFileDir ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent)
  1495. set(configFilePrefix1 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentName}Config")
  1496. set(configFilePrefix2 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentNameLower}-config")
  1497. if(NOT EXISTS "${configFilePrefix1}.cmake" AND
  1498. NOT EXISTS "${configFilePrefix2}.cmake")
  1499. configure_file(${inFileDir}/package-config.cmake.in
  1500. "${configFilePrefix2}.cmake" @ONLY
  1501. )
  1502. endif()
  1503. if(NOT EXISTS "${configFilePrefix1}Version.cmake" AND
  1504. NOT EXISTS "${configFilePrefix2}-version.cmake")
  1505. configure_file(${inFileDir}/package-config-version.cmake.in
  1506. "${configFilePrefix2}-version.cmake" @ONLY
  1507. )
  1508. endif()
  1509. # Now that we've created the redirected package config files, prevent
  1510. # find_package() from delegating to FetchContent and let it find these
  1511. # config files through its normal processing.
  1512. set(propertyName "${prefix}_override_find_package")
  1513. set(GLOBAL PROPERTY ${propertyName} FALSE)
  1514. set(${contentName}_DIR "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}"
  1515. CACHE INTERNAL "Redirected by FetchContent"
  1516. )
  1517. endfunction()
  1518. # Arguments are assumed to be the names of dependencies that have been
  1519. # declared previously and should be populated. It is not an error if
  1520. # any of them have already been populated (they will just be skipped in
  1521. # that case). The command is implemented as a macro so that the variables
  1522. # defined by the FetchContent_GetProperties() and FetchContent_Populate()
  1523. # calls will be available to the caller.
  1524. macro(FetchContent_MakeAvailable)
  1525. # We must append an item, even if the variable is unset, so prefix its value.
  1526. # We will strip that prefix when we pop the value at the end of the macro.
  1527. list(APPEND __cmake_fcCurrentVarsStack
  1528. "__fcprefix__${CMAKE_VERIFY_INTERFACE_HEADER_SETS}"
  1529. )
  1530. set(CMAKE_VERIFY_INTERFACE_HEADER_SETS FALSE)
  1531. get_property(__cmake_providerCommand GLOBAL PROPERTY
  1532. __FETCHCONTENT_MAKEAVAILABLE_SERIAL_PROVIDER
  1533. )
  1534. foreach(__cmake_contentName IN ITEMS ${ARGV})
  1535. string(TOLOWER ${__cmake_contentName} __cmake_contentNameLower)
  1536. # If user specified FETCHCONTENT_SOURCE_DIR_... for this dependency, that
  1537. # overrides everything else and we shouldn't try to use find_package() or
  1538. # a dependency provider.
  1539. string(TOUPPER ${__cmake_contentName} __cmake_contentNameUpper)
  1540. if("${FETCHCONTENT_SOURCE_DIR_${__cmake_contentNameUpper}}" STREQUAL "")
  1541. # Dependency provider gets first opportunity, but prevent infinite
  1542. # recursion if we are called again for the same thing
  1543. if(NOT "${__cmake_providerCommand}" STREQUAL "" AND
  1544. NOT DEFINED __cmake_fcProvider_${__cmake_contentNameLower})
  1545. message(VERBOSE
  1546. "Trying FETCHCONTENT_MAKEAVAILABLE_SERIAL dependency provider for "
  1547. "${__cmake_contentName}"
  1548. )
  1549. # It's still valid if there are no saved details. The project may have
  1550. # been written to assume a dependency provider is always set and will
  1551. # provide dependencies without having any declared details for them.
  1552. __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
  1553. set(__cmake_providerArgs
  1554. "FETCHCONTENT_MAKEAVAILABLE_SERIAL"
  1555. "${__cmake_contentName}"
  1556. )
  1557. # Empty arguments must be preserved because of things like
  1558. # GIT_SUBMODULES (see CMP0097)
  1559. foreach(__cmake_item IN LISTS __cmake_contentDetails)
  1560. string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
  1561. endforeach()
  1562. # This property might be defined but empty. As long as it is defined,
  1563. # find_package() can be called.
  1564. get_property(__cmake_addfpargs GLOBAL PROPERTY
  1565. _FetchContent_${contentNameLower}_find_package_args
  1566. DEFINED
  1567. )
  1568. if(__cmake_addfpargs)
  1569. get_property(__cmake_fpargs GLOBAL PROPERTY
  1570. _FetchContent_${contentNameLower}_find_package_args
  1571. )
  1572. string(APPEND __cmake_providerArgs " FIND_PACKAGE_ARGS")
  1573. foreach(__cmake_item IN LISTS __cmake_fpargs)
  1574. string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
  1575. endforeach()
  1576. endif()
  1577. # Calling the provider could lead to FetchContent_MakeAvailable() being
  1578. # called for a nested dependency. That nested call may occur in the
  1579. # current variable scope. We have to save and restore the variables we
  1580. # need preserved.
  1581. list(APPEND __cmake_fcCurrentVarsStack
  1582. ${__cmake_contentName}
  1583. ${__cmake_contentNameLower}
  1584. )
  1585. set(__cmake_fcProvider_${__cmake_contentNameLower} YES)
  1586. cmake_language(EVAL CODE "${__cmake_providerCommand}(${__cmake_providerArgs})")
  1587. unset(__cmake_fcProvider_${__cmake_contentNameLower})
  1588. list(POP_BACK __cmake_fcCurrentVarsStack
  1589. __cmake_contentNameLower
  1590. __cmake_contentName
  1591. )
  1592. unset(__cmake_providerArgs)
  1593. unset(__cmake_addfpargs)
  1594. unset(__cmake_fpargs)
  1595. unset(__cmake_item)
  1596. unset(__cmake_contentDetails)
  1597. FetchContent_GetProperties(${__cmake_contentName})
  1598. if(${__cmake_contentNameLower}_POPULATED)
  1599. continue()
  1600. endif()
  1601. endif()
  1602. # Check if we've been asked to try find_package() first, even if we
  1603. # have already populated this dependency. If we previously tried to
  1604. # use find_package() for this and it succeeded, those things might
  1605. # no longer be in scope, so we have to do it again.
  1606. get_property(__cmake_haveFpArgs GLOBAL PROPERTY
  1607. _FetchContent_${__cmake_contentNameLower}_find_package_args DEFINED
  1608. )
  1609. if(__cmake_haveFpArgs)
  1610. unset(__cmake_haveFpArgs)
  1611. message(VERBOSE "Trying find_package(${__cmake_contentName} ...) before FetchContent")
  1612. get_property(__cmake_fpArgs GLOBAL PROPERTY
  1613. _FetchContent_${__cmake_contentNameLower}_find_package_args
  1614. )
  1615. # This call could lead to FetchContent_MakeAvailable() being called for
  1616. # a nested dependency and it may occur in the current variable scope.
  1617. # We have to save/restore the variables we need to preserve.
  1618. list(APPEND __cmake_fcCurrentNameStack
  1619. ${__cmake_contentName}
  1620. ${__cmake_contentNameLower}
  1621. )
  1622. find_package(${__cmake_contentName} ${__cmake_fpArgs})
  1623. list(POP_BACK __cmake_fcCurrentNameStack
  1624. __cmake_contentNameLower
  1625. __cmake_contentName
  1626. )
  1627. unset(__cmake_fpArgs)
  1628. if(${__cmake_contentName}_FOUND)
  1629. FetchContent_SetPopulated(${__cmake_contentName})
  1630. FetchContent_GetProperties(${__cmake_contentName})
  1631. continue()
  1632. endif()
  1633. endif()
  1634. else()
  1635. unset(__cmake_haveFpArgs)
  1636. endif()
  1637. FetchContent_GetProperties(${__cmake_contentName})
  1638. if(NOT ${__cmake_contentNameLower}_POPULATED)
  1639. FetchContent_Populate(${__cmake_contentName})
  1640. __FetchContent_setupFindPackageRedirection(${__cmake_contentName})
  1641. # Only try to call add_subdirectory() if the populated content
  1642. # can be treated that way. Protecting the call with the check
  1643. # allows this function to be used for projects that just want
  1644. # to ensure the content exists, such as to provide content at
  1645. # a known location. We check the saved details for an optional
  1646. # SOURCE_SUBDIR which can be used in the same way as its meaning
  1647. # for ExternalProject. It won't matter if it was passed through
  1648. # to the ExternalProject sub-build, since it would have been
  1649. # ignored there.
  1650. set(__cmake_srcdir "${${__cmake_contentNameLower}_SOURCE_DIR}")
  1651. __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
  1652. if("${__cmake_contentDetails}" STREQUAL "")
  1653. message(FATAL_ERROR "No details have been set for content: ${__cmake_contentName}")
  1654. endif()
  1655. cmake_parse_arguments(__cmake_arg "" "SOURCE_SUBDIR" "" ${__cmake_contentDetails})
  1656. if(NOT "${__cmake_arg_SOURCE_SUBDIR}" STREQUAL "")
  1657. string(APPEND __cmake_srcdir "/${__cmake_arg_SOURCE_SUBDIR}")
  1658. endif()
  1659. if(EXISTS ${__cmake_srcdir}/CMakeLists.txt)
  1660. add_subdirectory(${__cmake_srcdir} ${${__cmake_contentNameLower}_BINARY_DIR})
  1661. endif()
  1662. unset(__cmake_srcdir)
  1663. unset(__cmake_contentDetails)
  1664. unset(__cmake_arg_SOURCE_SUBDIR)
  1665. endif()
  1666. endforeach()
  1667. # Prefix will be "__fcprefix__"
  1668. list(POP_BACK __cmake_fcCurrentVarsStack __cmake_original_verify_setting)
  1669. string(SUBSTRING "${__cmake_original_verify_setting}"
  1670. 12 -1 __cmake_original_verify_setting
  1671. )
  1672. set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${__cmake_original_verify_setting})
  1673. # clear local variables to prevent leaking into the caller's scope
  1674. unset(__cmake_contentName)
  1675. unset(__cmake_contentNameLower)
  1676. unset(__cmake_contentNameUpper)
  1677. unset(__cmake_providerCommand)
  1678. unset(__cmake_original_verify_setting)
  1679. endmacro()