You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.7KB

  1. .. highlight:: cmake
  2. Easy Mode: Using ``dds`` in a CMake Project
  3. ###########################################
  4. One of ``dds``'s primary goals is to inter-operate with other build systems
  5. cleanly. Because of CMakes ubiquity, ``dds`` includes built-in support for
  6. emitting files that can be imported into CMake.
  7. .. seealso::
  8. Before reading this page, be sure to read the :ref:`build-deps.gen-libman`
  9. section of the :doc:`build-deps` page, which will discuss how to use the
  10. ``dds build-deps`` subcommand.
  11. .. note::
  12. We'll first look as *easy mode*, but there's also an *easiest mode* for a
  13. one-line solution: :ref:`see below <cmake.pmm>`.
  14. .. _PMM: https://github.com/vector-of-bool/PMM
  15. Generating a CMake Import File
  16. ******************************
  17. ``build-deps`` accepts an ``--lmi-path`` argument, but also accepts a
  18. ``--cmake=<path>`` argument that serves a similar purpose: It will write a CMake
  19. file to ``<path>`` that can be ``include()``'d into a CMake project:
  20. .. code-block:: bash
  21. $ dds build-deps "neo-sqlite3^0.2.0" --cmake=deps.cmake
  22. This will write a file ``./deps.cmake`` that we can ``include()`` from a CMake
  23. project, which will then expose the ``neo-sqlite3`` package as a set of imported
  24. targets.
  25. Using the CMake Import File
  26. ===========================
  27. Once we have generated the CMake import file using ``dds build-deps``, we can
  28. simply import it in our ``CMakeLists.txt``::
  29. include(deps.cmake)
  30. Like with ``dds``, CMake wants us to explicitly declare how our build targets
  31. *use* other libraries. When we ``include()`` the generated CMake file, it will
  32. generate ``IMPORTED`` targets that can be linked against.
  33. In ``dds`` (and in libman), a library is identified by a combination of
  34. *namespace* and *name*, joined together with a slash ``/`` character. This
  35. *qualified name* of a library is decided by the original package author, and
  36. should be documented. In the case of ``neo-sqlite3``, the only library is
  37. ``neo/sqlite3``.
  38. When the generated import file imports a library, it creates a qualified name
  39. using a double-colon "``::``" instead of a slash. As such, our ``neo/sqlite3``
  40. is imported in CMake as ``neo::sqlite3``. We can link against it as we would
  41. with any other target::
  42. add_executable(my-application app.cpp)
  43. target_link_libraries(my-application PRIVATE neo::sqlite3)
  44. .. _cmake.pmm:
  45. *Easiest* Mode: PMM Support
  46. ***************************
  47. `PMM`_ is the *package package manager*, and can be used to control and access
  48. package managers from within CMake scripts. This includes controlling ``dds``.
  49. With PMM, we can automate all of the previous steps into a single line.
  50. Refer to the ``README.md`` file in `the PMM repo <PMM>`_ for information on how
  51. to get PMM into your CMake project. In short, download and place the
  52. ``pmm.cmake`` file in your repository, and ``include()`` the file near the top
  53. of your ``CMakeLists.txt``::
  54. include(pmm.cmake)
  55. The ``pmm()`` function also supports ``dds`` directly, and will automatically
  56. download a prebuilt ``dds`` for the host platform and invoke ``dds build-deps``
  57. in a single pass as part of CMake's configure process. This is especially useful
  58. for a CI environment where you want to have a stable ``dds`` version and always
  59. have your dependencies obtained just-in-time.
  60. To start, pass the ``DDS`` argument to ``pmm()`` to use it::
  61. pmm(DDS)
  62. .. note::
  63. The ``_deps`` directory and generated CMake imports file will be placed in
  64. the CMake build directory, out of the way of the rest of the project.
  65. .. note::
  66. The version of ``dds`` that PMM downloads depends on the version of PMM
  67. that is in use.
  68. This alone won't do anything useful, because you'll need to tell it what
  69. dependencies we want to install::
  70. pmm(DDS DEP_FILES dependencies.json5)
  71. You can also list your dependencies as inline strings in your CMakeLists.txt
  72. instead of a separate file::
  73. pmm(DDS DEPENDS neo-sqlite3^0.2.2)
  74. This invocation will run ``build-deps`` with the build options, generate a CMake
  75. imports file, and immediately ``include()`` it to import the generated CMake
  76. targets. ``pmm(DDS)`` will also generate a ``dds`` :doc:`toolchain <toolchains>`
  77. based on the current CMake build environment, ensuring that the generated
  78. packages have matching build options to the rest of the project. Refer to the
  79. PMM README for more details.
  80. .. code-block::
  81. :caption: ``CMakeLists.txt``
  82. :linenos:
  83. :emphasize-lines: 4,5
  84. cmake_minimum_required(VERSION 3.15)
  85. project(MyApplication VERSION 1.0.0)
  86. include(pmm.cmake)
  87. pmm(DDS DEPENDS neo-sqlite3^0.2.2)
  88. add_executable(my-application app.cpp)
  89. target_link_libraries(my-application PRIVATE neo::sqlite3)
  90. This removes the requirement that we write a separate dependencies file, and we
  91. no longer need to invoke ``dds build-deps`` externally, as it is all handled
  92. by ``pmm()``.