import os
Import('env')

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

env = env.Clone()
env['CURR_PROJECT'] = "StGermain"

#
#  Build the 'pcu' sub-project.
#

SConscript('pcu/SConscript', exports='env')
env.Prepend(LIBS=['pcu'])

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

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

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

objs = []
suites = []
tst_exp = []
tst_input = []

#
# Process each directory uniformly.
#

dirs = Split('Base/Foundation Base/IO Base/Container Base/Automation Base/Extensibility ' \
             'Base/Context Base Utils libStGermain')
for d in dirs:
    # Need the module name, which is just the directory.
    mod_name = env['ESCAPE']('"' + ''.join(d.split('/')) + '"')
    cpp_defs = [('CURR_MODULE_NAME', mod_name)] + env.get('CPPDEFINES', []) 

    # Setup where to look for files.
    src_dir = d + '/src'
    inc_dir = 'include/StGermain/' + d
    tst_dir = d + '/tests'
    tst_exp_dir = tst_dir + '/expected'
    tst_input_dir = tst_dir + '/input'
    tst_install_dir = 'tests/StGermain/' + d

    # Install the headers
    hdrs = env.Install(os.path.join(env['build_dir'],inc_dir), Glob(src_dir + '/*.h'))

    # Build our source files.
    srcs = Glob(src_dir + '/*.c') +  Glob(src_dir + '/*.cpp')
    for src in srcs:
        stg_cpp_defs = cpp_defs + [('SOURCE_FILE', '"\\"' + src.name + '\\""')]
        objs += env.SharedObject(src, CPPDEFINES=stg_cpp_defs )

    # Build any test suites we might find.
    # suites += env.Object(Glob(tst_dir + '/*Suite.c'))

    # Install any test expected and input files
    # tst_exp += env.Install(os.path.join(env['build_dir'],tst_install_dir,'/expected'), Glob(tst_exp_dir + '/*'))
    # tst_input += env.Install(os.path.join(env['build_dir'],tst_install_dir,'/input'), Glob(tst_input_dir + '/*'))

# Need to install headers from libStGermain.
hdrs = env.Install(os.path.join(env['build_dir'],'include/StGermain'), Glob('libStGermain/src/*.h'))

#
# Build shared library.
#

if env['shared_libs']:
    env.SharedLibrary('lib/StGermain', objs)
    env.SharedLibrary(env['prefix'] + '/lib/StGermain', objs)

