#!/usr/bin/env python
def build(ctx):

    # Create the RESFORT library. This is build in addition to the RESFORT
    # executable to allow for the use of an F2PY interface to the core
    # functions as well.
    src = ['shared/shared_constants.f90', 'shared/shared_containers.f90',
        'shared/shared_lapack_interfaces.f90', 'record/record_warning.f90',
        'shared/shared_auxiliary.f90',
        'shared/shared_utilities.f90',
        'solve/solve_auxiliary.f90', 'solve/solve_fortran.f90',
        'evaluate/evaluate_auxiliary.f90', 'evaluate/evaluate_fortran.f90',
        'estimate/estimate_auxiliary.f90', 'estimate/estimate_fortran.f90',
        'record/record_estimation.f90', 'record/record_solution.f90', 'record/record_simulation.f90',
        'simulate/simulate_fortran.f90',
        'optimizers/newuoa.f90', 'optimizers/dfpmin.f90', 'resfort_library.f90']

    flags = ctx.env.FCFLAGS + ['-fPIC']
    ctx(features=['fc', 'fcstlib'], source=src, target='resfort', fcflags=flags)

    ctx.add_group()

    # Compile scalar RESFORT executable.
    ctx(features=['fc', 'fcprogram'], source='resfort_scalar.f90',
        target='resfort_scalar')

    # Compile the parallel RESFORT executables. We follow the master/slave
    # design pattern.
    if ctx.env['PARALLELISM']:

        master = ['parallelism/parallelism_constants.f90',
            'parallelism/parallelism_auxiliary.f90',
            'resfort_parallel.f90']

        ctx(features=['fc', 'fcprogram'], source=master,
            target='resfort_parallel_master')

        ctx.add_group()

        slave = ['parallelism/parallelism_constants.f90',
            'parallelism/parallelism_auxiliary.f90',
            'parallelism/parallelism_slave.f90']

        ctx(features=['fc', 'fcprogram'], source=slave,
             target='resfort_parallel_slave')
