cmake-developer.7.rst 37 KB

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