
##############################################################################
#
# 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 = """
    blocktools.cpp
    blocktools2.cpp
    Brick.cpp
    DefaultAssembler2D.cpp
    DefaultAssembler3D.cpp
    domainhelpers.cpp
    LameAssembler2D.cpp
    LameAssembler3D.cpp
    MultiBrick.cpp
    MultiRectangle.cpp
    Rectangle.cpp
    RipleyDomain.cpp
    RipleyException.cpp
    WaveAssembler2D.cpp
    WaveAssembler3D.cpp
""".split()

headers = """
    AbstractAssembler.h
    blocktools.h
    Brick.h
    DefaultAssembler2D.h
    DefaultAssembler3D.h
    domainhelpers.h
    LameAssembler2D.h
    LameAssembler3D.h
    MultiBrick.h
    MultiRectangle.h
    Rectangle.h
    Ripley.h
    RipleyDomain.h
    RipleyException.h
    RipleySystemMatrix.h
    system_dep.h
    WaveAssembler2D.h
    WaveAssembler3D.h
""".split()

local_env.Prepend(LIBS = ['pasowrap', 'escript', 'paso', 'esysUtils'])
if local_env['silo']:
    local_env.Append(CPPDEFINES = ['USE_SILO'])
    local_env.AppendUnique(LIBS = env['silo_libs'])

if local_env['cuda']:
    sources.append('RipleySystemMatrix.cu')
    if env['openmp']:
        local_env.Append(CPPDEFINES = ['THRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_OMP'])

    local_env['NVCCFLAGS'] += ' -w'
    local_env['SHNVCCFLAGS']  += ' -w'
    local_env.Append(CPPDEFINES = ['USE_CUDA', 'THRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA'])
    #TODO: This needs a config option:
    local_env.Append(CPPDEFINES = ['CUSP_USE_TEXTURE_MEMORY'])
    local_env.Append(LIBS = ['cudart'])

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

module_name = 'ripley'

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

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

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

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

### Python wrapper ###
py_wrapper_local_env.Prepend(LIBS = ['ripley', 'pasowrap', 'escript', 'paso', 'esysUtils'])
py_wrapper_name = module_name + 'cpp'
py_wrapper_lib = py_wrapper_local_env.SharedLibrary(py_wrapper_name, 'ripleycpp.cpp')
env.Alias('build_ripleycpp_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_ripleycpp_lib', mod_inst)

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

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

