from sys import platform
from os import environ

Import(['debug', 'profile', 'config', 'bits', 'toolchain'])

envArgs = dict()
envArgs['ENV'] = environ
if 'scons-tool' in config:    envArgs['tools'] = [config['scons-tool']]
if 'target-arch' in config:   envArgs['TARGET_ARCH'] = config['target-arch']

env = Environment(**envArgs)

env.Append(CPPPATH=[
  config['includedirs']['boost-python'],
  config['includedirs']['boost-random'],
  config['includedirs']['boost-filesystem'],
  config['includedirs']['boost-system'],
  config['includedirs']['boost-timer'],
  config['includedirs']['boost-chrono'],
  config['includedirs']['python']
])
env.Append(LIBPATH=[
  config['libdirs']['boost-python'],
  config['libdirs']['boost-random'],
  config['libdirs']['boost-filesystem'],
  config['libdirs']['boost-system'],
  config['libdirs']['boost-timer'],
  config['libdirs']['boost-chrono'],
  config['libdirs']['python']
])
env.Append(LIBS=[
  config['libs']['boost-python'],
  config['libs']['boost-random'],
  config['libs']['boost-filesystem'],
  config['libs']['boost-system'],
  config['libs']['boost-timer'],
  config['libs']['boost-chrono'],
  config['libs']['rt'],
  config['libs']['python']
])

if 'scons-tool' in config:    Tool(config['scons-tool'])(env)
if 'compiler' in config:      env.Replace(CXX=config['compiler'])
if 'defines' in config:       env.Append(CPPDEFINES=config['defines'])
if 'compile-flags' in config: env.Append(CCFLAGS=config['compile-flags'])
if 'link-flags' in config:    env.Append(LINKFLAGS=config['link-flags'])

# Process any extra configuration that needs to be done for this platform.
if 'extra' in config: config['extra'](env)

buildEnv = env.Clone()
buildEnv.Append(CPPPATH=['src/'])

if debug == 0 and profile == 0:
  buildDir = 'release'
elif debug == 1:
  buildDir = 'debug'
elif profile == 1:
  buildDir = 'profile'
else:
  buildDir = 'generic'

buildEnv.VariantDir(buildDir, 'src', duplicate=0)

sources = Glob(buildDir + '/*.cpp') + Glob(buildDir + '/py-bindings/*.cpp')
results = dict()

apnslib = buildEnv.SharedLibrary(target='_apnsmod', source=sources)
results['apnslib'] = apnslib

#
# Unit tests
#

testEnv = env.Clone()

testEnv.VariantDir('test-build', 'src', duplicate=0)
testEnv.Append(CPPPATH=['src/', config['includedirs']['gtest']])
testEnv.Append(LIBPATH=config['libdirs']['gtest'])
testEnv.Append(LIBS=config['libs']['gtest'])

test_sources = [
  'hash_test',
  'board_test',
  'piece_test',
  'position_test',
  'movement_test',
  'search-algos_test',
  'tree_test',
  'util_test'
]

tests = list()
objList = testEnv.Glob('test-build/*.cpp')

def runTest(env, target, source):
  import subprocess
  app = str(source[0].abspath)
  if not subprocess.call(app):
    file(str(target[0]), 'w').write('OK\n')

for test in test_sources:
  program = testEnv.Program(target='tests/%s' % test, source=['tests/%s.cpp' % test, objList])
  stamp = testEnv.Command('tests/%s.passed' % test, program, runTest)

  tests.append(stamp)
  results[test] = stamp

t = Alias('tests_', tests)  # 'tests_' because without the underscore we have a cyclic dependency tests -> tests here.
results['tests'] = t

Return('results')

# vim:syntax=python
