##############################################################################
#
# Copyright (c) 2003-2016 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

import os
Import('*')

local_env = env.Clone()
local_dodgy = dodgy_env.Clone()
py_wrapper_local_env = env.Clone()

# Remove the sharedlibrary prefix on all platform - we don't want 'lib'
# mucking with our python modules
del py_wrapper_local_env['SHLIBPREFIX']

sources = """
    AbstractContinuousDomain.cpp
    AbstractDomain.cpp
    AbstractReducer.cpp
    AbstractSystemMatrix.cpp
    AbstractTransportProblem.cpp
    Data.cpp
    DataAbstract.cpp
    DataBlocks2D.cpp
    DataC.cpp
    DataConstant.cpp
    DataEmpty.cpp
    DataException.cpp
    DataExpanded.cpp
    DataFactory.cpp
    DataLazy.cpp
    DataMaths.cpp
    DataReady.cpp
    DataTagged.cpp
    DataTypes.cpp
    DataVector.cpp
    DomainException.cpp
    EscriptParams.cpp
    FunctionSpace.cpp
    FunctionSpaceException.cpp
    FunctionSpaceFactory.cpp
    LapackInverseHelper.cpp
    NonReducedVariable.cpp
    NullDomain.cpp
    MPIDataReducer.cpp
    MPIScalarReducer.cpp
    SplitWorld.cpp
    SplitWorldException.cpp
    SubWorld.cpp
    SystemMatrixException.cpp
    Taipan.cpp
    TestDomain.cpp
    TransportProblemException.cpp
    Utils.cpp
    WrappedArray.cpp
    SolverOptions.cpp
    SolverOptionsException.cpp
""".split()
    # blocktimer.c
headers = """
    AbstractContinuousDomain.h
    AbstractDomain.h
    AbstractReducer.h
    AbstractSystemMatrix.h
    AbstractTransportProblem.h
    BinaryOp.h
    Data.h
    DataAbstract.h
    DataAlgorithm.h
    DataBlocks2D.h
    DataC.h
    DataConstant.h
    DataEmpty.h
    DataException.h
    DataExpanded.h
    DataFactory.h
    DataLazy.h
    DataMaths.h
    DataReady.h
    DataTagged.h
    DataTypes.h
    DataVector.h
    Dodgy.h
    DomainException.h
    EscriptParams.h
    FunctionSpace.h
    FunctionSpaceException.h
    FunctionSpaceFactory.h
    LapackInverseHelper.h
    LocalOps.h
    NonReducedVariable.h
    NullDomain.h
    MPIDataReducer.h
    MPIScalarReducer.h
    Pointers.h
    SplitWorld.h
    SplitWorldException.h
    SubWorld.h
    SystemMatrixException.h
    Taipan.h
    TestDomain.h
    TransportProblemException.h
    UnaryFuncs.h
    UnaryOp.h
    UtilC.h
    Utils.h
    WrappedArray.h
    system_dep.h
    SolverOptions.h
    SolverOptionsException.h
""".split()
    # blocktimer.h

dodgy_sources = """
    Dodgy.cpp
""".split()

local_env.Prepend(LIBS = ['esysUtils'])
local_dodgy.Prepend(LIBS = ['esysUtils'])
if IS_WINDOWS:
    local_env.Append(CPPDEFINES = ['ESCRIPT_EXPORTS'])
    local_dodgy.Append(CPPDEFINES = ['ESCRIPT_EXPORTS'])

module_name = 'escript'

# specify to build shared object
if local_env['iknowwhatimdoing']:
    nonped=[local_dodgy.SharedObject(x) for x in dodgy_sources]
else:
    nonped=[]

lib = local_env.SharedLibrary(module_name, sources+nonped)
env.Alias('build_escript_lib', lib)

include_path = Dir('escript', local_env['incinstall'])
hdr_inst = local_env.Install(include_path, headers)
env.Alias('install_escript_headers', hdr_inst)

lib_inst = local_env.Install(local_env['libinstall'], lib)
env.Alias('install_escript_lib', lib_inst)

### Python wrapper ###
py_wrapper_local_env.Prepend(LIBS = ['escript', 'esysUtils'])
py_wrapper_name = module_name + 'cpp'
py_wrapper_lib = py_wrapper_local_env.SharedLibrary(py_wrapper_name, 'escriptcpp.cpp')
env.Alias('build_escriptcpp_lib', py_wrapper_lib)

tmp_inst = os.path.join(local_env['pyinstall'], 'escriptcore')
if IS_WINDOWS:
    wrapper_ext = '.pyd'
else:
    wrapper_ext = '.so'

share_name = os.path.join(tmp_inst, py_wrapper_name+wrapper_ext)
mod_inst = py_wrapper_local_env.InstallAs(target=share_name,
                                          source=py_wrapper_lib[0])
env.Alias('install_escriptcpp_lib', mod_inst)

# configure python module
local_env.SConscript(dirs = ['#/escriptcore/py_src'], variant_dir='py', duplicate=0)

# configure unit tests
local_env.SConscript(dirs = ['#/escriptcore/test'], variant_dir='test', duplicate=0, exports=['py_wrapper_lib'])

