cmake-developer.7.rst 35 KB

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