Advanced Configuration#
A lot can be achieved with simple configurations but some of the more esoteric aspects of software building may require more esoteric Fab features.
Understanding the Environment#
Fab itself does not support any environment variables. But a user script can obviously query the environment and make use of environment variables, and provide their values to Fab.
Configuration Reuse#
If you find you have multiple build configurations with duplicated code, it could be helpful to factor out the commonality into a shared module. Remember, your build configuration is just a Python script at the end of the day.
In Fab’s example configurations, we have two build scripts to compile GCOM. Much of the configuration for these two scripts is identical. We extracted the common steps into gcom_build_steps.py and used them in build_gcom_ar.py and build_gcom_so.py.
Separate grab and build scripts#
If you are running many builds from the same source, you may wish to grab your repo in a separate script and call it less frequently.
In this case your grab script might only contain a single step. You could import your grab configuration to find out where it put the source.
1my_grab_config = BuildConfig(project_label='<project_label>')
2
3if __name__ == '__main__':
4 with my_grab_config:
5 fcm_export(my_grab_config, src='my_repo')
1from my_grab import my_grab_config
2
3
4if __name__ == '__main__':
5 with BuildConfig(project_label='<project_label>') as state:
6 grab_folder(state, src=my_grab_config.source_root),
Housekeeping#
You can add a cleanup_prebuilds()
step, where you can explicitly control how long to keep prebuild files.
This may be useful, for example, if you often switch between two versions
of your code and want to keep the prebuild speed benefits when building
both.
If you do not add your own cleanup_prebuild step, Fab will automatically run a default step which will remove old files from the prebuilds folder. It will remove all prebuild files that are not part of the current build by default.
PSyKAlight (PSyclone overrides)#
If you need to override a PSyclone output file with a handcrafted version
you can use the overrides_folder
argument to the
psyclone()
step.
This specifies a normal folder containing source files. The step will delete any files it creates if there’s a matching filename in the overrides folder.
Two-Stage Compilation#
- The
compile_fortran()
step compiles files in passes, with each pass identifying all the files which can be compiled next, and compiling them in parallel.
Some projects have bottlenecks in their compile order, where lots of files are stuck behind a single file which is slow to compile. Inspired by Busby, Fab can perform two-stage compilation where all the modules are built first in fast passes using the -fsyntax-only flag, and then all the slower object compilation can follow in a single pass.
The potential benefit is that the bottleneck is shortened, but there is a tradeoff with having to run through all the files twice. Some compilers might not have this capability.
Two-stage compilation is configured with the two_stage_flag argument to the Fortran compiler.
1compile_fortran(state, two_stage_flag=True)
Managed arguments#
As noted above, Fab manages a few command line arguments for some of the tools it uses.
Fortran Preprocessors#
Fab knows about some preprocessors which are used with Fortran, currently fpp
and cpp. It will ensure the -P
flag is present to disable line numbering
directives in the output, which is currently required for fparser to parse the
output.
Fortran Compilers#
Fab knows about some Fortran compilers (currently gfortran or ifort). It will make sure the -c flag is present to compile but not link.
If the compiler flag which sets the module folder is present, i.e. -J
for
gfortran or -module
for ifort, Fab will remove the flag, with a
notification, as it needs to use this flag to control the output location.
Compilation Profiles#
Fab supports compilation profiles. A compilation profile is essentially a simple string that represents a set of compilation and linking flags to be used. For example, an application might have profiles for full-debug, fast-debug, and production. Compilation profiles can inherit settings, for example fast-debug might inherit from full-debug, but add optimisations. Compilation profile names are not case sensitive.
Any flag for any tool can make use of a profile, but in many cases this is
not necessary (think of options for rsync
, git
, svn
, …). Fab will
internally create a dummy profile, indicated by an empty string “”. If no
profile is specified, this default profile will be used.
A profile is defined as follows:
1tr = ToolRepository()
2gfortran = tr.get_tool(Category.FORTRAN_COMPILER, "gfortran")
3
4gfortran.define_profile("base")
5gfortran.define_profile("fast-debug", inherit_from="base")
6gfortran.define_profile("full-debug", inherit_from="fast-debug")
7
8gfortran.add_flags(["-g", "-std=f2008"], "base")
9gfortran.add_flags(["-O2"], "fast-debug")
10gfortran.add_flags(["-O0", "-fcheck=all"], "full-debug")
Line 3 defines a profile called base
, which does not inherit from any
other profile. Next, a profile fast-debug
is defined, which is based
on base
. It will add the flags -O2
to the command line, together
with the inherited flags from base, it will be using -g -std=f2008 -O2
Finally, a full-debug
profile is declared, based on fast-debug
.
Due to the inheritance, it will be using the options
-g -std=f2008 -O2 -O0 -fcheck=all
. Note that because of the precedence
of compiler flags, the no-optimisation flag -O0
will overwrite the
valued of -O2
.
Tools that do not require a profile can omit the parameter when defining flags:
1git = config.tool_box[Category.GIT]
2git.add_flags(["-c", "foo.bar=123"])
This effectively adds the flags to the to the dummy profile, allowing them to be used by other Fab functions.
By default, the dummy profile ""
is not used as a base class for
any other profile. But it can be convenient to set this up to make
user scripts slightly easier. Here is an example of the usage
in LFRic, where at startup time a consistent set of profile modes are
defined for each compiler and linker:
1tr = ToolRepository()
2for compiler in (tr[Category.C_COMPILER] +
3 tr[Category.FORTRAN_COMPILER] +
4 tr[Category.LINKER]):
5 compiler.define_profile("base", inherit_from="")
6 for profile in ["full-debug", "fast-debug", "production"]:
7 compiler.define_profile(profile, inherit_from="base")
Line 5 defines a base
profile, which inherits from the dummy
profile. Then a set of three profiles are defined, each
inheriting from base
, and therefore in turn from the dummy profile.
Later, the Intel Fortran compiler and linker ifort
are setup as follows:
1tr = ToolRepository()
2ifort = tr.get_tool(Category.FORTRAN_COMPILER, "ifort")
3ifort.add_flags(["-stand", "f08"], "base")
4ifort.add_flags(["-g", "-traceback"], "base")
5ifort.add_flags(["-O0", "-ftrapuv"], "full-debug")
6ifort.add_flags(["-O2", "-fp-model=strict"], "fast-debug")
7ifort.add_flags(["-O3", "-xhost"], "production")
8
9linker = tr.get_tool(Category.LINKER, "linker-ifort")
10linker.add_lib_flags("yaxt", ["-lyaxt", "-lyaxt_c"])
11linker.add_post_lib_flags(["-lstdc++"])
The setup of the compiler does not use the dummy profile at all,
so it will stay empty. It is up to the user to decide how to use the
profiles, it would be entirely valid not to use the base
profile, but
instead to use the dummy. But when setting up the linker, no profile is specified.
So line 10 and 11 will set these flags for the dummy. Because of base
inheriting from the dummy, and any other profile inheriting from base
,
this means these linker flags will be used for all profiles. It would
be equally valid to define these flags for the base
profile:
1linker = tr.get_tool(Category.LINKER, "linker-ifort")
2linker.add_lib_flags("yaxt", ["-lyaxt", "-lyaxt_c"], "base")
3linker.add_post_lib_flags(["-lstdc++"], "base")
This design was chosen because the most common use case for profiles involves changing compiler flags. Linker flags are typically left unaltered, so it is more intuitive for a user to omit profile modes for the linker.
The advantage of supporting the profile modes for linker is that
you can specify profile modes that require additional linking options.
One example is GNU’s address sanitizer, which requires to
add the compilation option -fsanitize=address
, and the linker option
-static-libasan
.
1tr = ToolRepository()
2gfortran = tr.get_tool(Category.FORTRAN_COMPILER, "gfortran")
3...
4gfortran.define_profile("memory-debug", "full-debug")
5gfortran.add_flags(["-fsanitize=address"], "memory-debug")
6linker = tr.get_tool(Category.LINKER, "linker-gfortran")
7linker.add_post_lib_flags(["-static-libasan"], "memory-debug")
This way, by just changing the profile, compilation and linking will be affected consistently.
Tool arguments#
Sometimes it is necessary to pass additional arguments when we call a software tool.
Linker flags#
Probably the most common instance of the need to pass additional arguments is to specify 3rd party libraries at the link stage. The linker tool allow for the definition of library-specific flags: for each library, the user can specify the required linker flags for this library. In the linking step, only the name of the libraries to be linked is then required. The linker object will then use the required linking flags. Typically, a site-specific setup set (see for example MetOffice/lfric-baf) will specify the right flags for each site, and the application itself only needs to list the name of the libraries. This way, the application-specific Fab script is independent from any site-specific settings. Still, an application-specific script can also overwrite any site-specific setting, for example if a newer version of a dependency is to be evaluated.
The settings for a library are defined as follows:
1 tr = ToolRepository()
2 linker = tr.get_tool(Category.LINKER, "linker-ifort")
3
4 linker.add_lib_flags("yaxt", ["-L/some_path", "-lyaxt", "-lyaxt_c"])
5 linker.add_lib_flags("xios", ["-lxios"])
This will define two libraries called yaxt
and xios
. In the link step,
the application only needs to specify the name of the libraries required, e.g.:
1link_exe(state, libs=["yaxt", "xios"])
The linker will then use the specified options.
A linker object also allows to define options that should always be added, either as options before any library details, or at the very end. For example:
1 linker.add_pre_lib_flags(["-L/my/common/library/path"])
2 linker.add_post_lib_flags(["-lstdc++"])
The pre_lib_flags can be used to specify library paths that contain
several libraries only once, and this makes it easy to evaluate a different
set of libraries. Additionally, this can also be used to add common
linking options, e.g. Cray’s -Ktrap=fp
.
The post_lib_flags can be used for additional common libraries that need to be linked in. For example, if the application contains a dependency to C++ but it is using the Fortran compiler for linking, then the C++ libraries need to be explicitly added. But if there are several libraries depending on it, you would have to specify this several times (forcing the linker to re-read the library several times). Instead, you can just add it to the post flags once.
The linker step itself can also take optional flags:
1link_exe(state, flags=['-Ktrap=fp'])
These flags will be added to the very end of the the linker options, i.e. after any other library or post-lib flag. Note that the example above is not actually recommended to use, since the specified flag is only valid for certain linker, and a Fab application script should in general not hard-code flags for a specific linker. Adding the flag to the linker instance itself, as shown further above, is the better approach.
Path-specific flags#
For preprocessing and compilation, we sometimes need to specify flags per-file. These steps accept both common flags and path specific flags.
1...
2compile_fortran(
3 common_flags=['-O2'],
4 path_flags=[
5 AddFlags('$output/um/*', ['-I' + '/gcom'])
6 ],
7)
This will add -O2
to every invocation of the tool, but only add the
*/gcom*
include path when processing files in the
*<project workspace>/build_output/um*
folder.
Path matching is done using Python’s fnmatch.
The $output
is a template, see AddFlags
.
We can currently only add flags for a path.
Note
This can require some understanding of where and when files are placed in
the build_output folder: It will generally match the structure you’ve
created in *<project workspace>/source*
, with your grab steps.
Early steps like preprocessors generally read files from *source*
and
write to *build_output*
.
Later steps like compilers generally read files which are already in
*build_output*
.
For more information on where files end up see Folder Structure.
Folder Structure#
It may be useful to understand how Fab uses the Project Workspace and in particular where it creates files within it.
<your $FAB_WORKSPACE>
<project workspace>
source/
build_output/
*.f90 (preprocessed Fortran files)
*.mod (compiled module files)
_prebuild/
*.an (analysis results)
*.o (compiled object files)
*.mod (mod files)
metrics/
my_program
log.txt
The project workspace folder takes its name from the project label passed in to the build configuration.
The source folder is where grab steps place their files.
The build_output folder is where steps put their processed files.
For example, a preprocessor reads .F90
from source and writes .f90
to build_output.
The _prebuild folder contains reusable output. Files in this folder include a hash value in their filenames.
The metrics folder contains some useful stats and graphs. See Metrics.
C Pragma Injector#
The C pragma injector creates new C files with .prag
file extensions, in the
source folder. The C preprocessor looks for the output of this step by default.
If not found, it will fall back to looking for .c
files in the source
listing.
1...
2c_pragma_injector(state)
3preprocess_c(state)
4...
Custom Steps#
If you need a custom build step, you can create a function with the @step decorator.
Some example custom steps are included in the Fab testing configurations. For example a simple example was created for building JULES.
The root_inc_files()
step copies all .inc
files in the source tree into the root of the source tree, to make subsequent
preprocessing flags easier to configure.
That is a simple example that doesn’t need to interact with the Artefact Store. Sometimes inserting a custom step means inserting a new Artefact Collection into the flow of data between steps.
We can tell a subsequent step to read our new artefacts, instead of using it’s
default Artefacts Getter. We do this using the source
argument,
which most Fab steps accept. (See Collection names)
1@step
2def custom_step(state):
3 state.artefact_store['custom_artefacts'] = do_something(state.artefact_store['step 1 artefacts'])
4
5
6with BuildConfig(project_label='<project label>') as state:
7 fab_step1(state)
8 custom_step(state)
9 fab_step2(state, source=CollectionGetter('custom_artefacts'))
Steps have access to multiprocessing methods through the
run_mp()
helper function. This processes artefacts in parallel.
1@step
2def custom_step(state):
3 input_files = state.artefact_store['custom_artefacts']
4 results = run_mp(state, items=input_files, func=do_something)
Collection names#
Most steps allow the collections they read from and write to to be changed.
Let’s imagine we need to upgrade a build script, adding a custom step to prepare our Fortran files for preprocessing.
1find_source_files(state) # this was already here
2
3# instead of this
4# preprocess_fortran(state)
5
6# we now do this
7my_new_step(state, output_collection='my_new_collection')
8preprocess_fortran(state, source=CollectionGetter('my_new_collection'))
9
10analyse(state) # this was already here
Parser Workarounds#
Sometimes the parser used by Fab to understand source code can be unable to parse valid source files due to bugs or shortcomings. In order to still be able to build such code a number of possible work-arounds are presented.
Unrecognised Dependencies#
If a language parser is not able to recognise a dependency within a file, then Fab won’t know the dependency needs to be compiled.
For example, some versions of fparser don’t recognise a call on a one-line if statement.
We can manually add the dependency using the unreferenced_deps argument to
analyse()
.
Pass in the name of the called function. Fab will find the file containing this symbol and add it, and all its dependencies, to every Build Tree.
1...
2analyse(state, root_symbol='my_prog', unreferenced_deps=['my_func'])
3...
Unparsable Files#
If a language parser is not able to process a file at all, then Fab won’t know about any of its symbols and dependencies. This can sometimes happen to valid code which compilers are able to process, for example if the language parser is still maturing and can’t yet handle an uncommon syntax.
In this case we can manually give Fab the analysis results using the
special_measure_analysis_results argument to
analyse()
.
Pass in a list of FortranParserWorkaround
objects,
one for every file that can’t be parsed. Each object contains the symbol
definitions and dependencies found in one source file.
1...
2analyse(
3 config,
4 root_symbol='my_prog',
5 special_measure_analysis_results=[
6 FortranParserWorkaround(
7 fpath=Path(state.build_output / "path/to/file.f90"),
8 module_defs={'my_mod'}, symbol_defs={'my_func'},
9 module_deps={'other_mod'}, symbol_deps={'other_func'}),
10 ])
11...
In the above snippet we tell Fab that file.f90
defines a module called
my_mod
and a function called my_func
, and depends on a module called
other_mod
and a function called other_func
.
Custom Step#
An alternative approach for some problems is to write a custom step to modify the source so that the language parser can process it. Here’s a simple example, based on a real workaround where the parser gets confused by a variable called NameListFile.
1@step
2def my_custom_code_fixes(state):
3 fpath = state.source_root / 'path/to/file.F90'
4 in = open(fpath, "rt").read()
5 out = in.replace("NameListFile", "MyRenamedVariable")
6 open(fpath, "wt").write(out)
7
8with BuildConfig(project_label='<project_label>') as state:
9 # grab steps first
10 my_custom_code_fixes(state)
11 # find_source_files, preprocess, etc, afterwards
A more detailed treatment of Custom Steps is given elsewhere.