Remove mdrun-only build configuration
[alexxy/gromacs.git] / docs / dev-manual / build-system.rst
1 .. highlight:: bash
2
3 Build system overview
4 =====================
5
6 The |Gromacs| build system uses CMake (version
7 |CMAKE_MINIMUM_REQUIRED_VERSION| or newer is required) to generate the
8 actual build system for the build tool choosen by the user.  See CMake
9 documentation for general introduction to CMake and how to use it.  This
10 documentation focuses on how the |Gromacs| build system is organized and
11 implemented, and what features it provides to developers (some of which may be
12 of interest to advanced users).
13
14 Most developers use ``make`` or ``ninja`` as the underlying build system, so
15 there can be parts of the build system that are specifically designed for
16 command-line consumption with these tools, and may not work ideally with other
17 environments, but basic building should be possible with all the environments
18 supported by CMake.
19
20 Also, the build system and version control is designed for out-of-source
21 builds.  In-source builds mostly work (there are a few custom targets that do
22 not), but no particular effort has been put to, e.g., having :file:`.gitignore`
23 files that exclude all the build outputs, or to have the ``clean`` target
24 remove all possible build outputs.
25
26 Build types
27 -----------
28
29 Build types is a CMake concept that provides overall control of how
30 the build tools are used on the given platform to produce executable
31 code. These can be set in CMake in various ways, including on a
32 command line such as ``cmake -DCMAKE_BUILD_TYPE=Debug``. |Gromacs|
33 supports the following standard CMake build types:
34
35 **Release**
36   Fully optimized code intended for use in production simulation. This is the
37   default.
38
39 **Debug**
40   Compiled code intended for use with debugging tools, with low optimization levels
41   and debug information for symbols.
42
43 **RelWithDebInfo**
44   As Release, but with debug information for symbol names, which can help debugging
45   issues that only emerge in optimized code.
46
47 **MinSizeRel**
48   As Release, but optimized to minimize the size of the resulting executable. This
49   is never a concern for |Gromacs| installations, so should not be used, but
50   probably works.
51
52 Additionally, |Gromacs| provides the following build types for development and
53 testing. Their implementations can be found in ``cmake/gmxBuildTypeXXX.cmake``.
54
55 **Reference**
56   This build type compiles a version of |Gromacs| aimed solely at correctness. All
57   parallelization and optimization possibilities are disabled. This build type is
58   compiled with gcc 5 to generate the regression test reference values, against
59   which all other |Gromacs| builds are tested.
60
61 **RelWithAssert**
62   As Release, but removes ``-DNDEBUG`` from compiler command lines, which makes
63   all assertion statements active (and can have other safety-related side effects
64   in |Gromacs| and code upon which it depends)
65
66 **Profile**
67   As Release, but adds ``-pg`` for use with profiling tools. This is not
68   likely to be effective for profiling the performance of :ref:`gmx mdrun`, but can
69   be useful for the tools.
70
71 **TSAN**
72   Builds |Gromacs| for use with ThreadSanitzer in gcc and clang
73   (http://clang.llvm.org/docs/ThreadSanitizer.html) to detect
74   data races. This disables the use of atomics in ThreadMPI,
75   preferring the mutex-based implementation.
76
77 **ASAN**
78   Builds |Gromacs| for use with AddressSanitzer in gcc and
79   clang (http://clang.llvm.org/docs/AddressSanitizer.html) to
80   detect many kinds of memory mis-use. By default, AddressSanitizer
81   includes LeakSanitizer.
82
83 **MSAN**
84   Builds |Gromacs| for use with AddressSanitzer in clang
85   (http://clang.llvm.org/docs/MemorySanitizer.html) to detect
86   reads of unitialized memory. This functionality requires that
87   dependencies of the |Gromacs| build have been built in a compatible
88   way (roughly, static libraries with ``-g -fsanitize=memory
89   -fno-omit-frame-pointer``), which generally requires at least the C++
90   standard library to have been built specially. The path where the
91   includes and libraries for dependencies should be found for this
92   build type is set in the CMake cache variable
93   ``GMX_MSAN_PATH``. Only internal XDR and internal fftpack are
94   supported at this time.
95
96 For all of the sanitizer builds, to get readable stack traces, you may
97 need to ensure that the ``ASAN_SYMBOLIZER_PATH`` environment variable
98 (or your ``PATH``) include that of the ``llvm-symbolizer`` binary.
99
100 With some generators, CMake generates the build system for more than a
101 single ``CMAKE_BUILD_TYPE`` from one pass over the ``CMakeLists.txt``
102 files, so any code that uses ``CMAKE_BUILD_TYPE`` in
103 ``CMakeLists.txt`` directly will break. |Gromacs| does use such CMake
104 code, so we do not fully support all these build types in such
105 generators (which includes Visual Studio).
106
107 CMake cache variables
108 ---------------------
109
110 This section provides a (currently incomplete) list of cache variables that
111 developers or advanced users can set to affect what CMake generates and/or what
112 will get built.
113
114 .. todo::
115
116    Figure out where to document basic variables intended for user
117    consumption, and how does it relate to documentation here.
118
119 .. todo::
120
121    Document the remaining variables below, and identify any variables
122    missing from the list.
123
124 Compiler flags
125 ^^^^^^^^^^^^^^
126
127 Standard CMake mechanism for specifying the compiler flags is to use
128 ``CMAKE_C_FLAGS``/``CMAKE_CXX_FLAGS`` for flags that affect all build types,
129 and :samp:`CMAKE_C_FLAGS_{buildtype}`/:samp:`CMAKE_CXX_FLAGS_{buildtype}` for
130 flags that only affect a specific build type.  CMake provides some default flags.
131
132 |Gromacs| determines its own set of default flags, grouped into two categories:
133
134 * Generic flags that are appended to the above default CMake flag variables
135   (possibly for multiple build types), generally specifying optimization flags
136   to use and controlling compiler warnings.
137 * Specific flags for certain features that the build system determines to be
138   necessary for successful compilation.  One example is flags that determine
139   what SIMD instruction set the compiler is allowed to use/needs to support.
140
141 All of the above flags are only added after testing that they work with the
142 provided compiler.
143
144 There is one cache variable to control the behavior of automatic compiler flags:
145
146 .. cmake:: GMX_SKIP_DEFAULT_CFLAGS
147
148    If set ``ON``, the build system will not add any compiler flags
149    automatically (neither generic nor specific as defined above), and will skip
150    most linker flags as well.
151    The default flags that would have been added are instead printed out when
152    :command:`cmake` is run, and the user can set the flags themselves using the
153    CMake variables.
154    If ``OFF`` (the default), the flags are added as described above.
155
156 The code the determine the default generic flags is in
157 :file:`cmake/gmxCFlags.cmake`.
158 Code that sets the specific flags (e.g., SIMD flags) is in the main
159 :file:`CMakeLists.txt`; search for :cmake:`GMX_SKIP_DEFAULT_CFLAGS`.
160 The variables used there can be traced back to the locations where the actual
161 flags to use are determined.
162
163 Variables affecting compilation/linking
164 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
165
166 .. cmake:: GMX_BROKEN_CALLOC
167
168 .. cmake:: GMX_BUILD_FOR_COVERAGE
169
170    Special variable set ``ON`` by Jenkins when doing a build for the coverage
171    job.  Allows the build system to set options to produce as useful coverage
172    metrics as possible.  Currently, it disables all asserts to avoid them
173    showing up as poor conditional coverage.
174    Defaults to ``OFF``, and there should not be any need to change this in a
175    manual build.
176
177    .. todo:: This could likely be replaced by a (yet another) build type.
178
179 .. cmake:: GMX_BUILD_OWN_FFTW
180
181 .. cmake:: GMX_BUILD_SHARED_EXE
182
183 .. cmake:: GMX_COMPILER_WARNINGS
184
185    If set ``ON``, various compiler warnings are enabled for compilers that
186    Jenkins uses for verification.
187    Defaults to ``OFF`` when building from a source tarball so that users
188    compiling with versions not tested on Jenkins are not exposed to our rather
189    aggressive warning flags that can trigger a lot of warnings with, e.g., new
190    versions of the compilers we use.
191    When building from a git repository, defaults to ``ON``.
192
193 .. cmake:: GMX_CYCLE_SUBCOUNTERS
194
195    If set to ``ON``, enables performance subcounters that offer more
196    fine-grained mdrun performance measurement and evaluation than the default
197    counters. See :doc:`/user-guide/mdrun-performance` for the description of
198    subcounters which are available.
199    Defaults to ``OFF``.
200
201 .. cmake:: GMX_ENABLE_CCACHE
202
203     If set to ``ON``, attempts to set up the `ccache <https://ccache.samba.org>`_
204     caching compiler wrapper to speed up repeated builds.
205     The ``ccache`` executable is searched for with ``find_package()`` if CMake
206     is being run with a compatible build type.
207     If the executable is found and a compatible compiler is configured,
208     CMake launch wrapper scripts are set.
209     If enabled, the ``ccache`` executable location discovered by CMake must be
210     accessible during build, as well.
211     Defaults to ``OFF`` to minimize build system complexity.
212
213 .. cmake:: GMX_INSTALL_DATASUBDIR
214
215    Sets the subdirectory under CMAKE_INSTALL_DATADIR where GROMACS-specific
216    read-only architecture-independent data files are installed. The default
217    is ``gromacs``, which means the files will go under ``share/gromacs``.
218    To alter the ``share`` part, change CMAKE_INSTALL_DATADIR.
219    See :doc:`relocatable-binaries` for how this influences the build.
220
221 .. cmake:: GMX_DOUBLE
222
223    Many part of |Gromacs| are implemented in terms of "real" precision,
224    which is actually either a single- or double-precision type,
225    according to the value of this flag. Some parts of the code
226    deliberately use single- or double-precision types, and these are
227    unaffected by this setting. See reference manual for further
228    information.
229
230 .. cmake:: GMX_RELAXED_DOUBLE_PRECISION
231
232    Permit a double-precision configuration to compute some quantities
233    to single-precision accuracy. Particularly on architectures where
234    only double-precision SIMD is available (e.g. Sparc machines such
235    as the K computer), it is faster to default to ``GMX_DOUBLE=ON``
236    and use SIMD than to use ``GMX_DOUBLE=OFF`` and use no
237    SIMD. However, if the user does not need full double precision,
238    then some optimizations can achieve the equivalent of
239    single-precision results (e.g. fewer Newton-Raphson iterations for
240    a reciprocal square root computation).
241
242 .. cmake:: GMX_EXTRAE
243
244 .. cmake:: GMX_EXTERNAL_BLAS
245
246 .. cmake:: GMX_EXTERNAL_LAPACK
247
248 .. cmake:: GMX_EXTERNAL_TNG
249
250 .. cmake:: GMX_FFT_LIBRARY
251
252 .. cmake:: GMX_GIT_VERSION_INFO
253
254    Whether to generate version information dynamically from git for each build
255    (e.g., HEAD commit hash).
256    Defaults to ``ON`` if the build is from a git repository and :command:`git`
257    is found, otherwise ``OFF``.
258    If ``OFF``, static version information from
259    :file:`cmake/gmxVersionInfo.cmake` is used.
260
261 .. cmake:: GMX_GPU
262
263 .. cmake:: GMX_CLANG_CUDA
264
265    Use clang for compiling CUDA GPU code, both host and device.
266
267 .. cmake:: GMX_CUDA_CLANG_FLAGS
268
269     Pass additional CUDA-only compiler flags to clang using this variable.
270
271 .. cmake:: CMAKE_INSTALL_LIBDIR
272
273    Sets the installation directory for libraries (default is determined by
274    standard CMake package ``GNUInstallDirs``).
275    See :doc:`relocatable-binaries` for how this influences the build.
276
277 .. cmake:: GMX_LOAD_PLUGINS
278
279 .. cmake:: GMX_MPI
280
281 .. cmake:: GMX_OPENMP
282
283 .. cmake:: GMX_PREFER_STATIC_LIBS
284
285 .. cmake:: GMX_SIMD
286
287 .. cmake:: GMX_SOFTWARE_INVSQRT
288
289 .. cmake:: GMX_THREAD_MPI
290
291 .. cmake:: GMX_USE_RDTSCP
292
293 .. cmake:: GMX_USE_TNG
294
295 .. cmake:: GMX_VMD_PLUGIN_PATH
296
297 .. cmake:: GMX_X11
298
299 .. cmake:: GMX_XML
300
301    Currently, this option has no effect on the compilation or linking, since
302    there is no code outside the tests that would use :file:`libxml2`.
303
304 Variables affecting the ``all`` target
305 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
306
307 .. cmake:: BUILD_TESTING
308
309    Standard variable created by CTest that enables/disables all tests.
310    Defaults to ``ON``.
311
312 .. cmake:: GMX_BUILD_HELP
313
314    Controls handling of man pages and shell completions.  Possible values:
315
316    ``OFF`` (default for builds from release source distribution)
317      Man pages and shell completions are not generated as part of the ``all``
318      target, and only installed if compiling from a source package.
319    ``AUTO`` (default for builds from development version)
320      Shell completions are generated by executing the :file:`gmx` binary as
321      part of the ``all`` target.  If it fails, a message is printed, but the
322      build succeeds.
323      Man pages need to be generated manually by invoking the ``man`` target.
324      Man pages and shell completions are installed if they have been
325      successfully generated.
326    ``ON``
327      Works the same as ``AUTO``, except that if invoking the :file:`gmx` binary
328      fails, the build fails as well.
329
330 .. cmake:: GMX_DEVELOPER_BUILD
331
332    If set ``ON``, the ``all`` target will include also the test binaries using
333    Google Test (if :cmake:`GMX_BUILD_UNITTESTS` is ``ON``).
334    Also, :cmake:`GMX_COMPILER_WARNINGS` is always enabled.
335    In the future, other developer convenience features (as well as features
336    inconvenient for a general user) can be added to the set controlled by this
337    variable.
338
339 .. cmake:: GMX_CLANG_TIDY
340
341   `clang-tidy <http://releases.llvm.org/9.0.0/tools/clang/tools/extra/docs/clang-tidy/index.html>`_
342   is used for static code analysis and (some) automated fixing of issues detected. clang-tidy is easy to install. It is contained in
343   the llvm binary `package <http://releases.llvm.org/download.html#9.0.0>`_. Only
344   version 9.0.* with libstdc++<7 or libc++ is supported. Others might miss tests or give false positives.
345   It is run automatically in gitlab CI for each commit. Many checks have fixes which can automatically be
346   applied. To run it, the build has to be configured with
347   ``cmake -DGMX_CLANG_TIDY=ON -DCMAKE_BUILD_TYPE=Debug``.
348   Any ``CMAKE_BUILD_TYPE`` which enables asserts (e.g. ASAN) works. Such a configured build will
349   run both the compiler as well as clang-tidy when building. The name of the clang-tidy executable is set with
350   ``-DCLANG_TIDY=...``, and the full path to it can be set with ``-DCLANG_TIDY_EXE=...``.
351   To apply the automatic fixes to the issue identified clang-tidy should be run seperately (running clang-tidy
352   with ``-fix-errors`` as part of the build can corrupt header files). To fix a specific file run
353   ``clang-tidy -fix-errors -header-filter '.*' {file}``, to fix all files in parallel
354   ``run-clang-tidy.py -fix -header-filter '.*' '(?<!/selection/parser\.cpp|selection/scanner\.cpp)$'``,
355   and to fix all modified files ``run-clang-tidy.py -fix -header-filter '.*' $(git diff HEAD --name-only)``.
356   The run-clang-tidy.py script is in the
357   ``share/clang/`` subfolder of the llvm distribution. ``clang-tidy`` has to be able to find the
358   ``compile_commands.json`` file. Either run from the build folder or add a symlink to the source folder.
359   :cmake:`GMX_ENABLE_CCACHE` does not work with clang-tidy.
360
361 Variables affecting special targets
362 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
363
364 .. cmake:: GMXAPI
365
366     If set ``ON``, the additional ``gmxapi`` C++ library is configured and the
367     ``gmxapi`` headers will be installed. Provides the additional build tree
368     targets ``gmxapi-cppdocs`` and ``gmxapi-cppdocs-dev`` when Doxygen is
369     available. Also exports CMake configuration files for ``gmxapi`` that allow
370     ``find_package(gmxapi)`` to import the ``Gromacs::gmxapi`` CMake target in
371     client projects that search the GROMACS installation root.
372
373 .. cmake:: GMX_BUILD_MANUAL
374
375    If set ``ON``, CMake detection for LaTeX and other prerequisites for the
376    reference PDF manual is done, and the ``manual`` target for building the
377    manual is generated.
378    If ``OFF`` (the default), all detection is skipped and the manual cannot be
379    built.
380
381    .. todo:: Consider if this is really necessary, or if we could just use
382       GMX_DEVELOPER_BUILD.
383
384 .. cmake:: GMX_BUILD_TARBALL
385
386    If set ``ON``, ``-dev`` suffix is stripped off from version strings and some
387    other version info logic is adjusted such that the man pages and other
388    documentation generated from this build is suitable for releasing (on the
389    web page and/or in the source distribution package).
390    Defaults to ``OFF``.
391
392 .. cmake:: GMX_BUILD_UNITTESTS
393
394    If ``ON``, test binaries using Google Test are built (either as the separate
395    ``tests`` targer, or also as part of the ``all`` target, depending on
396    :cmake:`GMX_DEVELOPER_BUILD`).  All dependencies required for building the
397    tests (Google Test and Google Mock frameworks, and tinyxml2) are
398    included in :file:`src/external/`.
399    Defaults to ``ON`` if :cmake:`BUILD_TESTING` is ``ON``.
400
401 .. cmake:: GMX_COMPACT_DOXYGEN
402
403    If set ``ON``, Doxygen configuration is changed to avoid generating large
404    dependency graphs, which makes it significantly faster to run Doxygen and
405    reduces disk usage.  This is typically useful when developing the
406    documentation to reduce the build times.
407    Defaults to ``OFF``.
408
409 .. cmake:: REGRESSIONTEST_DOWNLOAD
410
411    If set ``ON``, CMake will download the regression tests and extract them to
412    a local directory.  :cmake:`REGRESSIONTEST_PATH` is set to the extracted
413    tests.  Note that this happens during the configure phase, not during the
414    build.
415    After the download is done, the variable is automatically reset to ``OFF``
416    again to avoid repeated downloads.  Can be set to ``ON`` to download again.
417    Defaults to ``OFF``.
418
419 .. cmake:: REGRESSIONTEST_PATH
420
421    Path to extracted regression test suite matching the source tree (the
422    directory containing :file:`gmxtest.pl`)
423    If set, CTest tests are generated to run the regression tests.
424    Defaults to empty.
425
426 .. cmake:: SOURCE_MD5SUM
427
428    Sets the MD5 sum of the release tarball when generating the HTML
429    documentation.  It gets inserted into the download section of the HTML
430    pages.
431
432 External libraries
433 ------------------
434
435 .. todo::
436
437    List external libraries used (either from src/external/, or from the
438    system), whether they are required or optional, what functionality they
439    provide for Gromacs, and how to control their use.
440
441 Special targets
442 ---------------
443
444 In addition to the default ``all`` target, the generated build system has
445 several custom targets that are intended to be explicitly built to perform
446 various tasks (some of these may also run automatically).  There are various
447 other targets as well used internally by these, but those are typically not
448 intended to be invoked directly.
449
450 check
451    Builds all the binaries needed by the tests and runs the tests.  If some
452    types of tests are not available, shows a note to the user.
453    This is the main target intended for normal users to run the tests.
454    See :doc:`testutils`.
455 check-source
456    Runs a custom Python checker script to check for various source-level
457    issues.  Uses Doxygen XML documentation as well as rudimentary parsing of
458    some parts of the source files.
459    This target is used as part of the Jenkins documentation job.
460    All CMake code is currently in :file:`docs/doxygen/`.
461    See :doc:`gmxtree`.
462 completion
463    Runs the compiled :file:`gmx` executable to generate shell command-line
464    completion definitions.  This target is only added if
465    :cmake:`GMX_BUILD_HELP` is not ``OFF``, and it is run automatically as part
466    of the default ``all`` target.  See :cmake:`GMX_BUILD_HELP`.
467    All CMake code is in :file:`src/programs/`.
468 dep-graphs*
469    Builds include dependency graphs for the source files using :command:`dot`
470    from graphviz.
471    All CMake code is in :file:`docs/doxygen/`.
472    See :doc:`gmxtree`.
473 doxygen-*
474    Targets that run Doxygen to generate the documentation.
475    The ``doxygen-all`` target runs as part of the ``webpage`` target, which in
476    turn runs as part of the Jenkins documentation job.
477    All CMake code is in :file:`docs/doxygen/`.
478    See :doc:`doxygen`.
479 gmxapi-cppdocs
480     Builds API documentation for gmxapi. Useful to authors of client software.
481     Documentation is generated in :file:`docs/api-user` in the build directory.
482 gmxapi-cppdocs-dev
483     Extract documentation for gmxapi and GROMACS developers to
484     :file:`docs/api-dev`.
485 install-guide
486    Runs Sphinx to generate a plain-text INSTALL file for the source package.
487    The files is generated at :file:`docs/install-guide/text/`, from where it
488    gets put at the root of the source package by CPack.
489    All CMake code is in :file:`docs/`.
490 man
491    Runs Sphinx to generate man pages for the programs.
492    Internally, also runs the compiled :file:`gmx` executable to generate the
493    input files for Sphinx.
494    All CMake code is in :file:`docs/`.
495    See :cmake:`GMX_BUILD_HELP` for information on when the man pages are
496    installed.
497 manual
498    Runs LaTeX to generate the reference PDF manual.
499    All CMake code is in :file:`docs/manual/`.
500    See :cmake:`GMX_BUILD_MANUAL`.
501 package_source
502    Standard target created by CPack that builds a source package.
503    This target is used to generate the released source packages.
504 test
505    Standard target created by CTest that runs all the registered tests.
506    Note that this does not build the test binaries, only runs them, so you need
507    to first ensure that they are up-to-date.
508    See :doc:`testutils`.
509 tests
510    Builds all the binaries needed by the tests (but not ``gmx``).
511    See :doc:`testutils`.
512 webpage
513    Collection target that runs the other documentation targets to generate the
514    full set of HTML (and linked) documentaion.
515    This target is used as part of the Jenkins documentation job.
516    All CMake code is in :file:`docs/`.
517 webpage-sphinx
518    Runs Sphinx to generate most content for the HTML documentation (the set of
519    web pages this developer guide is also part of).
520    Internally, also runs the compiled :file:`gmx` executable to generate some
521    input files for Sphinx.
522    All CMake code is in :file:`docs/`.
523
524 Passing information to source code
525 ----------------------------------
526
527 The build system uses a few different mechanisms to influence the compilation:
528
529 * On the highest level, some CMake options select what files will be compiled.
530 * Some options are passed on the compiler command line using ``-D`` or
531   equivalent, such that they are available in every compilation unit.  This
532   should be used with care to keep the compiler command lines manageable.
533   You can find the current set of such defines with ::
534
535     git grep add_definitions
536
537 * A few header files are generated using CMake ``configure_file()`` and
538   included in the desired source files.  These files must exist for the
539   compilation to pass.  Only a few files use an ``#ifdef HAVE_CONFIG_H`` to
540   protect against inclusion in case the define is not set; this is used in
541   files that may get compiled outside the main build system.
542
543   :file:`buildinfo.h`
544     Contains various strings about the build environment, used mainly for
545     outputting version information to log files and when requested.
546   :file:`config.h`
547     Contains defines for conditional compilation within source files.
548   :file:`gmxpre-config.h`
549     Included by :file:`gmxpre.h` as the first thing in every source file.
550     Should only contain defines that are required before any other header for
551     correct operation.  For example, defines that affect the behavior of system
552     headers fall in this category.  See Doxygen documentation for
553     :file:`gmxpre.h`.
554
555   The above files are available through the INTERFACE_INCLUDE_DIR of
556   the ``common`` CMake target. I.e. to ``#include "config.h"``, be sure to
557   ``target_link_libraries(mymodule PRIVATE common)``
558
559   Additionally, the following file is generated by the build system:
560
561   :file:`baseversion-gen.cpp`
562     Provides definitions for declarations in :file:`baseversion_gen.h` for
563     version info output.  The contents are generated either from Git version
564     info, or from static version info if not building from a git repository.