Docker image builds: simplify doxygen version handling.
[alexxy/gromacs.git] / admin / containers / scripted_gmx_docker_builds.py
1 #!/usr/bin/env python
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
5 # Copyright (c) 2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 """
37 Generates a set of docker images used for running GROMACS CI on Gitlab.
38 The images are prepared according to a selection of build configuration targets
39 that hope to cover a broad enough scope of different possible systems,
40 allowing us to check compiler types and versions, as well as libraries used
41 for accelerators and parallel communication systems. Each combinations is
42 described as an entry in the build_configs dictionary, with the script
43 analysing the logic and adding build stages as needed.
44
45 Based on the example script provided by the NVidia HPCCM repository.
46
47 Authors:
48     * Paul Bauer <paul.bauer.q@gmail.com>
49     * Eric Irrgang <ericirrgang@gmail.com>
50     * Joe Jordan <e.jjordan12@gmail.com>
51
52 Usage::
53
54     $ python3 scripted_gmx_docker_builds.py --help
55     $ python3 scripted_gmx_docker_builds.py --format docker > Dockerfile && docker build .
56     $ python3 scripted_gmx_docker_builds.py | docker build -
57
58 """
59
60 import argparse
61 import collections
62 import typing
63 from distutils.version import StrictVersion
64
65 import hpccm
66 import hpccm.config
67 from hpccm.building_blocks.base import bb_base
68
69 try:
70     import utility
71 except ImportError:
72     raise RuntimeError(
73         'This module assumes availability of supporting modules in the same directory. Add the directory to '
74         'PYTHONPATH or invoke Python from within the module directory so module location can be resolved.')
75
76 # Basic packages for all final images.
77 _common_packages = ['build-essential',
78                     'ca-certificates',
79                     'ccache',
80                     'git',
81                     'gnupg',
82                     'libfftw3-dev',
83                     'libhwloc-dev',
84                     'liblapack-dev',
85                     'libx11-dev',
86                     'moreutils',
87                     'ninja-build',
88                     'rsync',
89                     'valgrind',
90                     'vim',
91                     'wget',
92                     'xsltproc']
93
94 # Extra packages needed to build Python installations from source.
95 _python_extra_packages = ['build-essential',
96                           'ca-certificates',
97                           'ccache',
98                           'curl',
99                           'git',
100                           'libbz2-dev',
101                           'libffi-dev',
102                           'liblzma-dev',
103                           'libncurses5-dev',
104                           'libncursesw5-dev',
105                           'libreadline-dev',
106                           'libsqlite3-dev',
107                           'libssl-dev',
108                           'llvm',
109                           'python-openssl',
110                           'vim',
111                           'wget',
112                           'zlib1g-dev']
113
114 # Extra packages needed for images for building documentation.
115 _docs_extra_packages = ['autoconf',
116                         'automake',
117                         'autopoint',
118                         'autotools-dev',
119                         'bison',
120                         'flex',
121                         'ghostscript',
122                         'graphviz',
123                         'help2man',
124                         'imagemagick',
125                         'libtool',
126                         'linkchecker',
127                         'mscgen',
128                         'm4',
129                         'texinfo',
130                         'texlive-latex-base',
131                         'texlive-latex-extra',
132                         'texlive-fonts-recommended',
133                         'texlive-fonts-extra']
134
135 # Supported Python versions for maintained branches.
136 _python_versions = ['3.6.10', '3.7.7', '3.8.2']
137
138 # Parse command line arguments
139 parser = argparse.ArgumentParser(description='GROMACS CI image creation script', parents=[utility.parser])
140
141 parser.add_argument('--format', type=str, default='docker',
142                     choices=['docker', 'singularity'],
143                     help='Container specification format (default: docker)')
144 parser.add_argument('--venvs', nargs='*', type=str, default=_python_versions,
145                     help='List of Python versions ("major.minor.patch") for which to install venvs. '
146                          'Default: {}'.format(' '.join(_python_versions)))
147
148
149 def base_image_tag(args) -> str:
150     # Check if we use CUDA images or plain linux images
151     if args.cuda is not None:
152         cuda_version_tag = 'nvidia/cuda:' + args.cuda + '-devel'
153         if args.centos is not None:
154             cuda_version_tag += '-centos' + args.centos
155         elif args.ubuntu is not None:
156             cuda_version_tag += '-ubuntu' + args.ubuntu
157         else:
158             raise RuntimeError('Logic error: no Linux distribution selected.')
159
160         base_image_tag = cuda_version_tag
161     else:
162         if args.centos is not None:
163             base_image_tag = 'centos:centos' + args.centos
164         elif args.ubuntu is not None:
165             base_image_tag = 'ubuntu:' + args.ubuntu
166         else:
167             raise RuntimeError('Logic error: no Linux distribution selected.')
168     return base_image_tag
169
170
171 def get_llvm_packages(args) -> typing.Iterable[str]:
172     # If we use the package version of LLVM, we need to install extra packages for it.
173     if (args.llvm is not None) and (args.tsan is None):
174         return ['libomp-dev',
175                 'clang-format-' + str(args.llvm),
176                 'clang-tidy-' + str(args.llvm)]
177     else:
178         return []
179
180
181 def get_compiler(args, tsan_stage: hpccm.Stage = None) -> bb_base:
182     # Compiler
183     if args.icc is not None:
184         raise RuntimeError('Intel compiler toolchain recipe not implemented yet')
185
186     if args.llvm is not None:
187         # Build our own version instead to get TSAN + OMP
188         if args.tsan is not None:
189             if tsan_stage is not None:
190                 compiler = tsan_stage.runtime(_from='tsan')
191             else:
192                 raise RuntimeError('No TSAN stage!')
193         # Build the default compiler if we don't need special support
194         else:
195             compiler = hpccm.building_blocks.llvm(extra_repository=True, version=args.llvm)
196
197     elif (args.gcc is not None):
198         compiler = hpccm.building_blocks.gnu(extra_repository=True,
199                                              version=args.gcc,
200                                              fortran=False)
201     else:
202         raise RuntimeError('Logic error: no compiler toolchain selected.')
203     return compiler
204
205
206 def get_mpi(args, compiler):
207     # If needed, add MPI to the image
208     if args.mpi is not None:
209         if args.mpi == 'openmpi':
210             use_cuda = False
211             if args.cuda is not None:
212                 use_cuda = True
213
214             if hasattr(compiler, 'toolchain'):
215                 return hpccm.building_blocks.openmpi(toolchain=compiler.toolchain, cuda=use_cuda, infiniband=False)
216             else:
217                 raise RuntimeError('compiler is not an HPCCM compiler building block!')
218
219         elif args.mpi == 'impi':
220             raise RuntimeError('Intel MPI recipe not implemented yet.')
221         else:
222             raise RuntimeError('Requested unknown MPI implementation.')
223     else:
224         return None
225
226
227 def get_opencl(args):
228     # Add OpenCL environment if needed
229     if (args.opencl is not None):
230         if args.opencl == 'nvidia':
231             if (args.cuda is None):
232                 raise RuntimeError('Need Nvidia environment for Nvidia OpenCL image')
233
234             return hpccm.building_blocks.packages(ospackages=['nvidia-opencl-dev'])
235
236         elif args.opencl == 'intel':
237             return hpccm.building_blocks.packages(
238                     apt_ppas=['ppa:intel-opencl/intel-opencl'],
239                     ospackages=['opencl-headers', 'ocl-icd-libopencl1',
240                                 'ocl-icd-opencl-dev', 'intel-opencl-icd'])
241
242         elif args.opencl == 'amd':
243             # libelf1 is a necessary dependency for something in the ROCm stack,
244             # which they should set up, but seem to have omitted.
245             return hpccm.building_blocks.packages(
246                     apt_keys=['http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key'],
247                     apt_repositories=['deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ xenial main'],
248                     ospackages=['ocl-icd-libopencl1', 'ocl-icd-opencl-dev', 'opencl-headers', 'libelf1', 'rocm-opencl'])
249     else:
250         return None
251
252
253 def get_clfft(args):
254     if (args.clfft is not None):
255         return hpccm.building_blocks.generic_cmake(
256             repository='https://github.com/clMathLibraries/clFFT.git',
257             prefix='/usr/local', recursive=True, branch=args.clfft, directory='clFFT/src')
258     else:
259         return None
260
261
262 def add_tsan_stage(input_args, output_stages: typing.Mapping[str, hpccm.Stage]):
263     """Isolate the expensive TSAN preparation stage.
264
265     This is a very expensive stage, but has few and disjoint dependencies, and
266     its output is easily compartmentalized (/usr/local) so we can isolate this
267     build stage to maximize build cache hits and reduce rebuild time, bookkeeping,
268     and final image size.
269     """
270     if not isinstance(output_stages, collections.abc.MutableMapping):
271         raise RuntimeError('Need output_stages container.')
272     tsan_stage = hpccm.Stage()
273     tsan_stage += hpccm.primitives.baseimage(image=base_image_tag(input_args), _as='tsan')
274
275     tsan_stage += hpccm.building_blocks.packages(ospackages=['git', 'ca-certificates', 'build-essential', 'cmake'])
276     # CMake will get duplicated later, but this is an expensive image, and it isn't worth optimizing
277     # out that duplication...
278     tsan_stage += hpccm.building_blocks.python(python3=True, python2=False, devel=False)
279
280     compiler_branch = 'release_' + str(input_args.llvm) + '0'
281     tsan_stage += hpccm.building_blocks.generic_cmake(
282         repository='https://git.llvm.org/git/llvm.git',
283         prefix='/usr/local', recursive=True, branch=compiler_branch,
284         cmake_opts=['-D CMAKE_BUILD_TYPE=Release', '-D LLVM_ENABLE_PROJECTS="clang;openmp;clang-tools-extra"',
285                     '-D LIBOMP_TSAN_SUPPORT=on'],
286         preconfigure=['export branch=' + compiler_branch,
287                       '(cd projects; git clone --depth=1 --branch $branch https://git.llvm.org/git/libcxx.git)',
288                       '(cd projects; git clone --depth=1 --branch $branch https://git.llvm.org/git/libcxxabi.git)',
289                       '(cd projects; git clone --depth=1 --branch $branch https://git.llvm.org/git/compiler-rt.git)',
290                       '(cd ..; git clone --depth=1 --branch $branch https://git.llvm.org/git/openmp.git)',
291                       '(cd ..; git clone --depth=1 --branch $branch https://git.llvm.org/git/clang.git)',
292                       '(cd ..; git clone --depth=1 --branch $branch https://git.llvm.org/git/clang-tools-extra.git)'],
293         postinstall=['ln -s /usr/local/bin/clang++ /usr/local/bin/clang++-' + str(input_args.llvm),
294                      'ln -s /usr/local/bin/clang-format /usr/local/bin/clang-format-' + str(input_args.llvm),
295                      'ln -s /usr/local/bin/clang-tidy /usr/local/bin/clang-tidy-' + str(input_args.llvm),
296                      'ln -s /usr/local/libexec/c++-analyzer /usr/local/bin/c++-analyzer-' + str(input_args.llvm)])
297     output_stages['tsan'] = tsan_stage
298
299
300 def prepare_venv(version: StrictVersion) -> typing.Sequence[str]:
301     """Get shell commands to set up the venv for the requested Python version."""
302     major = version.version[0]
303     minor = version.version[1]
304
305     pyenv = '$HOME/.pyenv/bin/pyenv'
306
307     py_ver = '{}.{}'.format(major, minor)
308     venv_path = '$HOME/venv/py{}'.format(py_ver)
309     commands = ['$({pyenv} prefix `{pyenv} whence python{py_ver}`)/bin/python -m venv {path}'.format(
310         pyenv=pyenv,
311         py_ver=py_ver,
312         path=venv_path
313     )]
314
315     commands.append('{path}/bin/python -m pip install --upgrade pip setuptools'.format(
316         path=venv_path
317     ))
318     # Install dependencies for building and testing gmxapi Python package.
319     # WARNING: Please keep this list synchronized with python_packaging/requirements-test.txt
320     # TODO: Get requirements.txt from an input argument.
321     commands.append("""{path}/bin/python -m pip install --upgrade \
322             'cmake>=3.13' \
323             'flake8>=3.7.7' \
324             'mpi4py>=3.0.3' \
325             'networkx>=2.0' \
326             'numpy>=1' \
327             'pip>=10.1' \
328             'pytest>=3.9' \
329             'setuptools>=42' \
330             'scikit-build>=0.10'""".format(path=venv_path))
331
332     return commands
333
334
335 def add_python_stages(building_blocks: typing.Mapping[str, bb_base],
336                       input_args,
337                       output_stages: typing.MutableMapping[str, hpccm.Stage]):
338     """Add the stage(s) necessary for the requested venvs.
339
340     One intermediate build stage is created for each venv (see --venv option).
341
342     Each stage partially populates Python installations and venvs in the home
343     directory. The home directory is collected by the 'pyenv' stage for use by
344     the main build stage.
345     """
346     if len(input_args.venvs) < 1:
347         raise RuntimeError('No venvs to build...')
348     if output_stages is None or not isinstance(output_stages, collections.abc.Mapping):
349         raise RuntimeError('Need a container for output stages.')
350
351     # Main Python stage that collects the environments from individual stages.
352     # We collect the stages individually, rather than chaining them, because the
353     # copy is a bit slow and wastes local Docker image space for each filesystem
354     # layer.
355     pyenv_stage = hpccm.Stage()
356     pyenv_stage += hpccm.primitives.baseimage(image=base_image_tag(input_args), _as='pyenv')
357     pyenv_stage += building_blocks['compiler']
358     pyenv_stage += building_blocks['mpi']
359     pyenv_stage += hpccm.building_blocks.packages(ospackages=_python_extra_packages)
360
361     for version in [StrictVersion(py_ver) for py_ver in sorted(input_args.venvs)]:
362         stage_name = 'py' + str(version)
363         stage = hpccm.Stage()
364         stage += hpccm.primitives.baseimage(image=base_image_tag(input_args), _as=stage_name)
365         stage += building_blocks['compiler']
366         stage += building_blocks['mpi']
367         stage += hpccm.building_blocks.packages(ospackages=_python_extra_packages)
368
369         # TODO: Use a non-root user for testing and Python virtual environments.
370         stage += hpccm.primitives.shell(commands=[
371             'curl https://pyenv.run | bash',
372             """echo 'export PYENV_ROOT="$HOME/.pyenv"' >> $HOME/.bashrc""",
373             """echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> $HOME/.bashrc""",
374             """echo 'eval "$(pyenv init -)"' >> $HOME/.bashrc""",
375             """echo 'eval "$(pyenv virtualenv-init -)"' >> $HOME/.bashrc"""])
376         pyenv = '$HOME/.pyenv/bin/pyenv'
377         commands = ['PYTHON_CONFIGURE_OPTS="--enable-shared" {pyenv} install -s {version}'.format(
378             pyenv=pyenv,
379             version=str(version))]
380         stage += hpccm.primitives.shell(commands=commands)
381
382         commands = prepare_venv(version)
383         stage += hpccm.primitives.shell(commands=commands)
384
385         # TODO: Update user home directory.
386         pyenv_stage += hpccm.primitives.copy(_from=stage_name, _mkdir=True, src=['/root/'],
387                                              dest='/root')
388
389         # Add the intermediate build stage to the sequence
390         output_stages[stage_name] = stage
391
392     # TODO: If we activate pyenv for login shells, the `global` "version" should be full-featured.
393     # # `version` should be a system installation or pyenv environment (or pyenv-virtualenv)
394     # # with the dependencies for all of the Python aspects of CMake-driven builds.
395     # commands = '{pyenv} global {version}'.format(
396     #             pyenv=pyenv,
397     #             version=...)
398     # pyenv_stage += hpccm.primitives.shell(commands=commands)
399
400     # Add the aggregating build stage to the sequence. This allows the main stage to copy
401     # the files in a single stage, potentially reducing the overall output image size.
402     output_stages['pyenv'] = pyenv_stage
403
404
405 def add_doxygen_stages(input_args,
406                        output_stages: typing.MutableMapping[str, hpccm.Stage]):
407     """Add appropriate stages according to doxygen input arguments."""
408     if input_args.doxygen is None:
409         return
410     if input_args.doxygen == '1.8.5':
411         doxygen_commit = 'ed4ed873ab0e7f15116e2052119a6729d4589f7a'
412         output_stages['main'] += hpccm.building_blocks.generic_autotools(
413             repository='https://github.com/westes/flex.git',
414             commit='f7788a9a0ecccdc953ed12043ccb59ca25714018',
415             prefix='/tmp/install-of-flex',
416             configure_opts=['--disable-shared'],
417             preconfigure=['./autogen.sh'])
418         output_stages['main'] += hpccm.building_blocks.generic_autotools(
419             repository='https://github.com/doxygen/doxygen.git',
420             commit=doxygen_commit,
421             prefix='',
422             configure_opts=[
423                 '--flex /tmp/install-of-flex/bin/flex',
424                 '--static'],
425             postinstall=[
426                 'sed -i \'/\"XPS\"/d;/\"PDF\"/d;/\"PS\"/d;/\"EPS\"/d;/disable ghostscript format types/d\' /etc/ImageMagick-6/policy.xml'])
427         output_stages['main'] += hpccm.building_blocks.pip(pip='pip3', packages=['sphinx==1.6.1'])
428     else:
429         raise RuntimeError('Unhandled doxygen version: {}'.format(input_args.doxygen))
430
431
432 def build_stages(args) -> typing.Iterable[hpccm.Stage]:
433     """Define and sequence the stages for the recipe corresponding to *args*."""
434
435     # A Dockerfile or Singularity recipe can have multiple build stages.
436     # The main build stage can copy files from previous stages, though only
437     # the last stage is included in the tagged output image. This means that
438     # large or expensive sets of build instructions can be isolated in
439     # local/temporary images, but all of the stages need to be output by this
440     # script, and need to occur in the correct order, so we create a sequence
441     # object early in this function.
442     stages = collections.OrderedDict()
443
444     # If we need the TSAN compilers, the early build is more involved.
445     if args.llvm is not None and args.tsan is not None:
446         add_tsan_stage(input_args=args, output_stages=stages)
447
448     # Building blocks are chunks of container-builder instructions that can be
449     # copied to any build stage with the addition operator.
450     building_blocks = collections.OrderedDict()
451
452     # These are the most expensive and most reusable layers, so we put them first.
453     building_blocks['compiler'] = get_compiler(args, tsan_stage=stages.get('tsan'))
454     building_blocks['mpi'] = get_mpi(args, building_blocks['compiler'])
455
456     # Install additional packages early in the build to optimize Docker build layer cache.
457     os_packages = _common_packages + get_llvm_packages(args)
458     if args.doxygen is not None:
459         os_packages += _docs_extra_packages
460     building_blocks['ospackages'] = hpccm.building_blocks.packages(ospackages=os_packages)
461
462     building_blocks['cmake'] = hpccm.building_blocks.cmake(eula=True, version=args.cmake)
463     building_blocks['opencl'] = get_opencl(args)
464     building_blocks['clfft'] = get_clfft(args)
465
466     # Add Python environments to MPI images, only, so we don't have to worry
467     # about whether to install mpi4py.
468     if args.mpi is not None and len(args.venvs) > 0:
469         add_python_stages(building_blocks=building_blocks, input_args=args, output_stages=stages)
470
471     # Create the stage from which the targeted image will be tagged.
472     stages['main'] = hpccm.Stage()
473
474     stages['main'] += hpccm.primitives.baseimage(image=base_image_tag(args))
475     for bb in building_blocks.values():
476         if bb is not None:
477             stages['main'] += bb
478
479     # We always add Python3 and Pip
480     stages['main'] += hpccm.building_blocks.python(python3=True, python2=False, devel=True)
481     stages['main'] += hpccm.building_blocks.pip(upgrade=True, pip='pip3',
482                                                 packages=['pytest', 'networkx', 'numpy'])
483
484     # Add documentation requirements (doxygen and sphinx + misc).
485     if args.doxygen is not None:
486         add_doxygen_stages(args, stages)
487
488     if 'pyenv' in stages and stages['pyenv'] is not None:
489         stages['main'] += hpccm.primitives.copy(_from='pyenv', _mkdir=True, src=['/root/.pyenv/'],
490                                                 dest='/root/.pyenv')
491         stages['main'] += hpccm.primitives.copy(_from='pyenv', _mkdir=True, src=['/root/venv/'],
492                                                 dest='/root/venv')
493         # TODO: Update user home directory.
494         # TODO: If we activate pyenv for login shells, the `global` "version" should be full-featured.
495         # stages['main'] += hpccm.primitives.copy(_from='pyenv', src=['/root/.bashrc'],
496         #                                         dest='/root/')
497
498     # Make sure that `python` resolves to something.
499     stages['main'] += hpccm.primitives.shell(commands=['test -x /usr/bin/python || '
500                                                        'update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && '
501                                                        '/usr/bin/python --version'])
502
503     # Note that the list of stages should be sorted in dependency order.
504     for build_stage in stages.values():
505         if build_stage is not None:
506             yield build_stage
507
508
509 if __name__ == '__main__':
510     args = parser.parse_args()
511
512     # Set container specification output format
513     hpccm.config.set_container_format(args.format)
514
515     container_recipe = build_stages(args)
516
517     # Output container specification
518     for stage in container_recipe:
519         print(stage)