basic detection of compiler (GCC or ICC) and CPU capabilities
try to detect Python path in Windows and allow direct specification through build option
plus other build system fixes
Help("""
Targets:
    all              - build everything,
    libs             - build all libraries
    demos            - build all demos
    models           - download/prepare all models
    docs             - compile doxygen documentation
    libs = (static-lib, python-module)
    static-lib       - ray tracer library to link with
    python-module    - ray tracer module for Python
    demos = (python-demos, cc-demos)
    python-demos     - Python demos, this depends on python-module
    cc-demos         - C++ demos
    models = (local-models, download-models)
    local-models     - prepare local models
    download-models  - download models which are not locally available
    no-docs = (libs, demos, models)
                     - everything but docs
    no-download = (libs, demos, local-models)
                     - everything but docs and downloadable models
Default target is no-download.
Options:
""")
import os, sys
env = Environment(ENV = {'PATH' : os.environ['PATH']})
Decider('MD5-timestamp')
opt = Options(['.optioncache'])
opt.AddOptions(
	BoolOption('intelc', 'use Intel C++ Compiler, if available', True),
	('flags', 'add additional compiler flags', ""),
)
if env['PLATFORM'] == 'win32':
	opt.AddOptions(
		('pythonpath', 'path to Python installation',
			'C:\\Python%c%c' % (sys.version[0], sys.version[2])),
		)
opt.Update(env)
opt.Save('.optioncache', env)
Help(opt.GenerateHelpText(env))
if env['intelc']:
	Tool("intelc")(env)
platform = 'unknown'
def CheckPlatform(context):
	global platform
	context.Message('Platform is... ')
	if sys.platform[:5] == 'linux':
		platform = 'linux'
	elif env['PLATFORM'] == 'posix':
		platform = 'posix'
	elif env['PLATFORM'] == 'win32':
		platform = 'win32'
	context.Result(platform)
	return True
cpu = 'unknown'
def CheckCPU(context):
	global cpu, platform
	context.Message('Checking CPU model... ')
	if (platform == 'linux'):
		if (os.system("cat /proc/cpuinfo | grep 'Core(TM)2 CPU' >/dev/null") == 0):
			cpu = 'core2'
	context.Result(cpu)
	return True
def CheckIntelC(context):
	global intelc
	context.Message('Checking for Intel C++ Compiler... ')
	intelc = "intelc" in env['TOOLS']
	context.Result(intelc)
	return intelc
def CheckGCC(context):
	global gcc, gccversion
	context.Message('Checking for GCC... ')
	gcc = "g++" in env['TOOLS']
	if gcc:
		gccversion = os.popen("g++ --version").read().split()[2]
		context.Result(gccversion)
	else:
		context.Result(False)
	return gcc
conf = Configure(env,
	custom_tests = {
		'CheckPlatform' : CheckPlatform, 'CheckCPU' : CheckCPU,
		'CheckIntelC' : CheckIntelC, 'CheckGCC' : CheckGCC})
conf.CheckPlatform()
conf.CheckCPU()
conf.CheckGCC()
conf.CheckIntelC()
env = conf.Finish()
if intelc:
	cc = 'intelc'
elif gcc:
	cc = 'gcc'
cpu_flags = ''
if cc == 'gcc':
	cpu_flags += '-ffast-math '
if cpu == 'core2':
	if (cc == 'intelc' or gccversion[:3] == '4.3'):
		cpu_flags += '-march=core2 -mtune=core2 '
	if cc == 'intelc':
		cpu_flags += '-xT '
	if cc == 'gcc':
		cpu_flags += '-msse3 -mfpmath=sse '
cpu_flags += env['flags']
if intelc:
	env.Append(CCFLAGS="-O3 -w1 " + cpu_flags)
elif gcc:
	env.Append(CCFLAGS="-O3 -Wall -pipe " + cpu_flags)
	# CCFLAGS= -fno-strict-aliasing
else:
	print "No supported compiler found."
	Exit(1)
print "Additional compiler flags: " + cpu_flags
# pthread
if env['PLATFORM'] == 'win32':
	env.Append(LIBS=["pthreadGC2"])
else:
	env.Append(CCFLAGS="-pthread ")
# float: -fsingle-precision-constant
# double: -DPYRIT_DOUBLE
(lib, pymodule) = SConscript('src/SConscript', build_dir='build/lib', duplicate=0, exports='env')
SConscript('ccdemos/SConscript', build_dir='build/ccdemos', duplicate=0, exports='env lib')
SConscript('demos/SConscript', exports='pymodule')
env.Alias('demos', ['cc-demos', 'python-demos'])
SConscript('models/SConscript')
env.Alias('docs', Command('docs/html', [], 'doxygen'))
env.Clean('docs', ['docs/html', 'docs/latex'])
env.Alias('no-docs', ['libs', 'demos', 'models'])
env.Alias('no-download', ['libs', 'demos', 'local-models'])
env.Alias('all', ['no-docs', 'docs'])
env.Alias('pyrit', 'no-download')
Default('pyrit')