cmake-developer.7.rst 34 KB

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