Miscellaneous Features.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. Step 11: Miscellaneous Features
  2. ===============================
  3. Some features don't fit well or aren't important enough to receive attention
  4. in the main tutorial, but deserve mention. These exercises collect some of those
  5. features. They should be considered "bonuses".
  6. There are many CMake features that are not covered by the tutorial, some of
  7. which are considered essential to the projects which use them. Others are
  8. in common use by packagers but see little discussion among software developers
  9. producing local builds.
  10. This list is not an exhaustive discussion of what remains of CMake's
  11. capabilities. It may grow or shrink with time and relevance.
  12. Exercise 1: Target Aliases
  13. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  14. This tutorial focuses on installing dependencies and consuming them from an
  15. install tree. It also recommends the use of package managers to facilitate
  16. this process. However, for a variety of reasons both historical and
  17. contemporary this is not always how CMake projects are consumed.
  18. It is possible to vendor a dependency's source code entirely in a parent project
  19. and consume it with :command:`add_subdirectory`. When performed, the target
  20. names exposed are those used within the project, not those exported via
  21. :command:`install(EXPORT)`. The target names will not have the namespace string
  22. that command prefixes to targets.
  23. Some projects wish to support this workflow with an interface consistent with
  24. the one presented to :command:`find_package` consumers. CMake supports this via
  25. :command:`add_library(ALIAS)` and :command:`add_executable(ALIAS)`.
  26. .. code-block:: cmake
  27. add_library(MyLib INTERFACE)
  28. add_library(MyProject::MyLib ALIAS MyLib)
  29. Goal
  30. ----
  31. Add a library alias for the ``MathFunctions`` library.
  32. Helpful Resources
  33. -----------------
  34. * :command:`add_library`
  35. Files to Edit
  36. -------------
  37. * ``TutorialProject/MathFunctions/CMakeLists.txt``
  38. Getting Started
  39. ---------------
  40. For this step we will only be editing the ``TutorialProject`` project in the
  41. ``Step11`` folder. Complete ``TODO 1``.
  42. Build and Run
  43. -------------
  44. To build the project we first need configure and install ``SimpleTest``.
  45. Navigate to ``Help/guide/Step11/SimpleTest`` and run the appropriate commands.
  46. .. code-block:: console
  47. cmake --preset tutorial
  48. cmake --install build
  49. Then navigate to ``Help/guide/Step11/TutorialProject`` and perform the usual build.
  50. .. code-block:: console
  51. cmake --preset tutorial
  52. cmake --build build
  53. There should be no observable change in behavior from adding the alias.
  54. Solution
  55. --------
  56. We add a single line to the ``MathFunctions`` CML.
  57. .. raw:: html
  58. <details><summary>TODO 1 Click to show/hide answer</summary>
  59. .. literalinclude:: Complete/TutorialProject/MathFunctions/CMakeLists.txt
  60. :caption: TODO 1: TutorialProject/MathFunctions/CMakeLists.txt
  61. :name: TutorialProject/MathFunctions/CMakeLists.txt-alias
  62. :language: cmake
  63. :start-at: ALIAS
  64. :end-at: ALIAS
  65. .. raw:: html
  66. </details>
  67. Exercise 2: Generator Expressions
  68. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  69. :manual:`Generator expressions <cmake-generator-expressions(7)>` are a
  70. complicated domain-specific language supported in some contexts within CMake.
  71. They are most easily understood as deferred-evaluation conditionals, they
  72. express requirements where the inputs to determine the correct behavior are not
  73. known during the CMake configuration stage.
  74. .. note::
  75. This is where generator expressions get their name, they are evaluated when
  76. the underlying build system is being generated.
  77. Generator expressions were commonly used in combination with
  78. :command:`target_include_directories` to express include directory requirements
  79. across the build and install tree, but file sets have superseded this use case.
  80. Their most common applications now are in multi-config generators and
  81. intricate dependency injection systems.
  82. .. code-block:: cmake
  83. target_compile_definitions(MyApp PRIVATE "MYAPP_BUILD_CONFIG=$<CONFIG>")
  84. Goal
  85. ----
  86. Add a generator expression to ``SimpleTest`` that checks the build configuration
  87. inside a compile definition.
  88. Helpful Resources
  89. -----------------
  90. * :command:`target_compile_definitions`
  91. * :manual:`cmake-generator-expressions(7)`
  92. Files to Edit
  93. -------------
  94. * ``SimpleTest/CMakeLists.txt``
  95. Getting Started
  96. ---------------
  97. For this step we will only be editing the ``SimpleTest`` project in the
  98. ``Step11`` folder. Complete ``TODO 2``.
  99. Build and Run
  100. -------------
  101. To build the project we first need configure and install ``SimpleTest``.
  102. Navigate to ``Help/guide/Step11/SimpleTest`` and run the appropriate commands.
  103. .. code-block:: console
  104. cmake --preset tutorial
  105. cmake --install build
  106. Then navigate to ``Help/guide/Step11/TutorialProject`` and perform the usual build.
  107. .. code-block:: console
  108. cmake --preset tutorial
  109. cmake --build build
  110. When running the ``TestMathFunctions`` binary directly, we should a message
  111. naming the build configuration used to build the executable (not necessarily the
  112. same as configuration used to configure ``SimpleTest``). On single configuration
  113. generators, the build configuration can be changed by setting
  114. :variable:`CMAKE_BUILD_TYPE`.
  115. Solution
  116. --------
  117. We add a single line to the ``SimpleTest`` CML.
  118. .. raw:: html
  119. <details><summary>TODO 2 Click to show/hide answer</summary>
  120. .. literalinclude:: Complete/SimpleTest/CMakeLists.txt
  121. :caption: TODO 2: SimpleTest/CMakeLists.txt
  122. :name: SimpleTest/CMakeLists.txt-target_compile_definitions
  123. :language: cmake
  124. :start-at: target_compile_definitions
  125. :end-at: target_compile_definitions
  126. .. raw:: html
  127. </details>