FetchContent.cmake 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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. The ``FetchContent`` module also supports defining and populating
  57. content in a single call, with no check for whether the content has been
  58. populated elsewhere already. This should not be done in projects, but may
  59. be appropriate for populating content in
  60. :ref:`CMake script mode <Script Processing Mode>`.
  61. See :command:`FetchContent_Populate` for details.
  62. Commands
  63. ^^^^^^^^
  64. .. command:: FetchContent_Declare
  65. .. code-block:: cmake
  66. FetchContent_Declare(
  67. <name>
  68. <contentOptions>...
  69. [EXCLUDE_FROM_ALL]
  70. [SYSTEM]
  71. [OVERRIDE_FIND_PACKAGE |
  72. FIND_PACKAGE_ARGS args...]
  73. )
  74. The ``FetchContent_Declare()`` function records the options that describe
  75. how to populate the specified content. If such details have already
  76. been recorded earlier in this project (regardless of where in the project
  77. hierarchy), this and all later calls for the same content ``<name>`` are
  78. ignored. This "first to record, wins" approach is what allows hierarchical
  79. projects to have parent projects override content details of child projects.
  80. The content ``<name>`` can be any string without spaces, but good practice
  81. would be to use only letters, numbers, and underscores. The name will be
  82. treated case-insensitively, and it should be obvious for the content it
  83. represents. It is often the name of the child project, or the value given
  84. to its top level :command:`project` command (if it is a CMake project).
  85. For well-known public projects, the name should generally be the official
  86. name of the project. Choosing an unusual name makes it unlikely that other
  87. projects needing that same content will use the same name, leading to
  88. the content being populated multiple times.
  89. The ``<contentOptions>`` can be any of the download, update, or patch options
  90. that the :command:`ExternalProject_Add` command understands. The configure,
  91. build, install, and test steps are explicitly disabled, so options related
  92. to those steps will be ignored. The ``SOURCE_SUBDIR`` option is an
  93. exception, see :command:`FetchContent_MakeAvailable` for details on how that
  94. affects behavior.
  95. .. versionchanged:: 3.30
  96. When policy :policy:`CMP0168` is set to ``NEW``, some output-related and
  97. directory-related options are ignored. See the policy documentation for
  98. details.
  99. In most cases, ``<contentOptions>`` will just be a couple of options defining
  100. the download method and method-specific details like a commit tag or archive
  101. hash. For example:
  102. .. code-block:: cmake
  103. FetchContent_Declare(
  104. googletest
  105. GIT_REPOSITORY https://github.com/google/googletest.git
  106. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  107. )
  108. FetchContent_Declare(
  109. myCompanyIcons
  110. URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
  111. URL_HASH MD5=5588a7b18261c20068beabfb4f530b87
  112. )
  113. FetchContent_Declare(
  114. myCompanyCertificates
  115. SVN_REPOSITORY svn+ssh://svn.mycompany.com/srv/svn/trunk/certs
  116. SVN_REVISION -r12345
  117. )
  118. Where contents are being fetched from a remote location and you do not
  119. control that server, it is advisable to use a hash for ``GIT_TAG`` rather
  120. than a branch or tag name. A commit hash is more secure and helps to
  121. confirm that the downloaded contents are what you expected.
  122. .. versionchanged:: 3.14
  123. Commands for the download, update, or patch steps can access the terminal.
  124. This may be needed for things like password prompts or real-time display
  125. of command progress.
  126. .. versionadded:: 3.22
  127. The :variable:`CMAKE_TLS_VERIFY`, :variable:`CMAKE_TLS_CAINFO`,
  128. :variable:`CMAKE_NETRC`, and :variable:`CMAKE_NETRC_FILE` variables now
  129. provide the defaults for their corresponding content options, just like
  130. they do for :command:`ExternalProject_Add`. Previously, these variables
  131. were ignored by the ``FetchContent`` module.
  132. .. versionadded:: 3.24
  133. ``FIND_PACKAGE_ARGS``
  134. This option is for scenarios where the
  135. :command:`FetchContent_MakeAvailable` command may first try a call to
  136. :command:`find_package` to satisfy the dependency for ``<name>``.
  137. By default, such a call would be simply ``find_package(<name>)``, but
  138. ``FIND_PACKAGE_ARGS`` can be used to provide additional arguments to be
  139. appended after the ``<name>``. ``FIND_PACKAGE_ARGS`` can also be given
  140. with nothing after it, which indicates that :command:`find_package` can
  141. still be called if :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` is
  142. set to ``OPT_IN``, or is not set.
  143. It would not normally be appropriate to specify ``REQUIRED`` as one of
  144. the additional arguments after ``FIND_PACKAGE_ARGS``. Doing so would
  145. mean the :command:`find_package` call must succeed, so none of the other
  146. details specified in the ``FetchContent_Declare()`` call would get a
  147. chance to be used as a fall-back.
  148. Everything after the ``FIND_PACKAGE_ARGS`` keyword is appended to the
  149. :command:`find_package` call, so all other ``<contentOptions>`` must
  150. come before the ``FIND_PACKAGE_ARGS`` keyword. If the
  151. :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to true
  152. at the time ``FetchContent_Declare()`` is called, a ``GLOBAL`` keyword
  153. will be appended to the :command:`find_package` arguments if it was
  154. not already specified. It will also be appended if
  155. ``FIND_PACKAGE_ARGS`` was not given, but
  156. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``ALWAYS``.
  157. ``OVERRIDE_FIND_PACKAGE`` cannot be used when ``FIND_PACKAGE_ARGS`` is
  158. given.
  159. :ref:`dependency_providers` discusses another way that
  160. :command:`FetchContent_MakeAvailable` calls can be redirected.
  161. ``FIND_PACKAGE_ARGS`` is intended for project control, whereas
  162. dependency providers allow users to override project behavior.
  163. ``OVERRIDE_FIND_PACKAGE``
  164. When a ``FetchContent_Declare(<name> ...)`` call includes this option,
  165. subsequent calls to ``find_package(<name> ...)`` will ensure that
  166. ``FetchContent_MakeAvailable(<name>)`` has been called, then use the
  167. config package files in the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
  168. directory (which are usually created by ``FetchContent_MakeAvailable()``).
  169. This effectively makes :command:`FetchContent_MakeAvailable` override
  170. :command:`find_package` for the named dependency, allowing the former to
  171. satisfy the package requirements of the latter. ``FIND_PACKAGE_ARGS``
  172. cannot be used when ``OVERRIDE_FIND_PACKAGE`` is given.
  173. If a :ref:`dependency provider <dependency_providers>` has been set
  174. and the project calls :command:`find_package` for the ``<name>``
  175. dependency, ``OVERRIDE_FIND_PACKAGE`` will not prevent the provider
  176. from seeing that call. Dependency providers always have the opportunity
  177. to intercept any direct call to :command:`find_package`, except if that
  178. call contains the ``BYPASS_PROVIDER`` option.
  179. .. versionadded:: 3.25
  180. ``SYSTEM``
  181. If the ``SYSTEM`` argument is provided, the :prop_dir:`SYSTEM` directory
  182. property of a subdirectory added by
  183. :command:`FetchContent_MakeAvailable` will be set to true. This will
  184. affect non-imported targets created as part of that command.
  185. See the :prop_tgt:`SYSTEM` target property documentation for a more
  186. detailed discussion of the effects.
  187. .. versionadded:: 3.28
  188. ``EXCLUDE_FROM_ALL``
  189. If the ``EXCLUDE_FROM_ALL`` argument is provided, then targets in the
  190. subdirectory added by :command:`FetchContent_MakeAvailable` will not be
  191. included in the ``ALL`` target by default, and may be excluded from IDE
  192. project files. See the :command:`add_subdirectory` ``EXCLUDE_FROM_ALL``
  193. argument documentation for a more detailed discussion of the effects.
  194. .. command:: FetchContent_MakeAvailable
  195. .. versionadded:: 3.14
  196. .. code-block:: cmake
  197. FetchContent_MakeAvailable(<name1> [<name2>...])
  198. This command ensures that each of the named dependencies are made available
  199. to the project by the time it returns. There must have been a call to
  200. :command:`FetchContent_Declare` for each dependency, and the first such call
  201. will control how that dependency will be made available, as described below.
  202. If ``<lowercaseName>_SOURCE_DIR`` is not set:
  203. * .. versionadded:: 3.24
  204. If a :ref:`dependency provider <dependency_providers>` is set, call the
  205. provider's command with ``FETCHCONTENT_MAKEAVAILABLE_SERIAL`` as the
  206. first argument, followed by the arguments of the first call to
  207. :command:`FetchContent_Declare` for ``<name>``. If ``SOURCE_DIR`` or
  208. ``BINARY_DIR`` were not part of the original declared arguments, they
  209. will be added with their default values.
  210. If :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``NEVER``
  211. when the details were declared, any ``FIND_PACKAGE_ARGS`` will be
  212. omitted. The ``OVERRIDE_FIND_PACKAGE`` keyword is also always omitted.
  213. If the provider fulfilled the request, ``FetchContent_MakeAvailable()``
  214. will consider that dependency handled, skip the remaining steps below,
  215. and move on to the next dependency in the list.
  216. * .. versionadded:: 3.24
  217. If permitted, :command:`find_package(<name> [<args>...]) <find_package>`
  218. will be called, where ``<args>...`` may be provided by the
  219. ``FIND_PACKAGE_ARGS`` option in :command:`FetchContent_Declare`.
  220. The value of the :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable
  221. at the time :command:`FetchContent_Declare` was called determines whether
  222. ``FetchContent_MakeAvailable()`` can call :command:`find_package`.
  223. If the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to
  224. true when ``FetchContent_MakeAvailable()`` is called, it still affects
  225. any imported targets created when that in turn calls
  226. :command:`find_package`, even if that variable was false when the
  227. corresponding details were declared.
  228. If the dependency was not satisfied by a provider or a
  229. :command:`find_package` call, ``FetchContent_MakeAvailable()`` then uses
  230. the following logic to make the dependency available:
  231. * If the dependency has already been populated earlier in this run, set
  232. the ``<lowercaseName>_POPULATED``, ``<lowercaseName>_SOURCE_DIR``, and
  233. ``<lowercaseName>_BINARY_DIR`` variables in the same way as a call to
  234. :command:`FetchContent_GetProperties`, then skip the remaining steps
  235. below and move on to the next dependency in the list.
  236. * Populate the dependency using the details recorded by an earlier call
  237. to :command:`FetchContent_Declare`.
  238. Halt with a fatal error if no such details have been recorded.
  239. :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>` can be used to override
  240. the declared details and use content provided at the specified location
  241. instead.
  242. * .. versionadded:: 3.24
  243. Ensure the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` directory
  244. contains a ``<lowercaseName>-config.cmake`` and a
  245. ``<lowercaseName>-config-version.cmake`` file (or equivalently,
  246. ``<name>Config.cmake`` and ``<name>ConfigVersion.cmake``).
  247. The directory that the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
  248. variable points to is cleared at the start of every CMake run.
  249. If no config file exists after populating the dependency in the previous
  250. step, a minimal one will be written which :command:`includes <include>`
  251. any ``<lowercaseName>-extra.cmake`` or ``<name>Extra.cmake`` file with
  252. the ``OPTIONAL`` flag (so the files can be missing and won't generate a
  253. warning). Similarly, if no config version file exists, a very simple
  254. one will be written which sets ``PACKAGE_VERSION_COMPATIBLE`` and
  255. ``PACKAGE_VERSION_EXACT`` to true. This ensures all future calls to
  256. :command:`find_package()` for the dependency will use the redirected
  257. config file, regardless of any version requirements.
  258. CMake cannot automatically determine an arbitrary dependency's version,
  259. so it cannot set ``PACKAGE_VERSION``.
  260. When a dependency is pulled in via :command:`add_subdirectory` in the
  261. next step, it may choose to overwrite the generated config version file
  262. in :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` with one that also sets
  263. ``PACKAGE_VERSION``.
  264. The dependency may also write a ``<lowercaseName>-extra.cmake`` or
  265. ``<name>Extra.cmake`` file to perform custom processing, or define any
  266. variables that their normal (installed) package config file would
  267. otherwise usually define (many projects don't do any custom processing
  268. or set any variables and therefore have no need to do this).
  269. If required, the main project can write these files instead if the
  270. dependency project doesn't do so. This allows the main project to
  271. add missing details from older dependencies that haven't or can't be
  272. updated to support this functionality.
  273. See `Integrating With find_package()`_ for examples.
  274. * If the top directory of the populated content contains a ``CMakeLists.txt``
  275. file, call :command:`add_subdirectory` to add it to the main build.
  276. It is not an error for there to be no ``CMakeLists.txt`` file, which
  277. allows the command to be used for dependencies that make downloaded
  278. content available at a known location, but which do not need or support
  279. being added directly to the build.
  280. .. versionadded:: 3.18
  281. The ``SOURCE_SUBDIR`` option can be given in the declared details to
  282. look somewhere below the top directory instead (i.e. the same way that
  283. ``SOURCE_SUBDIR`` is used by the :command:`ExternalProject_Add`
  284. command). The path provided with ``SOURCE_SUBDIR`` must be relative,
  285. and it will be treated as relative to the top directory. It can also
  286. point to a directory that does not contain a ``CMakeLists.txt`` file,
  287. or even to a directory that doesn't exist. This can be used to avoid
  288. adding a project that contains a ``CMakeLists.txt`` file in its top
  289. directory.
  290. .. versionadded:: 3.25
  291. If the ``SYSTEM`` keyword was included in the call to
  292. :command:`FetchContent_Declare`, the ``SYSTEM`` keyword will be
  293. added to the :command:`add_subdirectory` command.
  294. .. versionadded:: 3.28
  295. If the ``EXCLUDE_FROM_ALL`` keyword was included in the call to
  296. :command:`FetchContent_Declare`, the ``EXCLUDE_FROM_ALL`` keyword will
  297. be added to the :command:`add_subdirectory` command.
  298. .. versionadded:: 3.29
  299. :variable:`CMAKE_EXPORT_FIND_PACKAGE_NAME` is set to the dependency name
  300. before calling :command:`add_subdirectory`.
  301. Projects should aim to declare the details of all dependencies they might
  302. use before they call ``FetchContent_MakeAvailable()`` for any of them.
  303. This ensures that if any of the dependencies are also sub-dependencies of
  304. one or more of the others, the main project still controls the details
  305. that will be used (because it will declare them first before the
  306. dependencies get a chance to). In the following code samples, assume that
  307. the ``uses_other`` dependency also uses ``FetchContent`` to add the ``other``
  308. dependency internally:
  309. .. code-block:: cmake
  310. # WRONG: Should declare all details first
  311. FetchContent_Declare(uses_other ...)
  312. FetchContent_MakeAvailable(uses_other)
  313. FetchContent_Declare(other ...) # Will be ignored, uses_other beat us to it
  314. FetchContent_MakeAvailable(other) # Would use details declared by uses_other
  315. .. code-block:: cmake
  316. # CORRECT: All details declared first, so they will take priority
  317. FetchContent_Declare(uses_other ...)
  318. FetchContent_Declare(other ...)
  319. FetchContent_MakeAvailable(uses_other other)
  320. Note that :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` is explicitly set
  321. to false upon entry to ``FetchContent_MakeAvailable()``, and is restored to
  322. its original value before the command returns. Developers typically only
  323. want to verify header sets from the main project, not those from any
  324. dependencies. This local manipulation of the
  325. :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` variable provides that
  326. intuitive behavior. You can use variables like
  327. :variable:`CMAKE_PROJECT_INCLUDE` or
  328. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE` to turn verification back
  329. on for all or some dependencies. You can also set the
  330. :prop_tgt:`VERIFY_INTERFACE_HEADER_SETS` property of individual targets.
  331. .. command:: FetchContent_Populate
  332. The ``FetchContent_Populate()`` command is a self-contained call which can
  333. be used to perform content population as an isolated operation.
  334. It is rarely the right command to use, projects should almost always use
  335. :command:`FetchContent_Declare` and :command:`FetchContent_MakeAvailable`
  336. instead. The main use case for ``FetchContent_Populate()`` is in
  337. :ref:`CMake script mode <Script Processing Mode>` as part of implementing
  338. some other higher level custom feature.
  339. .. code-block:: cmake
  340. FetchContent_Populate(
  341. <name>
  342. [QUIET]
  343. [SUBBUILD_DIR <subBuildDir>]
  344. [SOURCE_DIR <srcDir>]
  345. [BINARY_DIR <binDir>]
  346. ...
  347. )
  348. At least one option must be specified after `<name>`, otherwise the call
  349. is interpreted differently (see :ref:`below <FetchContent_Populate-depName>`).
  350. The supported options for ``FetchContent_Populate()`` are the same as those
  351. for :command:`FetchContent_Declare()`, with a few exceptions. The following
  352. do not relate to populating content with ``FetchContent_Populate()`` and
  353. therefore are not supported:
  354. * ``EXCLUDE_FROM_ALL``
  355. * ``SYSTEM``
  356. * ``OVERRIDE_FIND_PACKAGE``
  357. * ``FIND_PACKAGE_ARGS``
  358. The few options shown in the signature above are either specific to
  359. ``FetchContent_Populate()``, or their behavior is slightly modified from how
  360. :command:`ExternalProject_Add` treats them:
  361. ``QUIET``
  362. The ``QUIET`` option can be given to hide the output associated with
  363. populating the specified content. If the population fails, the output will
  364. be shown regardless of whether this option was given or not so that the
  365. cause of the failure can be diagnosed. The :variable:`FETCHCONTENT_QUIET`
  366. variable has no effect on ``FetchContent_Populate()`` calls of this form
  367. where the content details are provided directly.
  368. .. versionchanged:: 3.30
  369. The ``QUIET`` option and :variable:`FETCHCONTENT_QUIET` variable have no
  370. effect when policy :policy:`CMP0168` is set to ``NEW``. The output is
  371. still quiet by default in that case, but verbosity is controlled by the
  372. message logging level (see :variable:`CMAKE_MESSAGE_LOG_LEVEL` and
  373. :option:`--log-level <cmake --log-level>`).
  374. ``SUBBUILD_DIR``
  375. The ``SUBBUILD_DIR`` argument can be provided to change the location of the
  376. sub-build created to perform the population. The default value is
  377. ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-subbuild``, and it would be
  378. unusual to need to override this default. If a relative path is specified,
  379. it will be interpreted as relative to :variable:`CMAKE_CURRENT_BINARY_DIR`.
  380. This option should not be confused with the ``SOURCE_SUBDIR`` option, which
  381. only affects the :command:`FetchContent_MakeAvailable` command.
  382. .. versionchanged:: 3.30
  383. ``SUBBUILD_DIR`` is ignored when policy :policy:`CMP0168` is set to
  384. ``NEW``, since there is no sub-build in that case.
  385. ``SOURCE_DIR``, ``BINARY_DIR``
  386. The ``SOURCE_DIR`` and ``BINARY_DIR`` arguments are supported by
  387. :command:`ExternalProject_Add`, but different default values are used by
  388. ``FetchContent_Populate()``. ``SOURCE_DIR`` defaults to
  389. ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-src``, and ``BINARY_DIR``
  390. defaults to ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-build``.
  391. If a relative path is specified, it will be interpreted as relative to
  392. :variable:`CMAKE_CURRENT_BINARY_DIR`.
  393. In addition to the above explicit options, any other unrecognized options are
  394. passed through unmodified to :command:`ExternalProject_Add` to set up the
  395. download, patch, and update steps. The following options are explicitly
  396. prohibited (they are disabled by the ``FetchContent_Populate()`` command):
  397. - ``CONFIGURE_COMMAND``
  398. - ``BUILD_COMMAND``
  399. - ``INSTALL_COMMAND``
  400. - ``TEST_COMMAND``
  401. With this form, the :variable:`FETCHCONTENT_FULLY_DISCONNECTED` and
  402. :variable:`FETCHCONTENT_UPDATES_DISCONNECTED` variables and policy
  403. :policy:`CMP0170` are ignored.
  404. When this form of ``FetchContent_Populate()`` returns, the following
  405. variables will be set in the scope of the caller:
  406. ``<lowercaseName>_SOURCE_DIR``
  407. The location where the populated content can be found upon return.
  408. ``<lowercaseName>_BINARY_DIR``
  409. A directory originally intended for use as a corresponding build directory,
  410. but is unlikely to be relevant when using this form of the command.
  411. If using ``FetchContent_Populate()`` within
  412. :ref:`CMake script mode <Script Processing Mode>`, be aware that the
  413. implementation sets up a sub-build which therefore requires a CMake
  414. generator and build tool to be available. If these cannot be found by
  415. default, then the :variable:`CMAKE_GENERATOR` and potentially the
  416. :variable:`CMAKE_MAKE_PROGRAM` variables will need to be set appropriately
  417. on the command line invoking the script.
  418. .. versionchanged:: 3.30
  419. If policy :policy:`CMP0168` is set to ``NEW``, no sub-build is used.
  420. Within :ref:`CMake script mode <Script Processing Mode>`, that allows
  421. ``FetchContent_Populate()`` to be called without any build tool or
  422. CMake generator.
  423. .. versionadded:: 3.18
  424. Added support for the ``DOWNLOAD_NO_EXTRACT`` option.
  425. .. _`FetchContent_Populate-depName`:
  426. The command supports another form, although it should no longer be used:
  427. .. code-block:: cmake
  428. FetchContent_Populate(<name>)
  429. .. versionchanged:: 3.30
  430. This form is deprecated. Policy :policy:`CMP0169` provides backward
  431. compatibility for projects that still need to use this form, but projects
  432. should be updated to use :command:`FetchContent_MakeAvailable` instead.
  433. In this form, the only argument given to ``FetchContent_Populate()`` is the
  434. ``<name>``. When used this way, the command assumes the content details have
  435. been recorded by an earlier call to :command:`FetchContent_Declare`. The
  436. details are stored in a global property, so they are unaffected by things
  437. like variable or directory scope. Therefore, it doesn't matter where in the
  438. project the details were previously declared, as long as they have been
  439. declared before the call to ``FetchContent_Populate()``. Those saved details
  440. are then used to populate the content using a method based on
  441. :command:`ExternalProject_Add` (see policy :policy:`CMP0168` for important
  442. behavioral aspects of how that is done).
  443. When this form of ``FetchContent_Populate()`` returns, the following
  444. variables will be set in the scope of the caller:
  445. ``<lowercaseName>_POPULATED``
  446. This will always be set to ``TRUE`` by the call.
  447. ``<lowercaseName>_SOURCE_DIR``
  448. The location where the populated content can be found upon return.
  449. ``<lowercaseName>_BINARY_DIR``
  450. A directory intended for use as a corresponding build directory.
  451. The values of the three variables can also be retrieved from anywhere in the
  452. project hierarchy using the :command:`FetchContent_GetProperties` command.
  453. The implementation ensures that if the content has already been populated
  454. in a previous CMake run, that content will be reused rather than repopulating
  455. again. For the common case where population involves downloading content,
  456. the cost of the download is only paid once. But note that it is an error to
  457. call ``FetchContent_Populate(<name>)`` with the same ``<name>`` more than
  458. once within a single CMake run. See :command:`FetchContent_GetProperties`
  459. for how to determine if population of a ``<name>`` has already been
  460. performed in the current run.
  461. .. command:: FetchContent_GetProperties
  462. When using saved content details, a call to
  463. :command:`FetchContent_MakeAvailable` or :command:`FetchContent_Populate`
  464. records information in global properties which can be queried at any time.
  465. This information may include the source and binary directories associated with
  466. the content, and also whether or not the content population has been processed
  467. during the current configure run.
  468. .. code-block:: cmake
  469. FetchContent_GetProperties(
  470. <name>
  471. [SOURCE_DIR <srcDirVar>]
  472. [BINARY_DIR <binDirVar>]
  473. [POPULATED <doneVar>]
  474. )
  475. The ``SOURCE_DIR``, ``BINARY_DIR``, and ``POPULATED`` options can be used to
  476. specify which properties should be retrieved. Each option accepts a value
  477. which is the name of the variable in which to store that property. Most of
  478. the time though, only ``<name>`` is given, in which case the call will then
  479. set the same variables as a call to
  480. :command:`FetchContent_MakeAvailable(name) <FetchContent_MakeAvailable>` or
  481. :command:`FetchContent_Populate(name) <FetchContent_Populate>`.
  482. Note that the ``SOURCE_DIR`` and ``BINARY_DIR`` values can be empty if the
  483. call is fulfilled by a :ref:`dependency provider <dependency_providers>`.
  484. This command is rarely needed when using
  485. :command:`FetchContent_MakeAvailable`. It is more commonly used as part of
  486. implementing the deprecated pattern with :command:`FetchContent_Populate`,
  487. which ensures that the relevant variables will always be defined regardless
  488. of whether or not the population has been performed elsewhere in the project
  489. already:
  490. .. code-block:: cmake
  491. # WARNING: This pattern is deprecated, don't use it!
  492. #
  493. # Check if population has already been performed
  494. FetchContent_GetProperties(depname)
  495. if(NOT depname_POPULATED)
  496. # Fetch the content using previously declared details
  497. FetchContent_Populate(depname)
  498. # Set custom variables, policies, etc.
  499. # ...
  500. # Bring the populated content into the build
  501. add_subdirectory(${depname_SOURCE_DIR} ${depname_BINARY_DIR})
  502. endif()
  503. .. command:: FetchContent_SetPopulated
  504. .. versionadded:: 3.24
  505. .. note::
  506. This command should only be called by
  507. :ref:`dependency providers <dependency_providers>`. Calling it in any
  508. other context is unsupported and future CMake versions may halt with a
  509. fatal error in such cases.
  510. .. code-block:: cmake
  511. FetchContent_SetPopulated(
  512. <name>
  513. [SOURCE_DIR <srcDir>]
  514. [BINARY_DIR <binDir>]
  515. )
  516. If a provider command fulfills a ``FETCHCONTENT_MAKEAVAILABLE_SERIAL``
  517. request, it must call this function before returning. The ``SOURCE_DIR``
  518. and ``BINARY_DIR`` arguments can be used to specify the values that
  519. :command:`FetchContent_GetProperties` should return for its corresponding
  520. arguments. Only provide ``SOURCE_DIR`` and ``BINARY_DIR`` if they have
  521. the same meaning as if they had been populated by the built-in
  522. :command:`FetchContent_MakeAvailable` implementation.
  523. Variables
  524. ^^^^^^^^^
  525. A number of cache variables can influence the behavior where details from a
  526. :command:`FetchContent_Declare` call are used to populate content.
  527. .. note::
  528. All of these variables are intended for the developer to customize behavior.
  529. They should not normally be set by the project.
  530. .. variable:: FETCHCONTENT_BASE_DIR
  531. In most cases, the saved details do not specify any options relating to the
  532. directories to use for the internal sub-build, final source, and build areas.
  533. It is generally best to leave these decisions up to the ``FetchContent``
  534. module to handle on the project's behalf. The ``FETCHCONTENT_BASE_DIR``
  535. cache variable controls the point under which all content population
  536. directories are collected, but in most cases, developers would not need to
  537. change this. The default location is ``${CMAKE_BINARY_DIR}/_deps``, but if
  538. developers change this value, they should aim to keep the path short and
  539. just below the top level of the build tree to avoid running into path
  540. length problems on Windows.
  541. .. variable:: FETCHCONTENT_QUIET
  542. The logging output during population can be quite verbose, making the
  543. configure stage quite noisy. This cache option (``ON`` by default) hides
  544. all population output unless an error is encountered. If experiencing
  545. problems with hung downloads, temporarily switching this option off may
  546. help diagnose which content population is causing the issue.
  547. .. versionchanged:: 3.30
  548. ``FETCHCONTENT_QUIET`` is ignored if policy :policy:`CMP0168` is set to
  549. ``NEW``. The output is still quiet by default in that case, but verbosity
  550. is controlled by the message logging level (see
  551. :variable:`CMAKE_MESSAGE_LOG_LEVEL` and
  552. :option:`--log-level <cmake --log-level>`).
  553. .. variable:: FETCHCONTENT_FULLY_DISCONNECTED
  554. When this option is enabled, no attempt is made to download or update
  555. any content. It is assumed that all content has already been populated in
  556. a previous run, or the source directories have been pointed at existing
  557. contents the developer has provided manually (using options described
  558. further below). When the developer knows that no changes have been made to
  559. any content details, turning this option ``ON`` can speed up
  560. the configure stage. It is ``OFF`` by default.
  561. .. note::
  562. The ``FETCHCONTENT_FULLY_DISCONNECTED`` variable is not an appropriate way
  563. to prevent any network access on the first run in a build directory.
  564. Doing so can break projects, lead to misleading error messages, and hide
  565. subtle population failures. This variable is specifically intended to
  566. only be turned on *after* the first time CMake has been run.
  567. If you want to prevent network access even on the first run, use a
  568. :ref:`dependency provider <dependency_providers>` and populate the
  569. dependency from local content instead.
  570. .. versionchanged:: 3.30
  571. The constraint that the source directory has already been populated when
  572. ``FETCHCONTENT_FULLY_DISCONNECTED`` is true is now enforced.
  573. See policy :policy:`CMP0170`.
  574. .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED
  575. This is a less severe download/update control compared to
  576. :variable:`FETCHCONTENT_FULLY_DISCONNECTED`. Instead of bypassing all
  577. download and update logic, ``FETCHCONTENT_UPDATES_DISCONNECTED`` only
  578. prevents the update step from making connections to remote servers
  579. when using the git or hg download methods. Updates still occur if details
  580. about the update step change, but the update is attempted with only the
  581. information already available locally (so switching to a different tag or
  582. commit that is already fetched locally will succeed, but switching to an
  583. unknown commit hash will fail). The download step is not affected, so if
  584. content has not been downloaded previously, it will still be downloaded
  585. when this option is enabled. This can speed up the configure step, but
  586. not as much as :variable:`FETCHCONTENT_FULLY_DISCONNECTED`.
  587. ``FETCHCONTENT_UPDATES_DISCONNECTED`` is ``OFF`` by default.
  588. .. variable:: FETCHCONTENT_TRY_FIND_PACKAGE_MODE
  589. .. versionadded:: 3.24
  590. This variable modifies the details that :command:`FetchContent_Declare`
  591. records for a given dependency. While it ultimately controls the behavior
  592. of :command:`FetchContent_MakeAvailable`, it is the variable's value when
  593. :command:`FetchContent_Declare` is called that gets used. It makes no
  594. difference what the variable is set to when
  595. :command:`FetchContent_MakeAvailable` is called. Since the variable should
  596. only be set by the user and not by projects directly, it will typically have
  597. the same value throughout anyway, so this distinction is not usually
  598. noticeable.
  599. ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` ultimately controls whether
  600. :command:`FetchContent_MakeAvailable` is allowed to call
  601. :command:`find_package` to satisfy a dependency. The variable can be set
  602. to one of the following values:
  603. ``OPT_IN``
  604. :command:`FetchContent_MakeAvailable` will only call
  605. :command:`find_package` if the :command:`FetchContent_Declare` call
  606. included a ``FIND_PACKAGE_ARGS`` keyword. This is also the default
  607. behavior if ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` is not set.
  608. ``ALWAYS``
  609. :command:`find_package` can be called by
  610. :command:`FetchContent_MakeAvailable` regardless of whether the
  611. :command:`FetchContent_Declare` call included a ``FIND_PACKAGE_ARGS``
  612. keyword or not. If no ``FIND_PACKAGE_ARGS`` keyword was given, the
  613. behavior will be as though ``FIND_PACKAGE_ARGS`` had been provided,
  614. with no additional arguments after it.
  615. ``NEVER``
  616. :command:`FetchContent_MakeAvailable` will not call
  617. :command:`find_package`. Any ``FIND_PACKAGE_ARGS`` given to the
  618. :command:`FetchContent_Declare` call will be ignored.
  619. As a special case, if the :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>`
  620. variable has a non-empty value for a dependency, it is assumed that the
  621. user is overriding all other methods of making that dependency available.
  622. ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` will have no effect on that
  623. dependency and :command:`FetchContent_MakeAvailable` will not try to call
  624. :command:`find_package` for it.
  625. In addition to the above, the following variables are also defined for each
  626. content name:
  627. .. variable:: FETCHCONTENT_SOURCE_DIR_<uppercaseName>
  628. If this is set, no download or update steps are performed for the specified
  629. content and the ``<lowercaseName>_SOURCE_DIR`` variable returned to the
  630. caller is pointed at this location. This gives developers a way to have a
  631. separate checkout of the content that they can modify freely without
  632. interference from the build. The build simply uses that existing source,
  633. but it still defines ``<lowercaseName>_BINARY_DIR`` to point inside its own
  634. build area. Developers are strongly encouraged to use this mechanism rather
  635. than editing the sources populated in the default location, as changes to
  636. sources in the default location can be lost when content population details
  637. are changed by the project.
  638. .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED_<uppercaseName>
  639. This is the per-content equivalent of
  640. :variable:`FETCHCONTENT_UPDATES_DISCONNECTED`. If the global option or
  641. this option is ``ON``, then updates for the git and hg methods will not
  642. contact any remote for the named content. They will only use information
  643. already available locally. Disabling updates for individual content can
  644. be useful for content whose details rarely change, while still leaving
  645. other frequently changing content with updates enabled.
  646. .. _`fetch-content-examples`:
  647. Examples
  648. ^^^^^^^^
  649. Typical Case
  650. """"""""""""
  651. This first fairly straightforward example ensures that some popular testing
  652. frameworks are available to the main build:
  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. )
  660. FetchContent_Declare(
  661. Catch2
  662. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  663. GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
  664. )
  665. # After the following call, the CMake targets defined by googletest and
  666. # Catch2 will be available to the rest of the build
  667. FetchContent_MakeAvailable(googletest Catch2)
  668. .. _FetchContent-find_package-integration-examples:
  669. Integrating With find_package()
  670. """""""""""""""""""""""""""""""
  671. For the previous example, if the user wanted to try to find ``googletest``
  672. and ``Catch2`` via :command:`find_package` first before trying to download
  673. and build them from source, they could set the
  674. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable to ``ALWAYS``.
  675. This would also affect any other calls to :command:`FetchContent_Declare`
  676. throughout the project, which might not be acceptable. The behavior can be
  677. enabled for just these two dependencies instead by adding ``FIND_PACKAGE_ARGS``
  678. to the declared details and leaving
  679. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` unset, or set to ``OPT_IN``:
  680. .. code-block:: cmake
  681. include(FetchContent)
  682. FetchContent_Declare(
  683. googletest
  684. GIT_REPOSITORY https://github.com/google/googletest.git
  685. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  686. FIND_PACKAGE_ARGS NAMES GTest
  687. )
  688. FetchContent_Declare(
  689. Catch2
  690. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  691. GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
  692. FIND_PACKAGE_ARGS
  693. )
  694. # This will try calling find_package() first for both dependencies
  695. FetchContent_MakeAvailable(googletest Catch2)
  696. For ``Catch2``, no additional arguments to :command:`find_package` are needed,
  697. so no additional arguments are provided after the ``FIND_PACKAGE_ARGS``
  698. keyword. For ``googletest``, its package is more commonly called ``GTest``,
  699. so arguments are added to support it being found by that name.
  700. If the user wanted to disable :command:`FetchContent_MakeAvailable` from
  701. calling :command:`find_package` for any dependency, even if it provided
  702. ``FIND_PACKAGE_ARGS`` in its declared details, they could set
  703. :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` to ``NEVER``.
  704. If the project wanted to indicate that these two dependencies should be
  705. downloaded and built from source and that :command:`find_package` calls
  706. should be redirected to use the built dependencies, the
  707. ``OVERRIDE_FIND_PACKAGE`` option should be used when declaring the content
  708. details:
  709. .. code-block:: cmake
  710. include(FetchContent)
  711. FetchContent_Declare(
  712. googletest
  713. GIT_REPOSITORY https://github.com/google/googletest.git
  714. GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
  715. OVERRIDE_FIND_PACKAGE
  716. )
  717. FetchContent_Declare(
  718. Catch2
  719. GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  720. GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
  721. OVERRIDE_FIND_PACKAGE
  722. )
  723. # The following will automatically forward through to FetchContent_MakeAvailable()
  724. find_package(googletest)
  725. find_package(Catch2)
  726. CMake provides a FindGTest module which defines some variables that older
  727. projects may use instead of linking to the imported targets. To support
  728. those cases, we can provide an extra file. In keeping with the
  729. "first to define, wins" philosophy of ``FetchContent``, we only write out
  730. that file if something else hasn't already done so.
  731. .. code-block:: cmake
  732. FetchContent_MakeAvailable(googletest)
  733. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extra.cmake AND
  734. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestExtra.cmake)
  735. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extra.cmake
  736. [=[
  737. if("${GTEST_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest)
  738. set(GTEST_LIBRARIES GTest::gtest)
  739. endif()
  740. if("${GTEST_MAIN_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest_main)
  741. set(GTEST_MAIN_LIBRARIES GTest::gtest_main)
  742. endif()
  743. if("${GTEST_BOTH_LIBRARIES}" STREQUAL "")
  744. set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
  745. endif()
  746. ]=])
  747. endif()
  748. Projects will also likely be using ``find_package(GTest)`` rather than
  749. ``find_package(googletest)``, but it is possible to make use of the
  750. :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` area to pull in the latter as
  751. a dependency of the former. This is likely to be sufficient to satisfy
  752. a typical ``find_package(GTest)`` call.
  753. .. code-block:: cmake
  754. FetchContent_MakeAvailable(googletest)
  755. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake AND
  756. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfig.cmake)
  757. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
  758. [=[
  759. include(CMakeFindDependencyMacro)
  760. find_dependency(googletest)
  761. ]=])
  762. endif()
  763. if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake AND
  764. NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfigVersion.cmake)
  765. file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
  766. [=[
  767. include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-config-version.cmake OPTIONAL)
  768. if(NOT PACKAGE_VERSION_COMPATIBLE)
  769. include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestConfigVersion.cmake OPTIONAL)
  770. endif()
  771. ]=])
  772. endif()
  773. Overriding Where To Find CMakeLists.txt
  774. """""""""""""""""""""""""""""""""""""""
  775. If the sub-project's ``CMakeLists.txt`` file is not at the top level of its
  776. source tree, the ``SOURCE_SUBDIR`` option can be used to tell ``FetchContent``
  777. where to find it. The following example shows how to use that option, and
  778. it also sets a variable which is meaningful to the subproject before pulling
  779. it into the main build (set as an ``INTERNAL`` cache variable to avoid
  780. problems with policy :policy:`CMP0077`):
  781. .. code-block:: cmake
  782. include(FetchContent)
  783. FetchContent_Declare(
  784. protobuf
  785. GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
  786. GIT_TAG ae50d9b9902526efd6c7a1907d09739f959c6297 # v3.15.0
  787. SOURCE_SUBDIR cmake
  788. )
  789. set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
  790. FetchContent_MakeAvailable(protobuf)
  791. Complex Dependency Hierarchies
  792. """"""""""""""""""""""""""""""
  793. In more complex project hierarchies, the dependency relationships can be more
  794. complicated. Consider a hierarchy where ``projA`` is the top level project and
  795. it depends directly on projects ``projB`` and ``projC``. Both ``projB`` and
  796. ``projC`` can be built standalone and they also both depend on another project
  797. ``projD``. ``projB`` additionally depends on ``projE``. This example assumes
  798. that all five projects are available on a company git server. The
  799. ``CMakeLists.txt`` of each project might have sections like the following:
  800. .. code-block:: cmake
  801. :caption: *projA*
  802. include(FetchContent)
  803. FetchContent_Declare(
  804. projB
  805. GIT_REPOSITORY [email protected]:git/projB.git
  806. GIT_TAG 4a89dc7e24ff212a7b5167bef7ab079d
  807. )
  808. FetchContent_Declare(
  809. projC
  810. GIT_REPOSITORY [email protected]:git/projC.git
  811. GIT_TAG 4ad4016bd1d8d5412d135cf8ceea1bb9
  812. )
  813. FetchContent_Declare(
  814. projD
  815. GIT_REPOSITORY [email protected]:git/projD.git
  816. GIT_TAG origin/integrationBranch
  817. )
  818. FetchContent_Declare(
  819. projE
  820. GIT_REPOSITORY [email protected]:git/projE.git
  821. GIT_TAG v2.3-rc1
  822. )
  823. # Order is important, see notes in the discussion further below
  824. FetchContent_MakeAvailable(projD projB projC)
  825. .. code-block:: cmake
  826. :caption: *projB*
  827. include(FetchContent)
  828. FetchContent_Declare(
  829. projD
  830. GIT_REPOSITORY [email protected]:git/projD.git
  831. GIT_TAG 20b415f9034bbd2a2e8216e9a5c9e632
  832. )
  833. FetchContent_Declare(
  834. projE
  835. GIT_REPOSITORY [email protected]:git/projE.git
  836. GIT_TAG 68e20f674a48be38d60e129f600faf7d
  837. )
  838. FetchContent_MakeAvailable(projD projE)
  839. .. code-block:: cmake
  840. :caption: *projC*
  841. include(FetchContent)
  842. FetchContent_Declare(
  843. projD
  844. GIT_REPOSITORY [email protected]:git/projD.git
  845. GIT_TAG 7d9a17ad2c962aa13e2fbb8043fb6b8a
  846. )
  847. FetchContent_MakeAvailable(projD)
  848. A few key points should be noted in the above:
  849. - ``projB`` and ``projC`` define different content details for ``projD``,
  850. but ``projA`` also defines a set of content details for ``projD``.
  851. Because ``projA`` will define them first, the details from ``projB`` and
  852. ``projC`` will not be used. The override details defined by ``projA``
  853. are not required to match either of those from ``projB`` or ``projC``, but
  854. it is up to the higher level project to ensure that the details it does
  855. define still make sense for the child projects.
  856. - In the ``projA`` call to :command:`FetchContent_MakeAvailable`, ``projD``
  857. is listed ahead of ``projB`` and ``projC``, so it will be populated before
  858. either ``projB`` or ``projC``. It isn't required for ``projA`` to do this,
  859. doing so ensures that ``projA`` fully controls the environment in which
  860. ``projD`` is brought into the build (directory properties are particularly
  861. relevant).
  862. - While ``projA`` defines content details for ``projE``, it does not need
  863. to explicitly call ``FetchContent_MakeAvailable(projE)`` or
  864. ``FetchContent_Populate(projD)`` itself. Instead, it leaves that to the
  865. child ``projB``. For higher level projects, it is often enough to just
  866. define the override content details and leave the actual population to the
  867. child projects. This saves repeating the same thing at each level of the
  868. project hierarchy unnecessarily, but it should only be done if directory
  869. properties set by dependencies are not expected to influence the population
  870. of the shared dependency (``projE`` in this case).
  871. Populating Content Without Adding It To The Build
  872. """""""""""""""""""""""""""""""""""""""""""""""""
  873. Projects don't always need to add the populated content to the build.
  874. Sometimes the project just wants to make the downloaded content available at
  875. a predictable location. The next example ensures that a set of standard
  876. company toolchain files (and potentially even the toolchain binaries
  877. themselves) is available early enough to be used for that same build.
  878. .. code-block:: cmake
  879. cmake_minimum_required(VERSION 3.14)
  880. include(FetchContent)
  881. FetchContent_Declare(
  882. mycom_toolchains
  883. URL https://intranet.mycompany.com//toolchains_1.3.2.tar.gz
  884. )
  885. FetchContent_MakeAvailable(mycom_toolchains)
  886. project(CrossCompileExample)
  887. The project could be configured to use one of the downloaded toolchains like
  888. so:
  889. .. code-block:: shell
  890. cmake -DCMAKE_TOOLCHAIN_FILE=_deps/mycom_toolchains-src/toolchain_arm.cmake /path/to/src
  891. When CMake processes the ``CMakeLists.txt`` file, it will download and unpack
  892. the tarball into ``_deps/mycompany_toolchains-src`` relative to the build
  893. directory. The :variable:`CMAKE_TOOLCHAIN_FILE` variable is not used until
  894. the :command:`project` command is reached, at which point CMake looks for the
  895. named toolchain file relative to the build directory. Because the tarball has
  896. already been downloaded and unpacked by then, the toolchain file will be in
  897. place, even the very first time that :program:`cmake` is run in the build directory.
  898. Populating Content In CMake Script Mode
  899. """""""""""""""""""""""""""""""""""""""
  900. This last example demonstrates how one might download and unpack a
  901. firmware tarball using CMake's :ref:`script mode <Script Processing Mode>`.
  902. The call to :command:`FetchContent_Populate` specifies all the content details
  903. and the unpacked firmware will be placed in a ``firmware`` directory below the
  904. current working directory.
  905. .. code-block:: cmake
  906. :caption: :file:`getFirmware.cmake`
  907. # NOTE: Intended to be run in script mode with cmake -P
  908. include(FetchContent)
  909. FetchContent_Populate(
  910. firmware
  911. URL https://mycompany.com/assets/firmware-1.23-arm.tar.gz
  912. URL_HASH MD5=68247684da89b608d466253762b0ff11
  913. SOURCE_DIR firmware
  914. )
  915. #]=======================================================================]
  916. # Control policies for most of the things defined by this module. Only a few
  917. # FetchContent_MakeAvailable() implementation details are excluded for
  918. # backward compatibility reasons (see just after the endblock()).
  919. block(SCOPE_FOR POLICIES)
  920. cmake_policy(VERSION 3.29)
  921. include(${CMAKE_CURRENT_LIST_DIR}/ExternalProject/shared_internal_commands.cmake)
  922. #=======================================================================
  923. # Recording and retrieving content details for later population
  924. #=======================================================================
  925. # Internal use, projects must not call this directly. It is
  926. # intended for use by FetchContent_Declare() only.
  927. #
  928. # Sets a content-specific global property (not meant for use
  929. # outside of functions defined here in this file) which can later
  930. # be retrieved using __FetchContent_getSavedDetails() with just the
  931. # same content name. If there is already a value stored in the
  932. # property, it is left unchanged and this call has no effect.
  933. # This allows parent projects to define the content details,
  934. # overriding anything a child project may try to set (properties
  935. # are not cached between runs, so the first thing to set it in a
  936. # build will be in control).
  937. function(__FetchContent_declareDetails contentName)
  938. string(TOLOWER ${contentName} contentNameLower)
  939. set(savedDetailsPropertyName "_FetchContent_${contentNameLower}_savedDetails")
  940. get_property(alreadyDefined GLOBAL PROPERTY ${savedDetailsPropertyName} DEFINED)
  941. if(alreadyDefined)
  942. return()
  943. endif()
  944. if("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "ALWAYS")
  945. set(__tryFindPackage TRUE)
  946. set(__tryFindPackageAllowed TRUE)
  947. elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "NEVER")
  948. set(__tryFindPackage FALSE)
  949. set(__tryFindPackageAllowed FALSE)
  950. elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "OPT_IN" OR
  951. NOT DEFINED FETCHCONTENT_TRY_FIND_PACKAGE_MODE)
  952. set(__tryFindPackage FALSE)
  953. set(__tryFindPackageAllowed TRUE)
  954. else()
  955. message(FATAL_ERROR
  956. "Unsupported value for FETCHCONTENT_TRY_FIND_PACKAGE_MODE: "
  957. "${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}"
  958. )
  959. endif()
  960. set(__cmdArgs)
  961. set(__findPackageArgs)
  962. set(__sawQuietKeyword NO)
  963. set(__sawGlobalKeyword NO)
  964. set(__direct_population NO)
  965. foreach(__item IN LISTS ARGN)
  966. if(__item STREQUAL "__DIRECT_POPULATION")
  967. set(__direct_population YES)
  968. continue()
  969. endif()
  970. if(DEFINED __findPackageArgs)
  971. # All remaining args are for find_package()
  972. string(APPEND __findPackageArgs " [==[${__item}]==]")
  973. if(__item STREQUAL "QUIET")
  974. set(__sawQuietKeyword YES)
  975. elseif(__item STREQUAL "GLOBAL")
  976. set(__sawGlobalKeyword YES)
  977. endif()
  978. continue()
  979. endif()
  980. # Still processing non-find_package() args
  981. if(__item STREQUAL "FIND_PACKAGE_ARGS")
  982. if(__tryFindPackageAllowed)
  983. set(__tryFindPackage TRUE)
  984. endif()
  985. # All arguments after this keyword are for find_package(). Define the
  986. # variable but with an empty value initially. This allows us to check
  987. # at the start of the loop whether to store remaining items in this
  988. # variable or not. Note that there could be no more args, which is still
  989. # a valid case because we automatically provide ${contentName} as the
  990. # package name and there may not need to be any further arguments.
  991. set(__findPackageArgs "")
  992. continue() # Don't store this item
  993. elseif(__item STREQUAL "OVERRIDE_FIND_PACKAGE")
  994. set(__tryFindPackageAllowed FALSE)
  995. # Define a separate dedicated property for find_package() to check
  996. # in its implementation. This will be a placeholder until FetchContent
  997. # actually does the population. After that, we will have created a
  998. # stand-in config file that find_package() will pick up instead.
  999. set(propertyName "_FetchContent_${contentNameLower}_override_find_package")
  1000. define_property(GLOBAL PROPERTY ${propertyName})
  1001. set_property(GLOBAL PROPERTY ${propertyName} TRUE)
  1002. endif()
  1003. string(APPEND __cmdArgs " [==[${__item}]==]")
  1004. endforeach()
  1005. set_property(GLOBAL PROPERTY
  1006. "_FetchContent_${contentNameLower}_direct_population" ${__direct_population}
  1007. )
  1008. define_property(GLOBAL PROPERTY ${savedDetailsPropertyName})
  1009. cmake_language(EVAL CODE
  1010. "set_property(GLOBAL PROPERTY ${savedDetailsPropertyName} ${__cmdArgs})"
  1011. )
  1012. if(__tryFindPackage AND __tryFindPackageAllowed)
  1013. set(propertyName "_FetchContent_${contentNameLower}_find_package_args")
  1014. define_property(GLOBAL PROPERTY ${propertyName})
  1015. if(NOT __sawQuietKeyword)
  1016. string(PREPEND __findPackageArgs "QUIET ")
  1017. endif()
  1018. if(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL AND NOT __sawGlobalKeyword)
  1019. string(APPEND __findPackageArgs " GLOBAL")
  1020. endif()
  1021. cmake_language(EVAL CODE
  1022. "set_property(GLOBAL PROPERTY ${propertyName} ${__findPackageArgs})"
  1023. )
  1024. endif()
  1025. endfunction()
  1026. # Internal use, projects must not call this directly. It is
  1027. # intended for use by the FetchContent_Declare() function.
  1028. #
  1029. # Retrieves details saved for the specified content in an
  1030. # earlier call to __FetchContent_declareDetails().
  1031. function(__FetchContent_getSavedDetails contentName outVar)
  1032. string(TOLOWER ${contentName} contentNameLower)
  1033. set(propertyName "_FetchContent_${contentNameLower}_savedDetails")
  1034. get_property(alreadyDefined GLOBAL PROPERTY ${propertyName} DEFINED)
  1035. if(NOT alreadyDefined)
  1036. message(FATAL_ERROR "No content details recorded for ${contentName}")
  1037. endif()
  1038. get_property(propertyValue GLOBAL PROPERTY ${propertyName})
  1039. set(${outVar} "${propertyValue}" PARENT_SCOPE)
  1040. endfunction()
  1041. # Saves population details of the content, sets defaults for the
  1042. # SOURCE_DIR and BUILD_DIR.
  1043. function(FetchContent_Declare contentName)
  1044. # Always check this even if we won't save these details.
  1045. # This helps projects catch errors earlier.
  1046. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  1047. list(FIND ARGN OVERRIDE_FIND_PACKAGE index_OVERRIDE_FIND_PACKAGE)
  1048. list(FIND ARGN FIND_PACKAGE_ARGS index_FIND_PACKAGE_ARGS)
  1049. if(index_OVERRIDE_FIND_PACKAGE GREATER_EQUAL 0 AND
  1050. index_FIND_PACKAGE_ARGS GREATER_EQUAL 0)
  1051. message(FATAL_ERROR
  1052. "Cannot specify both OVERRIDE_FIND_PACKAGE and FIND_PACKAGE_ARGS "
  1053. "when declaring details for ${contentName}"
  1054. )
  1055. endif()
  1056. # Because we are only looking for a subset of the supported keywords, we
  1057. # cannot check for multi-value arguments with this method. We will have to
  1058. # handle the URL keyword differently.
  1059. set(oneValueArgs
  1060. GIT_REPOSITORY
  1061. SVN_REPOSITORY
  1062. DOWNLOAD_NO_EXTRACT
  1063. DOWNLOAD_EXTRACT_TIMESTAMP
  1064. BINARY_DIR
  1065. SOURCE_DIR
  1066. )
  1067. cmake_parse_arguments(PARSE_ARGV 1 ARG "" "${oneValueArgs}" "")
  1068. string(TOLOWER ${contentName} contentNameLower)
  1069. if(NOT ARG_BINARY_DIR)
  1070. set(ARG_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  1071. endif()
  1072. if(NOT ARG_SOURCE_DIR)
  1073. set(ARG_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
  1074. endif()
  1075. if(ARG_GIT_REPOSITORY)
  1076. # We resolve the GIT_REPOSITORY here so that we get the right parent in the
  1077. # remote selection logic. In the sub-build, ExternalProject_Add() would see
  1078. # the private sub-build directory as the parent project, but the parent
  1079. # project should be the one that called FetchContent_Declare(). We resolve
  1080. # a relative repo here so that the sub-build's ExternalProject_Add() only
  1081. # ever sees a non-relative repo.
  1082. # Since these checks may be non-trivial on some platforms (notably Windows),
  1083. # don't perform them if we won't be using these details. This also allows
  1084. # projects to override calls with relative URLs when they have checked out
  1085. # the parent project in an unexpected way, such as from a mirror or fork.
  1086. set(savedDetailsPropertyName "_FetchContent_${contentNameLower}_savedDetails")
  1087. get_property(alreadyDefined GLOBAL PROPERTY ${savedDetailsPropertyName} DEFINED)
  1088. if(NOT alreadyDefined)
  1089. cmake_policy(GET CMP0150 cmp0150
  1090. PARENT_SCOPE # undocumented, do not use outside of CMake
  1091. )
  1092. _ep_resolve_git_remote(_resolved_git_repository
  1093. "${ARG_GIT_REPOSITORY}" "${cmp0150}" "${FETCHCONTENT_BASE_DIR}"
  1094. )
  1095. set(ARG_GIT_REPOSITORY "${_resolved_git_repository}")
  1096. endif()
  1097. endif()
  1098. if(ARG_SVN_REPOSITORY)
  1099. # Add a hash of the svn repository URL to the source dir. This works
  1100. # around the problem where if the URL changes, the download would
  1101. # fail because it tries to checkout/update rather than switch the
  1102. # old URL to the new one. We limit the hash to the first 7 characters
  1103. # so that the source path doesn't get overly long (which can be a
  1104. # problem on windows due to path length limits).
  1105. string(SHA1 urlSHA ${ARG_SVN_REPOSITORY})
  1106. string(SUBSTRING ${urlSHA} 0 7 urlSHA)
  1107. string(APPEND ARG_SOURCE_DIR "-${urlSHA}")
  1108. endif()
  1109. # The ExternalProject_Add() call in the sub-build won't see the CMP0135
  1110. # policy setting of our caller. Work out if that policy will be needed and
  1111. # explicitly set the relevant option if not already provided. The condition
  1112. # here is essentially an abbreviated version of the logic in
  1113. # ExternalProject's _ep_add_download_command() function.
  1114. if(NOT ARG_DOWNLOAD_NO_EXTRACT AND
  1115. NOT DEFINED ARG_DOWNLOAD_EXTRACT_TIMESTAMP)
  1116. list(FIND ARGN URL urlIndex)
  1117. if(urlIndex GREATER_EQUAL 0)
  1118. math(EXPR urlIndex "${urlIndex} + 1")
  1119. list(LENGTH ARGN numArgs)
  1120. if(urlIndex GREATER_EQUAL numArgs)
  1121. message(FATAL_ERROR
  1122. "URL keyword needs to be followed by at least one URL"
  1123. )
  1124. endif()
  1125. # If we have multiple URLs, none of them are allowed to be local paths.
  1126. # Therefore, we can test just the first URL, and if it is non-local, so
  1127. # will be the others if there are more.
  1128. list(GET ARGN ${urlIndex} firstUrl)
  1129. if(NOT IS_DIRECTORY "${firstUrl}")
  1130. cmake_policy(GET CMP0135 _FETCHCONTENT_CMP0135
  1131. PARENT_SCOPE # undocumented, do not use outside of CMake
  1132. )
  1133. if(_FETCHCONTENT_CMP0135 STREQUAL "")
  1134. message(AUTHOR_WARNING
  1135. "The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy "
  1136. "CMP0135 is not set. The policy's OLD behavior will be used. "
  1137. "When using a URL download, the timestamps of extracted files "
  1138. "should preferably be that of the time of extraction, otherwise "
  1139. "code that depends on the extracted contents might not be "
  1140. "rebuilt if the URL changes. The OLD behavior preserves the "
  1141. "timestamps from the archive instead, but this is usually not "
  1142. "what you want. Update your project to the NEW behavior or "
  1143. "specify the DOWNLOAD_EXTRACT_TIMESTAMP option with a value of "
  1144. "true to avoid this robustness issue."
  1145. )
  1146. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  1147. elseif(_FETCHCONTENT_CMP0135 STREQUAL "NEW")
  1148. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP FALSE)
  1149. else()
  1150. set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  1151. endif()
  1152. endif()
  1153. endif()
  1154. endif()
  1155. # Add back in the keyword args we pulled out and potentially tweaked/added
  1156. set(forward_args "${ARG_UNPARSED_ARGUMENTS}")
  1157. set(sep EXTERNALPROJECT_INTERNAL_ARGUMENT_SEPARATOR)
  1158. foreach(key IN LISTS oneValueArgs)
  1159. if(DEFINED ARG_${key})
  1160. list(PREPEND forward_args ${key} "${ARG_${key}}" ${sep})
  1161. set(sep "")
  1162. endif()
  1163. endforeach()
  1164. cmake_policy(GET CMP0168 cmp0168
  1165. PARENT_SCOPE # undocumented, do not use outside of CMake
  1166. )
  1167. if(cmp0168 STREQUAL "NEW")
  1168. list(PREPEND forward_args __DIRECT_POPULATION ${sep})
  1169. endif()
  1170. set(__argsQuoted)
  1171. foreach(__item IN LISTS forward_args)
  1172. string(APPEND __argsQuoted " [==[${__item}]==]")
  1173. endforeach()
  1174. cmake_language(EVAL CODE
  1175. "__FetchContent_declareDetails(${contentNameLower} ${__argsQuoted})"
  1176. )
  1177. endfunction()
  1178. #=======================================================================
  1179. # Set/get whether the specified content has been populated yet.
  1180. # The setter also records the source and binary dirs used.
  1181. #=======================================================================
  1182. # Semi-internal use. Projects must not call this directly. Dependency
  1183. # providers must call it if they satisfy a request made with the
  1184. # FETCHCONTENT_MAKEAVAILABLE_SERIAL method (that is the only permitted
  1185. # place to call it outside of the FetchContent module).
  1186. function(FetchContent_SetPopulated contentName)
  1187. cmake_parse_arguments(PARSE_ARGV 1 arg
  1188. ""
  1189. "SOURCE_DIR;BINARY_DIR"
  1190. ""
  1191. )
  1192. if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "")
  1193. message(FATAL_ERROR "Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}")
  1194. endif()
  1195. string(TOLOWER ${contentName} contentNameLower)
  1196. set(prefix "_FetchContent_${contentNameLower}")
  1197. set(propertyName "${prefix}_sourceDir")
  1198. define_property(GLOBAL PROPERTY ${propertyName})
  1199. if("${arg_SOURCE_DIR}" STREQUAL "")
  1200. # Don't discard a previously provided SOURCE_DIR
  1201. get_property(arg_SOURCE_DIR GLOBAL PROPERTY ${propertyName})
  1202. endif()
  1203. set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}")
  1204. set(propertyName "${prefix}_binaryDir")
  1205. define_property(GLOBAL PROPERTY ${propertyName})
  1206. if("${arg_BINARY_DIR}" STREQUAL "")
  1207. # Don't discard a previously provided BINARY_DIR
  1208. get_property(arg_BINARY_DIR GLOBAL PROPERTY ${propertyName})
  1209. endif()
  1210. set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}")
  1211. set(propertyName "${prefix}_populated")
  1212. define_property(GLOBAL PROPERTY ${propertyName})
  1213. set_property(GLOBAL PROPERTY ${propertyName} TRUE)
  1214. endfunction()
  1215. # Set variables in the calling scope for any of the retrievable
  1216. # properties. If no specific properties are requested, variables
  1217. # will be set for all retrievable properties.
  1218. #
  1219. # This function is intended to also be used by projects as the canonical
  1220. # way to detect whether they should call FetchContent_Populate()
  1221. # and pull the populated source into the build with add_subdirectory(),
  1222. # if they are using the populated content in that way.
  1223. function(FetchContent_GetProperties contentName)
  1224. string(TOLOWER ${contentName} contentNameLower)
  1225. set(options "")
  1226. set(oneValueArgs SOURCE_DIR BINARY_DIR POPULATED)
  1227. set(multiValueArgs "")
  1228. cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  1229. if(NOT ARG_SOURCE_DIR AND
  1230. NOT ARG_BINARY_DIR AND
  1231. NOT ARG_POPULATED)
  1232. # No specific properties requested, provide them all
  1233. set(ARG_SOURCE_DIR ${contentNameLower}_SOURCE_DIR)
  1234. set(ARG_BINARY_DIR ${contentNameLower}_BINARY_DIR)
  1235. set(ARG_POPULATED ${contentNameLower}_POPULATED)
  1236. endif()
  1237. set(prefix "_FetchContent_${contentNameLower}")
  1238. if(ARG_SOURCE_DIR)
  1239. set(propertyName "${prefix}_sourceDir")
  1240. get_property(value GLOBAL PROPERTY ${propertyName})
  1241. if(value)
  1242. set(${ARG_SOURCE_DIR} ${value} PARENT_SCOPE)
  1243. endif()
  1244. endif()
  1245. if(ARG_BINARY_DIR)
  1246. set(propertyName "${prefix}_binaryDir")
  1247. get_property(value GLOBAL PROPERTY ${propertyName})
  1248. if(value)
  1249. set(${ARG_BINARY_DIR} ${value} PARENT_SCOPE)
  1250. endif()
  1251. endif()
  1252. if(ARG_POPULATED)
  1253. set(propertyName "${prefix}_populated")
  1254. get_property(value GLOBAL PROPERTY ${propertyName} DEFINED)
  1255. set(${ARG_POPULATED} ${value} PARENT_SCOPE)
  1256. endif()
  1257. endfunction()
  1258. #=======================================================================
  1259. # Performing the population
  1260. #=======================================================================
  1261. # The value of contentName will always have been lowercased by the caller.
  1262. # All other arguments are assumed to be options that are understood by
  1263. # ExternalProject_Add(), except for QUIET and SUBBUILD_DIR.
  1264. function(__FetchContent_doPopulation contentName)
  1265. set(options
  1266. QUIET
  1267. # EXCLUDE_FROM_ALL and SYSTEM have no meaning for ExternalProject, they
  1268. # are only used by us in FetchContent_MakeAvailable(). We need to parse
  1269. # and discard them here.
  1270. EXCLUDE_FROM_ALL
  1271. SYSTEM
  1272. )
  1273. set(oneValueArgs
  1274. SUBBUILD_DIR
  1275. SOURCE_DIR
  1276. BINARY_DIR
  1277. # We need special processing if DOWNLOAD_NO_EXTRACT is true
  1278. DOWNLOAD_NO_EXTRACT
  1279. # Prevent the following from being passed through
  1280. CONFIGURE_COMMAND
  1281. BUILD_COMMAND
  1282. INSTALL_COMMAND
  1283. TEST_COMMAND
  1284. # We force these to be ON since we are always executing serially
  1285. # and we want all steps to have access to the terminal in case they
  1286. # need input from the command line (e.g. ask for a private key password)
  1287. # or they want to provide timely progress. We silently absorb and
  1288. # discard these if they are set by the caller.
  1289. USES_TERMINAL_DOWNLOAD
  1290. USES_TERMINAL_UPDATE
  1291. USES_TERMINAL_PATCH
  1292. # Internal options, may change at any time
  1293. __DIRECT_POPULATION
  1294. )
  1295. set(multiValueArgs "")
  1296. cmake_parse_arguments(PARSE_ARGV 1 ARG
  1297. "${options}" "${oneValueArgs}" "${multiValueArgs}")
  1298. if(DEFINED ARG___DIRECT_POPULATION)
  1299. # Direct call to FetchContent_Populate() with full details. The policy
  1300. # setting of its caller is included in the function arguments.
  1301. set(direct_population ${ARG___DIRECT_POPULATION})
  1302. else()
  1303. # FetchContent_Populate() called with only the name of a dependency.
  1304. # We need the policy setting of the corresponding FetchContent_Declare().
  1305. get_property(direct_population GLOBAL PROPERTY
  1306. "_FetchContent_${contentNameLower}_direct_population"
  1307. )
  1308. endif()
  1309. if(NOT ARG_SUBBUILD_DIR)
  1310. if(NOT direct_population)
  1311. message(FATAL_ERROR "Internal error: SUBBUILD_DIR not set")
  1312. endif()
  1313. elseif(NOT IS_ABSOLUTE "${ARG_SUBBUILD_DIR}")
  1314. set(ARG_SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SUBBUILD_DIR}")
  1315. endif()
  1316. if(NOT ARG_SOURCE_DIR)
  1317. message(FATAL_ERROR "Internal error: SOURCE_DIR not set")
  1318. elseif(NOT IS_ABSOLUTE "${ARG_SOURCE_DIR}")
  1319. set(ARG_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SOURCE_DIR}")
  1320. endif()
  1321. if(NOT ARG_BINARY_DIR)
  1322. message(FATAL_ERROR "Internal error: BINARY_DIR not set")
  1323. elseif(NOT IS_ABSOLUTE "${ARG_BINARY_DIR}")
  1324. set(ARG_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_BINARY_DIR}")
  1325. endif()
  1326. # Ensure the caller can know where to find the source and build directories
  1327. # with some convenient variables. Doing this here ensures the caller sees
  1328. # the correct result in the case where the default values are overridden by
  1329. # the content details set by the project.
  1330. set(${contentName}_SOURCE_DIR "${ARG_SOURCE_DIR}" PARENT_SCOPE)
  1331. set(${contentName}_BINARY_DIR "${ARG_BINARY_DIR}" PARENT_SCOPE)
  1332. if(direct_population)
  1333. __FetchContent_populateDirect()
  1334. else()
  1335. __FetchContent_populateSubbuild()
  1336. endif()
  1337. endfunction()
  1338. function(__FetchContent_populateDirect)
  1339. # Policies CMP0097, CMP0135 and CMP0150 are handled in FetchContent_Declare()
  1340. # and the stored arguments already account for them.
  1341. # For CMP0097, the arguments will always assume NEW behavior by the time
  1342. # we get to here, so ensure ExternalProject sees that.
  1343. set(_EP_CMP0097 NEW)
  1344. set(args_to_parse
  1345. "${ARG_UNPARSED_ARGUMENTS}"
  1346. SOURCE_DIR "${ARG_SOURCE_DIR}"
  1347. BINARY_DIR "${ARG_BINARY_DIR}"
  1348. )
  1349. if(ARG_DOWNLOAD_NO_EXTRACT)
  1350. list(APPEND args_to_parse DOWNLOAD_NO_EXTRACT YES)
  1351. endif()
  1352. get_property(cmake_role GLOBAL PROPERTY CMAKE_ROLE)
  1353. if(cmake_role STREQUAL "PROJECT")
  1354. # We don't support direct population where a project makes a direct call
  1355. # to FetchContent_Populate(). That always goes through ExternalProject and
  1356. # will soon be deprecated anyway.
  1357. set(function_for_args FetchContent_Declare)
  1358. elseif(cmake_role STREQUAL "SCRIPT")
  1359. set(function_for_args FetchContent_Populate)
  1360. else()
  1361. message(FATAL_ERROR "Unsupported context for direct population")
  1362. endif()
  1363. _ep_get_add_keywords(keywords)
  1364. _ep_parse_arguments_to_vars(
  1365. ${function_for_args}
  1366. "${keywords}"
  1367. ${contentName}
  1368. _EP_
  1369. "${args_to_parse}"
  1370. )
  1371. # We use a simplified set of directories here. We do not need the full set
  1372. # of directories that ExternalProject supports, and we don't need the
  1373. # extensive customization options it supports either. Note that
  1374. # _EP_SOURCE_DIR and _EP_BINARY_DIR are always included in the saved args,
  1375. # so we must not set them here.
  1376. set(_EP_STAMP_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-stamp")
  1377. set(_EP_TMP_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-tmp")
  1378. set(_EP_DOWNLOAD_DIR "${_EP_TMP_DIR}")
  1379. # If CMAKE_DISABLE_SOURCE_CHANGES is set to true and _EP_SOURCE_DIR is an
  1380. # existing directory in our source tree, calling file(MAKE_DIRECTORY) on it
  1381. # would cause a fatal error, even though it would be a no-op.
  1382. if(NOT EXISTS "${_EP_SOURCE_DIR}")
  1383. file(MAKE_DIRECTORY "${_EP_SOURCE_DIR}")
  1384. endif()
  1385. file(MAKE_DIRECTORY
  1386. "${_EP_BINARY_DIR}"
  1387. "${_EP_STAMP_DIR}"
  1388. "${_EP_TMP_DIR}"
  1389. )
  1390. # We take over the stamp files and use our own for detecting whether each
  1391. # step is up-to-date. The method used by ExternalProject is specific to
  1392. # using a sub-build and is not appropriate for us here.
  1393. set(download_script ${_EP_TMP_DIR}/download.cmake)
  1394. set(update_script ${_EP_TMP_DIR}/upload.cmake)
  1395. set(patch_script ${_EP_TMP_DIR}/patch.cmake)
  1396. _ep_add_download_command(${contentName}
  1397. SCRIPT_FILE ${download_script}
  1398. DEPENDS_VARIABLE download_depends
  1399. )
  1400. _ep_add_update_command(${contentName}
  1401. SCRIPT_FILE ${update_script}
  1402. DEPENDS_VARIABLE update_depends
  1403. )
  1404. _ep_add_patch_command(${contentName}
  1405. SCRIPT_FILE ${patch_script}
  1406. # No additional dependencies for the patch step
  1407. )
  1408. set(download_stamp ${_EP_STAMP_DIR}/download.stamp)
  1409. set(update_stamp ${_EP_STAMP_DIR}/upload.stamp)
  1410. set(patch_stamp ${_EP_STAMP_DIR}/patch.stamp)
  1411. __FetchContent_doStepDirect(
  1412. SCRIPT_FILE ${download_script}
  1413. STAMP_FILE ${download_stamp}
  1414. DEPENDS ${download_depends}
  1415. )
  1416. __FetchContent_doStepDirect(
  1417. SCRIPT_FILE ${update_script}
  1418. STAMP_FILE ${update_stamp}
  1419. DEPENDS ${update_depends} ${download_stamp}
  1420. )
  1421. __FetchContent_doStepDirect(
  1422. SCRIPT_FILE ${patch_script}
  1423. STAMP_FILE ${patch_stamp}
  1424. DEPENDS ${update_stamp}
  1425. )
  1426. endfunction()
  1427. function(__FetchContent_doStepDirect)
  1428. set(noValueOptions )
  1429. set(singleValueOptions
  1430. SCRIPT_FILE
  1431. STAMP_FILE
  1432. )
  1433. set(multiValueOptions
  1434. DEPENDS
  1435. )
  1436. cmake_parse_arguments(PARSE_ARGV 0 arg
  1437. "${noValueOptions}" "${singleValueOptions}" "${multiValueOptions}"
  1438. )
  1439. if(NOT EXISTS ${arg_STAMP_FILE})
  1440. set(do_step YES)
  1441. else()
  1442. set(do_step NO)
  1443. foreach(dep_file IN LISTS arg_DEPENDS arg_SCRIPT_FILE)
  1444. if(NOT EXISTS "${arg_STAMP_FILE}" OR
  1445. NOT EXISTS "${dep_file}" OR
  1446. NOT "${arg_STAMP_FILE}" IS_NEWER_THAN "${dep_file}")
  1447. set(do_step YES)
  1448. break()
  1449. endif()
  1450. endforeach()
  1451. endif()
  1452. if(do_step)
  1453. include(${arg_SCRIPT_FILE})
  1454. file(TOUCH "${arg_STAMP_FILE}")
  1455. endif()
  1456. endfunction()
  1457. function(__FetchContent_populateSubbuild)
  1458. # All argument parsing is done in __FetchContent_doPopulate(), since it is
  1459. # common to both the subbuild and direct population strategies.
  1460. # Parsed arguments are in ARG_... variables.
  1461. # The unparsed arguments may contain spaces, so build up ARG_EXTRA
  1462. # in such a way that it correctly substitutes into the generated
  1463. # CMakeLists.txt file with each argument quoted.
  1464. unset(ARG_EXTRA)
  1465. foreach(arg IN LISTS ARG_UNPARSED_ARGUMENTS)
  1466. set(ARG_EXTRA "${ARG_EXTRA} \"${arg}\"")
  1467. endforeach()
  1468. if(ARG_DOWNLOAD_NO_EXTRACT)
  1469. set(ARG_EXTRA "${ARG_EXTRA} DOWNLOAD_NO_EXTRACT YES")
  1470. set(__FETCHCONTENT_COPY_FILE
  1471. "
  1472. ExternalProject_Get_Property(${contentName}-populate DOWNLOADED_FILE)
  1473. get_filename_component(dlFileName \"\${DOWNLOADED_FILE}\" NAME)
  1474. ExternalProject_Add_Step(${contentName}-populate copyfile
  1475. COMMAND \"${CMAKE_COMMAND}\" -E copy_if_different
  1476. \"<DOWNLOADED_FILE>\" \"${ARG_SOURCE_DIR}\"
  1477. DEPENDEES patch
  1478. DEPENDERS configure
  1479. BYPRODUCTS \"${ARG_SOURCE_DIR}/\${dlFileName}\"
  1480. COMMENT \"Copying file to SOURCE_DIR\"
  1481. )
  1482. ")
  1483. else()
  1484. unset(__FETCHCONTENT_COPY_FILE)
  1485. endif()
  1486. # Hide output if requested, but save it to a variable in case there's an
  1487. # error so we can show the output upon failure. When not quiet, don't
  1488. # capture the output to a variable because the user may want to see the
  1489. # output as it happens (e.g. progress during long downloads). Combine both
  1490. # stdout and stderr in the one capture variable so the output stays in order.
  1491. if (ARG_QUIET)
  1492. set(outputOptions
  1493. OUTPUT_VARIABLE capturedOutput
  1494. ERROR_VARIABLE capturedOutput
  1495. )
  1496. else()
  1497. set(capturedOutput)
  1498. set(outputOptions)
  1499. message(STATUS "Populating ${contentName}")
  1500. endif()
  1501. if(CMAKE_GENERATOR)
  1502. set(subCMakeOpts "-G${CMAKE_GENERATOR}")
  1503. if(CMAKE_GENERATOR_PLATFORM)
  1504. list(APPEND subCMakeOpts "-A${CMAKE_GENERATOR_PLATFORM}")
  1505. endif()
  1506. if(CMAKE_GENERATOR_TOOLSET)
  1507. list(APPEND subCMakeOpts "-T${CMAKE_GENERATOR_TOOLSET}")
  1508. endif()
  1509. if(CMAKE_GENERATOR_INSTANCE)
  1510. list(APPEND subCMakeOpts "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}")
  1511. endif()
  1512. if(CMAKE_MAKE_PROGRAM)
  1513. list(APPEND subCMakeOpts "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
  1514. endif()
  1515. # GreenHills needs to know about the compiler and toolset to run the
  1516. # subbuild commands. Be sure to update the similar section in
  1517. # ExternalProject.cmake:_ep_extract_configure_command()
  1518. if(CMAKE_GENERATOR MATCHES "Green Hills MULTI")
  1519. list(APPEND subCMakeOpts
  1520. "-DGHS_TARGET_PLATFORM:STRING=${GHS_TARGET_PLATFORM}"
  1521. "-DGHS_PRIMARY_TARGET:STRING=${GHS_PRIMARY_TARGET}"
  1522. "-DGHS_TOOLSET_ROOT:STRING=${GHS_TOOLSET_ROOT}"
  1523. "-DGHS_OS_ROOT:STRING=${GHS_OS_ROOT}"
  1524. "-DGHS_OS_DIR:STRING=${GHS_OS_DIR}"
  1525. "-DGHS_BSP_NAME:STRING=${GHS_BSP_NAME}"
  1526. )
  1527. endif()
  1528. # Override the sub-build's configuration types for multi-config generators.
  1529. # This ensures we are not affected by any custom setting from the project
  1530. # and can always request a known configuration further below.
  1531. get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  1532. if(is_multi_config)
  1533. list(APPEND subCMakeOpts "-DCMAKE_CONFIGURATION_TYPES:STRING=Debug")
  1534. endif()
  1535. else()
  1536. # Likely we've been invoked via CMake's script mode where no
  1537. # generator is set (and hence CMAKE_MAKE_PROGRAM could not be
  1538. # trusted even if provided). We will have to rely on being
  1539. # able to find the default generator and build tool.
  1540. unset(subCMakeOpts)
  1541. endif()
  1542. set(__FETCHCONTENT_CACHED_INFO "")
  1543. set(__passthrough_vars
  1544. CMAKE_EP_GIT_REMOTE_UPDATE_STRATEGY
  1545. CMAKE_TLS_VERSION
  1546. CMAKE_TLS_VERIFY
  1547. CMAKE_TLS_CAINFO
  1548. CMAKE_NETRC
  1549. CMAKE_NETRC_FILE
  1550. )
  1551. foreach(var IN LISTS __passthrough_vars)
  1552. if(DEFINED ${var})
  1553. # Embed directly in the generated CMakeLists.txt file to avoid making
  1554. # the cmake command line excessively long. It also makes debugging and
  1555. # testing easier.
  1556. string(APPEND __FETCHCONTENT_CACHED_INFO "set(${var} [==[${${var}}]==])\n")
  1557. endif()
  1558. endforeach()
  1559. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  1560. list(FIND ARG_UNPARSED_ARGUMENTS GIT_REPOSITORY indexResult)
  1561. if(indexResult GREATER_EQUAL 0)
  1562. find_package(Git QUIET)
  1563. string(APPEND __FETCHCONTENT_CACHED_INFO "
  1564. # Pass through things we've already detected in the main project to avoid
  1565. # paying the cost of redetecting them again in ExternalProject_Add()
  1566. set(GIT_EXECUTABLE [==[${GIT_EXECUTABLE}]==])
  1567. set(GIT_VERSION_STRING [==[${GIT_VERSION_STRING}]==])
  1568. set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  1569. [==[${GIT_EXECUTABLE};${GIT_VERSION_STRING}]==]
  1570. )
  1571. ")
  1572. endif()
  1573. # Create and build a separate CMake project to carry out the population.
  1574. # If we've already previously done these steps, they will not cause
  1575. # anything to be updated, so extra rebuilds of the project won't occur.
  1576. # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
  1577. # has this set to something not findable on the PATH. We also ensured above
  1578. # that the Debug config will be defined for multi-config generators.
  1579. configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent/CMakeLists.cmake.in"
  1580. "${ARG_SUBBUILD_DIR}/CMakeLists.txt"
  1581. @ONLY
  1582. )
  1583. execute_process(
  1584. COMMAND ${CMAKE_COMMAND} ${subCMakeOpts} .
  1585. RESULT_VARIABLE result
  1586. ${outputOptions}
  1587. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  1588. )
  1589. if(result)
  1590. if(capturedOutput)
  1591. message("${capturedOutput}")
  1592. endif()
  1593. message(FATAL_ERROR "CMake step for ${contentName} failed: ${result}")
  1594. endif()
  1595. execute_process(
  1596. COMMAND ${CMAKE_COMMAND} --build . --config Debug
  1597. RESULT_VARIABLE result
  1598. ${outputOptions}
  1599. WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
  1600. )
  1601. if(result)
  1602. if(capturedOutput)
  1603. message("${capturedOutput}")
  1604. endif()
  1605. message(FATAL_ERROR "Build step for ${contentName} failed: ${result}")
  1606. endif()
  1607. endfunction()
  1608. option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist")
  1609. option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population")
  1610. option(FETCHCONTENT_QUIET "Enables QUIET option for all content population" ON)
  1611. set(FETCHCONTENT_BASE_DIR "${CMAKE_BINARY_DIR}/_deps" CACHE PATH "Directory under which to collect all populated content")
  1612. # Populate the specified content using details stored from
  1613. # an earlier call to FetchContent_Declare().
  1614. function(FetchContent_Populate contentName)
  1615. if(NOT contentName)
  1616. message(FATAL_ERROR "Empty contentName not allowed for FetchContent_Populate()")
  1617. endif()
  1618. if(ARGC EQUAL 1)
  1619. cmake_policy(GET CMP0169 cmp0169
  1620. PARENT_SCOPE # undocumented, do not use outside of CMake
  1621. )
  1622. if(NOT cmp0169 STREQUAL "OLD")
  1623. string(CONCAT msg
  1624. "Calling FetchContent_Populate(${contentName}) is deprecated, call "
  1625. "FetchContent_MakeAvailable(${contentName}) instead. "
  1626. "Policy CMP0169 can be set to OLD to allow "
  1627. "FetchContent_Populate(${contentName}) to be called directly for now, "
  1628. "but the ability to call it with declared details will be removed "
  1629. "completely in a future version."
  1630. )
  1631. if(cmp0169 STREQUAL "NEW")
  1632. message(FATAL_ERROR "${msg}")
  1633. else()
  1634. message(AUTHOR_WARNING "${msg}")
  1635. endif()
  1636. endif()
  1637. set(__doDirectArgs)
  1638. else()
  1639. cmake_policy(GET CMP0168 cmp0168
  1640. PARENT_SCOPE # undocumented, do not use outside of CMake
  1641. )
  1642. if(cmp0168 STREQUAL "NEW")
  1643. set(__doDirectArgs __DIRECT_POPULATION YES)
  1644. else()
  1645. set(__doDirectArgs __DIRECT_POPULATION NO)
  1646. endif()
  1647. endif()
  1648. cmake_policy(GET CMP0170 cmp0170
  1649. PARENT_SCOPE # undocumented, do not use outside of CMake
  1650. )
  1651. cmake_parse_arguments(PARSE_ARGV 1 __arg "" "" "")
  1652. set(__argsQuoted "[==[${contentName}]==] [==[${cmp0170}]==]")
  1653. foreach(__item IN LISTS __arg_UNPARSED_ARGUMENTS __doDirectArgs)
  1654. string(APPEND __argsQuoted " [==[${__item}]==]")
  1655. endforeach()
  1656. cmake_language(EVAL CODE "__FetchContent_Populate(${__argsQuoted})")
  1657. string(TOLOWER ${contentName} contentNameLower)
  1658. foreach(var IN ITEMS SOURCE_DIR BINARY_DIR POPULATED)
  1659. set(var "${contentNameLower}_${var}")
  1660. if(DEFINED ${var})
  1661. set(${var} "${${var}}" PARENT_SCOPE)
  1662. endif()
  1663. endforeach()
  1664. endfunction()
  1665. function(__FetchContent_Populate contentName cmp0170)
  1666. string(TOLOWER ${contentName} contentNameLower)
  1667. if(ARGN)
  1668. # This is the direct population form with details fully specified
  1669. # as part of the call, so we already have everything we need
  1670. __FetchContent_doPopulation(
  1671. ${contentNameLower}
  1672. SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-subbuild"
  1673. SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-src"
  1674. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-build"
  1675. ${ARGN} # Could override any of the above ..._DIR variables
  1676. )
  1677. # Pass source and binary dir variables back to the caller
  1678. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  1679. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  1680. # Don't set global properties, or record that we did this population, since
  1681. # this was a direct call outside of the normal declared details form.
  1682. # We only want to save values in the global properties for content that
  1683. # honors the hierarchical details mechanism so that projects are not
  1684. # robbed of the ability to override details set in nested projects.
  1685. return()
  1686. endif()
  1687. # No details provided, so assume they were saved from an earlier call
  1688. # to FetchContent_Declare(). Do a check that we haven't already
  1689. # populated this content before in case the caller forgot to check.
  1690. FetchContent_GetProperties(${contentName})
  1691. if(${contentNameLower}_POPULATED)
  1692. if("${${contentNameLower}_SOURCE_DIR}" STREQUAL "")
  1693. message(FATAL_ERROR
  1694. "Content ${contentName} already populated by find_package() or a "
  1695. "dependency provider"
  1696. )
  1697. else()
  1698. message(FATAL_ERROR
  1699. "Content ${contentName} already populated in ${${contentNameLower}_SOURCE_DIR}"
  1700. )
  1701. endif()
  1702. endif()
  1703. __FetchContent_getSavedDetails(${contentName} contentDetails)
  1704. if("${contentDetails}" STREQUAL "")
  1705. message(FATAL_ERROR "No details have been set for content: ${contentName}")
  1706. endif()
  1707. string(TOUPPER ${contentName} contentNameUpper)
  1708. set(FETCHCONTENT_SOURCE_DIR_${contentNameUpper}
  1709. "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}"
  1710. CACHE PATH "When not empty, overrides where to find pre-populated content for ${contentName}")
  1711. if(FETCHCONTENT_SOURCE_DIR_${contentNameUpper})
  1712. # The source directory has been explicitly provided in the cache,
  1713. # so no population is required. The build directory may still be specified
  1714. # by the declared details though.
  1715. if(NOT IS_ABSOLUTE "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1716. # Don't check this directory because we don't know what location it is
  1717. # expected to be relative to. We can't make this a hard error for backward
  1718. # compatibility reasons.
  1719. message(WARNING "Relative source directory specified. This is not safe, "
  1720. "as it depends on the calling directory scope.\n"
  1721. " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1722. elseif(NOT EXISTS "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1723. message(FATAL_ERROR "Manually specified source directory is missing:\n"
  1724. " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1725. endif()
  1726. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
  1727. cmake_parse_arguments(savedDetails "" "BINARY_DIR" "" ${contentDetails})
  1728. if(savedDetails_BINARY_DIR)
  1729. set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
  1730. else()
  1731. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  1732. endif()
  1733. elseif(FETCHCONTENT_FULLY_DISCONNECTED)
  1734. # Bypass population and assume source is already there from a previous run.
  1735. # Declared details may override the default source or build directories.
  1736. cmake_parse_arguments(savedDetails "" "SOURCE_DIR;BINARY_DIR" "" ${contentDetails})
  1737. if(savedDetails_SOURCE_DIR)
  1738. set(${contentNameLower}_SOURCE_DIR ${savedDetails_SOURCE_DIR})
  1739. else()
  1740. set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
  1741. endif()
  1742. if(NOT IS_ABSOLUTE "${${contentNameLower}_SOURCE_DIR}")
  1743. message(WARNING
  1744. "Relative source directory specified. This is not safe, as it depends "
  1745. "on the calling directory scope.\n"
  1746. " ${${contentNameLower}_SOURCE_DIR}"
  1747. )
  1748. set(${contentNameLower}_SOURCE_DIR
  1749. "${CMAKE_CURRENT_BINARY_DIR}/${${contentNameLower}_SOURCE_DIR}"
  1750. )
  1751. endif()
  1752. if(NOT EXISTS "${${contentNameLower}_SOURCE_DIR}")
  1753. if(cmp0170 STREQUAL "")
  1754. set(cmp0170 WARN)
  1755. endif()
  1756. string(CONCAT msg
  1757. "FETCHCONTENT_FULLY_DISCONNECTED is set to true, which requires the "
  1758. "source directory for dependency ${contentName} to already be populated. "
  1759. "This generally means it must not be set to true the first time CMake "
  1760. "is run in a build directory. The following source directory should "
  1761. "already be populated, but it doesn't exist:\n"
  1762. " ${${contentNameLower}_SOURCE_DIR}\n"
  1763. "Policy CMP0170 controls enforcement of this requirement."
  1764. )
  1765. if(cmp0170 STREQUAL "NEW")
  1766. message(FATAL_ERROR "${msg}")
  1767. elseif(NOT cmp0170 STREQUAL "OLD")
  1768. # Note that this is a user warning, not a project author warning.
  1769. # The user has set FETCHCONTENT_FULLY_DISCONNECTED in a scenario
  1770. # where that is not allowed.
  1771. message(WARNING "${msg}")
  1772. endif()
  1773. endif()
  1774. if(savedDetails_BINARY_DIR)
  1775. set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
  1776. else()
  1777. set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
  1778. endif()
  1779. else()
  1780. # Support both a global "disconnect all updates" and a per-content
  1781. # update test (either one being set disables updates for this content).
  1782. option(FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper}
  1783. "Enables UPDATE_DISCONNECTED behavior just for population of ${contentName}")
  1784. if(FETCHCONTENT_UPDATES_DISCONNECTED OR
  1785. FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper})
  1786. set(disconnectUpdates True)
  1787. else()
  1788. set(disconnectUpdates False)
  1789. endif()
  1790. if(FETCHCONTENT_QUIET)
  1791. set(quietFlag QUIET)
  1792. else()
  1793. unset(quietFlag)
  1794. endif()
  1795. set(__detailsQuoted)
  1796. foreach(__item IN LISTS contentDetails)
  1797. if(NOT __item STREQUAL "OVERRIDE_FIND_PACKAGE")
  1798. string(APPEND __detailsQuoted " [==[${__item}]==]")
  1799. endif()
  1800. endforeach()
  1801. cmake_language(EVAL CODE "
  1802. __FetchContent_doPopulation(
  1803. ${contentNameLower}
  1804. ${quietFlag}
  1805. UPDATE_DISCONNECTED ${disconnectUpdates}
  1806. SUBBUILD_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-subbuild\"
  1807. SOURCE_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src\"
  1808. BINARY_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build\"
  1809. # Put the saved details last so they can override any of the
  1810. # the options we set above (this can include SOURCE_DIR or
  1811. # BUILD_DIR)
  1812. ${__detailsQuoted}
  1813. )"
  1814. )
  1815. endif()
  1816. FetchContent_SetPopulated(
  1817. ${contentName}
  1818. SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}"
  1819. BINARY_DIR "${${contentNameLower}_BINARY_DIR}"
  1820. )
  1821. # Pass variables back to the caller. The variables passed back here
  1822. # must match what FetchContent_GetProperties() sets when it is called
  1823. # with just the content name.
  1824. set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
  1825. set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
  1826. set(${contentNameLower}_POPULATED True PARENT_SCOPE)
  1827. endfunction()
  1828. function(__FetchContent_setupFindPackageRedirection contentName)
  1829. __FetchContent_getSavedDetails(${contentName} contentDetails)
  1830. string(TOLOWER ${contentName} contentNameLower)
  1831. get_property(wantFindPackage GLOBAL PROPERTY
  1832. _FetchContent_${contentNameLower}_find_package_args
  1833. DEFINED
  1834. )
  1835. # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
  1836. list(FIND contentDetails OVERRIDE_FIND_PACKAGE indexResult)
  1837. if(NOT wantFindPackage AND indexResult EQUAL -1)
  1838. # No find_package() redirection allowed
  1839. return()
  1840. endif()
  1841. # We write out dep-config.cmake and dep-config-version.cmake file name
  1842. # forms here because they are forced to lowercase. FetchContent
  1843. # dependency names are case-insensitive, but find_package() config files
  1844. # are only case-insensitive for the -config and -config-version forms,
  1845. # not the Config and ConfigVersion forms.
  1846. set(inFileDir ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent)
  1847. set(configFilePrefix1 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentName}Config")
  1848. set(configFilePrefix2 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentNameLower}-config")
  1849. if(NOT EXISTS "${configFilePrefix1}.cmake" AND
  1850. NOT EXISTS "${configFilePrefix2}.cmake")
  1851. configure_file(${inFileDir}/package-config.cmake.in
  1852. "${configFilePrefix2}.cmake" @ONLY
  1853. )
  1854. endif()
  1855. if(NOT EXISTS "${configFilePrefix1}Version.cmake" AND
  1856. NOT EXISTS "${configFilePrefix2}-version.cmake")
  1857. configure_file(${inFileDir}/package-config-version.cmake.in
  1858. "${configFilePrefix2}-version.cmake" @ONLY
  1859. )
  1860. endif()
  1861. # Now that we've created the redirected package config files, prevent
  1862. # find_package() from delegating to FetchContent and let it find these
  1863. # config files through its normal processing.
  1864. set(propertyName "${prefix}_override_find_package")
  1865. set(GLOBAL PROPERTY ${propertyName} FALSE)
  1866. set(${contentName}_DIR "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}"
  1867. CACHE INTERNAL "Redirected by FetchContent"
  1868. )
  1869. endfunction()
  1870. # Arguments are assumed to be the names of dependencies that have been
  1871. # declared previously and should be populated. It is not an error if
  1872. # any of them have already been populated (they will just be skipped in
  1873. # that case). The command is implemented as a macro so that the variables
  1874. # defined by the FetchContent_GetProperties() and FetchContent_Populate()
  1875. # calls will be available to the caller.
  1876. macro(FetchContent_MakeAvailable)
  1877. # We must append an item, even if the variable is unset, so prefix its value.
  1878. # We will strip that prefix when we pop the value at the end of the macro.
  1879. list(APPEND __cmake_fcCurrentVarsStack
  1880. "__fcprefix__${CMAKE_VERIFY_INTERFACE_HEADER_SETS}"
  1881. )
  1882. set(CMAKE_VERIFY_INTERFACE_HEADER_SETS FALSE)
  1883. get_property(__cmake_providerCommand GLOBAL PROPERTY
  1884. __FETCHCONTENT_MAKEAVAILABLE_SERIAL_PROVIDER
  1885. )
  1886. foreach(__cmake_contentName IN ITEMS ${ARGV})
  1887. string(TOLOWER ${__cmake_contentName} __cmake_contentNameLower)
  1888. # If user specified FETCHCONTENT_SOURCE_DIR_... for this dependency, that
  1889. # overrides everything else and we shouldn't try to use find_package() or
  1890. # a dependency provider.
  1891. string(TOUPPER ${__cmake_contentName} __cmake_contentNameUpper)
  1892. if("${FETCHCONTENT_SOURCE_DIR_${__cmake_contentNameUpper}}" STREQUAL "")
  1893. # Dependency provider gets first opportunity, but prevent infinite
  1894. # recursion if we are called again for the same thing
  1895. if(NOT "${__cmake_providerCommand}" STREQUAL "" AND
  1896. NOT DEFINED __cmake_fcProvider_${__cmake_contentNameLower})
  1897. message(VERBOSE
  1898. "Trying FETCHCONTENT_MAKEAVAILABLE_SERIAL dependency provider for "
  1899. "${__cmake_contentName}"
  1900. )
  1901. if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
  1902. list(APPEND __cmake_fcCurrentVarsStack "${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
  1903. else()
  1904. # This just needs to be something that can't be a real package name
  1905. list(APPEND __cmake_fcCurrentVarsStack "<<::VAR_NOT_SET::>>")
  1906. endif()
  1907. set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
  1908. # It's still valid if there are no saved details. The project may have
  1909. # been written to assume a dependency provider is always set and will
  1910. # provide dependencies without having any declared details for them.
  1911. __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
  1912. set(__cmake_providerArgs
  1913. "FETCHCONTENT_MAKEAVAILABLE_SERIAL"
  1914. "${__cmake_contentName}"
  1915. )
  1916. # Empty arguments must be preserved because of things like
  1917. # GIT_SUBMODULES (see CMP0097)
  1918. foreach(__cmake_item IN LISTS __cmake_contentDetails)
  1919. string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
  1920. endforeach()
  1921. # This property might be defined but empty. As long as it is defined,
  1922. # find_package() can be called.
  1923. get_property(__cmake_addfpargs GLOBAL PROPERTY
  1924. _FetchContent_${__cmake_contentNameLower}_find_package_args
  1925. DEFINED
  1926. )
  1927. if(__cmake_addfpargs)
  1928. get_property(__cmake_fpargs GLOBAL PROPERTY
  1929. _FetchContent_${__cmake_contentNameLower}_find_package_args
  1930. )
  1931. string(APPEND __cmake_providerArgs " FIND_PACKAGE_ARGS")
  1932. foreach(__cmake_item IN LISTS __cmake_fpargs)
  1933. string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
  1934. endforeach()
  1935. endif()
  1936. # Calling the provider could lead to FetchContent_MakeAvailable() being
  1937. # called for a nested dependency. That nested call may occur in the
  1938. # current variable scope. We have to save and restore the variables we
  1939. # need preserved.
  1940. list(APPEND __cmake_fcCurrentVarsStack
  1941. ${__cmake_contentName}
  1942. ${__cmake_contentNameLower}
  1943. )
  1944. set(__cmake_fcProvider_${__cmake_contentNameLower} YES)
  1945. # The provider needs to see policies from our caller, so we need a
  1946. # helper macro defined outside our policy block. We pass through a
  1947. # variable name rather than variable contents to avoid any potential
  1948. # problems with parsing macro arguments.
  1949. set(__cmake_fcCode "${__cmake_providerCommand}(${__cmake_providerArgs})")
  1950. __FetchContent_MakeAvailable_eval_code(__cmake_fcCode)
  1951. list(POP_BACK __cmake_fcCurrentVarsStack
  1952. __cmake_contentNameLower
  1953. __cmake_contentName
  1954. CMAKE_EXPORT_FIND_PACKAGE_NAME
  1955. )
  1956. if(CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "<<::VAR_NOT_SET::>>")
  1957. unset(CMAKE_EXPORT_FIND_PACKAGE_NAME)
  1958. endif()
  1959. unset(__cmake_fcCode)
  1960. unset(__cmake_fcProvider_${__cmake_contentNameLower})
  1961. unset(__cmake_providerArgs)
  1962. unset(__cmake_addfpargs)
  1963. unset(__cmake_fpargs)
  1964. unset(__cmake_item)
  1965. unset(__cmake_contentDetails)
  1966. FetchContent_GetProperties(${__cmake_contentName})
  1967. if(${__cmake_contentNameLower}_POPULATED)
  1968. continue()
  1969. endif()
  1970. endif()
  1971. # Check if we've been asked to try find_package() first, even if we
  1972. # have already populated this dependency. If we previously tried to
  1973. # use find_package() for this and it succeeded, those things might
  1974. # no longer be in scope, so we have to do it again.
  1975. get_property(__cmake_haveFpArgs GLOBAL PROPERTY
  1976. _FetchContent_${__cmake_contentNameLower}_find_package_args DEFINED
  1977. )
  1978. if(__cmake_haveFpArgs)
  1979. unset(__cmake_haveFpArgs)
  1980. message(VERBOSE "Trying find_package(${__cmake_contentName} ...) before FetchContent")
  1981. get_property(__cmake_fpArgs GLOBAL PROPERTY
  1982. _FetchContent_${__cmake_contentNameLower}_find_package_args
  1983. )
  1984. # This call could lead to FetchContent_MakeAvailable() being called for
  1985. # a nested dependency and it may occur in the current variable scope.
  1986. # We have to save/restore the variables we need to preserve.
  1987. list(APPEND __cmake_fcCurrentNameStack
  1988. ${__cmake_contentName}
  1989. ${__cmake_contentNameLower}
  1990. )
  1991. # We pass variable names rather than their contents so as to avoid any
  1992. # potential problems with macro argument parsing
  1993. __FetchContent_MakeAvailable_find_package(__cmake_contentName __cmake_fpArgs)
  1994. list(POP_BACK __cmake_fcCurrentNameStack
  1995. __cmake_contentNameLower
  1996. __cmake_contentName
  1997. )
  1998. unset(__cmake_fpArgs)
  1999. if(${__cmake_contentName}_FOUND)
  2000. FetchContent_SetPopulated(${__cmake_contentName})
  2001. FetchContent_GetProperties(${__cmake_contentName})
  2002. continue()
  2003. endif()
  2004. endif()
  2005. else()
  2006. unset(__cmake_haveFpArgs)
  2007. endif()
  2008. FetchContent_GetProperties(${__cmake_contentName})
  2009. if(NOT ${__cmake_contentNameLower}_POPULATED)
  2010. cmake_policy(GET CMP0170 __cmake_fc_cmp0170
  2011. PARENT_SCOPE # undocumented, do not use outside of CMake
  2012. )
  2013. __FetchContent_Populate(${__cmake_contentName} "${__cmake_fc_cmp0170}")
  2014. unset(__cmake_fc_cmp0170)
  2015. __FetchContent_setupFindPackageRedirection(${__cmake_contentName})
  2016. # Only try to call add_subdirectory() if the populated content
  2017. # can be treated that way. Protecting the call with the check
  2018. # allows this function to be used for projects that just want
  2019. # to ensure the content exists, such as to provide content at
  2020. # a known location. We check the saved details for an optional
  2021. # SOURCE_SUBDIR which can be used in the same way as its meaning
  2022. # for ExternalProject. It won't matter if it was passed through
  2023. # to the ExternalProject sub-build, since it would have been
  2024. # ignored there.
  2025. set(__cmake_srcdir "${${__cmake_contentNameLower}_SOURCE_DIR}")
  2026. __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
  2027. if("${__cmake_contentDetails}" STREQUAL "")
  2028. message(FATAL_ERROR "No details have been set for content: ${__cmake_contentName}")
  2029. endif()
  2030. cmake_parse_arguments(__cmake_arg "EXCLUDE_FROM_ALL;SYSTEM" "SOURCE_SUBDIR" "" ${__cmake_contentDetails})
  2031. if(NOT "${__cmake_arg_SOURCE_SUBDIR}" STREQUAL "")
  2032. string(APPEND __cmake_srcdir "/${__cmake_arg_SOURCE_SUBDIR}")
  2033. endif()
  2034. if(EXISTS ${__cmake_srcdir}/CMakeLists.txt)
  2035. if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
  2036. list(APPEND __cmake_fcCurrentVarsStack "${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
  2037. else()
  2038. # This just needs to be something that can't be a real package name
  2039. list(APPEND __cmake_fcCurrentVarsStack "<<::VAR_NOT_SET::>>")
  2040. endif()
  2041. set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
  2042. set(__cmake_add_subdirectory_args ${__cmake_srcdir} ${${__cmake_contentNameLower}_BINARY_DIR})
  2043. if(__cmake_arg_EXCLUDE_FROM_ALL)
  2044. list(APPEND __cmake_add_subdirectory_args EXCLUDE_FROM_ALL)
  2045. endif()
  2046. if(__cmake_arg_SYSTEM)
  2047. list(APPEND __cmake_add_subdirectory_args SYSTEM)
  2048. endif()
  2049. # We pass a variable name rather than its contents so as to avoid any
  2050. # potential problems with macro argument parsing. It's highly unlikely
  2051. # in this case, but still theoretically possible someone might try to
  2052. # use a directory name that looks like a CMake variable evaluation.
  2053. __FetchContent_MakeAvailable_add_subdirectory(__cmake_add_subdirectory_args)
  2054. list(POP_BACK __cmake_fcCurrentVarsStack CMAKE_EXPORT_FIND_PACKAGE_NAME)
  2055. if(CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "<<::VAR_NOT_SET::>>")
  2056. unset(CMAKE_EXPORT_FIND_PACKAGE_NAME)
  2057. endif()
  2058. endif()
  2059. unset(__cmake_srcdir)
  2060. unset(__cmake_contentDetails)
  2061. unset(__cmake_arg_EXCLUDE_FROM_ALL)
  2062. unset(__cmake_arg_SYSTEM)
  2063. unset(__cmake_arg_SOURCE_SUBDIR)
  2064. unset(__cmake_add_subdirectory_args)
  2065. endif()
  2066. endforeach()
  2067. # Prefix will be "__fcprefix__"
  2068. list(POP_BACK __cmake_fcCurrentVarsStack __cmake_original_verify_setting)
  2069. string(SUBSTRING "${__cmake_original_verify_setting}"
  2070. 12 -1 __cmake_original_verify_setting
  2071. )
  2072. set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${__cmake_original_verify_setting})
  2073. # clear local variables to prevent leaking into the caller's scope
  2074. unset(__cmake_contentName)
  2075. unset(__cmake_contentNameLower)
  2076. unset(__cmake_contentNameUpper)
  2077. unset(__cmake_providerCommand)
  2078. unset(__cmake_original_verify_setting)
  2079. endmacro()
  2080. endblock() # End of FetchContent module's policy scope
  2081. # These are factored out here outside our policies block to preserve policy
  2082. # settings of the scope from which FetchContent was included. Any project or
  2083. # user code that actually relies on this is fragile and should enforce its own
  2084. # policies instead, but we keep these here to preserve backward compatibility.
  2085. macro(__FetchContent_MakeAvailable_eval_code code_var)
  2086. cmake_language(EVAL CODE "${${code_var}}")
  2087. endmacro()
  2088. macro(__FetchContent_MakeAvailable_find_package first_arg_var remaining_args_var)
  2089. find_package(${${first_arg_var}} ${${remaining_args_var}})
  2090. endmacro()
  2091. macro(__FetchContent_MakeAvailable_add_subdirectory args_var)
  2092. add_subdirectory(${${args_var}})
  2093. endmacro()