#!/usr/bin/python
# Project: Soar <http://soar.googlecode.com>
# Author: Jonathan Voigt <voigtjr@gmail.com>
#
import distutils.sysconfig
import sys
import os
import SCons.Script

Import('env')
clone = env.Clone()

# Targets, sources, other constants
tclsml_wrap = env.File('#ClientSMLSWIG/Tcl/Tcl_sml_ClientInterface_wrap.cpp')
tclsml_i = env.File('#ClientSMLSWIG/Tcl/Tcl_sml_ClientInterface.i')
swig_cmdline = 'swig -o ClientSMLSWIG/Tcl/Tcl_sml_ClientInterface_wrap.cpp -c++ -tcl -pkgversion 9.0.1 -Wall -IClientSML/src -IElementXML/src -IConnectionSML/src ClientSMLSWIG/Tcl/Tcl_sml_ClientInterface.i'
tclsml_shlib = 'Tcl_sml_ClientInterface'
makepkg_script = env.File('#ClientSMLSWIG/Tcl/SconsMakeTclSMLPackage.tcl')
tclsml_pkgfolder = env.Dir('$PREFIX/lib/Tcl_sml_ClientInterface')
tclsml_mkpkg = 'tclsh ' + str(makepkg_script) + ' ' + str(tclsml_pkgfolder)

# Scanner for dependencies in .i files
SWIGScanner = SCons.Scanner.ClassicCPP("SWIGScan",".i","CPPPATH",'^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")')
clone.Append(SCANNERS=[SWIGScanner])

# Create the wrapp cpp
env.Command(tclsml_wrap, tclsml_i, swig_cmdline)

# Prepare environment to build shared library
clone.Prepend(CPPPATH = Split('#ClientSML/src #ElementXML/src #ConnectionSML/src /usr/include/tcl'))

if os.name == 'posix':
	clone.Append(CXXFLAGS = Split('-Wno-unused -fno-strict-aliasing'))

clone.Append(LIBS = Split('ClientSML ConnectionSML ElementXML tcl8.4'))
clone.Append(LIBPATH = ['$PREFIX/lib'])

if sys.platform == 'darwin':
	clone.Append(SHLINKFLAGS = Split('$LINKFLAGS -bundle -flat_namespace -undefined suppress'))

clone.Append(CPPFLAGS = ['-w'])

shlib = clone.SharedLibrary(tclsml_shlib, tclsml_wrap) 

shlib_installed = env.Install(tclsml_pkgfolder, shlib)
env.Command(str(tclsml_pkgfolder) + '/pkgIndex.tcl', shlib_installed, tclsml_mkpkg)
env.Clean(shlib, tclsml_pkgfolder)

