Add documentation for using Doxygen
[alexxy/gromacs.git] / doxygen / doxygen.md
1 Using Doxygen {#page_doxygen}
2 =============
3
4 This page documents how Doxygen is set up in the \Gromacs source tree,
5 as well as guidelines for adding new Doxygen comments.  Examples are included,
6 as well as tips and tricks for avoiding Doxygen warnings.  The guidelines focus
7 on C++ code and other new code that follows the new module layout.
8 Parts of the guidelines are still applicable to documenting older code (e.g.,
9 within `gmxlib/` or `mdlib/`), in particular the guidelines about formatting
10 the Doxygen comments and the use of \c \\internal.
11 \ref page_codelayout documents the overall structure of the documentation.
12
13 To get started quickly, you only need to read the first two sections to
14 understand the overall structure of the documentation, and take a look at the
15 examples at the end.  The remaining sections provide the details for
16 understanding why the examples are the way they are, and for more complex
17 situations.  They are meant more as a reference to look up solutions for
18 particular problems, rather than single-time reading.  To understand or find
19 individual Doxygen commands, you should first look at Doxygen documentation
20 (http://www.stack.nl/~dimitri/doxygen/manual/index.html).
21
22
23 Documentation flavors
24 =====================
25 The \Gromacs source tree is set up to produce three different levels of Doxygen
26 documentation:
27
28 1. Public API documentation (suffix `-user`), which documents functions and
29    classes exported from the library and intended for use outside the \Gromacs
30    library.
31 2. Library API documentation (suffix `-lib`), which additionally includes
32    functions and classes that are designed to be used from other parts of
33    \Gromacs, as well as some guidelines that are mostly of interest to
34    developers.
35 3. Full documentation (suffix `-full`), which includes (nearly) all (documented)
36    functions and classes in the source tree.
37
38 Each subsequent level of documentation includes all the documentation from the
39 levels above it.  The suffixes above refer to the suffixes of Doxygen input and
40 output files, as well as the name of the output directory.  When all the
41 flavors have been built, the front pages of the documentation contain links to
42 the other flavors, and explain the differences in more detail.
43
44 As a general guideline, the public API documentation should be kept free of
45 anything that a user linking against an unmodified \Gromacs does not see.
46 In other words, the public API documentation should mainly document the
47 contents of installed headers, and provide the necessary overview of using
48 those.  Also, verbosity requirements for the public API documentation are
49 higher: ideally, readers of the documentation could immediately start using the
50 API based on the documentation, without any need to look at the implementation.
51
52 Similarly, the library API documentation should not contain things that other
53 modules in \Gromacs can or should never call.  In particular, anything declared
54 locally in source files should be only available in the full documentation.
55 Also, if something is documented, and is not identified to be in the library
56 API, then it should not be necessary to call that function from outside its
57 module.
58
59
60 Building the documentation
61 ==========================
62
63 If you simply want to see up-to-date documentation, you can go to
64 http://jenkins.gromacs.org/job/Doxygen_Nightly_master/javadoc/html-lib/index.xhtml
65 to see the documentation for the current development version.
66 Jenkins also runs Doxygen for all changes pushed to Gerrit for master, and the
67 resulting documentation can be viewed from the link posted by Jenkins.  The
68 Doxygen build is marked as unstable if it introduces any Doxygen warnings.
69
70 You may need to build the documentation locally if you want to check the
71 results after adding/modifying a significant amount of comments.  This is
72 recommended in particular if you do not have much experience with Doxygen.
73 It is a good idea to build with all the different settings to see that the
74 result is what you want, and that you do not produce any warnings.
75 For local work, it is generally a good idea to set `GMX_COMPACT_DOXYGEN=ON`
76 CMake option, which removes some large generated graphs from the documentation
77 and speeds up the process significantly.
78
79 All files related to Doxygen reside in the `doxygen/` subdirectory in the source
80 and build trees.  In a freshly checked out source tree, this directory contains
81 various `Doxyfile-*.cmakein` files.  When you run CMake, corresponding files
82 `Doxyfile-user`, `Doxyfile-lib`, and `Doxyfile-full` are generated at the
83 corresponding location in the build tree.  There is also a
84 `Doxyfile-common.cmakein`, which is used to produce `Doxyfile-common`.
85 This file contains settings that are shared between all the input files.
86 `Doxyfile-compact` provides the extra settings for `GMX_COMPACT_DOXYGEN=ON`.
87
88 You can run Doxygen directly with one of the generated files (all output will
89 be produced under the current working directory), or build one of the
90 `doc-user`, `doc-lib`, and `doc-full` targets.  The targets run Doxygen in a
91 quieter mode and only show the warnings if there were any, and put the output
92 under `doxygen/` in the build tree.  The `doc-all` target builds all three
93 targets with less typing.
94
95 The generated documentation is put under `html-user/`, `html-lib/`, and/or
96 `html-full/` in the output directory.  Open `index.xhtml` file from one of
97 these subdirectories to start browsing (for \Gromacs developers, the
98 `html-lib/` is a reasonable starting point).  Log files with all Doxygen
99 warnings are also produced as `doxygen-*.log`, so you can inspect them after
100 the run.
101
102 You will need Doxygen 1.8.5 to build the current documentation.  Other versions
103 may work, but likely also produce warnings.  Additionally,
104 [graphviz](http://www.graphviz.org) and
105 [mscgen](http://www.mcternan.me.uk/mscgen/) are required for some graphs in
106 the documentation, and `latex` for formulas.  Working versions are likely
107 available through most package managers.  It is possible to build the
108 documentation without these tools, but you will see some errors and the related
109 figures will be missing from the documentation.
110
111
112 General guidelines for Doxygen markup
113 =====================================
114
115 Doxygen provides quite a few different alternative styles for documenting the
116 source code.  There are subtleties in how Doxygen treats the different types of
117 comments, and this also depends somewhat on the Doxygen configuration.  It is
118 possible to change the meaning of a comment by just changing the style of
119 comment it is enclosed in.  To avoid such issues, and to avoid needing to
120 manage all the alternatives, a single style throughout the source tree is
121 preferable.  When it comes to treatment of styles, \Gromacs uses the default
122 Doxygen configuration with one exception: `JAVADOC_AUTOBRIEF` is set ON to
123 allow more convenient one-line brief descriptions in C code.
124
125 Majority of existing comments in \Gromacs uses Qt-style comments (`/*!` and
126 `//!` instead of `/*``*` and `///`, \c \\brief instead of \c \@brief etc.),
127 so these should be used also for new documentation.  There is a single
128 exception for brief comments in C code; see below.
129
130 Similarly, existing comments use `/*!` for multiline comments in both C and
131 C++ code, instead of using multiple `//!` lines for C++.  The rationale is that
132 since the code will be a mixture of both languages for a long time, it is more
133 uniform to use similar style in both.  Also, since files will likely transition
134 from C to C++ gradually, rewriting the comments because of different style
135 issues should not generally be necessary.  Finally, multi-line `//!` comments
136 can work differently depending on Doxygen configuration, so it is better to
137 avoid that ambiguity.
138
139 When adding comments, ensure that a short brief description is always produced.
140 This is used in various listings, and should briefly explain the purpose of the
141 method without unnecessarily expanding those lists.
142 The basic guideline is to start all comment blocks with \c \\brief (possibly
143 after some other Doxygen commands).
144 If you want to avoid the \c \\brief for one-liners, you can use `//!`, but the
145 description must fit on a single line; otherwise, it is not interpreted as a
146 brief comment.  Note in particular that a simple `/*!` without a \c \\brief
147 does not produce a brief description.
148 Also note that \c \\brief marks the whole following paragraph as a brief
149 description, so you should insert an empty line after the intended brief
150 description.
151
152 In C code, `//` comments must be avoided because some compilers do not like
153 them.  If you want to avoid the \c \\brief for one-liners in C code, use
154 `/*``*` instead of `//!`.  If you do this, the brief description should not
155 contain unescaped periods except at the end.  Because of this, you should
156 prefer `//!` in C++ code.
157
158 Put the documentation comments in the header file that contains the
159 declaration, if such a header exists.
160 Implementation-specific comments that do not influence how a method
161 is used can go into the source file, just before the method definition, with an
162 \c \\internal tag in the beginning of the comment block.  Doxygen-style comments
163 within functions are not generally usable.
164
165 At times, you may need to exclude some part of a header or a source file such
166 that Doxygen does not see it at all.  In general, you should try to avoid this,
167 but it may be necessary to remove some functions that you do not want to appear
168 in the public API documentation, and which would generate warnings if left
169 undocumented, or to avoid Doxygen warnings from code it does not understand.
170 Prefer \c \\cond and \c \\endcond to do this.  If \c \\cond does not work for
171 you, you can also use \c \#ifndef `DOXYGEN`.  If you exclude a class method in
172 a header, you also need to exclude it in the source code to avoid warnings.
173
174
175 \Gromacs specifics
176 =================
177
178 The general guidelines on the style of Doxygen comments were given above.
179 This section introduces \Gromacs specific constructs currently used in Doxygen
180 documentation, as well as how \Gromacs uses Doxygen groups to organize the
181 documentation.
182
183 Controlling documentation visibility
184 ------------------------------------
185
186 To control in which level of documentation a certain function appears, three
187 different mechanisms are used:
188 * Global Doxygen configuration.  This is mainly used to include
189   declarations local to source files only in the full documentation.
190   You can find the details from the `Doxyfile-*.cmakein` files, and some of
191   them are also mentioned below on individual code constructs.
192 * The standard Doxygen commands \c \\internal and \c \\endinternal mark the
193   documentation to be only extracted into the full documentation
194   (`INTERNAL_DOCS` is `ON` only for the full documentation).
195   In addition, \Gromacs specific custom Doxygen commands \c \\libinternal and
196   \c \\endlibinternal are provided, which only exclude the documentation from
197   the public API documentation.  These are implemented by expanding the
198   commands to either \c \\internal or to a no-op, depending on the
199   documentation level.
200 * Doxygen commands \c \\if and \c \\cond can be used with section names
201   `libapi` and `internal` to only include the documentation in library API and
202   the full documentation, respectively.  `libapi` is also defined in the full
203   documentation.  These are declared using `ENABLED_SECTIONS` in the Doxygen
204   configuration files.
205
206 Examples of locations where it is necessary to use these explicit commands are
207 given below in the sections on individual code constructs.
208
209 Modules as Doxygen groups
210 -------------------------
211
212 As described in \ref page_codelayout, each subdirectory under `src/gromacs/`
213 represents a _module_, i.e., a somewhat coherent collection of routines.
214 Doxygen cannot automatically generate a list of routines in a module; it only
215 extracts various alphabetical indexes that contain more or less all documented
216 functions and classes.  To help reading the documentation, the routines for a
217 module should be visible in one place.
218
219 \Gromacs uses Doxygen groups to achieve this: for each documented module, there
220 is a \c \\defgroup definition for the module, and all the relevant classes and
221 functions need to be manually added to this group using \c \\ingroup and
222 \c \\addtogroup.
223 The group page also provides a natural place for overview documentation about
224 the module, and can be navigated to directly from the "Modules" tab in the
225 generated documentation.
226
227 Public API and library API groups
228 ---------------------------------
229
230 In addition to the module groups, two fixed groups are provided:
231 `group_publicapi` and `group_libraryapi`.  Classes and files can be added to
232 these groups using \Gromacs specific custom \c \\inpublicapi and
233 \c \\inlibraryapi commands.  The generated group documentation pages are not
234 very useful, but annotated classes and files show the API definition under the
235 name, making this information more easily accessible.  These commands in
236 file-level comments are also used for some automatic intermodule dependency
237 validation (see below).
238
239 Note that functions, enumerations, and other entities that do not have a
240 separate page in the generated documentation can only belong to one group;
241 in such a case, the module group is preferred over the API group.
242
243 Automatic dependency checking
244 -----------------------------
245
246 The build system provides three targets, `depcheck`, `doccheck`, and
247 `depgraphs`, that depend on correct usage of the above commands, in particular
248 the visibility and API definitions in file-level comments.
249 These checks also provide some level of enforcement for rules about
250 dependencies between the modules, but currently the checks are not run
251 automatically.
252
253 The `depcheck` target checks for issues that either completely or subtly break
254 the installed headers: an installed header including a non-installed header, or
255 an installed header installing another header using a path that does not
256 resolve correctly when the headers are installed.  It also checks for
257 consistent usage of
258 \code
259 #include "..." // This should be used for Gromacs headers
260 \endcode
261 versus
262 \code
263 #include <...> // This should be used for system and external headers.
264 \endcode
265
266 The `doccheck` target additionally checks that if an included file is
267 documented, it is not used against the API definition specified in the
268 file-level comment.  For example, including a file without any API definition
269 from another module produces an error.  Another example of a check is that a
270 non-installed header with public documentation triggers an error.
271
272 The `depgraphs` target generates include dependency graphs.  One graph is
273 produced that shows all the modules under `src/gromacs/`, and their include
274 dependencies.  Additionally, a file-level graph is produced for each module,
275 showing the include dependencies within that module.  Currently, these are
276 mostly for eye candy, but they can also be used for analyzing problematic
277 dependencies to clean up the architecture.  A legend for the graphs is
278 currently included as comments in `admin/includedeps.py`.  The output is put in
279 `doxygen/depgraphs/`.
280
281 These targets require Python 2.7; the graph generation additionally requires
282 `graphviz`.
283
284
285 Documenting specific code constructs
286 ====================================
287
288 This section describes the techical details and some tips and tricks for
289 documenting specific code constructs such that useful documentation is
290 produced.  If you are wondering where to document a certain piece of
291 information, see the documentation structure section on \ref page_codelayout.
292 The focus of the documentation should be on the overview content: Doxygen pages
293 and the module documentation.  An experienced developer can relatively easily
294 read and understand individual functions, but the documentation should help
295 in getting the big picture.
296
297 Doxygen pages
298 -------------
299
300 The pages that are accessible through navigation from the front page are
301 written using Markdown and are located under `doxygen/`.  Each page should be
302 placed in the page hierarchy by making it a subpage of another page, i.e., it
303 should be referenced once using \c \\subpage.  `mainpage.md` is the root of the
304 hierarchy.
305
306 If you need to exclude a page from the public API docs, you need to add it to
307 an exclude list in `Doxyfile-user.cmakein`, and ensure that there are no
308 references to the page from public API documentation.  \c \\if `libapi` can be
309 used to add references in content that is otherwise public.
310 Generally, the pages should be on a high enough level and provide overview
311 content that is useful enough such that it is not necessary to exclude them
312 from the library API documentation.
313
314 Modules
315 -------
316
317 For each module, decide on a header file that is the most important one for
318 that module (if there is no self-evident header, it may be better to designate,
319 e.g., module-doc.h for this purpose, but this is currently not done for any
320 module).  This header should contain the \c \\defgroup definition for the
321 module.  The name of the group should be `module_`<em>name</em>, where _name_
322 is the name of the subdirectory that hosts the module.
323
324 The module should be added to an appropriate group (see `doxygen/misc.cpp` for
325 definitions) using \c \\ingroup to organize the "Modules" tab in the generated
326 documentation.
327
328 One or more contact persons who know about the contents of the module should be
329 listed using \c \\author commands.  This provides a point of contact if one
330 has questions.
331
332 Classes/structs
333 ---------------
334
335 Classes and structs in header files appear always in Doxygen documentation,
336 even if their enclosing file is not documented.  So start the documentation
337 blocks of classes that are not part of the public API with \c \\internal or
338 \c \\libinternal.  Classes declared locally in source files or in unnamed
339 namespaces only appear in the full documentation.
340
341 If a whole class is not documented, this does not currently generate any
342 warning.  The class is simply exluded from the documentation.  But if a member
343 of a documented class is not documented, a warning is generated.  Guidelines for
344 documenting free functions apply to methods of a class as well.
345
346 For base classes, the API classification (\c \\inpublicapi or
347 \c \\inlibraryapi) should be based on where the class is meant to be
348 subclassed.  The visibility (\c \\internal or \c \\libinternal), in contrast,
349 should reflect the API classification of derived classes such that the base
350 class documentation is always generated together with the derived classes.
351
352 For classes that are meant to be subclassed and have protected members, the
353 protected members should only appear at the documentation level where the class
354 is meant to be subclassed.  For example, if a class is meant to be subclassed
355 only within a module, the protected members should only appear in the
356 full documentation.  This can be accomplished using \c \\cond (note that you
357 will need to add the \c \\cond command also to the source files to hide the
358 same methods from Doxygen, otherwise you will get confusing warnings).
359
360 Methods/functions/enums/macros
361 ------------------------------
362
363 These items do not appear in the documentation unless their enclosing scope is
364 documented.  For class members, the scope is the class; otherwise, it is the
365 namespace if one exists, or the file.  An \c \\addtogroup can also define a
366 scope if the group has higher visibility than the scope outside it.
367 So if a function is not within a namespace (mostly applicable to C code) and
368 has the same visibility as its enclosing file, it is not necessary to add a
369 \c \\internal or \c \\libinternal.
370
371 Static functions are currently extracted for all documentation flavors to allow
372 headers to declare `static inline` functions (used in, for example, math code).
373 Functions in anonymous namespaces are only extracted into the full
374 documentation.  Together with the above rules, this means that you should avoid
375 putting a `static` function within a documented namespace, even within source
376 files, or it may inadvertently appear in the public API documentation.
377
378 If you want to exclude an item from the documentation, you need to put in
379 inside a \c \\cond block such that Doxygen does not see it.
380 Otherwise, a warning for an undocumented function is generated.  You need to
381 enclose both the declaration and the definition with \c \\cond.
382
383 Files
384 -----
385
386 Each documented file should start with a documentation block (right after the
387 copyright notice) that documents the file.  See the examples section for exact
388 formatting.  Things to note:
389 * Please do not specify the file name explicitly after \c \\file.  By default,
390   a file comment applies to the file it is contained in, and an explicit file
391   name only adds one more thing that can get out of date.
392 * \c \\brief cannot appear on the same line as the \c \\file, but should be on
393   the next line.
394 * \c \\internal or \c \\libinternal should indicate where the header is visible.
395   As a general guideline, all installed headers should appear in the public API
396   documentation, i.e., not contain these commands.  If nothing else, then to
397   document that it does not contain any public API functions.  Headers that
398   declare anything in the library API should be marked with \c \\libinternal,
399   and the rest with \c \\internal.
400 * All source files, as well as most test files, should be documented with
401   \c \\internal, since they do not provide anything to public or library API,
402   and this avoids unintentionally extracting things from the file into those
403   documentations.  Shared test files used in tests from other modules should be
404   marked with \c \\libinternal.
405 * \c \\inpublicapi or \c \\inlibraryapi should be used to indicate where the
406   header is meant to be directly included.
407 * As with files, one or more contact persons should be listed with \c \\author.
408   If you make significant modifications or additions to a file, consider adding
409   an \c \\author line for yourself.
410
411 Directories
412 -----------
413
414 Directory documentation does not typically contain useful information beyond a
415 possible brief description, since they correspond very closely to modules, and
416 the modules themselves are documented.  A brief description is still useful to
417 provide a high-level overview of the source tree on the generated "Files" page.
418 A reference to the module is typically sufficient as a brief description for a
419 directory.  All directories are currently documented in
420 `doxygen/directories.cpp`.
421
422
423 Examples
424 ========
425
426 Basic C++
427 ---------
428
429 Here is an example of documenting a C++ class and its containing header file.
430 Comments in the code and the actual documentation explain the used Doxygen
431 constructs.
432
433 \includelineno doxygen-example.cpp
434
435 Basic C
436 -------
437
438 Here is another example of documenting a C header file (so avoiding all
439 C++-style comments), and including free functions.  It also demonstrates the use
440 of \c \\addtogroup to add multiple functions into a module group without repeated
441 \c \\ingroup tags.
442
443 \includelineno doxygen-example.c
444
445 Scoping and visibility rules
446 ----------------------------
447
448 The rules where Doxygen expects something to be documented, and when are
449 commands like \c \\internal needed, can be complex.  The examples below
450 describe some of the pitfalls.
451
452 \includelineno doxygen-example-scoping.cpp
453
454 Module documentation
455 --------------------
456
457 Documenting a new module should place a comment like this in a central header
458 for the module, such that the "Modules" tab in the generated documentation can
459 be used to navigate to the module.
460
461 \includelineno doxygen-example-module.cpp
462
463 Common mistakes
464 ---------------
465
466 The most common mistake, in particular in C code, is to forget to document the
467 file.  This causes Doxygen to ignore most of the comments in the file, so it
468 does not validate the contents of the comments either, nor is it possible to
469 actually check how the generated documentation looks like.
470
471 The following example shows some other common mistakes that do not produce
472 correct documentation.  The issues are explained in normal comments above each
473 code fragment.
474
475 \includelineno doxygen-example-issues.cpp
476
477 Existing code
478 -------------
479
480 More examples you can find by looking at existing code in the source tree.  In
481 particular new C++ code such as that in the `src/gromacs/analysisdata/` and
482 `src/gromacs/options/` subdirectories contains a large amount of code
483 documented mostly along these guidelines.  Some comments in
484 `src/gromacs/selection/` (in particular, any C-like code) predate the
485 introduction of these guidelines, so those are not the best examples.