## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
import sys
import subprocess

from waflib import Options
from waflib.Errors import WafError

def configure(conf):
    if Options.options.enable_mpi:
        # try to detect openmpi installation
        mpi = conf.check_cfg(path='mpic++', args='-showme',
                             package='', uselib_store='MPI', mandatory=False)
        if mpi:
            conf.env.append_value('DEFINES_MPI', 'NS3_OPENMPI')
        else:
            # try the MPICH2 flags
            mpi = conf.check_cfg(path='mpic++', args='-compile-info -link-info',
                                 package='', uselib_store='MPI', mandatory=False)
            if mpi:
                conf.env.append_value('DEFINES_MPI', 'NS3_MPICH')
        if mpi:
            conf.env.append_value('DEFINES_MPI', 'NS3_MPI')
            conf.env['ENABLE_MPI'] = True
            for libpath in conf.env.LIBPATH_MPI:
                if 'mpi' in libpath:
                    conf.env.append_value('LINKFLAGS_MPI', '-Wl,-rpath,'+libpath)
                    # Bug #2437, using OpenMPI 1.6.5 (possibly later versions)
                    # if upstream OpenMPI bug clears at some point, this
                    # can be removed 
                    # conf.env.append_value('CXXFLAGS', '-Wno-literal-suffix')
            conf.report_optional_feature("mpi", "MPI Support", True, '')            
        else:
            conf.report_optional_feature("mpi", "MPI Support", False, 'mpic++ not found')
            conf.env['MODULES_NOT_BUILT'].append('mpi')
    else:
        conf.report_optional_feature("mpi", "MPI Support", False, 'option --enable-mpi not selected')
        conf.env['MODULES_NOT_BUILT'].append('mpi')


def build(bld):
    # Don't do anything for this module if mpi's not enabled.
    if 'mpi' in bld.env['MODULES_NOT_BUILT']:
        return

    sim = bld.create_ns3_module('mpi', ['core', 'network'])
    sim.source = [
        'model/distributed-simulator-impl.cc',
        'model/granted-time-window-mpi-interface.cc',
        'model/mpi-receiver.cc',
        'model/null-message-simulator-impl.cc',
        'model/null-message-mpi-interface.cc',
        'model/remote-channel-bundle.cc',
        'model/remote-channel-bundle-manager.cc',
        'model/mpi-interface.cc', 
        ]

    # MPI tests are based on examples that are run as tests, only test when examples are built.
    if bld.env['ENABLE_EXAMPLES']:
        module_test = bld.create_ns3_module_test_library('mpi')
        module_test.source = [
            'test/mpi-test-suite.cc',
        ]

    headers = bld(features='ns3header')
    headers.module = 'mpi'
    headers.source = [
        'model/mpi-receiver.h',
        'model/mpi-interface.h',
        'model/parallel-communication-interface.h',
        ]

    if bld.env['ENABLE_MPI']:
        sim.use.append('MPI')

    if bld.env['ENABLE_EXAMPLES']:
        bld.recurse('examples')
      
    bld.ns3_python_bindings()
