

##############################################################################
#
# 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()
local_unroll_env.Append(CFLAGS = env['dudley_assemble_flags'])


# 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_Single2_2D.cpp
	Assemble_PDE_Single2_3D.cpp
	Assemble_PDE_System2_2D.cpp
	Assemble_PDE_System2_3D.cpp
	Assemble_addToSystemMatrix.cpp
	Assemble_getAssembleParameters.cpp
	Assemble_getSize.cpp
	Assemble_integrate.cpp
	Assemble_interpolate.cpp
	Assemble_setNormal.cpp
	ElementFile.cpp
	ElementFile_allocTable.cpp
	ElementFile_copyTable.cpp
	ElementFile_createColoring.cpp
	ElementFile_distributeByRankOfDOF.cpp
	ElementFile_gather.cpp
	ElementFile_jacobeans.cpp
	ElementFile_markNodes.cpp
	ElementFile_optimizeOrdering.cpp
	ElementFile_relableNodes.cpp
	ElementFile_scatter.cpp
	ElementFile_setCoordinates.cpp
	ElementFile_setNodeRange.cpp
	ElementFile_setTags.cpp
	ElementType.cpp
	Dudley.cpp
	IndexList.cpp
	Mesh.cpp
	Mesh_createNodeFileMappings.cpp
	Mesh_distributeByRankOfDOF.cpp
	Mesh_findMatchingFaces.cpp
	Mesh_getPattern.cpp
	Mesh_markNodes.cpp
	Mesh_optimizeDOFDistribution.cpp
	Mesh_optimizeDOFLabeling.cpp
	Mesh_prepare.cpp
	Mesh_print.cpp
	Mesh_read.cpp
	Mesh_readGmsh.cpp
	Mesh_relableElementNodes.cpp
	Mesh_resolveNodeIds.cpp
	Mesh_setCoordinates.cpp
	Mesh_tagmaps.cpp
	Mesh_tet4.cpp
	Mesh_tri3.cpp
	Mesh_write.cpp
	NodeFile.cpp
	NodeFile_allocTable.cpp
	NodeFile_copyTable.cpp
	NodeFile_createDenseLabelings.cpp
	NodeFile_gather.cpp
	NodeFile_scatter.cpp
	NodeFile_setCoordinates.cpp
	NodeFile_setIdRange.cpp
	NodeFile_setTags.cpp
	NodeMapping.cpp
	ShapeTable.cpp
	TagMap.cpp
	Util.cpp
	CPPAdapter/DudleyAdapterException.cpp
	CPPAdapter/DudleyError.cpp
	CPPAdapter/MeshAdapter.cpp
	CPPAdapter/MeshAdapterFactory.cpp
""".split()
unroll_sources= """
	Assemble_gradient.cpp
	Assemble_jacobeans.cpp
""".split()


headers = """
	Assemble.h
	Dudley.h
	DudleyVersion.h
	ElementFile.h
	ElementType.h
	IndexList.h
	Mesh.h
	NodeFile.h
	NodeMapping.h
	TriangularMesh.h
	ShapeTable.h
	TagMap.h
	Util.h
""".split()
cppadapter_headers = """
	CPPAdapter/DudleyAdapterException.h
	CPPAdapter/DudleyError.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 = ['DUDLEY_EXPORTS'])

module_name = 'dudley'

un = [ local_unroll_env.SharedObject(x) for x in unroll_sources]

lib = local_env.SharedLibrary(module_name, sources+un)
env.Alias('build_dudley_lib', lib)

include_path = Dir('dudley', 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_dudley_headers', [hdr_inst1, hdr_inst2])

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

### Python wrapper ###
if not env['build_shared']:
    py_wrapper_local_env.Prepend(LIBS = ['dudley', 'pasowrap', 'escript', 'esysUtils'])
else:
    py_wrapper_local_env.Prepend(LIBS = ['dudley', 'pasowrap', 'escript', 'paso', 'esysUtils'])
py_wrapper_name = module_name + 'cpp'
py_wrapper_lib = py_wrapper_local_env.SharedLibrary(py_wrapper_name, 'CPPAdapter/dudleycpp.cpp')
env.Alias('build_dudleycpp_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_dudleycpp_lib', mod_inst)

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

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

