FetchContent.cmake 83 KB

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