###
# Copyright (c) 2002, Tamer Fahmy <tamer@tammura.at>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of the copyright holder nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import os, sys
import distutils.sysconfig

SetOption('implicit_cache', 1)

opts = Options('custom.py')
opts.AddOptions(BoolOption('warnings', 'compilation with -Wall', 0),
                BoolOption('debug', 'debug output and symbols', 0))

env = Environment(ENV = os.environ,
                  options = opts,
                  tools=['default'])

Help(opts.GenerateHelpText(env))

if env['debug']:
    env.Append(CCFLAGS = (str(Platform()) == 'win32') and ['/Zi'] or ['-g'])
else:
    env.Append(CCFLAGS = (str(Platform()) == 'win32') and ['/O2'] or ['-O2'])

if str(Platform()) != 'win32' and env['warnings']:
    env.Append(CCFLAGS = '-Wall')


if str(Platform()) == 'win32':
    env.Append(CPPPATH = os.getenv("COIN3DDIR") + os.sep + 'include')
    env.Append(CPPDEFINES = 'COIN_DLL')
    env.Append(CPPFLAGS = Split('/wd4244 /wd4049'))
    env.Append(LIBPATH = (sys.exec_prefix + os.sep + 'libs').split())
    env.Append(LIBPATH = os.getenv('COIN3DDIR') + '\\lib' )
    env.Append(LIBS = 'coin2.lib')
    INCLUDE_DIR = os.getenv("COIN3DDIR") + os.sep + 'include'
else:
    env.ParseConfig('coin-config --cppflags --libs --ldflags')
    INCLUDE_DIR = os.popen('coin-config --includedir').read()

PIVY_ROOT = '../..'

env.Append(CPPPATH = ['.', PIVY_ROOT + '/interfaces', distutils.sysconfig.get_python_inc()])
env.Append(LIBPATH = [PIVY_ROOT, distutils.sysconfig.get_python_lib()])
env.Append(LINKFLAGS = distutils.sysconfig.get_config_vars().get('LINKFORSHARED', '').split())

SWIG_SUPPRESS_WARNINGS = '-w302,306,307,312,389,361,362,467,503,509,510'
env.Append(SWIGFLAGS = '-c++ -python -includeall -modern -D__PIVY__ ' +
           '-I. -I%s/interfaces -I%s/fake_headers -I/usr/local/include -I%s %s' %
           (PIVY_ROOT, PIVY_ROOT, INCLUDE_DIR, SWIG_SUPPRESS_WARNINGS))

if str(Platform()) == 'win32':
    env.Append(SWIGFLAGS = '-DPIVY_WIN32')

lib = env.SharedLibrary('_shapescale', Split('shapescale.i ShapeScale.cpp'),
                        SHLIBPREFIX = '',
                        SHLIBSUFFIX = distutils.sysconfig.get_config_vars()['SO'],
                        SWIGCXXFILESUFFIX = '_wrap.cpp')

env.Clean(lib, Split('shapescale.py shapescale.pyc'))
