
##############################################################################
#
# 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()

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

sources = """
    Assemble_AverageElementData.cpp
    Assemble_CopyElementData.cpp
    Assemble_CopyNodalData.cpp
    Assemble_LumpedSystem.cpp
    Assemble_NodeCoordinates.cpp
    Assemble_PDE.cpp
    Assemble_PDE_Points.cpp
    Assemble_PDE_Single_1D.cpp
    Assemble_PDE_Single_2D.cpp
    Assemble_PDE_Single_3D.cpp
    Assemble_PDE_Single_C.cpp
    Assemble_PDE_System_1D.cpp
    Assemble_PDE_System_2D.cpp
    Assemble_PDE_System_3D.cpp
    Assemble_PDE_System_C.cpp
    Assemble_addToSystemMatrix.cpp
    Assemble_getAssembleParameters.cpp
    Assemble_getNormal.cpp
    Assemble_getSize.cpp
    Assemble_gradient.cpp
    Assemble_integrate.cpp
    Assemble_interpolate.cpp
    Assemble_jacobians.cpp
    ElementFile.cpp
    ElementFile_jacobians.cpp
    Finley.cpp
    IndexList.cpp
    Mesh.cpp
    Mesh_addPoints.cpp
    Mesh_findMatchingFaces.cpp
    Mesh_getPattern.cpp
    Mesh_glueFaces.cpp
    Mesh_hex20.cpp
    Mesh_hex8.cpp
    Mesh_joinFaces.cpp
    Mesh_merge.cpp
    Mesh_optimizeDOFDistribution.cpp
    Mesh_read.cpp
    Mesh_readGmsh.cpp
    Mesh_rec4.cpp
    Mesh_rec8.cpp
    Mesh_write.cpp
    NodeFile.cpp
    Quadrature.cpp
    ReferenceElements.cpp
    ShapeFunctions.cpp
    Util.cpp
    CPPAdapter/FinleyAdapterException.cpp
    CPPAdapter/MeshAdapter.cpp
    CPPAdapter/MeshAdapterFactory.cpp
""".split()

headers = """
    Assemble.h
    ElementFile.h
    Finley.h
    FinleyVersion.h
    IndexList.h
    Mesh.h
    NodeFile.h
    NodeMapping.h
    Quadrature.h
    RectangularMesh.h
    ReferenceElements.h
    ShapeFunctions.h
    Util.h
    ReferenceElementSets.h
""".split()

cppadapter_headers = """
    CPPAdapter/FinleyAdapterException.h
    CPPAdapter/MeshAdapter.h
    CPPAdapter/MeshAdapterFactory.h
    CPPAdapter/system_dep.h
""".split()

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

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

module_name = 'finley'

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

include_path = Dir('finley', local_env['incinstall'])
cppadapter_include_path = Dir('CppAdapter', include_path)

hdr_inst1 = local_env.Install(include_path, headers)
hdr_inst2 = local_env.Install(cppadapter_include_path, cppadapter_headers)
env.Alias('install_finley_headers', [hdr_inst1, hdr_inst2])

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

### Python wrapper ###
if not env['build_shared']:
    py_wrapper_local_env.Prepend(LIBS = ['finley', 'pasowrap', 'escript', 'esysUtils'])
else:
    py_wrapper_local_env.Prepend(LIBS = ['finley', 'pasowrap', 'escript', 'paso', 'esysUtils'])

py_wrapper_name = module_name + 'cpp'
py_wrapper_lib = py_wrapper_local_env.SharedLibrary(py_wrapper_name, 'CPPAdapter/finleycpp.cpp')
env.Alias('build_finleycpp_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_finleycpp_lib', mod_inst)

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

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

