###############################################################################
##
##  Copyright (c) Tavendo GmbH and/or collaborators. All rights reserved.
##
##  Redistribution and use in source and binary forms, with or without
##  modification, are permitted provided that the following conditions are met:
##
##  1. Redistributions of source code must retain the above copyright notice,
##     this list of conditions and the following disclaimer.
##
##  2. 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.
##
##  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 HOLDER 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, commands


# This is our default build enviroment
#
env = Environment()


# Toolchain configuration
#
if 'CXX' in os.environ:
    env["CXX"] = os.environ['CXX']

if env['CXX'].startswith('g++'):

    GCC_VERSION = commands.getoutput(env['CXX'] + ' -dumpversion')
    if GCC_VERSION < "4.3.0":
        raise SCons.Errors.UserError, "GCC version {} detected with no or insufficient C++ 11 support detected".format(GCC_VERSION)

    env.Append(CXXFLAGS = ['-std=c++11',
                           '-O2',
                           '-Wall',
                           '-pedantic',
                           '-Wno-deprecated-declarations',
                           '-Wno-unused-local-typedefs',
                           '-Wl,--no-as-needed',
                           '-pthread'])

    env.Append(LINKFLAGS = ['-pthread'])

    print("Using GNU toolchain")

elif env['CXX'].startswith('clang++'):

    env.Append(CXXFLAGS = ['-std=c++11',
                           '-stdlib=libc++',
                           '-O2',
                           '-Wall',
                           '-pedantic',
                           '-Wno-unused-value',
                           '-Wno-deprecated',
                           '-pthread'])

    env.Append(LINKFLAGS = ['-stdlib=libc++', '-pthread'])

    print("Using clang toolchain")

else:
    raise SCons.Errors.UserError, "Don't know how to configure build environment for toolchain {}".format(env['CXX'])


# Boost
#
if os.environ.has_key('BOOST_ROOT'):
    env.Append(CPPPATH = [os.environ['BOOST_ROOT']])
    env.Append(LIBPATH = [os.path.join(os.environ['BOOST_ROOT'], 'stage', 'lib')])
elif os.environ.has_key('BOOST_INCLUDES') and os.environ.has_key('BOOST_LIBS'):
    env.Append(CPPPATH = [os.environ['BOOST_INCLUDES']])
    env.Append(LIBPATH = [os.environ['BOOST_LIBS']])
else:
    raise SCons.Errors.UserError, "Neither BOOST_ROOT, nor BOOST_INCLUDES + BOOST_LIBS was set!"


# MsgPack
#
if os.environ.has_key('MSGPACK_ROOT'):
    env.Append(CPPPATH = [os.path.join(os.environ['MSGPACK_ROOT'], 'include')])
    env.Append(LIBPATH = [os.path.join(os.environ['MSGPACK_ROOT'], 'lib')])
elif os.environ.has_key('MSGPACK_INCLUDES') and os.environ.has_key('MSGPACK_LIBS'):
    env.Append(CPPPATH = [os.environ['MSGPACK_INCLUDES']])
    env.Append(LIBPATH = [os.environ['MSGPACK_LIBS']])
else:
    raise SCons.Errors.UserError, "Neither MSGPACK_ROOT, nor MSGPACK_INCLUDES + MSGPACK_LIBS was set!"


# Autobahn
#
if os.environ.has_key('AUTOBAHN_ROOT'):
    env.Append(CPPPATH = [os.environ['AUTOBAHN_ROOT']])
else:
    raise SCons.Errors.UserError, "AUTOBAHN_ROOT was not set!"


# Example
#
hello = env.Program("hello.cpp", LIBS=[
  'boost_thread',
  'boost_system',
  'msgpackc'])
