## ----------------------------------------------------------------------
'''

SConscript for tradigiTOOLs

Before running scons, you'll need to set the Maya version to build against.
Example in tcsh:

setenv MAYA_VER "2014"

You'll also want to adjust the MAYA_DIRECTORY paths below for your 
personal install.

'''
## ----------------------------------------------------------------------

import os, sys
platform = sys.platform

if 'MAYA_VER' in os.environ.keys():
	MAYA_VERSION = os.environ['MAYA_VER']
else:
	raise Exception('BuildError: MAYA_VER not specified in environment.')

env = Environment(
	CPPDEFINES=[
		# '_DEBUG',
		'TDT_MAYA_2011',
		'TDT_MAYA_2009',
		'_BOOL',
		'REQUIRE_IOSTREAM',
		# 'CIE_TDTMELFILE="cie_tradigitools.mel"' % str(MAYA_VERSION)
		# '_DEBUG',
	],

	LIBS = [
		'OpenMaya',
		'OpenMayaAnim',
		'Foundation',
	],
)

if platform == 'darwin':
	MAYA_DIRECTORY = "/Applications/Autodesk/maya%s" % MAYA_VERSION
	MAYA_HEADERS_DIR = "%s/devkit/include" % MAYA_DIRECTORY
	MAYA_LIBRARY_DIR = "%s/Maya.app/Contents/MacOS" % MAYA_DIRECTORY
	env.Append(	CCFLAGS='-O2 -m64 -fPIC')
	env.Append( CPPDEFINES=['MAC_PLUGIN','OSMac_'] )
	INSTALL_DIRECTORY = "../install_mac/tradigiTOOLs%s/payload/plug-ins" % MAYA_VERSION.partition(".")[0]
	TARGET = 'tradigiTOOLs_%s.bundle' % MAYA_VERSION

elif platform.startswith('linux'):
	MAYA_DIRECTORY = "/usr/autodesk/maya%s-x64" % MAYA_VERSION
	MAYA_HEADERS_DIR = "%s/include" % MAYA_DIRECTORY
	MAYA_LIBRARY_DIR = "%s/lib" % MAYA_DIRECTORY
	env.Append(	CCFLAGS='-O2 -m64 -fPIC')
	env.Append( CPPDEFINES=[ 'LINUX_PLUGIN' ] )
	INSTALL_DIRECTORY = "../install_lin/tradigiTOOLs%s/plug-ins" % MAYA_VERSION.partition(".")[0]
	TARGET = 'tradigiTOOLs_%s.so' % MAYA_VERSION

elif platform == 'win32':
	MAYA_DIRECTORY = "C:/aw/maya%s" % MAYA_VERSION
	MAYA_HEADERS_DIR = "%s/include" % MAYA_DIRECTORY
	MAYA_LIBRARY_DIR = "%s/lib" % MAYA_DIRECTORY
	env.Append( CCFLAGS=['/EHsc', '/MT'])
	env.Append( LINKFLAGS=['/export:initializePlugin', 
						'/export:uninitializePlugin', 
						'/INCREMENTAL:NO',
						'/SUBSYSTEM:WINDOWS',
						'/OPT:REF',
						'/OPT:ICF',
						# '/MT',
						'/DYNAMICBASE:NO',
						'/MACHINE:X64',
						# '/NODEFAULTLIB:LIBCMT',
						])
	env.Append( CPPDEFINES=['NT_PLUGIN'] )
	env.Append( LIBS=['Image',
					'kernel32',
					'user32',
					'gdi32',
					'winspool',
					'comdlg32',
					'advapi32',
					'shell32',
					'ole32',
					'oleaut32',
					'uuid',
					'odbc32',
					'odbccp32',
		])

	INSTALL_DIRECTORY = "C:/FUNhouse/tradigiTOOLs/plug-ins/"
	TARGET = 'tradigiTOOLs_%s.dll' % MAYA_VERSION


env.Append( CPPPATH=[ MAYA_HEADERS_DIR ] )
# env.Append( CPPDEFINES=['BIG_ENDIAN'] )
env.Append( LIBPATH = [ MAYA_LIBRARY_DIR ] )


sources = [
	'ANIMTools.cpp',
	'ANIMToolsUI.cpp',
	'AboutCommand.cpp',
	'Breakdown.cpp',
	'BreakdownCommand.cpp',
	'BreakdownList.cpp',
	'CurveCleanerCommand.cpp',
	'IncrementalSaveCommand.cpp',
	'RetimingCommand.cpp',
	'SetKeyCommand.cpp',
	'ShotMaskCommand.cpp',
]

## This is no longer necessary
## but I'm leaving it in the source because it's interesting and 
## might be useful to someone later.
'''
def config_h_build(target, source, env):
	## we cut off the .5 for extension versions when writing the header
	config_h_defines = { 'MAYA_VERSION': MAYA_VERSION.rpartition(".")[0] }

	print("Generating config.h with the following settings:",
				  config_h_defines.items())

	for a_target, a_source in zip(target, source):
		config_h = file(str(a_target), "w")
		config_h_in = file(str(a_source), "r")
		config_h.write(config_h_in.read() % config_h_defines)
		config_h_in.close()
		config_h.close()

env.AlwaysBuild(env.Command('config.h', 'config.h.in', config_h_build))
'''

env.SharedLibrary(target = TARGET, source = sources, SHLIBPREFIX='')

env.Install(INSTALL_DIRECTORY, TARGET)
env.Alias('install', INSTALL_DIRECTORY)

