#    MynahSA Top Level SConstruct file
#    Copyright (C) 2006 Mynah-Software Ltd. All Rights Reserved.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License, version 2 
#    as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import sys
import string
import SCons

#User modifyable options
DEBUG = 0

#Specify path options for OpenSSL - only if required.  This generally means you must specify these items on Windows
# and Solaris, and generally they are not needed on linux/macosx.
OPENSSL_INC_PATH=''     #'c:\openssl\include'
OPENSSL_LIB_PATH=''     #c:\openssl\lib'
    
# Boost library specifications.  Enabling MYNAHSA_USE_BOOST replaces the MynahSA built-in
#  threading, shared pointer and type traits libraries with Boost's versions.  If your projet uses
#  boost for other things, it is strongly recommended that you configure and use boost below.
MYNAHSA_USE_BOOST = 0
#The name of the boost::thread library, only used if MYNAHSA_USE_BOOST is 1
BOOST_THREADS_LIBRARY = 'boost_thread-gcc-mt-d-1_33_1'
#If boost requires a BOOST_HAS_THREADS definition, set this to 1.  
#  It is obvious if this is required because error messages stating boost has not been compiled
#  with threads result otherwise.
BOOST_REQUIRES_THREADS = 0
# The path to the boost libraries, only used if MYNAHSA_USE_BOOST is 1
BOOST_LIB_PATH='/usr/local/lib64/boost-1_33_1'
# The path to the boost include files, only used if MYNAHSA_USE_BOOST is 1
BOOST_INC_PATH='/usr/local/include/boost-1_33_1'    
    
    
# initial variable declarations.  These can be edited here
defines = []                 # definitions passed to compiliation stage
libs = []                    # libraries to link against
ldflags = []                 # link flags
libpath = []                 # library search path
cflags = []                  # compile flags
incpath = []                 # include paths


# Code for building follows
env = None                   # SConstruct environment

if MYNAHSA_USE_BOOST == 1:
  if BOOST_REQUIRES_THREADS == 1:
    defines += ['BOOST_HAS_THREADS']
  incpath += [BOOST_INC_PATH]
  libpath += [BOOST_LIB_PATH]
  libs += [BOOST_THREADS_LIBRARY]
  defines += ['MYNAHSA_USE_BOOST']
   
   
   
if OPENSSL_INC_PATH != '':
    incpath += [OPENSSL_INC_PATH]
if OPENSSL_LIB_PATH != '':
    libpath += [OPENSSL_LIB_PATH]


# add local paths to incpath
incpath += ['#.']          # note the pound character denotes directory local to top leve SConstruct file
libpath += ['#/lib']
            
# add local definition for debugging
if DEBUG==1:
    defines += ['DEBUG']

if os.name== 'nt':
    osname = 'nt'
elif os.name == 'posix':
    osname = os.uname()[0]  # set to Linux for linux, SunOS for Solaris
    
if osname == 'nt':    

    env = Environment(ENV = {'PATH' : os.environ[ 'PATH' ] },
                      tools=['msvs','default'],
                      toolpath=['.'],
                      MSVS_VERSION='7.1',
                      INCDIR=incpath,
                      CPPPATH=incpath)

    env['ENV']['TMP'] = os.environ['TMP']  # this is a workaround for a SCons bug
    
    defines = ['WIN32',
               'OPENSSL_SYSNAME_WIN32',
               'WIN32_LEAN_AND_MEAN',
               'L_ENDIAN',
               'DSO_WIN32',
               '_CRT_SECURE_NO_DEPRECATE',
               'OPENSSL_SYSNAME_WINNT']
    
    incpath += string.split(SCons.Tool.msvc.get_msvc_default_paths('7.1')[0],';')

    
    #Switch on exception handling, multithreaded debug, etc..
    cflags   = ['/EHsc', '/MD', '/GR', '/Z7']
    
    # for different dependencies
    libs += ['kernel32',
            'user32',
            'gdi32',
            'comdlg32',
            'advapi32',
            'shell32',
            'ole32',
            'oleaut32',
            'uuid',
            'imm32',
            'winmm',
            'wsock32',
            'winspool',
            'delayimp',
            'netapi32',
            'iphlpapi',
            'libeay32',
            'ssleay32']
    
#    libs += [BOOST_THREADS_WIN32]
    
    libpath +=  string.split(SCons.Tool.msvc.get_msvc_default_paths('7.1')[1],';')

    env = Environment(ENV = {'PATH' : os.environ[ 'PATH' ] },
                      tools=['msvs','default'],
                      toolpath=['.'],
                      MSVS_VERSION='7.1',
                      INCDIR=incpath,
                      CPPPATH=incpath,
                      CPPDEFINES=defines,
                      CCFLAGS=cflags,
                      LIBS=['mynahsa']+libs,
                      LINKFLAGS=ldflags,
                      LIBPATH=libpath)
    env['ENV']['TMP'] = os.environ['TMP']
    
elif osname == 'Linux':
    libs += Split('ssl crypto pthread')


    if DEBUG == 1:
        cflags += ['-ggdb']
    else:
        cflags += ['-O2']
        
    env = Environment(INCDIR=incpath,
                      CPPPATH=incpath,
                      CPPDEFINES=defines,
                      CCFLAGS=cflags,
                      LIBS=['mynahsa']+libs,
                      LINKFLAGS=ldflags,
                      LIBPATH=libpath)
    
elif osname == 'SunOS':
    # configure options for solaris
    libs += Split('ssl crypto nsl socket resolv pthread')
    if DEBUG == 1:
        cflags += ['-g']
    else:
        cflags += ['-O2']
        

    env = Environment(INCDIR=incpath,
                      CPPPATH=incpath,
                      CPPDEFINES=defines,
                      CCFLAGS=cflags,
                      LIBS=['mynahsa']+libs,
                      LINKFLAGS=ldflags,
                      LIBPATH=libpath)

elif osname == 'Darwin':
    # configure options for MacOSX
    libs += Split('ssl crypto pthread')
    if DEBUG == 1:
        cflags += ['-g']
    else:
        cflags += ['-O2']

    env = Environment(INCDIR=incpath,
                      CPPPATH=incpath,
                      CPPDEFINES=defines,
                      CCFLAGS=cflags,
                      LIBS=['mynahsa']+libs,
                      LINKFLAGS=ldflags,
                      LIBPATH=libpath)

Export('env')


lib = SConscript('src/SConstruct')
SConscript('src/SConstruct')

examples = SConscript('examples/SConstruct')
tests = SConscript('test/SConstruct')
Depends(tests, lib)
Depends(examples, lib)
