
##############################################################################
#
# 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()
py_wrapper_local_env = env.Clone()
local_unroll_env = env.Clone()


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

sources = """
    AbstractAssembler.cpp
    Brick.cpp
    BrickGradients.cpp
    BrickIntegrals.cpp
    BrickReductions.cpp
    DefaultAssembler2D.cpp
    DefaultAssembler3D.cpp
    domainhelpers.cpp
    Rectangle.cpp
    RectangleGradients.cpp
    RectangleIntegrals.cpp
    RectangleReductions.cpp
    SpeckleyDomain.cpp
    SpeckleyException.cpp
    WaveAssembler2D.cpp
    WaveAssembler3D.cpp
""".split()

headers = """
    AbstractAssembler.h
    Brick.h
    DefaultAssembler2D.h
    DefaultAssembler3D.h
    domainhelpers.h
    lagrange_functions.h
    Rectangle.h
    Speckley.h
    SpeckleyDomain.h
    SpeckleyException.h
    system_dep.h
    WaveAssembler2D.h
    WaveAssembler3D.h
""".split()

local_env.Prepend(LIBS = ['escript', 'esysUtils'])

if 'ripley' in env['domains']:
    local_env.Append(CPPDEFINES = ['USE_RIPLEY'])
    py_wrapper_local_env.Append(CPPDEFINES = ['USE_RIPLEY'])
    local_env.Append(LIBS = ['ripley'])
    sources += ['CrossDomainCoupler.cpp']
    headers += ['CrossDomainCoupler.h']

if local_env['silo']:
    local_env.Append(CPPDEFINES = ['USE_SILO'])
    local_env.AppendUnique(LIBS = env['silo_libs'])

if IS_WINDOWS:
    local_env.Append(CPPDEFINES = ['Speckley_EXPORTS'])

module_name = 'speckley'

lib = local_env.SharedLibrary(module_name, sources)
env.Alias('build_speckley_lib', lib)

include_path = Dir('speckley', local_env['incinstall'])

hdr_inst = local_env.Install(include_path, headers)
env.Alias('install_speckley_headers', hdr_inst)

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

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

tmp_inst = os.path.join(local_env['pyinstall'], module_name)
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_speckleycpp_lib', mod_inst)

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

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

