#!/usr/bin/python
import sys, os

# CLAM unit tests build

clamToolPath="../scons/sconstools"

options = Options('options.cache')

options.Add( PathOption( 'clam_prefix', 'Prefix were CLAM was installed', '') )
if sys.platform == 'darwin' :
	options.Add( PathOption( 'cppunit_prefix', 'Prefix were cppunit was installed', '/opt/local' ))
else:
	options.Add( PathOption( 'cppunit_prefix', 'Prefix were cppunit was installed', '/usr' ))
options.Add( BoolOption( 'release', 'Build CLAM Tests enabling compiler optimizations', 'no') )
options.Add( PathOption( 'test_data_path', 'Path to the TestDataPrefix', '') )
options.Add(BoolOption('crossmingw', 'Using MinGW crosscompiler mode in linux', 'no') )

env = Environment( ENV=os.environ, tools=['default', 'clam'], toolpath=[clamToolPath], options=options)

options.Update(env)
options.Save('options.cache', env)
Help(options.GenerateHelpText(env))

env['project'] = 'CLAMTests'


env.SConsignFile() # Single signature file

crosscompiling = env["crossmingw"]
if crosscompiling :
	env.Tool('crossmingw', toolpath=[clamToolPath])

env.EnableClamModules([
	'clam_core',
	'clam_audioio',
	'clam_processing'
	], env['clam_prefix'] )

env.Append( CCFLAGS=['-O3', '-fomit-frame-pointer', '-g','-Wall'] )
env.Append( CPPFLAGS = ['-DTEST_DATA_PATH="\\"%s\\""' % env['test_data_path'] ] )
env.Append( CPPFLAGS = ['-DCLAM_MODULE="\\"tests\\""'] )

if sys.platform == 'win32' or crosscompiling :
	env.ParseConfig( env['cppunit_prefix']+'/bin/cppunit-config --cflags' )
#	env.ParseConfig( env['cppunit_prefix']+'/bin/cppunit-config --libs' )
	if crosscompiling :
		env.PrependUnique( LIBS=['cppunit'] )
	else:
		env.PrependUnique( LIBS=['cppunit_vc7'] )
	env.AppendUnique( LIBPATH=[env['cppunit_prefix']+'/lib'] )
else:
	env.ParseConfig( 'pkg-config --cflags --libs cppunit')

if sys.platform == 'darwin':
	env.AppendUnique( LIBS=['xerces-c'] )
	env.AppendUnique( LIBPATH=['/opt/local/lib'] )


if sys.platform == 'win32' :
	if env['release'] :
		env.Append( CPPFLAGS = ['-DWIN32'] )
		env.Append( CCFLAGS = '/FD /GR /GX /MD /O2 /GL /W3 /Zm1000' )	
		env.Append( LINKFLAGS = ['/LTCG'] )
	else :
		env.Append( CPPFLAGS = ['-DWIN32', '-D_DEBUG'] )
		env.Append( CCFLAGS = '/D /FD /GR /GX /GZ /MDd /Od /W3 /ZI /Zm1000' )
		env.Append( LINKFLAGS = ['/OPT:NOREF', '/OPT:NOICF', '/DEBUG'] )

#TODO: KLUDGE to allow old style and new style CLAM headers
env.Append(CPPPATH=env['clam_prefix']+'/include/CLAM')
env.Append(CPPPATH=env['clam_prefix']+'/include')
env.Append(CPPPATH='include')
env.Append(CPPPATH=os.path.join('..','UnitTests', 'CommonHelpers'))
env.Append(CPPPATH=os.path.join('..','UnitTests'))
env.Append(CPPPATH=os.path.join('..','FunctionalTests','CommonHelpers'))

Export( 'env' )
SConscript('UnitTests/SConscript')
SConscript('FunctionalTests/SConscript')

Alias( 'all', ['run_unit_tests','run_functional_tests'] )

