cmake-developer.7.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. .. cmake-manual-description: CMake Developer Reference
  2. cmake-developer(7)
  3. ******************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. This manual is intended for reference by developers modifying the CMake
  9. source tree itself, and by those authoring externally-maintained modules.
  10. See https://cmake.org/get-involved/ to get involved in development of
  11. CMake upstream.
  12. Adding Compile Features
  13. =======================
  14. CMake reports an error if a compiler whose features are known does not report
  15. support for a particular requested feature. A compiler is considered to have
  16. known features if it reports support for at least one feature.
  17. When adding a new compile feature to CMake, it is therefore necessary to list
  18. support for the feature for all CompilerIds which already have one or more
  19. feature supported, if the new feature is available for any version of the
  20. compiler.
  21. When adding the first supported feature to a particular CompilerId, it is
  22. necessary to list support for all features known to cmake (See
  23. :variable:`CMAKE_C_COMPILE_FEATURES` and
  24. :variable:`CMAKE_CXX_COMPILE_FEATURES` as appropriate), where available for
  25. the compiler. Ensure that the ``CMAKE_<LANG>_STANDARD_DEFAULT`` is set to
  26. the computed internal variable ``CMAKE_<LANG>_STANDARD_COMPUTED_DEFAULT``
  27. for compiler versions which should be supported.
  28. It is sensible to record the features for the most recent version of a
  29. particular CompilerId first, and then work backwards. It is sensible to
  30. try to create a continuous range of versions of feature releases of the
  31. compiler. Gaps in the range indicate incorrect features recorded for
  32. intermediate releases.
  33. Generally, features are made available for a particular version if the
  34. compiler vendor documents availability of the feature with that
  35. version. Note that sometimes partially implemented features appear to
  36. be functional in previous releases (such as ``cxx_constexpr`` in GNU 4.6,
  37. though availability is documented in GNU 4.7), and sometimes compiler vendors
  38. document availability of features, though supporting infrastructure is
  39. not available (such as ``__has_feature(cxx_generic_lambdas)`` indicating
  40. non-availability in Clang 3.4, though it is documented as available, and
  41. fixed in Clang 3.5). Similar cases for other compilers and versions
  42. need to be investigated when extending CMake to support them.
  43. When a vendor releases a new version of a known compiler which supports
  44. a previously unsupported feature, and there are already known features for
  45. that compiler, the feature should be listed as supported in CMake for
  46. that version of the compiler as soon as reasonably possible.
  47. Standard-specific/compiler-specific variables such
  48. ``CMAKE_CXX98_COMPILE_FEATURES`` are deliberately not documented. They
  49. only exist for the compiler-specific implementation of adding the ``-std``
  50. compile flag for compilers which need that.
  51. Help
  52. ====
  53. The ``Help`` directory contains CMake help manual source files.
  54. They are written using the `reStructuredText`_ markup syntax and
  55. processed by `Sphinx`_ to generate the CMake help manuals.
  56. .. _`reStructuredText`: http://docutils.sourceforge.net/docs/ref/rst/introduction.html
  57. .. _`Sphinx`: http://sphinx-doc.org
  58. Markup Constructs
  59. -----------------
  60. In addition to using Sphinx to generate the CMake help manuals, we
  61. also use a C++-implemented document processor to print documents for
  62. the ``--help-*`` command-line help options. It supports a subset of
  63. reStructuredText markup. When authoring or modifying documents,
  64. please verify that the command-line help looks good in addition to the
  65. Sphinx-generated html and man pages.
  66. The command-line help processor supports the following constructs
  67. defined by reStructuredText, Sphinx, and a CMake extension to Sphinx.
  68. ..
  69. Note: This list must be kept consistent with the cmRST implementation.
  70. CMake Domain directives
  71. Directives defined in the `CMake Domain`_ for defining CMake
  72. documentation objects are printed in command-line help output as
  73. if the lines were normal paragraph text with interpretation.
  74. CMake Domain interpreted text roles
  75. Interpreted text roles defined in the `CMake Domain`_ for
  76. cross-referencing CMake documentation objects are replaced by their
  77. link text in command-line help output. Other roles are printed
  78. literally and not processed.
  79. ``code-block`` directive
  80. Add a literal code block without interpretation. The command-line
  81. help processor prints the block content without the leading directive
  82. line and with common indentation replaced by one space.
  83. ``include`` directive
  84. Include another document source file. The command-line help
  85. processor prints the included document inline with the referencing
  86. document.
  87. literal block after ``::``
  88. A paragraph ending in ``::`` followed by a blank line treats
  89. the following indented block as literal text without interpretation.
  90. The command-line help processor prints the ``::`` literally and
  91. prints the block content with common indentation replaced by one
  92. space.
  93. ``note`` directive
  94. Call out a side note. The command-line help processor prints the
  95. block content as if the lines were normal paragraph text with
  96. interpretation.
  97. ``parsed-literal`` directive
  98. Add a literal block with markup interpretation. The command-line
  99. help processor prints the block content without the leading
  100. directive line and with common indentation replaced by one space.
  101. ``productionlist`` directive
  102. Render context-free grammar productions. The command-line help
  103. processor prints the block content as if the lines were normal
  104. paragraph text with interpretation.
  105. ``replace`` directive
  106. Define a ``|substitution|`` replacement.
  107. The command-line help processor requires a substitution replacement
  108. to be defined before it is referenced.
  109. ``|substitution|`` reference
  110. Reference a substitution replacement previously defined by
  111. the ``replace`` directive. The command-line help processor
  112. performs the substitution and replaces all newlines in the
  113. replacement text with spaces.
  114. ``toctree`` directive
  115. Include other document sources in the Table-of-Contents
  116. document tree. The command-line help processor prints
  117. the referenced documents inline as part of the referencing
  118. document.
  119. Inline markup constructs not listed above are printed literally in the
  120. command-line help output. We prefer to use inline markup constructs that
  121. look correct in source form, so avoid use of \\-escapes in favor of inline
  122. literals when possible.
  123. Explicit markup blocks not matching directives listed above are removed from
  124. command-line help output. Do not use them, except for plain ``..`` comments
  125. that are removed by Sphinx too.
  126. Note that nested indentation of blocks is not recognized by the
  127. command-line help processor. Therefore:
  128. * Explicit markup blocks are recognized only when not indented
  129. inside other blocks.
  130. * Literal blocks after paragraphs ending in ``::`` but not
  131. at the top indentation level may consume all indented lines
  132. following them.
  133. Try to avoid these cases in practice.
  134. CMake Domain
  135. ------------
  136. CMake adds a `Sphinx Domain`_ called ``cmake``, also called the
  137. "CMake Domain". It defines several "object" types for CMake
  138. documentation:
  139. ``command``
  140. A CMake language command.
  141. ``generator``
  142. A CMake native build system generator.
  143. See the :manual:`cmake(1)` command-line tool's ``-G`` option.
  144. ``manual``
  145. A CMake manual page, like this :manual:`cmake-developer(7)` manual.
  146. ``module``
  147. A CMake module.
  148. See the :manual:`cmake-modules(7)` manual
  149. and the :command:`include` command.
  150. ``policy``
  151. A CMake policy.
  152. See the :manual:`cmake-policies(7)` manual
  153. and the :command:`cmake_policy` command.
  154. ``prop_cache, prop_dir, prop_gbl, prop_sf, prop_inst, prop_test, prop_tgt``
  155. A CMake cache, directory, global, source file, installed file, test,
  156. or target property, respectively. See the :manual:`cmake-properties(7)`
  157. manual and the :command:`set_property` command.
  158. ``variable``
  159. A CMake language variable.
  160. See the :manual:`cmake-variables(7)` manual
  161. and the :command:`set` command.
  162. Documentation objects in the CMake Domain come from two sources.
  163. First, the CMake extension to Sphinx transforms every document named
  164. with the form ``Help/<type>/<file-name>.rst`` to a domain object with
  165. type ``<type>``. The object name is extracted from the document title,
  166. which is expected to be of the form::
  167. <object-name>
  168. -------------
  169. and to appear at or near the top of the ``.rst`` file before any other
  170. lines starting in a letter, digit, or ``<``. If no such title appears
  171. literally in the ``.rst`` file, the object name is the ``<file-name>``.
  172. If a title does appear, it is expected that ``<file-name>`` is equal
  173. to ``<object-name>`` with any ``<`` and ``>`` characters removed.
  174. Second, the CMake Domain provides directives to define objects inside
  175. other documents:
  176. .. code-block:: rst
  177. .. command:: <command-name>
  178. This indented block documents <command-name>.
  179. .. variable:: <variable-name>
  180. This indented block documents <variable-name>.
  181. Object types for which no directive is available must be defined using
  182. the first approach above.
  183. .. _`Sphinx Domain`: http://sphinx-doc.org/domains.html
  184. Cross-References
  185. ----------------
  186. Sphinx uses reStructuredText interpreted text roles to provide
  187. cross-reference syntax. The `CMake Domain`_ provides for each
  188. domain object type a role of the same name to cross-reference it.
  189. CMake Domain roles are inline markup of the forms::
  190. :type:`name`
  191. :type:`text <name>`
  192. where ``type`` is the domain object type and ``name`` is the
  193. domain object name. In the first form the link text will be
  194. ``name`` (or ``name()`` if the type is ``command``) and in
  195. the second form the link text will be the explicit ``text``.
  196. For example, the code:
  197. .. code-block:: rst
  198. * The :command:`list` command.
  199. * The :command:`list(APPEND)` sub-command.
  200. * The :command:`list() command <list>`.
  201. * The :command:`list(APPEND) sub-command <list>`.
  202. * The :variable:`CMAKE_VERSION` variable.
  203. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  204. produces:
  205. * The :command:`list` command.
  206. * The :command:`list(APPEND)` sub-command.
  207. * The :command:`list() command <list>`.
  208. * The :command:`list(APPEND) sub-command <list>`.
  209. * The :variable:`CMAKE_VERSION` variable.
  210. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  211. Note that CMake Domain roles differ from Sphinx and reStructuredText
  212. convention in that the form ``a<b>``, without a space preceding ``<``,
  213. is interpreted as a name instead of link text with an explicit target.
  214. This is necessary because we use ``<placeholders>`` frequently in
  215. object names like ``OUTPUT_NAME_<CONFIG>``. The form ``a <b>``,
  216. with a space preceding ``<``, is still interpreted as a link text
  217. with an explicit target.
  218. Style
  219. -----
  220. Style: Section Headers
  221. ^^^^^^^^^^^^^^^^^^^^^^
  222. When marking section titles, make the section decoration line as long as
  223. the title text. Use only a line below the title, not above. For
  224. example:
  225. .. code-block:: rst
  226. Title Text
  227. ----------
  228. Capitalize the first letter of each non-minor word in the title.
  229. The section header underline character hierarchy is
  230. * ``#``: Manual group (part) in the master document
  231. * ``*``: Manual (chapter) title
  232. * ``=``: Section within a manual
  233. * ``-``: Subsection or `CMake Domain`_ object document title
  234. * ``^``: Subsubsection or `CMake Domain`_ object document section
  235. * ``"``: Paragraph or `CMake Domain`_ object document subsection
  236. Style: Whitespace
  237. ^^^^^^^^^^^^^^^^^
  238. Use two spaces for indentation. Use two spaces between sentences in
  239. prose.
  240. Style: Line Length
  241. ^^^^^^^^^^^^^^^^^^
  242. Prefer to restrict the width of lines to 75-80 columns. This is not a
  243. hard restriction, but writing new paragraphs wrapped at 75 columns
  244. allows space for adding minor content without significant re-wrapping of
  245. content.
  246. Style: Prose
  247. ^^^^^^^^^^^^
  248. Use American English spellings in prose.
  249. Style: Starting Literal Blocks
  250. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  251. Prefer to mark the start of literal blocks with ``::`` at the end of
  252. the preceding paragraph. In cases where the following block gets
  253. a ``code-block`` marker, put a single ``:`` at the end of the preceding
  254. paragraph.
  255. Style: CMake Command Signatures
  256. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  257. Command signatures should be marked up as plain literal blocks, not as
  258. cmake ``code-blocks``.
  259. Signatures are separated from preceding content by a section header.
  260. That is, use:
  261. .. code-block:: rst
  262. ... preceding paragraph.
  263. Normal Libraries
  264. ^^^^^^^^^^^^^^^^
  265. ::
  266. add_library(<lib> ...)
  267. This signature is used for ...
  268. Signatures of commands should wrap optional parts with square brackets,
  269. and should mark list of optional arguments with an ellipsis (``...``).
  270. Elements of the signature which are specified by the user should be
  271. specified with angle brackets, and may be referred to in prose using
  272. ``inline-literal`` syntax.
  273. Style: Boolean Constants
  274. ^^^^^^^^^^^^^^^^^^^^^^^^
  275. Use "``OFF``" and "``ON``" for boolean values which can be modified by
  276. the user, such as :prop_tgt:`POSITION_INDEPENDENT_CODE`. Such properties
  277. may be "enabled" and "disabled". Use "``True``" and "``False``" for
  278. inherent values which can't be modified after being set, such as the
  279. :prop_tgt:`IMPORTED` property of a build target.
  280. Style: Inline Literals
  281. ^^^^^^^^^^^^^^^^^^^^^^
  282. Mark up references to keywords in signatures, file names, and other
  283. technical terms with ``inline-literal`` syntax, for example:
  284. .. code-block:: rst
  285. If ``WIN32`` is used with :command:`add_executable`, the
  286. :prop_tgt:`WIN32_EXECUTABLE` target property is enabled. That command
  287. creates the file ``<name>.exe`` on Windows.
  288. Style: Cross-References
  289. ^^^^^^^^^^^^^^^^^^^^^^^
  290. Mark up linkable references as links, including repeats.
  291. An alternative, which is used by wikipedia
  292. (`<http://en.wikipedia.org/wiki/WP:REPEATLINK>`_),
  293. is to link to a reference only once per article. That style is not used
  294. in CMake documentation.
  295. Style: Referencing CMake Concepts
  296. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  297. If referring to a concept which corresponds to a property, and that
  298. concept is described in a high-level manual, prefer to link to the
  299. manual section instead of the property. For example:
  300. .. code-block:: rst
  301. This command creates an :ref:`Imported Target <Imported Targets>`.
  302. instead of:
  303. .. code-block:: rst
  304. This command creates an :prop_tgt:`IMPORTED` target.
  305. The latter should be used only when referring specifically to the
  306. property.
  307. References to manual sections are not automatically created by creating
  308. a section, but code such as:
  309. .. code-block:: rst
  310. .. _`Imported Targets`:
  311. creates a suitable anchor. Use an anchor name which matches the name
  312. of the corresponding section. Refer to the anchor using a
  313. cross-reference with specified text.
  314. Imported Targets need the ``IMPORTED`` term marked up with care in
  315. particular because the term may refer to a command keyword
  316. (``IMPORTED``), a target property (:prop_tgt:`IMPORTED`), or a
  317. concept (:ref:`Imported Targets`).
  318. Where a property, command or variable is related conceptually to others,
  319. by for example, being related to the buildsystem description, generator
  320. expressions or Qt, each relevant property, command or variable should
  321. link to the primary manual, which provides high-level information. Only
  322. particular information relating to the command should be in the
  323. documentation of the command.
  324. Style: Referencing CMake Domain Objects
  325. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  326. When referring to `CMake Domain`_ objects such as properties, variables,
  327. commands etc, prefer to link to the target object and follow that with
  328. the type of object it is. For example:
  329. .. code-block:: rst
  330. Set the :prop_tgt:`AUTOMOC` target property to ``ON``.
  331. Instead of
  332. .. code-block:: rst
  333. Set the target property :prop_tgt:`AUTOMOC` to ``ON``.
  334. The ``policy`` directive is an exception, and the type us usually
  335. referred to before the link:
  336. .. code-block:: rst
  337. If policy :prop_tgt:`CMP0022` is set to ``NEW`` the behavior is ...
  338. However, markup self-references with ``inline-literal`` syntax.
  339. For example, within the :command:`add_executable` command
  340. documentation, use
  341. .. code-block:: rst
  342. ``add_executable``
  343. not
  344. .. code-block:: rst
  345. :command:`add_executable`
  346. which is used elsewhere.
  347. Modules
  348. =======
  349. The ``Modules`` directory contains CMake-language ``.cmake`` module files.
  350. Module Documentation
  351. --------------------
  352. To document CMake module ``Modules/<module-name>.cmake``, modify
  353. ``Help/manual/cmake-modules.7.rst`` to reference the module in the
  354. ``toctree`` directive, in sorted order, as::
  355. /module/<module-name>
  356. Then add the module document file ``Help/module/<module-name>.rst``
  357. containing just the line::
  358. .. cmake-module:: ../../Modules/<module-name>.cmake
  359. The ``cmake-module`` directive will scan the module file to extract
  360. reStructuredText markup from comment blocks that start in ``.rst:``.
  361. At the top of ``Modules/<module-name>.cmake``, begin with the following
  362. license notice:
  363. .. code-block:: cmake
  364. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  365. # file Copyright.txt or https://cmake.org/licensing for details.
  366. After this notice, add a *BLANK* line. Then, add documentation using
  367. a :ref:`Line Comment` block of the form:
  368. .. code-block:: cmake
  369. #.rst:
  370. # <module-name>
  371. # -------------
  372. #
  373. # <reStructuredText documentation of module>
  374. or a :ref:`Bracket Comment` of the form:
  375. ::
  376. #[[.rst:
  377. <module-name>
  378. -------------
  379. <reStructuredText documentation of module>
  380. #]]
  381. Any number of ``=`` may be used in the opening and closing brackets
  382. as long as they match. Content on the line containing the closing
  383. bracket is excluded if and only if the line starts in ``#``.
  384. Additional such ``.rst:`` comments may appear anywhere in the module file.
  385. All such comments must start with ``#`` in the first column.
  386. For example, a ``Modules/Findxxx.cmake`` module may contain:
  387. ::
  388. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  389. # file Copyright.txt or https://cmake.org/licensing for details.
  390. #.rst:
  391. # FindXxx
  392. # -------
  393. #
  394. # This is a cool module.
  395. # This module does really cool stuff.
  396. # It can do even more than you think.
  397. #
  398. # It even needs two paragraphs to tell you about it.
  399. # And it defines the following variables:
  400. #
  401. # * VAR_COOL: this is great isn't it?
  402. # * VAR_REALLY_COOL: cool right?
  403. <code>
  404. #[========================================[.rst:
  405. .. command:: xxx_do_something
  406. This command does something for Xxx::
  407. xxx_do_something(some arguments)
  408. #]========================================]
  409. macro(xxx_do_something)
  410. <code>
  411. endmacro()
  412. Test the documentation formatting by running
  413. ``cmake --help-module <module-name>``, and also by enabling the
  414. ``SPHINX_HTML`` and ``SPHINX_MAN`` options to build the documentation.
  415. Edit the comments until generated documentation looks satisfactory. To
  416. have a .cmake file in this directory NOT show up in the modules
  417. documentation, simply leave out the ``Help/module/<module-name>.rst``
  418. file and the ``Help/manual/cmake-modules.7.rst`` toctree entry.
  419. .. _`Find Modules`:
  420. Find Modules
  421. ------------
  422. A "find module" is a ``Modules/Find<PackageName>.cmake`` file to be loaded
  423. by the :command:`find_package` command when invoked for ``<PackageName>``.
  424. The primary task of a find module is to determine whether a package
  425. exists on the system, set the ``<PackageName>_FOUND`` variable to reflect
  426. this and provide any variables, macros and imported targets required to
  427. use the package. A find module is useful in cases where an upstream
  428. library does not provide a
  429. :ref:`config file package <Config File Packages>`.
  430. The traditional approach is to use variables for everything, including
  431. libraries and executables: see the `Standard Variable Names`_ section
  432. below. This is what most of the existing find modules provided by CMake
  433. do.
  434. The more modern approach is to behave as much like
  435. :ref:`config file packages <Config File Packages>` files as possible, by
  436. providing :ref:`imported target <Imported targets>`. This has the advantage
  437. of propagating :ref:`Target Usage Requirements` to consumers.
  438. In either case (or even when providing both variables and imported
  439. targets), find modules should provide backwards compatibility with old
  440. versions that had the same name.
  441. A FindFoo.cmake module will typically be loaded by the command::
  442. find_package(Foo [major[.minor[.patch[.tweak]]]]
  443. [EXACT] [QUIET] [REQUIRED]
  444. [[COMPONENTS] [components...]]
  445. [OPTIONAL_COMPONENTS components...]
  446. [NO_POLICY_SCOPE])
  447. See the :command:`find_package` documentation for details on what
  448. variables are set for the find module. Most of these are dealt with by
  449. using :module:`FindPackageHandleStandardArgs`.
  450. Briefly, the module should only locate versions of the package
  451. compatible with the requested version, as described by the
  452. ``Foo_FIND_VERSION`` family of variables. If ``Foo_FIND_QUIETLY`` is
  453. set to true, it should avoid printing messages, including anything
  454. complaining about the package not being found. If ``Foo_FIND_REQUIRED``
  455. is set to true, the module should issue a ``FATAL_ERROR`` if the package
  456. cannot be found. If neither are set to true, it should print a
  457. non-fatal message if it cannot find the package.
  458. Packages that find multiple semi-independent parts (like bundles of
  459. libraries) should search for the components listed in
  460. ``Foo_FIND_COMPONENTS`` if it is set , and only set ``Foo_FOUND`` to
  461. true if for each searched-for component ``<c>`` that was not found,
  462. ``Foo_FIND_REQUIRED_<c>`` is not set to true. The ``HANDLE_COMPONENTS``
  463. argument of ``find_package_handle_standard_args()`` can be used to
  464. implement this.
  465. If ``Foo_FIND_COMPONENTS`` is not set, which modules are searched for
  466. and required is up to the find module, but should be documented.
  467. For internal implementation, it is a generally accepted convention that
  468. variables starting with underscore are for temporary use only.
  469. Like all modules, find modules should be properly documented. To add a
  470. module to the CMake documentation, follow the steps in the `Module
  471. Documentation`_ section above.
  472. .. _`CMake Developer Standard Variable Names`:
  473. Standard Variable Names
  474. ^^^^^^^^^^^^^^^^^^^^^^^
  475. For a ``FindXxx.cmake`` module that takes the approach of setting
  476. variables (either instead of or in addition to creating imported
  477. targets), the following variable names should be used to keep things
  478. consistent between find modules. Note that all variables start with
  479. ``Xxx_`` to make sure they do not interfere with other find modules; the
  480. same consideration applies to macros, functions and imported targets.
  481. ``Xxx_INCLUDE_DIRS``
  482. The final set of include directories listed in one variable for use by
  483. client code. This should not be a cache entry.
  484. ``Xxx_LIBRARIES``
  485. The libraries to link against to use Xxx. These should include full
  486. paths. This should not be a cache entry.
  487. ``Xxx_DEFINITIONS``
  488. Definitions to use when compiling code that uses Xxx. This really
  489. shouldn't include options such as ``-DHAS_JPEG`` that a client
  490. source-code file uses to decide whether to ``#include <jpeg.h>``
  491. ``Xxx_EXECUTABLE``
  492. Where to find the Xxx tool.
  493. ``Xxx_Yyy_EXECUTABLE``
  494. Where to find the Yyy tool that comes with Xxx.
  495. ``Xxx_LIBRARY_DIRS``
  496. Optionally, the final set of library directories listed in one
  497. variable for use by client code. This should not be a cache entry.
  498. ``Xxx_ROOT_DIR``
  499. Where to find the base directory of Xxx.
  500. ``Xxx_VERSION_Yy``
  501. Expect Version Yy if true. Make sure at most one of these is ever true.
  502. ``Xxx_WRAP_Yy``
  503. If False, do not try to use the relevant CMake wrapping command.
  504. ``Xxx_Yy_FOUND``
  505. If False, optional Yy part of Xxx system is not available.
  506. ``Xxx_FOUND``
  507. Set to false, or undefined, if we haven't found, or don't want to use
  508. Xxx.
  509. ``Xxx_NOT_FOUND_MESSAGE``
  510. Should be set by config-files in the case that it has set
  511. ``Xxx_FOUND`` to FALSE. The contained message will be printed by the
  512. :command:`find_package` command and by
  513. ``find_package_handle_standard_args()`` to inform the user about the
  514. problem.
  515. ``Xxx_RUNTIME_LIBRARY_DIRS``
  516. Optionally, the runtime library search path for use when running an
  517. executable linked to shared libraries. The list should be used by
  518. user code to create the ``PATH`` on windows or ``LD_LIBRARY_PATH`` on
  519. UNIX. This should not be a cache entry.
  520. ``Xxx_VERSION``
  521. The full version string of the package found, if any. Note that many
  522. existing modules provide ``Xxx_VERSION_STRING`` instead.
  523. ``Xxx_VERSION_MAJOR``
  524. The major version of the package found, if any.
  525. ``Xxx_VERSION_MINOR``
  526. The minor version of the package found, if any.
  527. ``Xxx_VERSION_PATCH``
  528. The patch version of the package found, if any.
  529. The following names should not usually be used in CMakeLists.txt files, but
  530. are typically cache variables for users to edit and control the
  531. behaviour of find modules (like entering the path to a library manually)
  532. ``Xxx_LIBRARY``
  533. The path of the Xxx library (as used with :command:`find_library`, for
  534. example).
  535. ``Xxx_Yy_LIBRARY``
  536. The path of the Yy library that is part of the Xxx system. It may or
  537. may not be required to use Xxx.
  538. ``Xxx_INCLUDE_DIR``
  539. Where to find headers for using the Xxx library.
  540. ``Xxx_Yy_INCLUDE_DIR``
  541. Where to find headers for using the Yy library of the Xxx system.
  542. To prevent users being overwhelmed with settings to configure, try to
  543. keep as many options as possible out of the cache, leaving at least one
  544. option which can be used to disable use of the module, or locate a
  545. not-found library (e.g. ``Xxx_ROOT_DIR``). For the same reason, mark
  546. most cache options as advanced. For packages which provide both debug
  547. and release binaries, it is common to create cache variables with a
  548. ``_LIBRARY_<CONFIG>`` suffix, such as ``Foo_LIBRARY_RELEASE`` and
  549. ``Foo_LIBRARY_DEBUG``.
  550. While these are the standard variable names, you should provide
  551. backwards compatibility for any old names that were actually in use.
  552. Make sure you comment them as deprecated, so that no-one starts using
  553. them.
  554. A Sample Find Module
  555. ^^^^^^^^^^^^^^^^^^^^
  556. We will describe how to create a simple find module for a library
  557. ``Foo``.
  558. The first thing that is needed is a license notice.
  559. .. code-block:: cmake
  560. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  561. # file Copyright.txt or https://cmake.org/licensing for details.
  562. Next we need module documentation. CMake's documentation system requires you
  563. to follow the license notice with a blank line and then with a documentation
  564. marker and the name of the module. You should follow this with a simple
  565. statement of what the module does.
  566. .. code-block:: cmake
  567. #.rst:
  568. # FindFoo
  569. # -------
  570. #
  571. # Finds the Foo library
  572. #
  573. More description may be required for some packages. If there are
  574. caveats or other details users of the module should be aware of, you can
  575. add further paragraphs below this. Then you need to document what
  576. variables and imported targets are set by the module, such as
  577. .. code-block:: cmake
  578. # This will define the following variables::
  579. #
  580. # Foo_FOUND - True if the system has the Foo library
  581. # Foo_VERSION - The version of the Foo library which was found
  582. #
  583. # and the following imported targets::
  584. #
  585. # Foo::Foo - The Foo library
  586. If the package provides any macros, they should be listed here, but can
  587. be documented where they are defined. See the `Module
  588. Documentation`_ section above for more details.
  589. Now the actual libraries and so on have to be found. The code here will
  590. obviously vary from module to module (dealing with that, after all, is the
  591. point of find modules), but there tends to be a common pattern for libraries.
  592. First, we try to use ``pkg-config`` to find the library. Note that we
  593. cannot rely on this, as it may not be available, but it provides a good
  594. starting point.
  595. .. code-block:: cmake
  596. find_package(PkgConfig)
  597. pkg_check_modules(PC_Foo QUIET Foo)
  598. This should define some variables starting ``PC_Foo_`` that contain the
  599. information from the ``Foo.pc`` file.
  600. Now we need to find the libraries and include files; we use the
  601. information from ``pkg-config`` to provide hints to CMake about where to
  602. look.
  603. .. code-block:: cmake
  604. find_path(Foo_INCLUDE_DIR
  605. NAMES foo.h
  606. PATHS ${PC_Foo_INCLUDE_DIRS}
  607. PATH_SUFFIXES Foo
  608. )
  609. find_library(Foo_LIBRARY
  610. NAMES foo
  611. PATHS ${PC_Foo_LIBRARY_DIRS}
  612. )
  613. If you have a good way of getting the version (from a header file, for
  614. example), you can use that information to set ``Foo_VERSION`` (although
  615. note that find modules have traditionally used ``Foo_VERSION_STRING``,
  616. so you may want to set both). Otherwise, attempt to use the information
  617. from ``pkg-config``
  618. .. code-block:: cmake
  619. set(Foo_VERSION ${PC_Foo_VERSION})
  620. Now we can use :module:`FindPackageHandleStandardArgs` to do most of the
  621. rest of the work for us
  622. .. code-block:: cmake
  623. include(FindPackageHandleStandardArgs)
  624. find_package_handle_standard_args(Foo
  625. FOUND_VAR Foo_FOUND
  626. REQUIRED_VARS
  627. Foo_LIBRARY
  628. Foo_INCLUDE_DIR
  629. VERSION_VAR Foo_VERSION
  630. )
  631. This will check that the ``REQUIRED_VARS`` contain values (that do not
  632. end in ``-NOTFOUND``) and set ``Foo_FOUND`` appropriately. It will also
  633. cache those values. If ``Foo_VERSION`` is set, and a required version
  634. was passed to :command:`find_package`, it will check the requested version
  635. against the one in ``Foo_VERSION``. It will also print messages as
  636. appropriate; note that if the package was found, it will print the
  637. contents of the first required variable to indicate where it was found.
  638. At this point, we have to provide a way for users of the find module to
  639. link to the library or libraries that were found. There are two
  640. approaches, as discussed in the `Find Modules`_ section above. The
  641. traditional variable approach looks like
  642. .. code-block:: cmake
  643. if(Foo_FOUND)
  644. set(Foo_LIBRARIES ${Foo_LIBRARY})
  645. set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
  646. set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
  647. endif()
  648. If more than one library was found, all of them should be included in
  649. these variables (see the `Standard Variable Names`_ section for more
  650. information).
  651. When providing imported targets, these should be namespaced (hence the
  652. ``Foo::`` prefix); CMake will recognize that values passed to
  653. :command:`target_link_libraries` that contain ``::`` in their name are
  654. supposed to be imported targets (rather than just library names), and
  655. will produce appropriate diagnostic messages if that target does not
  656. exist (see policy :policy:`CMP0028`).
  657. .. code-block:: cmake
  658. if(Foo_FOUND AND NOT TARGET Foo::Foo)
  659. add_library(Foo::Foo UNKNOWN IMPORTED)
  660. set_target_properties(Foo::Foo PROPERTIES
  661. IMPORTED_LOCATION "${Foo_LIBRARY}"
  662. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  663. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  664. )
  665. endif()
  666. One thing to note about this is that the ``INTERFACE_INCLUDE_DIRECTORIES`` and
  667. similar properties should only contain information about the target itself, and
  668. not any of its dependencies. Instead, those dependencies should also be
  669. targets, and CMake should be told that they are dependencies of this target.
  670. CMake will then combine all the necessary information automatically.
  671. The type of the :prop_tgt:`IMPORTED` target created in the
  672. :command:`add_library` command can always be specified as ``UNKNOWN``
  673. type. This simplifies the code in cases where static or shared variants may
  674. be found, and CMake will determine the type by inspecting the files.
  675. If the library is available with multiple configurations, the
  676. :prop_tgt:`IMPORTED_CONFIGURATIONS` target property should also be
  677. populated:
  678. .. code-block:: cmake
  679. if(Foo_FOUND)
  680. if (NOT TARGET Foo::Foo)
  681. add_library(Foo::Foo UNKNOWN IMPORTED)
  682. endif()
  683. if (Foo_LIBRARY_RELEASE)
  684. set_property(TARGET Foo::Foo APPEND PROPERTY
  685. IMPORTED_CONFIGURATIONS RELEASE
  686. )
  687. set_target_properties(Foo::Foo PROPERTIES
  688. IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
  689. )
  690. endif()
  691. if (Foo_LIBRARY_DEBUG)
  692. set_property(TARGET Foo::Foo APPEND PROPERTY
  693. IMPORTED_CONFIGURATIONS DEBUG
  694. )
  695. set_target_properties(Foo::Foo PROPERTIES
  696. IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
  697. )
  698. endif()
  699. set_target_properties(Foo::Foo PROPERTIES
  700. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  701. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  702. )
  703. endif()
  704. The ``RELEASE`` variant should be listed first in the property
  705. so that the variant is chosen if the user uses a configuration which is
  706. not an exact match for any listed ``IMPORTED_CONFIGURATIONS``.
  707. Most of the cache variables should be hidden in the ``ccmake`` interface unless
  708. the user explicitly asks to edit them.
  709. .. code-block:: cmake
  710. mark_as_advanced(
  711. Foo_INCLUDE_DIR
  712. Foo_LIBRARY
  713. )
  714. If this module replaces an older version, you should set compatibility variables
  715. to cause the least disruption possible.
  716. .. code-block:: cmake
  717. # compatibility variables
  718. set(Foo_VERSION_STRING ${Foo_VERSION})