cmake-developer.7.rst 35 KB

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