import sys, platform

Import('env', 'compiler', 'lsb_build')


# svs library objects
svs_env = env.Clone()
svs_env['LIBS'] = []

srcdirs = ['src', 'src/filters', 'src/commands', 'src/models']
incdirs = [env.Dir(d).srcnode() for d in 'src src/models eigen ccd'.split()]
scu_src = ['SVS.cxx']

if compiler == 'g++':
	flags = [
		# By default Eigen will try to align all fixed size vectors to 128-bit
		# boundaries to enable SIMD instructions on hardware such as SSE. However,
		# this requires that you modify every class that has such vectors as members
		# so that they are correctly allocated. This seems like more trouble than
		# it's worth at the moment, so I'm disabling it.
		'-DEIGEN_DONT_ALIGN',
		'-Wno-enum-compare',
	]
	if lsb_build:
		flags.append('-DEIGEN_ALLOCA=aligned_malloc')   # alloca isn't in LSB
	
	if sys.platform == "darwin":
		srcdirs.append('src/osx')
		incdirs.append('src/osx')
		scu_src.append('src/osx/platform_specific.cpp')
	else:
		srcdirs.append('src/posix')
		incdirs.append('src/posix')
		scu_src.append('src/posix/platform_specific.cpp')

elif compiler == 'msvc':
	flags = [
		'/D', 'EIGEN_DONT_ALIGN',
		'/wd4307',
		'/wd4308',   # disables two warnings generated by Eigen code
	]
	srcdirs.append('src/windows')
	incdirs.append('src/windows')
	scu_src.append('src/windows/platform_specific.cpp')
else:
	flags = []

src = []
if env['SCU']:
	src = scu_src
else:
	for d in srcdirs:
		src += Glob(d + '/*.cpp')

svs_env.Prepend(
	CPPPATH = incdirs,
	CPPFLAGS = flags
)

ccd_env = env.Clone()
ccd_env['LIBS'] = []
ccd_env['CPPPATH'] = [env.Dir('ccd').srcnode()]
ccd_src = Glob('ccd/*.c')

if compiler == 'msvc':
	ccd_env.Append(CPPFLAGS=['/D', 'WIN32'])
	
if GetOption('static'):
	svs_objs = svs_env.Object(src) + ccd_env.Object(ccd_src)
else:
	svs_objs = svs_env.SharedObject(src) + ccd_env.SharedObject(ccd_src)

svs_inc = ['SVS/src']
Return('svs_objs', 'svs_inc')
