1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
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.
23 This module enables populating content at configure time via any method
24 supported by the :module:`ExternalProject` module. Whereas
25 :command:`ExternalProject_Add` downloads at build time, the
26 ``FetchContent`` module makes content available immediately, allowing the
27 configure step to use the content in commands like :command:`add_subdirectory`,
28 :command:`include` or :command:`file` operations.
30 Content population details should be defined separately from the command that
31 performs the actual population. This separation ensures that all the
32 dependency details are defined before anything might try to use them to
33 populate content. This is particularly important in more complex project
34 hierarchies where dependencies may be shared between multiple projects.
36 The following shows a typical example of declaring content details for some
37 dependencies and then ensuring they are populated with a separate call:
43 GIT_REPOSITORY https://github.com/google/googletest.git
44 GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
48 URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
49 URL_HASH MD5=5588a7b18261c20068beabfb4f530b87
52 FetchContent_MakeAvailable(googletest myCompanyIcons)
54 The :command:`FetchContent_MakeAvailable` command ensures the named
55 dependencies have been populated, either by an earlier call or by populating
56 them itself. When performing the population, it will also add them to the
57 main build, if possible, so that the main build can use the populated
58 projects' targets, etc. See the command's documentation for how these steps
61 When using a hierarchical project arrangement, projects at higher levels in
62 the hierarchy are able to override the declared details of content specified
63 anywhere lower in the project hierarchy. The first details to be declared
64 for a given dependency take precedence, regardless of where in the project
65 hierarchy that occurs. Similarly, the first call that tries to populate a
66 dependency "wins", with subsequent populations reusing the result of the
67 first instead of repeating the population again.
68 See the :ref:`Examples <fetch-content-examples>` which demonstrate
71 In some cases, the main project may need to have more precise control over
72 the population, or it may be required to explicitly define the population
73 steps in a way that cannot be captured by the declared details alone.
74 For such situations, the lower level :command:`FetchContent_GetProperties` and
75 :command:`FetchContent_Populate` commands can be used. These lack the richer
76 features provided by :command:`FetchContent_MakeAvailable` though, so their
77 direct use should be considered a last resort. The typical pattern of such
78 custom steps looks like this:
82 # NOTE: Where possible, prefer to use FetchContent_MakeAvailable()
83 # instead of custom logic like this
85 # Check if population has already been performed
86 FetchContent_GetProperties(depname)
87 if(NOT depname_POPULATED)
88 # Fetch the content using previously declared details
89 FetchContent_Populate(depname)
91 # Set custom variables, policies, etc.
94 # Bring the populated content into the build
95 add_subdirectory(${depname_SOURCE_DIR} ${depname_BINARY_DIR})
98 The ``FetchContent`` module also supports defining and populating
99 content in a single call, with no check for whether the content has been
100 populated elsewhere already. This should not be done in projects, but may
101 be appropriate for populating content in CMake's script mode.
102 See :command:`FetchContent_Populate` for details.
107 .. command:: FetchContent_Declare
109 .. code-block:: cmake
111 FetchContent_Declare(
116 [OVERRIDE_FIND_PACKAGE |
117 FIND_PACKAGE_ARGS args...]
120 The ``FetchContent_Declare()`` function records the options that describe
121 how to populate the specified content. If such details have already
122 been recorded earlier in this project (regardless of where in the project
123 hierarchy), this and all later calls for the same content ``<name>`` are
124 ignored. This "first to record, wins" approach is what allows hierarchical
125 projects to have parent projects override content details of child projects.
127 The content ``<name>`` can be any string without spaces, but good practice
128 would be to use only letters, numbers and underscores. The name will be
129 treated case-insensitively and it should be obvious for the content it
130 represents, often being the name of the child project or the value given
131 to its top level :command:`project` command (if it is a CMake project).
132 For well-known public projects, the name should generally be the official
133 name of the project. Choosing an unusual name makes it unlikely that other
134 projects needing that same content will use the same name, leading to
135 the content being populated multiple times.
137 The ``<contentOptions>`` can be any of the download, update or patch options
138 that the :command:`ExternalProject_Add` command understands. The configure,
139 build, install and test steps are explicitly disabled and therefore options
140 related to them will be ignored. The ``SOURCE_SUBDIR`` option is an
141 exception, see :command:`FetchContent_MakeAvailable` for details on how that
144 In most cases, ``<contentOptions>`` will just be a couple of options defining
145 the download method and method-specific details like a commit tag or archive
148 .. code-block:: cmake
150 FetchContent_Declare(
152 GIT_REPOSITORY https://github.com/google/googletest.git
153 GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
156 FetchContent_Declare(
158 URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz
159 URL_HASH MD5=5588a7b18261c20068beabfb4f530b87
162 FetchContent_Declare(
163 myCompanyCertificates
164 SVN_REPOSITORY svn+ssh://svn.mycompany.com/srv/svn/trunk/certs
168 Where contents are being fetched from a remote location and you do not
169 control that server, it is advisable to use a hash for ``GIT_TAG`` rather
170 than a branch or tag name. A commit hash is more secure and helps to
171 confirm that the downloaded contents are what you expected.
173 .. versionchanged:: 3.14
174 Commands for the download, update or patch steps can access the terminal.
175 This may be needed for things like password prompts or real-time display
178 .. versionadded:: 3.22
179 The :variable:`CMAKE_TLS_VERIFY`, :variable:`CMAKE_TLS_CAINFO`,
180 :variable:`CMAKE_NETRC` and :variable:`CMAKE_NETRC_FILE` variables now
181 provide the defaults for their corresponding content options, just like
182 they do for :command:`ExternalProject_Add`. Previously, these variables
183 were ignored by the ``FetchContent`` module.
185 .. versionadded:: 3.24
187 ``FIND_PACKAGE_ARGS``
188 This option is for scenarios where the
189 :command:`FetchContent_MakeAvailable` command may first try a call to
190 :command:`find_package` to satisfy the dependency for ``<name>``.
191 By default, such a call would be simply ``find_package(<name>)``, but
192 ``FIND_PACKAGE_ARGS`` can be used to provide additional arguments to be
193 appended after the ``<name>``. ``FIND_PACKAGE_ARGS`` can also be given
194 with nothing after it, which indicates that :command:`find_package` can
195 still be called if :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` is
196 set to ``OPT_IN`` or is not set.
198 It would not normally be appropriate to specify ``REQUIRED`` as one of
199 the additional arguments after ``FIND_PACKAGE_ARGS``. Doing so would
200 mean the :command:`find_package` call must succeed, so none of the other
201 details specified in the ``FetchContent_Declare()`` call would get a
202 chance to be used as a fall-back.
204 Everything after the ``FIND_PACKAGE_ARGS`` keyword is appended to the
205 :command:`find_package` call, so all other ``<contentOptions>`` must
206 come before the ``FIND_PACKAGE_ARGS`` keyword. If the
207 :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to true
208 at the time ``FetchContent_Declare()`` is called, a ``GLOBAL`` keyword
209 will be appended to the :command:`find_package` arguments if it was
210 not already specified. It will also be appended if
211 ``FIND_PACKAGE_ARGS`` was not given, but
212 :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``ALWAYS``.
214 ``OVERRIDE_FIND_PACKAGE`` cannot be used when ``FIND_PACKAGE_ARGS`` is
217 :ref:`dependency_providers` discusses another way that
218 :command:`FetchContent_MakeAvailable` calls can be redirected.
219 ``FIND_PACKAGE_ARGS`` is intended for project control, whereas
220 dependency providers allow users to override project behavior.
222 ``OVERRIDE_FIND_PACKAGE``
223 When a ``FetchContent_Declare(<name> ...)`` call includes this option,
224 subsequent calls to ``find_package(<name> ...)`` will ensure that
225 ``FetchContent_MakeAvailable(<name>)`` has been called, then use the
226 config package files in the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
227 directory (which are usually created by ``FetchContent_MakeAvailable()``).
228 This effectively makes :command:`FetchContent_MakeAvailable` override
229 :command:`find_package` for the named dependency, allowing the former to
230 satisfy the package requirements of the latter. ``FIND_PACKAGE_ARGS``
231 cannot be used when ``OVERRIDE_FIND_PACKAGE`` is given.
233 If a :ref:`dependency provider <dependency_providers>` has been set
234 and the project calls :command:`find_package` for the ``<name>``
235 dependency, ``OVERRIDE_FIND_PACKAGE`` will not prevent the provider
236 from seeing that call. Dependency providers always have the opportunity
237 to intercept any direct call to :command:`find_package`, except if that
238 call contains the ``BYPASS_PROVIDER`` option.
240 .. versionadded:: 3.25
243 If the ``SYSTEM`` argument is provided, the :prop_dir:`SYSTEM` directory
244 property of a subdirectory added by
245 :command:`FetchContent_MakeAvailable` will be set to true. This will
246 affect non-imported targets created as part of that command.
247 See the :prop_tgt:`SYSTEM` target property documentation for a more
248 detailed discussion of the effects.
250 .. versionadded:: 3.28
253 If the ``EXCLUDE_FROM_ALL`` argument is provided, then targets in the
254 subdirectory added by :command:`FetchContent_MakeAvailable` will not be
255 included in the ``ALL`` target by default, and may be excluded from IDE
256 project files. See the :command:`add_subdirectory` ``EXCLUDE_FROM_ALL``
257 argument documentation for a more detailed discussion of the effects.
259 .. command:: FetchContent_MakeAvailable
261 .. versionadded:: 3.14
263 .. code-block:: cmake
265 FetchContent_MakeAvailable(<name1> [<name2>...])
267 This command ensures that each of the named dependencies are made available
268 to the project by the time it returns. There must have been a call to
269 :command:`FetchContent_Declare` for each dependency, and the first such call
270 will control how that dependency will be made available, as described below.
272 If ``<lowercaseName>_SOURCE_DIR`` is not set:
274 * .. versionadded:: 3.24
276 If a :ref:`dependency provider <dependency_providers>` is set, call the
277 provider's command with ``FETCHCONTENT_MAKEAVAILABLE_SERIAL`` as the
278 first argument, followed by the arguments of the first call to
279 :command:`FetchContent_Declare` for ``<name>``. If ``SOURCE_DIR`` or
280 ``BINARY_DIR`` were not part of the original declared arguments, they
281 will be added with their default values.
282 If :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``NEVER``
283 when the details were declared, any ``FIND_PACKAGE_ARGS`` will be
284 omitted. The ``OVERRIDE_FIND_PACKAGE`` keyword is also always omitted.
285 If the provider fulfilled the request, ``FetchContent_MakeAvailable()``
286 will consider that dependency handled, skip the remaining steps below
287 and move on to the next dependency in the list.
289 * .. versionadded:: 3.24
291 If permitted, :command:`find_package(<name> [<args>...]) <find_package>`
292 will be called, where ``<args>...`` may be provided by the
293 ``FIND_PACKAGE_ARGS`` option in :command:`FetchContent_Declare`.
294 The value of the :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable
295 at the time :command:`FetchContent_Declare` was called determines whether
296 ``FetchContent_MakeAvailable()`` can call :command:`find_package`.
297 If the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to
298 true when ``FetchContent_MakeAvailable()`` is called, it still affects
299 any imported targets created when that in turn calls
300 :command:`find_package`, even if that variable was false when the
301 corresponding details were declared.
303 If the dependency was not satisfied by a provider or a
304 :command:`find_package` call, ``FetchContent_MakeAvailable()`` then uses
305 the following logic to make the dependency available:
307 * If the dependency has already been populated earlier in this run, set
308 the ``<lowercaseName>_POPULATED``, ``<lowercaseName>_SOURCE_DIR`` and
309 ``<lowercaseName>_BINARY_DIR`` variables in the same way as a call to
310 :command:`FetchContent_GetProperties`, then skip the remaining steps
311 below and move on to the next dependency in the list.
313 * Call :command:`FetchContent_Populate` to populate the dependency using
314 the details recorded by an earlier call to :command:`FetchContent_Declare`.
315 Halt with a fatal error if no such details have been recorded.
316 :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>` can be used to override
317 the declared details and use content provided at the specified location
320 * .. versionadded:: 3.24
322 Ensure the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` directory
323 contains a ``<lowercaseName>-config.cmake`` and a
324 ``<lowercaseName>-config-version.cmake`` file (or equivalently
325 ``<name>Config.cmake`` and ``<name>ConfigVersion.cmake``).
326 The directory that the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR`
327 variable points to is cleared at the start of every CMake run.
328 If no config file exists when :command:`FetchContent_Populate` returns,
329 a minimal one will be written which :command:`includes <include>` any
330 ``<lowercaseName>-extra.cmake`` or ``<name>Extra.cmake`` file with the
331 ``OPTIONAL`` flag (so the files can be missing and won't generate a
332 warning). Similarly, if no config version file exists, a very simple
333 one will be written which sets ``PACKAGE_VERSION_COMPATIBLE`` and
334 ``PACKAGE_VERSION_EXACT`` to true. This ensures all future calls to
335 :command:`find_package()` for the dependency will use the redirected
336 config file, regardless of any version requirements.
337 CMake cannot automatically determine an arbitrary dependency's version,
338 so it cannot set ``PACKAGE_VERSION``.
339 When a dependency is pulled in via :command:`add_subdirectory` in the
340 next step, it may choose to overwrite the generated config version file
341 in :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` with one that also sets
343 The dependency may also write a ``<lowercaseName>-extra.cmake`` or
344 ``<name>Extra.cmake`` file to perform custom processing or define any
345 variables that their normal (installed) package config file would
346 otherwise usually define (many projects don't do any custom processing
347 or set any variables and therefore have no need to do this).
348 If required, the main project can write these files instead if the
349 dependency project doesn't do so. This allows the main project to
350 add missing details from older dependencies that haven't or can't be
351 updated to support this functionality.
352 See `Integrating With find_package()`_ for examples.
354 * If the top directory of the populated content contains a ``CMakeLists.txt``
355 file, call :command:`add_subdirectory` to add it to the main build.
356 It is not an error for there to be no ``CMakeLists.txt`` file, which
357 allows the command to be used for dependencies that make downloaded
358 content available at a known location, but which do not need or support
359 being added directly to the build.
361 .. versionadded:: 3.18
362 The ``SOURCE_SUBDIR`` option can be given in the declared details to
363 look somewhere below the top directory instead (i.e. the same way that
364 ``SOURCE_SUBDIR`` is used by the :command:`ExternalProject_Add`
365 command). The path provided with ``SOURCE_SUBDIR`` must be relative
366 and will be treated as relative to the top directory. It can also
367 point to a directory that does not contain a ``CMakeLists.txt`` file
368 or even to a directory that doesn't exist. This can be used to avoid
369 adding a project that contains a ``CMakeLists.txt`` file in its top
372 .. versionadded:: 3.25
373 If the ``SYSTEM`` keyword was included in the call to
374 :command:`FetchContent_Declare`, the ``SYSTEM`` keyword will be
375 added to the :command:`add_subdirectory` command as well.
377 .. versionadded:: 3.28
378 If the ``EXCLUDE_FROM_ALL`` keyword was included in the call to
379 :command:`FetchContent_Declare`, the ``EXCLUDE_FROM_ALL`` keyword will
380 be added to the :command:`add_subdirectory` command as well.
382 .. versionadded:: 3.29
383 :variable:`CMAKE_EXPORT_FIND_PACKAGE_NAME` is set to the dependency name
384 before calling :command:`add_subdirectory`.
386 Projects should aim to declare the details of all dependencies they might
387 use before they call ``FetchContent_MakeAvailable()`` for any of them.
388 This ensures that if any of the dependencies are also sub-dependencies of
389 one or more of the others, the main project still controls the details
390 that will be used (because it will declare them first before the
391 dependencies get a chance to). In the following code samples, assume that
392 the ``uses_other`` dependency also uses ``FetchContent`` to add the ``other``
393 dependency internally:
395 .. code-block:: cmake
397 # WRONG: Should declare all details first
398 FetchContent_Declare(uses_other ...)
399 FetchContent_MakeAvailable(uses_other)
401 FetchContent_Declare(other ...) # Will be ignored, uses_other beat us to it
402 FetchContent_MakeAvailable(other) # Would use details declared by uses_other
404 .. code-block:: cmake
406 # CORRECT: All details declared first, so they will take priority
407 FetchContent_Declare(uses_other ...)
408 FetchContent_Declare(other ...)
409 FetchContent_MakeAvailable(uses_other other)
411 Note that :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` is explicitly set
412 to false upon entry to ``FetchContent_MakeAvailable()``, and is restored to
413 its original value before the command returns. Developers typically only
414 want to verify header sets from the main project, not those from any
415 dependencies. This local manipulation of the
416 :variable:`CMAKE_VERIFY_INTERFACE_HEADER_SETS` variable provides that
417 intuitive behavior. You can use variables like
418 :variable:`CMAKE_PROJECT_INCLUDE` or
419 :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE` to turn verification back
420 on for all or some dependencies. You can also set the
421 :prop_tgt:`VERIFY_INTERFACE_HEADER_SETS` property of individual targets.
423 .. command:: FetchContent_Populate
426 Where possible, prefer to use :command:`FetchContent_MakeAvailable`
427 instead of implementing population manually with this command.
429 .. code-block:: cmake
431 FetchContent_Populate(<name>)
433 In most cases, the only argument given to ``FetchContent_Populate()`` is the
434 ``<name>``. When used this way, the command assumes the content details have
435 been recorded by an earlier call to :command:`FetchContent_Declare`. The
436 details are stored in a global property, so they are unaffected by things
437 like variable or directory scope. Therefore, it doesn't matter where in the
438 project the details were previously declared, as long as they have been
439 declared before the call to ``FetchContent_Populate()``. Those saved details
440 are then used to construct a call to :command:`ExternalProject_Add` in a
441 private sub-build to perform the content population immediately. The
442 implementation of ``ExternalProject_Add()`` ensures that if the content has
443 already been populated in a previous CMake run, that content will be reused
444 rather than repopulating them again. For the common case where population
445 involves downloading content, the cost of the download is only paid once.
447 An internal global property records when a particular content population
448 request has been processed. If ``FetchContent_Populate()`` is called more
449 than once for the same content name within a configure run, the second call
450 will halt with an error. Projects can and should check whether content
451 population has already been processed with the
452 :command:`FetchContent_GetProperties` command before calling
453 ``FetchContent_Populate()``.
455 ``FetchContent_Populate()`` will set three variables in the scope of the
458 ``<lowercaseName>_POPULATED``
459 This will always be set to ``TRUE`` by the call.
461 ``<lowercaseName>_SOURCE_DIR``
462 The location where the populated content can be found upon return.
464 ``<lowercaseName>_BINARY_DIR``
465 A directory intended for use as a corresponding build directory.
467 The main use case for the ``<lowercaseName>_SOURCE_DIR`` and
468 ``<lowercaseName>_BINARY_DIR`` variables is to call
469 :command:`add_subdirectory` immediately after population:
471 .. code-block:: cmake
473 FetchContent_Populate(FooBar)
474 add_subdirectory(${foobar_SOURCE_DIR} ${foobar_BINARY_DIR})
476 The values of the three variables can also be retrieved from anywhere in the
477 project hierarchy using the :command:`FetchContent_GetProperties` command.
479 The ``FetchContent_Populate()`` command also supports a syntax allowing the
480 content details to be specified directly rather than using any saved
481 details. This is more low-level and use of this form is generally to be
482 avoided in favor of using saved content details as outlined above.
483 Nevertheless, in certain situations it can be useful to invoke the content
484 population as an isolated operation (typically as part of implementing some
485 other higher level feature or when using CMake in script mode):
487 .. code-block:: cmake
489 FetchContent_Populate(
492 [SUBBUILD_DIR <subBuildDir>]
493 [SOURCE_DIR <srcDir>]
494 [BINARY_DIR <binDir>]
498 This form has a number of key differences to that where only ``<name>`` is
501 - All required population details are assumed to have been provided directly
502 in the call to ``FetchContent_Populate()``. Any saved details for
503 ``<name>`` are ignored.
504 - No check is made for whether content for ``<name>`` has already been
506 - No global property is set to record that the population has occurred.
507 - No global properties record the source or binary directories used for the
509 - The ``FETCHCONTENT_FULLY_DISCONNECTED`` and
510 ``FETCHCONTENT_UPDATES_DISCONNECTED`` cache variables are ignored.
512 The ``<lowercaseName>_SOURCE_DIR`` and ``<lowercaseName>_BINARY_DIR``
513 variables are still returned to the caller, but since these locations are
514 not stored as global properties when this form is used, they are only
515 available to the calling scope and below rather than the entire project
516 hierarchy. No ``<lowercaseName>_POPULATED`` variable is set in the caller's
517 scope with this form.
519 The supported options for ``FetchContent_Populate()`` are the same as those
520 for :command:`FetchContent_Declare()`. Those few options shown just
521 above are either specific to ``FetchContent_Populate()`` or their behavior is
522 slightly modified from how :command:`ExternalProject_Add` treats them:
525 The ``QUIET`` option can be given to hide the output associated with
526 populating the specified content. If the population fails, the output will
527 be shown regardless of whether this option was given or not so that the
528 cause of the failure can be diagnosed. The global ``FETCHCONTENT_QUIET``
529 cache variable has no effect on ``FetchContent_Populate()`` calls where the
530 content details are provided directly.
533 The ``SUBBUILD_DIR`` argument can be provided to change the location of the
534 sub-build created to perform the population. The default value is
535 ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-subbuild`` and it would be
536 unusual to need to override this default. If a relative path is specified,
537 it will be interpreted as relative to :variable:`CMAKE_CURRENT_BINARY_DIR`.
538 This option should not be confused with the ``SOURCE_SUBDIR`` option which
539 only affects the :command:`FetchContent_MakeAvailable` command.
541 ``SOURCE_DIR``, ``BINARY_DIR``
542 The ``SOURCE_DIR`` and ``BINARY_DIR`` arguments are supported by
543 :command:`ExternalProject_Add`, but different default values are used by
544 ``FetchContent_Populate()``. ``SOURCE_DIR`` defaults to
545 ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-src`` and ``BINARY_DIR``
546 defaults to ``${CMAKE_CURRENT_BINARY_DIR}/<lowercaseName>-build``.
547 If a relative path is specified, it will be interpreted as relative to
548 :variable:`CMAKE_CURRENT_BINARY_DIR`.
550 In addition to the above explicit options, any other unrecognized options are
551 passed through unmodified to :command:`ExternalProject_Add` to perform the
552 download, patch and update steps. The following options are explicitly
553 prohibited (they are disabled by the ``FetchContent_Populate()`` command):
555 - ``CONFIGURE_COMMAND``
557 - ``INSTALL_COMMAND``
560 If using ``FetchContent_Populate()`` within CMake's script mode, be aware
561 that the implementation sets up a sub-build which therefore requires a CMake
562 generator and build tool to be available. If these cannot be found by
563 default, then the :variable:`CMAKE_GENERATOR` and/or
564 :variable:`CMAKE_MAKE_PROGRAM` variables will need to be set appropriately
565 on the command line invoking the script.
567 .. versionadded:: 3.18
568 Added support for the ``DOWNLOAD_NO_EXTRACT`` option.
570 .. command:: FetchContent_GetProperties
572 When using saved content details, a call to
573 :command:`FetchContent_MakeAvailable` or :command:`FetchContent_Populate`
574 records information in global properties which can be queried at any time.
575 This information may include the source and binary directories associated with
576 the content and also whether or not the content population has been processed
577 during the current configure run.
579 .. code-block:: cmake
581 FetchContent_GetProperties(
583 [SOURCE_DIR <srcDirVar>]
584 [BINARY_DIR <binDirVar>]
585 [POPULATED <doneVar>]
588 The ``SOURCE_DIR``, ``BINARY_DIR`` and ``POPULATED`` options can be used to
589 specify which properties should be retrieved. Each option accepts a value
590 which is the name of the variable in which to store that property. Most of
591 the time though, only ``<name>`` is given, in which case the call will then
592 set the same variables as a call to
593 :command:`FetchContent_MakeAvailable(name) <FetchContent_MakeAvailable>` or
594 :command:`FetchContent_Populate(name) <FetchContent_Populate>`.
595 Note that the ``SOURCE_DIR`` and ``BINARY_DIR`` values can be empty if the
596 call is fulfilled by a :ref:`dependency provider <dependency_providers>`.
598 This command is rarely needed when using
599 :command:`FetchContent_MakeAvailable`. It is more commonly used as part of
600 implementing the following pattern with :command:`FetchContent_Populate`,
601 which ensures that the relevant variables will always be defined regardless
602 of whether or not the population has been performed elsewhere in the project
605 .. code-block:: cmake
607 # Check if population has already been performed
608 FetchContent_GetProperties(depname)
609 if(NOT depname_POPULATED)
610 # Fetch the content using previously declared details
611 FetchContent_Populate(depname)
613 # Set custom variables, policies, etc.
616 # Bring the populated content into the build
617 add_subdirectory(${depname_SOURCE_DIR} ${depname_BINARY_DIR})
620 .. command:: FetchContent_SetPopulated
622 .. versionadded:: 3.24
625 This command should only be called by
626 :ref:`dependency providers <dependency_providers>`. Calling it in any
627 other context is unsupported and future CMake versions may halt with a
628 fatal error in such cases.
630 .. code-block:: cmake
632 FetchContent_SetPopulated(
634 [SOURCE_DIR <srcDir>]
635 [BINARY_DIR <binDir>]
638 If a provider command fulfills a ``FETCHCONTENT_MAKEAVAILABLE_SERIAL``
639 request, it must call this function before returning. The ``SOURCE_DIR``
640 and ``BINARY_DIR`` arguments can be used to specify the values that
641 :command:`FetchContent_GetProperties` should return for its corresponding
642 arguments. Only provide ``SOURCE_DIR`` and ``BINARY_DIR`` if they have
643 the same meaning as if they had been populated by the built-in
644 :command:`FetchContent_MakeAvailable` implementation.
650 A number of cache variables can influence the behavior where details from a
651 :command:`FetchContent_Declare` call are used to populate content.
654 All of these variables are intended for the developer to customize behavior.
655 They should not normally be set by the project.
657 .. variable:: FETCHCONTENT_BASE_DIR
659 In most cases, the saved details do not specify any options relating to the
660 directories to use for the internal sub-build, final source and build areas.
661 It is generally best to leave these decisions up to the ``FetchContent``
662 module to handle on the project's behalf. The ``FETCHCONTENT_BASE_DIR``
663 cache variable controls the point under which all content population
664 directories are collected, but in most cases, developers would not need to
665 change this. The default location is ``${CMAKE_BINARY_DIR}/_deps``, but if
666 developers change this value, they should aim to keep the path short and
667 just below the top level of the build tree to avoid running into path
668 length problems on Windows.
670 .. variable:: FETCHCONTENT_QUIET
672 The logging output during population can be quite verbose, making the
673 configure stage quite noisy. This cache option (``ON`` by default) hides
674 all population output unless an error is encountered. If experiencing
675 problems with hung downloads, temporarily switching this option off may
676 help diagnose which content population is causing the issue.
678 .. variable:: FETCHCONTENT_FULLY_DISCONNECTED
680 When this option is enabled, no attempt is made to download or update
681 any content. It is assumed that all content has already been populated in
682 a previous run or the source directories have been pointed at existing
683 contents the developer has provided manually (using options described
684 further below). When the developer knows that no changes have been made to
685 any content details, turning this option ``ON`` can significantly speed up
686 the configure stage. It is ``OFF`` by default.
690 The ``FETCHCONTENT_FULLY_DISCONNECTED`` variable is not an appropriate way
691 to prevent any network access on the first run in a build directory.
692 Doing so can break projects, lead to misleading error messages, and hide
693 subtle population failures. This variable is specifically intended to
694 only be turned on *after* the first time CMake has been run.
695 If you want to prevent network access even on the first run, use a
696 :ref:`dependency provider <dependency_providers>` and populate the
697 dependency from local content instead.
699 .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED
701 This is a less severe download/update control compared to
702 :variable:`FETCHCONTENT_FULLY_DISCONNECTED`. Instead of bypassing all
703 download and update logic, ``FETCHCONTENT_UPDATES_DISCONNECTED`` only
704 prevents the update step from making connections to remote servers
705 when using the git or hg download methods. Updates still occur if details
706 about the update step change, but the update is attempted with only the
707 information already available locally (so switching to a different tag or
708 commit that is already fetched locally will succeed, but switching to an
709 unknown commit hash will fail). The download step is not affected, so if
710 content has not been downloaded previously, it will still be downloaded
711 when this option is enabled. This can speed up the configure step, but
712 not as much as :variable:`FETCHCONTENT_FULLY_DISCONNECTED`.
713 ``FETCHCONTENT_UPDATES_DISCONNECTED`` is ``OFF`` by default.
715 .. variable:: FETCHCONTENT_TRY_FIND_PACKAGE_MODE
717 .. versionadded:: 3.24
719 This variable modifies the details that :command:`FetchContent_Declare`
720 records for a given dependency. While it ultimately controls the behavior
721 of :command:`FetchContent_MakeAvailable`, it is the variable's value when
722 :command:`FetchContent_Declare` is called that gets used. It makes no
723 difference what the variable is set to when
724 :command:`FetchContent_MakeAvailable` is called. Since the variable should
725 only be set by the user and not by projects directly, it will typically have
726 the same value throughout anyway, so this distinction is not usually
729 ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` ultimately controls whether
730 :command:`FetchContent_MakeAvailable` is allowed to call
731 :command:`find_package` to satisfy a dependency. The variable can be set
732 to one of the following values:
735 :command:`FetchContent_MakeAvailable` will only call
736 :command:`find_package` if the :command:`FetchContent_Declare` call
737 included a ``FIND_PACKAGE_ARGS`` keyword. This is also the default
738 behavior if ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` is not set.
741 :command:`find_package` can be called by
742 :command:`FetchContent_MakeAvailable` regardless of whether the
743 :command:`FetchContent_Declare` call included a ``FIND_PACKAGE_ARGS``
744 keyword or not. If no ``FIND_PACKAGE_ARGS`` keyword was given, the
745 behavior will be as though ``FIND_PACKAGE_ARGS`` had been provided,
746 with no additional arguments after it.
749 :command:`FetchContent_MakeAvailable` will not call
750 :command:`find_package`. Any ``FIND_PACKAGE_ARGS`` given to the
751 :command:`FetchContent_Declare` call will be ignored.
753 As a special case, if the :variable:`FETCHCONTENT_SOURCE_DIR_<uppercaseName>`
754 variable has a non-empty value for a dependency, it is assumed that the
755 user is overriding all other methods of making that dependency available.
756 ``FETCHCONTENT_TRY_FIND_PACKAGE_MODE`` will have no effect on that
757 dependency and :command:`FetchContent_MakeAvailable` will not try to call
758 :command:`find_package` for it.
760 In addition to the above, the following variables are also defined for each
763 .. variable:: FETCHCONTENT_SOURCE_DIR_<uppercaseName>
765 If this is set, no download or update steps are performed for the specified
766 content and the ``<lowercaseName>_SOURCE_DIR`` variable returned to the
767 caller is pointed at this location. This gives developers a way to have a
768 separate checkout of the content that they can modify freely without
769 interference from the build. The build simply uses that existing source,
770 but it still defines ``<lowercaseName>_BINARY_DIR`` to point inside its own
771 build area. Developers are strongly encouraged to use this mechanism rather
772 than editing the sources populated in the default location, as changes to
773 sources in the default location can be lost when content population details
774 are changed by the project.
776 .. variable:: FETCHCONTENT_UPDATES_DISCONNECTED_<uppercaseName>
778 This is the per-content equivalent of
779 :variable:`FETCHCONTENT_UPDATES_DISCONNECTED`. If the global option or
780 this option is ``ON``, then updates for the git and hg methods will not
781 contact any remote for the named content. They will only use information
782 already available locally. Disabling updates for individual content can
783 be useful for content whose details rarely change, while still leaving
784 other frequently changing content with updates enabled.
786 .. _`fetch-content-examples`:
794 This first fairly straightforward example ensures that some popular testing
795 frameworks are available to the main build:
797 .. code-block:: cmake
799 include(FetchContent)
800 FetchContent_Declare(
802 GIT_REPOSITORY https://github.com/google/googletest.git
803 GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
805 FetchContent_Declare(
807 GIT_REPOSITORY https://github.com/catchorg/Catch2.git
808 GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
811 # After the following call, the CMake targets defined by googletest and
812 # Catch2 will be available to the rest of the build
813 FetchContent_MakeAvailable(googletest Catch2)
815 .. _FetchContent-find_package-integration-examples:
817 Integrating With find_package()
818 """""""""""""""""""""""""""""""
820 For the previous example, if the user wanted to try to find ``googletest``
821 and ``Catch2`` via :command:`find_package` first before trying to download
822 and build them from source, they could set the
823 :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable to ``ALWAYS``.
824 This would also affect any other calls to :command:`FetchContent_Declare`
825 throughout the project, which might not be acceptable. The behavior can be
826 enabled for just these two dependencies instead by adding ``FIND_PACKAGE_ARGS``
827 to the declared details and leaving
828 :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` unset, or set to ``OPT_IN``:
830 .. code-block:: cmake
832 include(FetchContent)
833 FetchContent_Declare(
835 GIT_REPOSITORY https://github.com/google/googletest.git
836 GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
837 FIND_PACKAGE_ARGS NAMES GTest
839 FetchContent_Declare(
841 GIT_REPOSITORY https://github.com/catchorg/Catch2.git
842 GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
846 # This will try calling find_package() first for both dependencies
847 FetchContent_MakeAvailable(googletest Catch2)
849 For ``Catch2``, no additional arguments to :command:`find_package` are needed,
850 so no additional arguments are provided after the ``FIND_PACKAGE_ARGS``
851 keyword. For ``googletest``, its package is more commonly called ``GTest``,
852 so arguments are added to support it being found by that name.
854 If the user wanted to disable :command:`FetchContent_MakeAvailable` from
855 calling :command:`find_package` for any dependency, even if it provided
856 ``FIND_PACKAGE_ARGS`` in its declared details, they could set
857 :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` to ``NEVER``.
859 If the project wanted to indicate that these two dependencies should be
860 downloaded and built from source and that :command:`find_package` calls
861 should be redirected to use the built dependencies, the
862 ``OVERRIDE_FIND_PACKAGE`` option should be used when declaring the content
865 .. code-block:: cmake
867 include(FetchContent)
868 FetchContent_Declare(
870 GIT_REPOSITORY https://github.com/google/googletest.git
871 GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
872 OVERRIDE_FIND_PACKAGE
874 FetchContent_Declare(
876 GIT_REPOSITORY https://github.com/catchorg/Catch2.git
877 GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
878 OVERRIDE_FIND_PACKAGE
881 # The following will automatically forward through to FetchContent_MakeAvailable()
882 find_package(googletest)
885 CMake provides a FindGTest module which defines some variables that older
886 projects may use instead of linking to the imported targets. To support
887 those cases, we can provide an extra file. In keeping with the
888 "first to define, wins" philosophy of ``FetchContent``, we only write out
889 that file if something else hasn't already done so.
891 .. code-block:: cmake
893 FetchContent_MakeAvailable(googletest)
895 if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extra.cmake AND
896 NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestExtra.cmake)
897 file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-extra.cmake
899 if("${GTEST_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest)
900 set(GTEST_LIBRARIES GTest::gtest)
902 if("${GTEST_MAIN_LIBRARIES}" STREQUAL "" AND TARGET GTest::gtest_main)
903 set(GTEST_MAIN_LIBRARIES GTest::gtest_main)
905 if("${GTEST_BOTH_LIBRARIES}" STREQUAL "")
906 set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
911 Projects will also likely be using ``find_package(GTest)`` rather than
912 ``find_package(googletest)``, but it is possible to make use of the
913 :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` area to pull in the latter as
914 a dependency of the former. This is likely to be sufficient to satisfy
915 a typical ``find_package(GTest)`` call.
917 .. code-block:: cmake
919 FetchContent_MakeAvailable(googletest)
921 if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake AND
922 NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfig.cmake)
923 file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
925 include(CMakeFindDependencyMacro)
926 find_dependency(googletest)
930 if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake AND
931 NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfigVersion.cmake)
932 file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
934 include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-config-version.cmake OPTIONAL)
935 if(NOT PACKAGE_VERSION_COMPATIBLE)
936 include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestConfigVersion.cmake OPTIONAL)
941 Overriding Where To Find CMakeLists.txt
942 """""""""""""""""""""""""""""""""""""""
944 If the sub-project's ``CMakeLists.txt`` file is not at the top level of its
945 source tree, the ``SOURCE_SUBDIR`` option can be used to tell ``FetchContent``
946 where to find it. The following example shows how to use that option, and
947 it also sets a variable which is meaningful to the subproject before pulling
948 it into the main build (set as an ``INTERNAL`` cache variable to avoid
949 problems with policy :policy:`CMP0077`):
951 .. code-block:: cmake
953 include(FetchContent)
954 FetchContent_Declare(
956 GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
957 GIT_TAG ae50d9b9902526efd6c7a1907d09739f959c6297 # v3.15.0
960 set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
961 FetchContent_MakeAvailable(protobuf)
963 Complex Dependency Hierarchies
964 """"""""""""""""""""""""""""""
966 In more complex project hierarchies, the dependency relationships can be more
967 complicated. Consider a hierarchy where ``projA`` is the top level project and
968 it depends directly on projects ``projB`` and ``projC``. Both ``projB`` and
969 ``projC`` can be built standalone and they also both depend on another project
970 ``projD``. ``projB`` additionally depends on ``projE``. This example assumes
971 that all five projects are available on a company git server. The
972 ``CMakeLists.txt`` of each project might have sections like the following:
974 .. code-block:: cmake
977 include(FetchContent)
978 FetchContent_Declare(
980 GIT_REPOSITORY git@mycompany.com:git/projB.git
981 GIT_TAG 4a89dc7e24ff212a7b5167bef7ab079d
983 FetchContent_Declare(
985 GIT_REPOSITORY git@mycompany.com:git/projC.git
986 GIT_TAG 4ad4016bd1d8d5412d135cf8ceea1bb9
988 FetchContent_Declare(
990 GIT_REPOSITORY git@mycompany.com:git/projD.git
991 GIT_TAG origin/integrationBranch
993 FetchContent_Declare(
995 GIT_REPOSITORY git@mycompany.com:git/projE.git
999 # Order is important, see notes in the discussion further below
1000 FetchContent_MakeAvailable(projD projB projC)
1003 .. code-block:: cmake
1006 include(FetchContent)
1007 FetchContent_Declare(
1009 GIT_REPOSITORY git@mycompany.com:git/projD.git
1010 GIT_TAG 20b415f9034bbd2a2e8216e9a5c9e632
1012 FetchContent_Declare(
1014 GIT_REPOSITORY git@mycompany.com:git/projE.git
1015 GIT_TAG 68e20f674a48be38d60e129f600faf7d
1018 FetchContent_MakeAvailable(projD projE)
1021 .. code-block:: cmake
1024 include(FetchContent)
1025 FetchContent_Declare(
1027 GIT_REPOSITORY git@mycompany.com:git/projD.git
1028 GIT_TAG 7d9a17ad2c962aa13e2fbb8043fb6b8a
1031 # This particular version of projD requires workarounds
1032 FetchContent_GetProperties(projD)
1033 if(NOT projd_POPULATED)
1034 FetchContent_Populate(projD)
1036 # Copy an additional/replacement file into the populated source
1037 file(COPY someFile.c DESTINATION ${projd_SOURCE_DIR}/src)
1039 add_subdirectory(${projd_SOURCE_DIR} ${projd_BINARY_DIR})
1042 A few key points should be noted in the above:
1044 - ``projB`` and ``projC`` define different content details for ``projD``,
1045 but ``projA`` also defines a set of content details for ``projD``.
1046 Because ``projA`` will define them first, the details from ``projB`` and
1047 ``projC`` will not be used. The override details defined by ``projA``
1048 are not required to match either of those from ``projB`` or ``projC``, but
1049 it is up to the higher level project to ensure that the details it does
1050 define still make sense for the child projects.
1051 - In the ``projA`` call to :command:`FetchContent_MakeAvailable`, ``projD``
1052 is listed ahead of ``projB`` and ``projC`` to ensure that ``projA`` is in
1053 control of how ``projD`` is populated.
1054 - While ``projA`` defines content details for ``projE``, it does not need
1055 to explicitly call ``FetchContent_MakeAvailable(projE)`` or
1056 ``FetchContent_Populate(projD)`` itself. Instead, it leaves that to the
1057 child ``projB``. For higher level projects, it is often enough to just
1058 define the override content details and leave the actual population to the
1059 child projects. This saves repeating the same thing at each level of the
1060 project hierarchy unnecessarily.
1062 Populating Content Without Adding It To The Build
1063 """""""""""""""""""""""""""""""""""""""""""""""""
1065 Projects don't always need to add the populated content to the build.
1066 Sometimes the project just wants to make the downloaded content available at
1067 a predictable location. The next example ensures that a set of standard
1068 company toolchain files (and potentially even the toolchain binaries
1069 themselves) is available early enough to be used for that same build.
1071 .. code-block:: cmake
1073 cmake_minimum_required(VERSION 3.14)
1075 include(FetchContent)
1076 FetchContent_Declare(
1078 URL https://intranet.mycompany.com//toolchains_1.3.2.tar.gz
1080 FetchContent_MakeAvailable(mycom_toolchains)
1082 project(CrossCompileExample)
1084 The project could be configured to use one of the downloaded toolchains like
1087 .. code-block:: shell
1089 cmake -DCMAKE_TOOLCHAIN_FILE=_deps/mycom_toolchains-src/toolchain_arm.cmake /path/to/src
1091 When CMake processes the ``CMakeLists.txt`` file, it will download and unpack
1092 the tarball into ``_deps/mycompany_toolchains-src`` relative to the build
1093 directory. The :variable:`CMAKE_TOOLCHAIN_FILE` variable is not used until
1094 the :command:`project` command is reached, at which point CMake looks for the
1095 named toolchain file relative to the build directory. Because the tarball has
1096 already been downloaded and unpacked by then, the toolchain file will be in
1097 place, even the very first time that :program:`cmake` is run in the build directory.
1099 Populating Content In CMake Script Mode
1100 """""""""""""""""""""""""""""""""""""""
1102 This last example demonstrates how one might download and unpack a
1103 firmware tarball using CMake's :manual:`script mode <cmake(1)>`. The call to
1104 :command:`FetchContent_Populate` specifies all the content details and the
1105 unpacked firmware will be placed in a ``firmware`` directory below the
1106 current working directory.
1108 .. code-block:: cmake
1109 :caption: :file:`getFirmware.cmake`
1111 # NOTE: Intended to be run in script mode with cmake -P
1112 include(FetchContent)
1113 FetchContent_Populate(
1115 URL https://mycompany.com/assets/firmware-1.23-arm.tar.gz
1116 URL_HASH MD5=68247684da89b608d466253762b0ff11
1120 #]=======================================================================]
1122 include(${CMAKE_CURRENT_LIST_DIR}/ExternalProject/shared_internal_commands.cmake)
1124 #=======================================================================
1125 # Recording and retrieving content details for later population
1126 #=======================================================================
1128 # Internal use, projects must not call this directly. It is
1129 # intended for use by FetchContent_Declare() only.
1131 # Sets a content-specific global property (not meant for use
1132 # outside of functions defined here in this file) which can later
1133 # be retrieved using __FetchContent_getSavedDetails() with just the
1134 # same content name. If there is already a value stored in the
1135 # property, it is left unchanged and this call has no effect.
1136 # This allows parent projects to define the content details,
1137 # overriding anything a child project may try to set (properties
1138 # are not cached between runs, so the first thing to set it in a
1139 # build will be in control).
1140 function(__FetchContent_declareDetails contentName)
1142 string(TOLOWER ${contentName} contentNameLower)
1143 set(savedDetailsPropertyName "_FetchContent_${contentNameLower}_savedDetails")
1144 get_property(alreadyDefined GLOBAL PROPERTY ${savedDetailsPropertyName} DEFINED)
1149 if("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "ALWAYS")
1150 set(__tryFindPackage TRUE)
1151 set(__tryFindPackageAllowed TRUE)
1152 elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "NEVER")
1153 set(__tryFindPackage FALSE)
1154 set(__tryFindPackageAllowed FALSE)
1155 elseif("${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}" STREQUAL "OPT_IN" OR
1156 NOT DEFINED FETCHCONTENT_TRY_FIND_PACKAGE_MODE)
1157 set(__tryFindPackage FALSE)
1158 set(__tryFindPackageAllowed TRUE)
1161 "Unsupported value for FETCHCONTENT_TRY_FIND_PACKAGE_MODE: "
1162 "${FETCHCONTENT_TRY_FIND_PACKAGE_MODE}"
1167 set(__findPackageArgs)
1168 set(__sawQuietKeyword NO)
1169 set(__sawGlobalKeyword NO)
1170 foreach(__item IN LISTS ARGN)
1171 if(DEFINED __findPackageArgs)
1172 # All remaining args are for find_package()
1173 string(APPEND __findPackageArgs " [==[${__item}]==]")
1174 if(__item STREQUAL "QUIET")
1175 set(__sawQuietKeyword YES)
1176 elseif(__item STREQUAL "GLOBAL")
1177 set(__sawGlobalKeyword YES)
1182 # Still processing non-find_package() args
1183 if(__item STREQUAL "FIND_PACKAGE_ARGS")
1184 if(__tryFindPackageAllowed)
1185 set(__tryFindPackage TRUE)
1187 # All arguments after this keyword are for find_package(). Define the
1188 # variable but with an empty value initially. This allows us to check
1189 # at the start of the loop whether to store remaining items in this
1190 # variable or not. Note that there could be no more args, which is still
1191 # a valid case because we automatically provide ${contentName} as the
1192 # package name and there may not need to be any further arguments.
1193 set(__findPackageArgs "")
1194 continue() # Don't store this item
1195 elseif(__item STREQUAL "OVERRIDE_FIND_PACKAGE")
1196 set(__tryFindPackageAllowed FALSE)
1197 # Define a separate dedicated property for find_package() to check
1198 # in its implementation. This will be a placeholder until FetchContent
1199 # actually does the population. After that, we will have created a
1200 # stand-in config file that find_package() will pick up instead.
1201 set(propertyName "_FetchContent_${contentNameLower}_override_find_package")
1202 define_property(GLOBAL PROPERTY ${propertyName})
1203 set_property(GLOBAL PROPERTY ${propertyName} TRUE)
1206 string(APPEND __cmdArgs " [==[${__item}]==]")
1209 define_property(GLOBAL PROPERTY ${savedDetailsPropertyName})
1210 cmake_language(EVAL CODE
1211 "set_property(GLOBAL PROPERTY ${savedDetailsPropertyName} ${__cmdArgs})"
1214 if(__tryFindPackage AND __tryFindPackageAllowed)
1215 set(propertyName "_FetchContent_${contentNameLower}_find_package_args")
1216 define_property(GLOBAL PROPERTY ${propertyName})
1217 if(NOT __sawQuietKeyword)
1218 string(PREPEND __findPackageArgs "QUIET ")
1220 if(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL AND NOT __sawGlobalKeyword)
1221 string(APPEND __findPackageArgs " GLOBAL")
1223 cmake_language(EVAL CODE
1224 "set_property(GLOBAL PROPERTY ${propertyName} ${__findPackageArgs})"
1231 # Internal use, projects must not call this directly. It is
1232 # intended for use by the FetchContent_Declare() function.
1234 # Retrieves details saved for the specified content in an
1235 # earlier call to __FetchContent_declareDetails().
1236 function(__FetchContent_getSavedDetails contentName outVar)
1238 string(TOLOWER ${contentName} contentNameLower)
1239 set(propertyName "_FetchContent_${contentNameLower}_savedDetails")
1240 get_property(alreadyDefined GLOBAL PROPERTY ${propertyName} DEFINED)
1241 if(NOT alreadyDefined)
1242 message(FATAL_ERROR "No content details recorded for ${contentName}")
1244 get_property(propertyValue GLOBAL PROPERTY ${propertyName})
1245 set(${outVar} "${propertyValue}" PARENT_SCOPE)
1250 # Saves population details of the content, sets defaults for the
1251 # SOURCE_DIR and BUILD_DIR.
1252 function(FetchContent_Declare contentName)
1254 # Always check this even if we won't save these details.
1255 # This helps projects catch errors earlier.
1256 # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
1257 list(FIND ARGN OVERRIDE_FIND_PACKAGE index_OVERRIDE_FIND_PACKAGE)
1258 list(FIND ARGN FIND_PACKAGE_ARGS index_FIND_PACKAGE_ARGS)
1259 if(index_OVERRIDE_FIND_PACKAGE GREATER_EQUAL 0 AND
1260 index_FIND_PACKAGE_ARGS GREATER_EQUAL 0)
1262 "Cannot specify both OVERRIDE_FIND_PACKAGE and FIND_PACKAGE_ARGS "
1263 "when declaring details for ${contentName}"
1267 # Because we are only looking for a subset of the supported keywords, we
1268 # cannot check for multi-value arguments with this method. We will have to
1269 # handle the URL keyword differently.
1274 DOWNLOAD_EXTRACT_TIMESTAMP
1279 cmake_parse_arguments(PARSE_ARGV 1 ARG "" "${oneValueArgs}" "")
1281 string(TOLOWER ${contentName} contentNameLower)
1283 if(NOT ARG_BINARY_DIR)
1284 set(ARG_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
1287 if(NOT ARG_SOURCE_DIR)
1288 set(ARG_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
1291 if(ARG_GIT_REPOSITORY)
1292 # We resolve the GIT_REPOSITORY here so that we get the right parent in the
1293 # remote selection logic. In the sub-build, ExternalProject_Add() would see
1294 # the private sub-build directory as the parent project, but the parent
1295 # project should be the one that called FetchContent_Declare(). We resolve
1296 # a relative repo here so that the sub-build's ExternalProject_Add() only
1297 # ever sees a non-relative repo.
1298 # Since these checks may be non-trivial on some platforms (notably Windows),
1299 # don't perform them if we won't be using these details. This also allows
1300 # projects to override calls with relative URLs when they have checked out
1301 # the parent project in an unexpected way, such as from a mirror or fork.
1302 set(savedDetailsPropertyName "_FetchContent_${contentNameLower}_savedDetails")
1303 get_property(alreadyDefined GLOBAL PROPERTY ${savedDetailsPropertyName} DEFINED)
1304 if(NOT alreadyDefined)
1305 cmake_policy(GET CMP0150 cmp0150
1306 PARENT_SCOPE # undocumented, do not use outside of CMake
1308 _ep_resolve_git_remote(_resolved_git_repository
1309 "${ARG_GIT_REPOSITORY}" "${cmp0150}" "${FETCHCONTENT_BASE_DIR}"
1311 set(ARG_GIT_REPOSITORY "${_resolved_git_repository}")
1315 if(ARG_SVN_REPOSITORY)
1316 # Add a hash of the svn repository URL to the source dir. This works
1317 # around the problem where if the URL changes, the download would
1318 # fail because it tries to checkout/update rather than switch the
1319 # old URL to the new one. We limit the hash to the first 7 characters
1320 # so that the source path doesn't get overly long (which can be a
1321 # problem on windows due to path length limits).
1322 string(SHA1 urlSHA ${ARG_SVN_REPOSITORY})
1323 string(SUBSTRING ${urlSHA} 0 7 urlSHA)
1324 string(APPEND ARG_SOURCE_DIR "-${urlSHA}")
1327 # The ExternalProject_Add() call in the sub-build won't see the CMP0135
1328 # policy setting of our caller. Work out if that policy will be needed and
1329 # explicitly set the relevant option if not already provided. The condition
1330 # here is essentially an abbreviated version of the logic in
1331 # ExternalProject's _ep_add_download_command() function.
1332 if(NOT ARG_DOWNLOAD_NO_EXTRACT AND
1333 NOT DEFINED ARG_DOWNLOAD_EXTRACT_TIMESTAMP)
1334 list(FIND ARGN URL urlIndex)
1335 if(urlIndex GREATER_EQUAL 0)
1336 math(EXPR urlIndex "${urlIndex} + 1")
1337 list(LENGTH ARGN numArgs)
1338 if(urlIndex GREATER_EQUAL numArgs)
1340 "URL keyword needs to be followed by at least one URL"
1343 # If we have multiple URLs, none of them are allowed to be local paths.
1344 # Therefore, we can test just the first URL, and if it is non-local, so
1345 # will be the others if there are more.
1346 list(GET ARGN ${urlIndex} firstUrl)
1347 if(NOT IS_DIRECTORY "${firstUrl}")
1348 cmake_policy(GET CMP0135 _FETCHCONTENT_CMP0135
1349 PARENT_SCOPE # undocumented, do not use outside of CMake
1351 if(_FETCHCONTENT_CMP0135 STREQUAL "")
1352 message(AUTHOR_WARNING
1353 "The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy "
1354 "CMP0135 is not set. The policy's OLD behavior will be used. "
1355 "When using a URL download, the timestamps of extracted files "
1356 "should preferably be that of the time of extraction, otherwise "
1357 "code that depends on the extracted contents might not be "
1358 "rebuilt if the URL changes. The OLD behavior preserves the "
1359 "timestamps from the archive instead, but this is usually not "
1360 "what you want. Update your project to the NEW behavior or "
1361 "specify the DOWNLOAD_EXTRACT_TIMESTAMP option with a value of "
1362 "true to avoid this robustness issue."
1364 set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
1365 elseif(_FETCHCONTENT_CMP0135 STREQUAL "NEW")
1366 set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP FALSE)
1368 set(ARG_DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
1374 # Add back in the keyword args we pulled out and potentially tweaked/added
1375 set(sep EXTERNALPROJECT_INTERNAL_ARGUMENT_SEPARATOR)
1376 foreach(key IN LISTS oneValueArgs)
1377 if(DEFINED ARG_${key})
1378 list(PREPEND ARG_UNPARSED_ARGUMENTS ${key} "${ARG_${key}}" ${sep})
1384 foreach(__item IN LISTS ARG_UNPARSED_ARGUMENTS)
1385 string(APPEND __argsQuoted " [==[${__item}]==]")
1387 cmake_language(EVAL CODE
1388 "__FetchContent_declareDetails(${contentNameLower} ${__argsQuoted})"
1394 #=======================================================================
1395 # Set/get whether the specified content has been populated yet.
1396 # The setter also records the source and binary dirs used.
1397 #=======================================================================
1399 # Semi-internal use. Projects must not call this directly. Dependency
1400 # providers must call it if they satisfy a request made with the
1401 # FETCHCONTENT_MAKEAVAILABLE_SERIAL method (that is the only permitted
1402 # place to call it outside of the FetchContent module).
1403 function(FetchContent_SetPopulated contentName)
1405 cmake_parse_arguments(PARSE_ARGV 1 arg
1407 "SOURCE_DIR;BINARY_DIR"
1410 if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "")
1411 message(FATAL_ERROR "Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}")
1414 string(TOLOWER ${contentName} contentNameLower)
1415 set(prefix "_FetchContent_${contentNameLower}")
1417 set(propertyName "${prefix}_sourceDir")
1418 define_property(GLOBAL PROPERTY ${propertyName})
1419 if("${arg_SOURCE_DIR}" STREQUAL "")
1420 # Don't discard a previously provided SOURCE_DIR
1421 get_property(arg_SOURCE_DIR GLOBAL PROPERTY ${propertyName})
1423 set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}")
1425 set(propertyName "${prefix}_binaryDir")
1426 define_property(GLOBAL PROPERTY ${propertyName})
1427 if("${arg_BINARY_DIR}" STREQUAL "")
1428 # Don't discard a previously provided BINARY_DIR
1429 get_property(arg_BINARY_DIR GLOBAL PROPERTY ${propertyName})
1431 set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}")
1433 set(propertyName "${prefix}_populated")
1434 define_property(GLOBAL PROPERTY ${propertyName})
1435 set_property(GLOBAL PROPERTY ${propertyName} TRUE)
1440 # Set variables in the calling scope for any of the retrievable
1441 # properties. If no specific properties are requested, variables
1442 # will be set for all retrievable properties.
1444 # This function is intended to also be used by projects as the canonical
1445 # way to detect whether they should call FetchContent_Populate()
1446 # and pull the populated source into the build with add_subdirectory(),
1447 # if they are using the populated content in that way.
1448 function(FetchContent_GetProperties contentName)
1450 string(TOLOWER ${contentName} contentNameLower)
1453 set(oneValueArgs SOURCE_DIR BINARY_DIR POPULATED)
1454 set(multiValueArgs "")
1456 cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
1458 if(NOT ARG_SOURCE_DIR AND
1459 NOT ARG_BINARY_DIR AND
1461 # No specific properties requested, provide them all
1462 set(ARG_SOURCE_DIR ${contentNameLower}_SOURCE_DIR)
1463 set(ARG_BINARY_DIR ${contentNameLower}_BINARY_DIR)
1464 set(ARG_POPULATED ${contentNameLower}_POPULATED)
1467 set(prefix "_FetchContent_${contentNameLower}")
1470 set(propertyName "${prefix}_sourceDir")
1471 get_property(value GLOBAL PROPERTY ${propertyName})
1473 set(${ARG_SOURCE_DIR} ${value} PARENT_SCOPE)
1478 set(propertyName "${prefix}_binaryDir")
1479 get_property(value GLOBAL PROPERTY ${propertyName})
1481 set(${ARG_BINARY_DIR} ${value} PARENT_SCOPE)
1486 set(propertyName "${prefix}_populated")
1487 get_property(value GLOBAL PROPERTY ${propertyName} DEFINED)
1488 set(${ARG_POPULATED} ${value} PARENT_SCOPE)
1494 #=======================================================================
1495 # Performing the population
1496 #=======================================================================
1498 # The value of contentName will always have been lowercased by the caller.
1499 # All other arguments are assumed to be options that are understood by
1500 # ExternalProject_Add(), except for QUIET and SUBBUILD_DIR.
1501 function(__FetchContent_directPopulate contentName)
1505 # EXCLUDE_FROM_ALL and SYSTEM have no meaning for ExternalProject, they
1506 # are only used by us in FetchContent_MakeAvailable(). We need to parse
1507 # and discard them here.
1515 # We need special processing if DOWNLOAD_NO_EXTRACT is true
1517 # Prevent the following from being passed through
1522 # We force these to be ON since we are always executing serially
1523 # and we want all steps to have access to the terminal in case they
1524 # need input from the command line (e.g. ask for a private key password)
1525 # or they want to provide timely progress. We silently absorb and
1526 # discard these if they are set by the caller.
1527 USES_TERMINAL_DOWNLOAD
1528 USES_TERMINAL_UPDATE
1531 set(multiValueArgs "")
1533 cmake_parse_arguments(PARSE_ARGV 1 ARG
1534 "${options}" "${oneValueArgs}" "${multiValueArgs}")
1536 if(NOT ARG_SUBBUILD_DIR)
1537 message(FATAL_ERROR "Internal error: SUBBUILD_DIR not set")
1538 elseif(NOT IS_ABSOLUTE "${ARG_SUBBUILD_DIR}")
1539 set(ARG_SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SUBBUILD_DIR}")
1542 if(NOT ARG_SOURCE_DIR)
1543 message(FATAL_ERROR "Internal error: SOURCE_DIR not set")
1544 elseif(NOT IS_ABSOLUTE "${ARG_SOURCE_DIR}")
1545 set(ARG_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_SOURCE_DIR}")
1548 if(NOT ARG_BINARY_DIR)
1549 message(FATAL_ERROR "Internal error: BINARY_DIR not set")
1550 elseif(NOT IS_ABSOLUTE "${ARG_BINARY_DIR}")
1551 set(ARG_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${ARG_BINARY_DIR}")
1554 # Ensure the caller can know where to find the source and build directories
1555 # with some convenient variables. Doing this here ensures the caller sees
1556 # the correct result in the case where the default values are overridden by
1557 # the content details set by the project.
1558 set(${contentName}_SOURCE_DIR "${ARG_SOURCE_DIR}" PARENT_SCOPE)
1559 set(${contentName}_BINARY_DIR "${ARG_BINARY_DIR}" PARENT_SCOPE)
1561 # The unparsed arguments may contain spaces, so build up ARG_EXTRA
1562 # in such a way that it correctly substitutes into the generated
1563 # CMakeLists.txt file with each argument quoted.
1565 foreach(arg IN LISTS ARG_UNPARSED_ARGUMENTS)
1566 set(ARG_EXTRA "${ARG_EXTRA} \"${arg}\"")
1569 if(ARG_DOWNLOAD_NO_EXTRACT)
1570 set(ARG_EXTRA "${ARG_EXTRA} DOWNLOAD_NO_EXTRACT YES")
1571 set(__FETCHCONTENT_COPY_FILE
1573 ExternalProject_Get_Property(${contentName}-populate DOWNLOADED_FILE)
1574 get_filename_component(dlFileName \"\${DOWNLOADED_FILE}\" NAME)
1576 ExternalProject_Add_Step(${contentName}-populate copyfile
1577 COMMAND \"${CMAKE_COMMAND}\" -E copy_if_different
1578 \"<DOWNLOADED_FILE>\" \"${ARG_SOURCE_DIR}\"
1581 BYPRODUCTS \"${ARG_SOURCE_DIR}/\${dlFileName}\"
1582 COMMENT \"Copying file to SOURCE_DIR\"
1586 unset(__FETCHCONTENT_COPY_FILE)
1589 # Hide output if requested, but save it to a variable in case there's an
1590 # error so we can show the output upon failure. When not quiet, don't
1591 # capture the output to a variable because the user may want to see the
1592 # output as it happens (e.g. progress during long downloads). Combine both
1593 # stdout and stderr in the one capture variable so the output stays in order.
1596 OUTPUT_VARIABLE capturedOutput
1597 ERROR_VARIABLE capturedOutput
1602 message(STATUS "Populating ${contentName}")
1606 set(subCMakeOpts "-G${CMAKE_GENERATOR}")
1607 if(CMAKE_GENERATOR_PLATFORM)
1608 list(APPEND subCMakeOpts "-A${CMAKE_GENERATOR_PLATFORM}")
1610 if(CMAKE_GENERATOR_TOOLSET)
1611 list(APPEND subCMakeOpts "-T${CMAKE_GENERATOR_TOOLSET}")
1613 if(CMAKE_GENERATOR_INSTANCE)
1614 list(APPEND subCMakeOpts "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}")
1616 if(CMAKE_MAKE_PROGRAM)
1617 list(APPEND subCMakeOpts "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
1620 # GreenHills needs to know about the compiler and toolset to run the
1621 # subbuild commands. Be sure to update the similar section in
1622 # ExternalProject.cmake:_ep_extract_configure_command()
1623 if(CMAKE_GENERATOR MATCHES "Green Hills MULTI")
1624 list(APPEND subCMakeOpts
1625 "-DGHS_TARGET_PLATFORM:STRING=${GHS_TARGET_PLATFORM}"
1626 "-DGHS_PRIMARY_TARGET:STRING=${GHS_PRIMARY_TARGET}"
1627 "-DGHS_TOOLSET_ROOT:STRING=${GHS_TOOLSET_ROOT}"
1628 "-DGHS_OS_ROOT:STRING=${GHS_OS_ROOT}"
1629 "-DGHS_OS_DIR:STRING=${GHS_OS_DIR}"
1630 "-DGHS_BSP_NAME:STRING=${GHS_BSP_NAME}"
1634 # Override the sub-build's configuration types for multi-config generators.
1635 # This ensures we are not affected by any custom setting from the project
1636 # and can always request a known configuration further below.
1637 get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
1639 list(APPEND subCMakeOpts "-DCMAKE_CONFIGURATION_TYPES:STRING=Debug")
1643 # Likely we've been invoked via CMake's script mode where no
1644 # generator is set (and hence CMAKE_MAKE_PROGRAM could not be
1645 # trusted even if provided). We will have to rely on being
1646 # able to find the default generator and build tool.
1650 set(__FETCHCONTENT_CACHED_INFO "")
1651 set(__passthrough_vars
1652 CMAKE_EP_GIT_REMOTE_UPDATE_STRATEGY
1659 foreach(var IN LISTS __passthrough_vars)
1661 # Embed directly in the generated CMakeLists.txt file to avoid making
1662 # the cmake command line excessively long. It also makes debugging and
1664 string(APPEND __FETCHCONTENT_CACHED_INFO "set(${var} [==[${${var}}]==])\n")
1668 # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
1669 list(FIND ARG_UNPARSED_ARGUMENTS GIT_REPOSITORY indexResult)
1670 if(indexResult GREATER_EQUAL 0)
1671 find_package(Git QUIET)
1672 string(APPEND __FETCHCONTENT_CACHED_INFO "
1673 # Pass through things we've already detected in the main project to avoid
1674 # paying the cost of redetecting them again in ExternalProject_Add()
1675 set(GIT_EXECUTABLE [==[${GIT_EXECUTABLE}]==])
1676 set(GIT_VERSION_STRING [==[${GIT_VERSION_STRING}]==])
1677 set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
1678 [==[${GIT_EXECUTABLE};${GIT_VERSION_STRING}]==]
1683 # Create and build a separate CMake project to carry out the population.
1684 # If we've already previously done these steps, they will not cause
1685 # anything to be updated, so extra rebuilds of the project won't occur.
1686 # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
1687 # has this set to something not findable on the PATH. We also ensured above
1688 # that the Debug config will be defined for multi-config generators.
1689 configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent/CMakeLists.cmake.in"
1690 "${ARG_SUBBUILD_DIR}/CMakeLists.txt"
1694 COMMAND ${CMAKE_COMMAND} ${subCMakeOpts} .
1695 RESULT_VARIABLE result
1697 WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
1701 message("${capturedOutput}")
1703 message(FATAL_ERROR "CMake step for ${contentName} failed: ${result}")
1706 COMMAND ${CMAKE_COMMAND} --build . --config Debug
1707 RESULT_VARIABLE result
1709 WORKING_DIRECTORY "${ARG_SUBBUILD_DIR}"
1713 message("${capturedOutput}")
1715 message(FATAL_ERROR "Build step for ${contentName} failed: ${result}")
1721 option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist")
1722 option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population")
1723 option(FETCHCONTENT_QUIET "Enables QUIET option for all content population" ON)
1724 set(FETCHCONTENT_BASE_DIR "${CMAKE_BINARY_DIR}/_deps" CACHE PATH "Directory under which to collect all populated content")
1726 # Populate the specified content using details stored from
1727 # an earlier call to FetchContent_Declare().
1728 function(FetchContent_Populate contentName)
1731 message(FATAL_ERROR "Empty contentName not allowed for FetchContent_Populate()")
1734 string(TOLOWER ${contentName} contentNameLower)
1737 # This is the direct population form with details fully specified
1738 # as part of the call, so we already have everything we need
1739 __FetchContent_directPopulate(
1741 SUBBUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-subbuild"
1742 SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-src"
1743 BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${contentNameLower}-build"
1744 ${ARGN} # Could override any of the above ..._DIR variables
1747 # Pass source and binary dir variables back to the caller
1748 set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
1749 set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
1751 # Don't set global properties, or record that we did this population, since
1752 # this was a direct call outside of the normal declared details form.
1753 # We only want to save values in the global properties for content that
1754 # honors the hierarchical details mechanism so that projects are not
1755 # robbed of the ability to override details set in nested projects.
1759 # No details provided, so assume they were saved from an earlier call
1760 # to FetchContent_Declare(). Do a check that we haven't already
1761 # populated this content before in case the caller forgot to check.
1762 FetchContent_GetProperties(${contentName})
1763 if(${contentNameLower}_POPULATED)
1764 if("${${contentNameLower}_SOURCE_DIR}" STREQUAL "")
1766 "Content ${contentName} already populated by find_package() or a "
1767 "dependency provider"
1771 "Content ${contentName} already populated in ${${contentNameLower}_SOURCE_DIR}"
1776 __FetchContent_getSavedDetails(${contentName} contentDetails)
1777 if("${contentDetails}" STREQUAL "")
1778 message(FATAL_ERROR "No details have been set for content: ${contentName}")
1781 string(TOUPPER ${contentName} contentNameUpper)
1782 set(FETCHCONTENT_SOURCE_DIR_${contentNameUpper}
1783 "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}"
1784 CACHE PATH "When not empty, overrides where to find pre-populated content for ${contentName}")
1786 if(FETCHCONTENT_SOURCE_DIR_${contentNameUpper})
1787 # The source directory has been explicitly provided in the cache,
1788 # so no population is required. The build directory may still be specified
1789 # by the declared details though.
1791 if(NOT IS_ABSOLUTE "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
1792 # Don't check this directory because we don't know what location it is
1793 # expected to be relative to. We can't make this a hard error for backward
1794 # compatibility reasons.
1795 message(WARNING "Relative source directory specified. This is not safe, "
1796 "as it depends on the calling directory scope.\n"
1797 " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
1798 elseif(NOT EXISTS "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
1799 message(FATAL_ERROR "Manually specified source directory is missing:\n"
1800 " FETCHCONTENT_SOURCE_DIR_${contentNameUpper} --> ${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
1803 set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_SOURCE_DIR_${contentNameUpper}}")
1805 cmake_parse_arguments(savedDetails "" "BINARY_DIR" "" ${contentDetails})
1807 if(savedDetails_BINARY_DIR)
1808 set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
1810 set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
1813 elseif(FETCHCONTENT_FULLY_DISCONNECTED)
1814 # Bypass population and assume source is already there from a previous run.
1815 # Declared details may override the default source or build directories.
1817 cmake_parse_arguments(savedDetails "" "SOURCE_DIR;BINARY_DIR" "" ${contentDetails})
1819 if(savedDetails_SOURCE_DIR)
1820 set(${contentNameLower}_SOURCE_DIR ${savedDetails_SOURCE_DIR})
1822 set(${contentNameLower}_SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src")
1825 if(savedDetails_BINARY_DIR)
1826 set(${contentNameLower}_BINARY_DIR ${savedDetails_BINARY_DIR})
1828 set(${contentNameLower}_BINARY_DIR "${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build")
1832 # Support both a global "disconnect all updates" and a per-content
1833 # update test (either one being set disables updates for this content).
1834 option(FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper}
1835 "Enables UPDATE_DISCONNECTED behavior just for population of ${contentName}")
1836 if(FETCHCONTENT_UPDATES_DISCONNECTED OR
1837 FETCHCONTENT_UPDATES_DISCONNECTED_${contentNameUpper})
1838 set(disconnectUpdates True)
1840 set(disconnectUpdates False)
1843 if(FETCHCONTENT_QUIET)
1844 set(quietFlag QUIET)
1849 set(__detailsQuoted)
1850 foreach(__item IN LISTS contentDetails)
1851 if(NOT __item STREQUAL "OVERRIDE_FIND_PACKAGE")
1852 string(APPEND __detailsQuoted " [==[${__item}]==]")
1855 cmake_language(EVAL CODE "
1856 __FetchContent_directPopulate(
1859 UPDATE_DISCONNECTED ${disconnectUpdates}
1860 SUBBUILD_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-subbuild\"
1861 SOURCE_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src\"
1862 BINARY_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build\"
1863 # Put the saved details last so they can override any of the
1864 # the options we set above (this can include SOURCE_DIR or
1871 FetchContent_SetPopulated(
1873 SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}"
1874 BINARY_DIR "${${contentNameLower}_BINARY_DIR}"
1877 # Pass variables back to the caller. The variables passed back here
1878 # must match what FetchContent_GetProperties() sets when it is called
1879 # with just the content name.
1880 set(${contentNameLower}_SOURCE_DIR "${${contentNameLower}_SOURCE_DIR}" PARENT_SCOPE)
1881 set(${contentNameLower}_BINARY_DIR "${${contentNameLower}_BINARY_DIR}" PARENT_SCOPE)
1882 set(${contentNameLower}_POPULATED True PARENT_SCOPE)
1886 function(__FetchContent_setupFindPackageRedirection contentName)
1888 __FetchContent_getSavedDetails(${contentName} contentDetails)
1890 string(TOLOWER ${contentName} contentNameLower)
1891 get_property(wantFindPackage GLOBAL PROPERTY
1892 _FetchContent_${contentNameLower}_find_package_args
1896 # Avoid using if(... IN_LIST ...) so we don't have to alter policy settings
1897 list(FIND contentDetails OVERRIDE_FIND_PACKAGE indexResult)
1898 if(NOT wantFindPackage AND indexResult EQUAL -1)
1899 # No find_package() redirection allowed
1903 # We write out dep-config.cmake and dep-config-version.cmake file name
1904 # forms here because they are forced to lowercase. FetchContent
1905 # dependency names are case-insensitive, but find_package() config files
1906 # are only case-insensitive for the -config and -config-version forms,
1907 # not the Config and ConfigVersion forms.
1908 set(inFileDir ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent)
1909 set(configFilePrefix1 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentName}Config")
1910 set(configFilePrefix2 "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${contentNameLower}-config")
1911 if(NOT EXISTS "${configFilePrefix1}.cmake" AND
1912 NOT EXISTS "${configFilePrefix2}.cmake")
1913 configure_file(${inFileDir}/package-config.cmake.in
1914 "${configFilePrefix2}.cmake" @ONLY
1917 if(NOT EXISTS "${configFilePrefix1}Version.cmake" AND
1918 NOT EXISTS "${configFilePrefix2}-version.cmake")
1919 configure_file(${inFileDir}/package-config-version.cmake.in
1920 "${configFilePrefix2}-version.cmake" @ONLY
1924 # Now that we've created the redirected package config files, prevent
1925 # find_package() from delegating to FetchContent and let it find these
1926 # config files through its normal processing.
1927 set(propertyName "${prefix}_override_find_package")
1928 set(GLOBAL PROPERTY ${propertyName} FALSE)
1929 set(${contentName}_DIR "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}"
1930 CACHE INTERNAL "Redirected by FetchContent"
1935 # Arguments are assumed to be the names of dependencies that have been
1936 # declared previously and should be populated. It is not an error if
1937 # any of them have already been populated (they will just be skipped in
1938 # that case). The command is implemented as a macro so that the variables
1939 # defined by the FetchContent_GetProperties() and FetchContent_Populate()
1940 # calls will be available to the caller.
1941 macro(FetchContent_MakeAvailable)
1943 # We must append an item, even if the variable is unset, so prefix its value.
1944 # We will strip that prefix when we pop the value at the end of the macro.
1945 list(APPEND __cmake_fcCurrentVarsStack
1946 "__fcprefix__${CMAKE_VERIFY_INTERFACE_HEADER_SETS}"
1948 set(CMAKE_VERIFY_INTERFACE_HEADER_SETS FALSE)
1950 get_property(__cmake_providerCommand GLOBAL PROPERTY
1951 __FETCHCONTENT_MAKEAVAILABLE_SERIAL_PROVIDER
1953 foreach(__cmake_contentName IN ITEMS ${ARGV})
1954 string(TOLOWER ${__cmake_contentName} __cmake_contentNameLower)
1956 # If user specified FETCHCONTENT_SOURCE_DIR_... for this dependency, that
1957 # overrides everything else and we shouldn't try to use find_package() or
1958 # a dependency provider.
1959 string(TOUPPER ${__cmake_contentName} __cmake_contentNameUpper)
1960 if("${FETCHCONTENT_SOURCE_DIR_${__cmake_contentNameUpper}}" STREQUAL "")
1961 # Dependency provider gets first opportunity, but prevent infinite
1962 # recursion if we are called again for the same thing
1963 if(NOT "${__cmake_providerCommand}" STREQUAL "" AND
1964 NOT DEFINED __cmake_fcProvider_${__cmake_contentNameLower})
1966 "Trying FETCHCONTENT_MAKEAVAILABLE_SERIAL dependency provider for "
1967 "${__cmake_contentName}"
1970 if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
1971 list(APPEND __cmake_fcCurrentVarsStack "${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
1973 # This just needs to be something that can't be a real package name
1974 list(APPEND __cmake_fcCurrentVarsStack "<<::VAR_NOT_SET::>>")
1976 set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
1978 # It's still valid if there are no saved details. The project may have
1979 # been written to assume a dependency provider is always set and will
1980 # provide dependencies without having any declared details for them.
1981 __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
1982 set(__cmake_providerArgs
1983 "FETCHCONTENT_MAKEAVAILABLE_SERIAL"
1984 "${__cmake_contentName}"
1986 # Empty arguments must be preserved because of things like
1987 # GIT_SUBMODULES (see CMP0097)
1988 foreach(__cmake_item IN LISTS __cmake_contentDetails)
1989 string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
1992 # This property might be defined but empty. As long as it is defined,
1993 # find_package() can be called.
1994 get_property(__cmake_addfpargs GLOBAL PROPERTY
1995 _FetchContent_${__cmake_contentNameLower}_find_package_args
1998 if(__cmake_addfpargs)
1999 get_property(__cmake_fpargs GLOBAL PROPERTY
2000 _FetchContent_${__cmake_contentNameLower}_find_package_args
2002 string(APPEND __cmake_providerArgs " FIND_PACKAGE_ARGS")
2003 foreach(__cmake_item IN LISTS __cmake_fpargs)
2004 string(APPEND __cmake_providerArgs " [==[${__cmake_item}]==]")
2008 # Calling the provider could lead to FetchContent_MakeAvailable() being
2009 # called for a nested dependency. That nested call may occur in the
2010 # current variable scope. We have to save and restore the variables we
2012 list(APPEND __cmake_fcCurrentVarsStack
2013 ${__cmake_contentName}
2014 ${__cmake_contentNameLower}
2017 set(__cmake_fcProvider_${__cmake_contentNameLower} YES)
2018 cmake_language(EVAL CODE "${__cmake_providerCommand}(${__cmake_providerArgs})")
2020 list(POP_BACK __cmake_fcCurrentVarsStack
2021 __cmake_contentNameLower
2023 CMAKE_EXPORT_FIND_PACKAGE_NAME
2025 if(CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "<<::VAR_NOT_SET::>>")
2026 unset(CMAKE_EXPORT_FIND_PACKAGE_NAME)
2029 unset(__cmake_fcProvider_${__cmake_contentNameLower})
2030 unset(__cmake_providerArgs)
2031 unset(__cmake_addfpargs)
2032 unset(__cmake_fpargs)
2034 unset(__cmake_contentDetails)
2036 FetchContent_GetProperties(${__cmake_contentName})
2037 if(${__cmake_contentNameLower}_POPULATED)
2042 # Check if we've been asked to try find_package() first, even if we
2043 # have already populated this dependency. If we previously tried to
2044 # use find_package() for this and it succeeded, those things might
2045 # no longer be in scope, so we have to do it again.
2046 get_property(__cmake_haveFpArgs GLOBAL PROPERTY
2047 _FetchContent_${__cmake_contentNameLower}_find_package_args DEFINED
2049 if(__cmake_haveFpArgs)
2050 unset(__cmake_haveFpArgs)
2051 message(VERBOSE "Trying find_package(${__cmake_contentName} ...) before FetchContent")
2052 get_property(__cmake_fpArgs GLOBAL PROPERTY
2053 _FetchContent_${__cmake_contentNameLower}_find_package_args
2056 # This call could lead to FetchContent_MakeAvailable() being called for
2057 # a nested dependency and it may occur in the current variable scope.
2058 # We have to save/restore the variables we need to preserve.
2059 list(APPEND __cmake_fcCurrentNameStack
2060 ${__cmake_contentName}
2061 ${__cmake_contentNameLower}
2063 find_package(${__cmake_contentName} ${__cmake_fpArgs})
2064 list(POP_BACK __cmake_fcCurrentNameStack
2065 __cmake_contentNameLower
2068 unset(__cmake_fpArgs)
2070 if(${__cmake_contentName}_FOUND)
2071 FetchContent_SetPopulated(${__cmake_contentName})
2072 FetchContent_GetProperties(${__cmake_contentName})
2077 unset(__cmake_haveFpArgs)
2080 FetchContent_GetProperties(${__cmake_contentName})
2081 if(NOT ${__cmake_contentNameLower}_POPULATED)
2082 FetchContent_Populate(${__cmake_contentName})
2083 __FetchContent_setupFindPackageRedirection(${__cmake_contentName})
2085 # Only try to call add_subdirectory() if the populated content
2086 # can be treated that way. Protecting the call with the check
2087 # allows this function to be used for projects that just want
2088 # to ensure the content exists, such as to provide content at
2089 # a known location. We check the saved details for an optional
2090 # SOURCE_SUBDIR which can be used in the same way as its meaning
2091 # for ExternalProject. It won't matter if it was passed through
2092 # to the ExternalProject sub-build, since it would have been
2094 set(__cmake_srcdir "${${__cmake_contentNameLower}_SOURCE_DIR}")
2095 __FetchContent_getSavedDetails(${__cmake_contentName} __cmake_contentDetails)
2096 if("${__cmake_contentDetails}" STREQUAL "")
2097 message(FATAL_ERROR "No details have been set for content: ${__cmake_contentName}")
2099 cmake_parse_arguments(__cmake_arg "EXCLUDE_FROM_ALL;SYSTEM" "SOURCE_SUBDIR" "" ${__cmake_contentDetails})
2100 if(NOT "${__cmake_arg_SOURCE_SUBDIR}" STREQUAL "")
2101 string(APPEND __cmake_srcdir "/${__cmake_arg_SOURCE_SUBDIR}")
2104 if(EXISTS ${__cmake_srcdir}/CMakeLists.txt)
2105 if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
2106 list(APPEND __cmake_fcCurrentVarsStack "${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
2108 # This just needs to be something that can't be a real package name
2109 list(APPEND __cmake_fcCurrentVarsStack "<<::VAR_NOT_SET::>>")
2111 set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
2113 set(__cmake_add_subdirectory_args ${__cmake_srcdir} ${${__cmake_contentNameLower}_BINARY_DIR})
2114 if(__cmake_arg_EXCLUDE_FROM_ALL)
2115 list(APPEND __cmake_add_subdirectory_args EXCLUDE_FROM_ALL)
2117 if(__cmake_arg_SYSTEM)
2118 list(APPEND __cmake_add_subdirectory_args SYSTEM)
2120 add_subdirectory(${__cmake_add_subdirectory_args})
2122 list(POP_BACK __cmake_fcCurrentVarsStack CMAKE_EXPORT_FIND_PACKAGE_NAME)
2123 if(CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "<<::VAR_NOT_SET::>>")
2124 unset(CMAKE_EXPORT_FIND_PACKAGE_NAME)
2128 unset(__cmake_srcdir)
2129 unset(__cmake_contentDetails)
2130 unset(__cmake_arg_EXCLUDE_FROM_ALL)
2131 unset(__cmake_arg_SYSTEM)
2132 unset(__cmake_arg_SOURCE_SUBDIR)
2133 unset(__cmake_add_subdirectory_args)
2137 # Prefix will be "__fcprefix__"
2138 list(POP_BACK __cmake_fcCurrentVarsStack __cmake_original_verify_setting)
2139 string(SUBSTRING "${__cmake_original_verify_setting}"
2140 12 -1 __cmake_original_verify_setting
2142 set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${__cmake_original_verify_setting})
2144 # clear local variables to prevent leaking into the caller's scope
2145 unset(__cmake_contentName)
2146 unset(__cmake_contentNameLower)
2147 unset(__cmake_contentNameUpper)
2148 unset(__cmake_providerCommand)
2149 unset(__cmake_original_verify_setting)