import os.path

Import('env')

from bundle_tool import *
TOOL_BUNDLE(env)

if env['debug']:
	env.AppendUnique(CCFLAGS=['-g', '-O0', '-fno-common'])
else:
	env.AppendUnique(CCFLAGS=['-O2', '-fno-common'])
#	Commented this line out, as there is a bug in gcc 4.0.1 on MacOSX, -s corrupts executables.
#	env.AppendUnique(LINKFLAGS=['-Wl,-s'])

#env.AppendUnique(CCFLAGS=['-O3', '-faltivec', '-finline-functions', '-ffast-math', '-fno-common'])

env.AppendUnique(CPPPATH = [env.Dir('#/aqsistypes/posix')])
env.AppendUnique(CPPDEFINES=[('NDEBUG', '1'), ('PLUGINS', '1'), ('USE_TIMERS', 1), ('_CLOCKTICKS', '1')])

# Point at the boost headers.
# todo: need to find a way of determining this location automatically
env.Replace(boost_include_path = ['/usr/local/include/boost-1_33'])

# Find out where the TIFF headers are, look in common places for Fink and DarwinPorts.
tiffiofile = env.FindFile('tiffio.h', ['$tiff_include_path', '/usr/local/include', '/opt/local/include'])
if tiffiofile != None:
	env.Replace(tiff_include_path = os.path.dirname(tiffiofile.abspath))

# Find out where the TIFF libraries are, look in common places for Fink and DarwinPorts.
tifflibfile = env.FindFile('libtiff.a', ['$tiff_lib_path', '/usr/local/lib', '/opt/local/lib'])
if tifflibfile != None:
	env.Replace(tiff_lib_path = os.path.dirname(tifflibfile.abspath))
env.Replace(tiff_lib = 'tiff')

# Find out where the JPEG headers are, look in common places for Fink and DarwinPorts.
jpegiofile = env.FindFile('jpeglib.h', ['$jpeg_include_path', '/usr/local/include', '/opt/local/include'])
if jpegiofile != None:
	env.Replace(jpeg_include_path = os.path.dirname(jpegiofile.abspath))

# Find out where the JPEG libraries are, look in common places for Fink and DarwinPorts.
jpeglibfile = env.FindFile('libjpeg.a', ['$jpeg_lib_path', '/usr/local/lib', '/opt/local/lib'])
if jpeglibfile != None:
	env.Replace(jpeg_lib_path = os.path.dirname(jpeglibfile.abspath))
env.Replace(jpeg_lib = 'jpeg')

# Find out where the zlib headers are, look in common places for Fink and DarwinPorts.
zlibiofile = env.FindFile('zlib.h', ['$zlib_include_path', '/usr/local/include', '/opt/local/include'])
if zlibiofile != None:
	env.Replace(zlib_include_path = os.path.dirname(zlibiofile.abspath))

# Find out where the zlib libraries are, look in common places for Fink and DarwinPorts.
zliblibfile = env.FindFile('libz.a', ['$zlib_lib_path', '/usr/local/lib', '/opt/local/lib'])
if zliblibfile != None:
	env.Replace(zlib_lib_path = os.path.dirname(zliblibfile.abspath))
env.Replace(z_lib = 'z')

# Find out where the OpenEXR libraries are, look in common places for Fink and DarwinPorts.
exriofile = env.FindFile('half.h', ['$exr_include_path', '/usr/local/include/OpenEXR', '/opt/local/include/OpenEXR'])
if exriofile != None:
	env.Replace(exr_include_path = os.path.dirname(exriofile.abspath))

exrlibfile = env.FindFile('libIlmImf.a', ['$exr_lib_path', '/usr/local/lib', '/opt/local/lib'])
if exrlibfile != None:
	env.Replace(exr_lib_path = os.path.dirname(exrlibfile.abspath))
env.Replace(exr_lib = 'IlmImf')
env.Replace(exr2tif_libs = ['IlmImf', 'Half', 'Iex', 'Imath'])

# Find out where the fltk headers are, look in common places for Fink and DarwinPorts.
fltkfile = env.FindFile('FL/fl.h', ['$fltk_include_path', '/usr/local/include', '/opt/local/include'])
if fltkfile != None:
	env.Replace(fltk_include_path = os.path.split(os.path.dirname(fltkfile.abspath))[0])

# Find out where the fltk libraries are, look in common places for Fink and DarwinPorts.
fltklibfile = env.FindFile('libfltk.a', ['$fltk_lib_path', '/usr/local/lib', '/opt/local/lib'])
if fltklibfile != None:
	env.Replace(fltk_lib_path = os.path.dirname(fltklibfile.abspath))
env.Replace(fltk_lib = 'fltk')

# Ensure that shared libraries have their install name set to not include a path, all shared libraries we link 
# against will be in the bin directory with the executables.
env.AppendUnique(SHLINKFLAGS = ['-install_name', '@executable_path' + os.path.sep + '${TARGET.file}'])

env.AppendUnique(SHLINKFLAGS = ['-single_module', '-flat_namespace', '-undefined', 'suppress'])

# Setup the xsltproc program location
env.Replace(XSLTPROC = 'xsltproc')

env.Replace(YACC = '${bison}')
env.Replace(LEX = '${flex}')
env.Replace(YACCFLAGS = '--no-lines -d')

env.Replace(aqsis_linkflags = ['-framework', 'Carbon'])

env.Replace(slparse_ccflags = ['-O0'])
env.Replace(libaqsis_linkflags = ['-framework', 'Carbon'])

