cmake-developer.7.rst 32 KB

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