FetchContent.cmake 101 KB

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