#! /bin/env python

################################################################################
#	\file   SConscript
#	\author Gregory Diamos <gregory.diamos@gatech.edu>
#   \date   Monday August 8, 2011
#	\brief  The SCons master build script for Archaeopteryx
################################################################################

import os
import inspect

## The script begins here
# try to import an environment first
try:
	Import('env')
except:
	try:
		exec open("../scripts/which.py")
		prefix = '../'
	except:
		exec open("scripts/which.py")
		prefix = './'
	exec open(prefix + "scripts/build_environment.py")
	env = Environment()
  
libs = env['EXTRA_LIBS']

# set the build root if a higher level script didn't already do it
if not 'ARCHAEOPTERYX_BUILD_ROOT' in env:
	env.Replace(ARCHAEOPTERYX_BUILD_ROOT = env['BUILD_ROOT'])

# include the build directory in case of generated files
env.AppendUnique(CPPPATH = env['ARCHAEOPTERYX_BUILD_ROOT'])

# find all source files in the source tree
sources = []
intermediate_headers = []
directories = ['archaeopteryx/executive/implementation',
	'archaeopteryx/util/implementation',
	'archaeopteryx/runtime/implementation',
	'archaeopteryx/driver/implementation',
	'archaeopteryx/ir/implementation']
extensions = ['*.cu']

for dir in directories:
	for ext in extensions:
		regexp = os.path.join(dir, ext)
		sources.extend(env.Glob(regexp))

# Create the archaeopteryx library
libarchaeopteryx = env.PTXLibrary('archaeopteryx', sources)

testFileAccessesKernel = env.PTXFile('TestFileAccess.ptx',
	['archaeopteryx/util/test/TestFileAccessKernel.cu'])
testFileAccessesKernelBin = env.PTXBinary('libTestFileAccess',
	[libarchaeopteryx, testFileAccessesKernel])
testFileAccessesInc = env.PTXInclude('TestFileAccessesKernel.inc',
	testFileAccessesKernelBin)
archaeopteryxModuleInc = env.PTXInclude('ArchaeopteryxModule.inc',
	libarchaeopteryx)

# find all host source files in the source tree
host_sources = []
directories = ['archaeopteryx/util/host-implementation',
	'archaeopteryx/driver/host-implementation']
extensions = ["*.cpp"]

for dir in directories:
	for ext in extensions:
		regexp = os.path.join(dir, ext)
		host_sources.extend(env.Glob(regexp))

# Create the archaeopteryx host library
libarchaeopteryxHost = env.SharedLibrary('archaeopteryxHost', host_sources)

libs.append('-larchaeopteryxHost')

# Add normal program
simulator = env.Program('archaeopteryx-simulator',
	'archaeopteryx/tools/archaeopteryx-simulator.cpp', LIBS=libs)
env.Depends(simulator, libarchaeopteryxHost)
Default(simulator)

# Create the archaeopteryx unit tests
tests = []

tests.append(('TestFileAccesses',
	'archaeopteryx/util/test/TestFileAccess.cpp', 'basic'))
#tests.append(('TestRuntime',
#	'archaeopteryx/runtime/test/TestRuntime.cpp', 'basic'))

for test in tests:
	program = env.Program(test[0], [test[1]], LIBS=libs)
	env.Depends(program, libarchaeopteryxHost)

if env['test_level'] != 'none':
	print 'Adding unit tests to the build...'

level_map = { 'none' : 0, 'basic' : 1, 'full' : 2 }

for test in tests:
	if level_map[env['test_level']] >= level_map[test[2]]:
		print 'Adding test ' + test[0]
		Default(test[0])

