fab.tools.compiler module#

This file contains the base class for any compiler, and derived classes for gcc, gfortran, icc, ifort

class fab.tools.compiler.Compiler(name, exec_name, suite, category, mpi=False, compile_flag=None, output_flag=None, openmp_flag=None)#

Bases: CompilerSuiteTool

This is the base class for any compiler. It provides flags for

  • compilation only (-c),

  • naming the output file (-o),

  • OpenMP

Parameters:
  • name (str) – name of the compiler.

  • exec_name (Union[str, Path]) – name of the executable to start.

  • suite (str) – name of the compiler suite this tool belongs to.

  • category (Category) – the Category (C_COMPILER or FORTRAN_COMPILER).

  • mpi (bool) – whether the compiler or linker support MPI. (default: False)

  • compile_flag (Optional[str]) – the compilation flag to use when only requesting compilation (not linking). (default: None)

  • output_flag (Optional[str]) – the compilation flag to use to indicate the name of the output file (default: None)

  • openmp_flag (Optional[str]) – the flag to use to enable OpenMP. If no flag is specified, it is assumed that the compiler does not support OpenMP. (default: None)

get_hash()#
Return type:

int

Returns:

a hash based on the compiler name and version.

property openmp: bool#
Returns:

if the compiler supports openmp or not

property openmp_flag: str#
Returns:

The flag to enable OpenMP for this compiler.

compile_file(input_file, output_file, openmp, add_flags=None)#

Compiles a file. It will add the flag for compilation-only automatically, as well as the output directives. The current working directory for the command is set to the folder where the source file lives when compile_file is called. This is done to stop the compiler inserting folder information into the mod files, which would cause them to have different checksums depending on where they live.

Parameters:
  • input_file (Path) – the path of the input file.

  • output_file (Path) – the path of the output file.

  • opemmp – whether OpenMP should be used or not.

  • add_flags (Optional[List[str]]) – additional compiler flags. (default: None)

check_available()#

Checks if the compiler is available. While the method in the Tools base class would be sufficient (when using –version), in case of a compiler we also want to store the compiler version. So, re-implement check_available in a way that will automatically store the compiler version for later usage.

Return type:

bool

Returns:

whether the compiler is available or not. We do this by requesting the compiler version.

get_version()#

Try to get the version of the given compiler.

Expects a version in a certain part of the –version output, which must adhere to the n.n.n format, with at least 2 parts.

Return type:

Tuple[int, ...]

Returns:

a tuple of at least 2 integers, representing the version e.g. (6, 10, 1) for version ‘6.10.1’.

Raises:

RuntimeError – if the compiler was not found, or if it returned an unrecognised output from the version command.

run_version_command(version_command='--version')#

Run the compiler’s command to get its version.

Parameters:

version_command (Optional[str]) – The compiler argument used to get version info. (default: '--version')

Return type:

str

Returns:

The output from the version command.

Raises:

RuntimeError – if the compiler was not found, or raised an error.

parse_version_output(category, version_output)#

Extract the numerical part from the version output. Implemented in specific compilers.

Return type:

str

get_version_string()#

Get a string representing the version of the given compiler.

Return type:

str

Returns:

a string of at least 2 numeric version components, i.e. major.minor[.patch, …]

Raises:

RuntimeError – if the compiler was not found, or if it returned an unrecognised output from the version command.

class fab.tools.compiler.CCompiler(name, exec_name, suite, mpi=False, compile_flag=None, output_flag=None, openmp_flag=None)#

Bases: Compiler

This is the base class for a C compiler. It just sets the category of the compiler as convenience.

Parameters:
  • name (str) – name of the compiler.

  • exec_name (str) – name of the executable to start.

  • suite (str) – name of the compiler suite.

  • mpi (bool) – whether the compiler or linker support MPI. (default: False)

  • compile_flag (Optional[str]) – the compilation flag to use when only requesting compilation (not linking). (default: None)

  • output_flag (Optional[str]) – the compilation flag to use to indicate the name of the output file (default: None)

  • openmp_flag (Optional[str]) – the flag to use to enable OpenMP (default: None)

class fab.tools.compiler.FortranCompiler(name, exec_name, suite, mpi=False, compile_flag=None, output_flag=None, openmp_flag=None, module_folder_flag=None, syntax_only_flag=None)#

Bases: Compiler

This is the base class for a Fortran compiler. It is a compiler that needs to support a module output path and support for syntax-only compilation (which will only generate the .mod files).

Parameters:
  • name (str) – name of the compiler.

  • exec_name (str) – name of the executable to start.

  • suite (str) – name of the compiler suite.

  • mpi (bool) – whether the compiler or linker support MPI. (default: False)

  • compile_flag (Optional[str]) – the compilation flag to use when only requesting compilation (not linking). (default: None)

  • output_flag (Optional[str]) – the compilation flag to use to indicate the name of the output file (default: None)

  • module_folder_flag (Optional[str]) – the compiler flag to indicate where to store created module files. (default: None)

  • openmp_flag (Optional[str]) – the flag to use to enable OpenMP (default: None)

  • syntax_only_flag (Optional[str]) – flag to indicate to only do a syntax check. The side effect is that the module files are created. (default: None)

property has_syntax_only: bool#
Returns:

whether this compiler supports a syntax-only feature.

set_module_output_path(path)#

Sets the output path for modules.

Params path:

the path to the output directory.

compile_file(input_file, output_file, openmp, add_flags=None, syntax_only=False)#

Compiles a file.

Parameters:
  • input_file (Path) – the name of the input file.

  • output_file (Path) – the name of the output file.

  • add_flags (Optional[List[str]]) – additional flags for the compiler. (default: None)

  • syntax_only (bool) – if set, the compiler will only do a syntax check (default: False)

class fab.tools.compiler.GnuVersionHandling#

Bases: object

Mixin to handle version information from GNU compilers

parse_version_output(category, version_output)#

Extract the numerical part from a GNU compiler’s version output

Parameters:
  • name – the compiler’s name

  • category (Category) – the compiler’s Category

  • version_output (str) – the full version output from the compiler

Return type:

str

Returns:

the actual version as a string

Raises:

RuntimeError – if the output is not in an expected format.

class fab.tools.compiler.Gcc(name='gcc', exec_name='gcc', mpi=False)#

Bases: GnuVersionHandling, CCompiler

Class for GNU’s gcc compiler.

Parameters:
  • name (str) – name of this compiler. (default: 'gcc')

  • exec_name (str) – name of the executable. (default: 'gcc')

  • mpi (bool) – whether the compiler supports MPI. (default: False)

class fab.tools.compiler.MpiGcc#

Bases: Gcc

Class for a simple wrapper around gcc that supports MPI. It calls mpicc.

class fab.tools.compiler.Gfortran(name='gfortran', exec_name='gfortran', mpi=False)#

Bases: GnuVersionHandling, FortranCompiler

Class for GNU’s gfortran compiler.

Parameters:
  • name (str) – name of this compiler. (default: 'gfortran')

  • exec_name (str) – name of the executable. (default: 'gfortran')

  • mpi (bool) – whether the compiler supports MPI. (default: False)

class fab.tools.compiler.MpiGfortran#

Bases: Gfortran

Class for a simple wrapper around gfortran that supports MPI. It calls mpif90.

class fab.tools.compiler.IntelVersionHandling#

Bases: object

Mixin to handle version information from Intel compilers

parse_version_output(category, version_output)#

Extract the numerical part from an Intel compiler’s version output

Parameters:
  • name – the compiler’s name

  • version_output (str) – the full version output from the compiler

Return type:

str

Returns:

the actual version as a string

Raises:

RuntimeError – if the output is not in an expected format.

class fab.tools.compiler.Icc(name='icc', exec_name='icc', mpi=False)#

Bases: IntelVersionHandling, CCompiler

Class for the Intel’s icc compiler.

Parameters:
  • name (str) – name of this compiler. (default: 'icc')

  • exec_name (str) – name of the executable. (default: 'icc')

  • mpi (bool) – whether the compiler supports MPI. (default: False)

class fab.tools.compiler.MpiIcc#

Bases: Icc

Class for a simple wrapper around icc that supports MPI. It calls mpicc.

class fab.tools.compiler.Ifort(name='ifort', exec_name='ifort', mpi=False)#

Bases: IntelVersionHandling, FortranCompiler

Class for Intel’s ifort compiler.

Parameters:
  • name (str) – name of this compiler. (default: 'ifort')

  • exec_name (str) – name of the executable. (default: 'ifort')

  • mpi (bool) – whether the compiler supports MPI. (default: False)

class fab.tools.compiler.MpiIfort#

Bases: Ifort

Class for a simple wrapper around ifort that supports MPI. It calls mpif90.