cmake-developer.7.rst 33 KB

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