FetchContent.cmake 102 KB

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