Import('env', 'sys')

# Core library environment with OpenEXR, libpng and libjpeg
coreEnv = env.Clone()
if coreEnv.has_key('OEXRLIBDIR'):
	coreEnv.Prepend(LIBPATH=env['OEXRLIBDIR'])
if coreEnv.has_key('OEXRINCLUDE'):
	coreEnv.Prepend(CPPPATH=env['OEXRINCLUDE'])
if coreEnv.has_key('OEXRFLAGS'):
	coreEnv.Prepend(CPPFLAGS=env['OEXRFLAGS'])
if coreEnv.has_key('OEXRLIB'):
	coreEnv.Prepend(LIBS=env['OEXRLIB'])
if coreEnv.has_key('PNGLIBDIR'):
	coreEnv.Prepend(LIBPATH=env['PNGLIBDIR'])
if coreEnv.has_key('PNGINCLUDE'):
	coreEnv.Prepend(CPPPATH=env['PNGINCLUDE'])
if coreEnv.has_key('PNGLIB'):
	coreEnv.Prepend(LIBS=env['PNGLIB'])
if coreEnv.has_key('JPEGLIBDIR'):
	coreEnv.Prepend(LIBPATH=env['JPEGLIBDIR'])
if coreEnv.has_key('JPEGINCLUDE'):
	coreEnv.Prepend(CPPPATH=env['JPEGINCLUDE'])
if coreEnv.has_key('JPEGLIB'):
	coreEnv.Prepend(LIBS=env['JPEGLIB'])

coreEnv.Prepend(CPPDEFINES = [['MTS_BUILD_MODULE', 'MTS_MODULE_CORE']])
libcore_objects = [
	'class.cpp', 'object.cpp', 'statistics.cpp', 'thread.cpp', 'brent.cpp',
	'logger.cpp', 'appender.cpp', 'formatter.cpp', 'lock.cpp', 
	'random.cpp', 'timer.cpp',  'util.cpp', 'properties.cpp', 
	'transform.cpp', 'spectrum.cpp', 'aabb.cpp', 'stream.cpp', 
	'fstream.cpp', 'plugin.cpp', 'triangle.cpp', 'bitmap.cpp',
	'serialization.cpp', 'sstream.cpp', 'cstream.cpp', 'mstream.cpp', 
	'sched.cpp', 'sched_remote.cpp', 'sshstream.cpp', 'wavelet.cpp',
	'zstream.cpp', 'shvector.cpp', 'fresolver.cpp', 'quad.cpp', 'mmap.cpp'
]

# Add some platform-specific components
if sys.platform == 'darwin':
	coreEnv_osx = coreEnv.Clone()
	coreEnv_osx['CXXFLAGS'].remove('-fstrict-aliasing')
	coreEnv_osx['CXXFLAGS'].remove('-ftree-vectorize')
	coreEnv_osx['CXXFLAGS'].append('-fno-strict-aliasing')
	libcore_objects += coreEnv_osx.SharedObject('platform_darwin.mm')
elif sys.platform == 'win32':
	libcore_objects += coreEnv.SharedObject('getopt.c')
	libcore_objects += coreEnv.SharedObject('platform_win32.cpp')

libcore = coreEnv.SharedLibrary('mitsuba-core', libcore_objects)

if sys.platform == "darwin":
	coreEnv.AddPostAction(libcore, 'install_name_tool -id @executable_path/../Frameworks/libmitsuba-core.dylib $TARGET')

env.Append(LIBS=['mitsuba-core'])
env.Append(LIBPATH=['#src/libcore'])
