load_cache.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. load_cache
  2. ----------
  3. Load in the values from another project's ``CMakeCache.txt`` cache file. This
  4. is useful for projects that depend on another project built in a separate
  5. directory tree.
  6. This command has two signatures. The recommended signature is:
  7. .. signature::
  8. load_cache(<build-dir> READ_WITH_PREFIX <prefix> <entry>...)
  9. :target: READ_WITH_PREFIX
  10. Loads the cache file from the specified ``<build-dir>`` build directory and
  11. retrieves the listed cache entries. The retrieved values are stored in local
  12. variables, with their names prefixed by the provided ``<prefix>``. This only
  13. reads the cache values; it does not create or modify entries in the local
  14. project's cache.
  15. ``READ_WITH_PREFIX <prefix>``
  16. For each cache ``<entry>``, a local variable is created using the specified
  17. ``<prefix>`` followed by the entry name.
  18. This signature can be also used in :option:`cmake -P` script mode.
  19. The following signature of this command is strongly discouraged, but it is
  20. provided for backward compatibility:
  21. .. signature::
  22. load_cache(<build-dir> [EXCLUDE <entry>...] [INCLUDE_INTERNALS <entry>...])
  23. :target: raw
  24. This form loads the cache file from the specified ``<build-dir>`` build
  25. directory and imports all its non-internal cache entries into the local
  26. project's cache as internal cache variables. By default, only non-internal
  27. entries are imported, unless the ``INCLUDE_INTERNALS`` option is used.
  28. The options are:
  29. ``EXCLUDE <entry>...``
  30. This option can be used to exclude a given list of non-internal cache
  31. entries when importing values.
  32. ``INCLUDE_INTERNALS <entry>...``
  33. This option can be used to provide a list of internal cache entries to
  34. include in addition to the non-internal cache entries.
  35. This signature can be used only in CMake projects. Script mode is not
  36. supported.
  37. .. note::
  38. Instead of loading the outside project's cache file and manually accessing
  39. variables, a more robust and convenient approach is to use the
  40. :command:`export` command in the outside project, when available. This allows
  41. the project to provide its targets, configuration, or features in a
  42. structured and maintainable way, making integration simpler and less
  43. error-prone.
  44. Examples
  45. ^^^^^^^^
  46. Reading specific cache variables from another project and storing them as local
  47. variables:
  48. .. code-block:: cmake
  49. load_cache(
  50. path/to/other-project/build-dir
  51. READ_WITH_PREFIX prefix_
  52. OTHER_PROJECT_CACHE_VAR_1
  53. OTHER_PROJECT_CACHE_VAR_2
  54. )
  55. message(STATUS "${prefix_OTHER_PROJECT_CACHE_VAR_1")
  56. message(STATUS "${prefix_OTHER_PROJECT_CACHE_VAR_2")
  57. # Outputs:
  58. # -- some-value...
  59. # -- another-value...
  60. Reading all non-internal cache entries from another project and storing them as
  61. internal cache variables using the obsolete signature:
  62. .. code-block:: cmake
  63. load_cache(path/to/other-project/build-dir)
  64. message(STATUS "${OTHER_PROJECT_CACHE_VAR_1")
  65. message(STATUS "${OTHER_PROJECT_CACHE_VAR_2")
  66. # Outputs:
  67. # -- some-value...
  68. # -- another-value...
  69. Excluding specific non-internal cache entries and including internal ones using
  70. the obsolete signature:
  71. .. code-block:: cmake
  72. load_cache(
  73. path/to/other-project/build-dir
  74. EXCLUDE OTHER_PROJECT_CACHE_VAR_2
  75. INCLUDE_INTERNALS OTHER_PROJECT_INTERNAL_CACHE_VAR
  76. )
  77. message(STATUS "${OTHER_PROJECT_CACHE_VAR_1")
  78. message(STATUS "${OTHER_PROJECT_CACHE_VAR_2")
  79. message(STATUS "${OTHER_PROJECT_INTERNAL_CACHE_VAR}")
  80. # Outputs:
  81. # -- some-value...
  82. # --
  83. # -- some-internal-value...