Import('env', 'sys', 'os')

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

coreEnv.Prepend(CPPDEFINES = [['MTS_BUILD_MODULE', 'MTS_MODULE_CORE']])


if sys.platform == 'win32':
        coreEnv.Append(LIBS=['psapi'])

libcore_objects = [
        'class.cpp', 'object.cpp', 'statistics.cpp', 'thread.cpp', 'brent.cpp',
        'logger.cpp', 'appender.cpp', 'formatter.cpp', 'lock.cpp', 'qmc.cpp',
        'random.cpp', 'timer.cpp',  'util.cpp', 'properties.cpp', 'half.cpp',
        'transform.cpp', 'spectrum.cpp', 'aabb.cpp', 'stream.cpp', 'math.cpp',
        'fstream.cpp', 'plugin.cpp', 'triangle.cpp', 'bitmap.cpp',
        'fmtconv.cpp', 'serialization.cpp', 'sstream.cpp', 'cstream.cpp',
        'mstream.cpp', 'sched.cpp', 'sched_remote.cpp', 'sshstream.cpp',
        'zstream.cpp', 'shvector.cpp', 'fresolver.cpp', 'rfilter.cpp',
        'quad.cpp', 'mmap.cpp', 'chisquare.cpp', 'warp.cpp', 'vmf.cpp',
        'tls.cpp', 'ssemath.cpp', 'spline.cpp', 'track.cpp'
]

# Add some platform-specific components
if sys.platform == 'darwin':
        coreEnv_osx = coreEnv.Clone()
        coreEnv_osx.ConfigureForObjectiveCPP()
        libcore_objects += coreEnv_osx.SharedObject('platform_darwin.mm')
elif sys.platform == 'win32':
        libcore_objects += coreEnv.SharedObject('getopt.c')

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

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

env.Append(LIBS=['mitsuba-core'])
env.Append(LIBPATH=[os.path.join(env['BUILDDIR'], 'libcore')])
