cmake-developer.7.rst 35 KB

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