Import('env')
import os

#
# Need to make a copy because SCons uses the environment
# at its final state, so StGermain ends up depending on
# StgDomain, etc.
#

env = env.Clone()

#
# Inside each project we will be accessing headers without the
# project name as a prefix, so we need to let SCons know how to
# find those headers.
#

env.Append(CPPPATH=env['build_dir'] + '/include/libUnderworldPy')

#
# Keep a list of all the objects we build so we can make a library
# afterwards.
#

wrapperDict = {}

wrapperDict['StGermain_Tools'] = ['StGermain_Tools.c', 'StGermain_Tools_wrap.cxx']
wrapperDict['gLucifer'] = ['gLucifer_wrap.cxx']
wrapperDict['petsc'] = ['petsc_wrap.cxx']
wrapperDict['c_arrays'] = ['c_arrays_wrap.cxx']
wrapperDict['c_pointers'] = ['c_pointers_wrap.cxx']
wrapperDict['Underworld'] = ['Underworld_wrap.cxx']
wrapperDict['StgDomain'] = ['StgDomain_wrap.cxx']
wrapperDict['StGermain'] = ['StGermain_wrap.cxx']
wrapperDict['StgFEM'] = ['StgFEM_wrap.cxx']
wrapperDict['Solvers'] = ['Solvers_wrap.cxx']
wrapperDict['PICellerator'] = ['PICellerator_wrap.cxx']
wrapperDict['Function'] = ['Function_wrap.cxx']

objs = []

#
# Process each directory uniformly.
#

# drop the shlibprefix for these as there's a particular name expected by the py modules,
# for example _StGermain.so
env['SHLIBPREFIX'] = '_'
for key,value in wrapperDict.items():
    # Build our source files.
    objs = []
    for file in value:
        srcs = Glob('./'+file)
        objs += env.SharedObject(srcs, CPPDEFINES=env.get('CPPDEFINES', []))

    #
    # Build shared library.
    #
    if env['shared_libs']:
        env.SharedLibrary('lib/'+key, objs)

# add symlinks for osx.. 
import os
if env['SHLIBSUFFIX'] == '.dylib':
    def SymLink(target, source, env):
        os.symlink(os.path.abspath(str(source[0])), os.path.abspath(str(target[0])))
    for key,value in wrapperDict.items():
        destLink   = os.path.join(env["build_dir"],'lib', '_'+key+'.so')
        sourceFile = os.path.join(env["build_dir"],'lib', '_'+key+'.dylib')
        env.Command(destLink, sourceFile, SymLink)
        destLink   = os.path.join(env["prefix"],'lib', '_'+key+'.so')
        sourceFile = os.path.join(env["prefix"],'lib', '_'+key+'.dylib')
        env.Command(destLink, sourceFile, SymLink)
