# Copyright AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
import os

Import('env')

if not env.has_key('GTEST_DIR'):
    print('GTEST_DIR not specified skipping alljoyn_c unit test build')
else:
    gtest_env = env.Clone();
    gtest_dir = gtest_env['GTEST_DIR']
    vars = Variables();
    vars.AddVariables(('GTEST_HOME', '', gtest_dir))
    vars.Update(gtest_env)

    if gtest_env['CXX'].startswith('clang'):
        # Suppress a clang warning in google-test code.
        gtest_env.Append(CPPFLAGS = ['-Wno-unused-private-field'])

    if gtest_dir == '/usr':
        gtest_src_base = os.path.join(gtest_dir, 'src', 'gtest')
    else:
        gtest_src_base = gtest_dir

    if gtest_env['OS_GROUP'] == 'windows':
        # gTest does not require the same CPPDEFINES as AllJoyn core.
        gtest_env.Append(CPPDEFINES = ['WIN32', '_LIB'])
        # don't use the _DEBUG define unless the /MDd compiler flag is specified
        #gtest_env.Append(CPPDEFINES = ['WIN32', '_DEBUG', '_LIB'])
        gtest_env.Append(CXXFLAGS = ['/EHsc'])

    if gtest_env['OS_CONF'] == 'android':
        # used by gtest to prevent use of wcscasecmp and set GTEST_HAS_STD_WSTRING=0
        gtest_env.Append(CPPDEFINES = ['ANDROID'])

    # tr1::tuple is not avalible for android or darwin
    if gtest_env['OS_CONF'] in [ 'android', 'darwin' ]:
        gtest_env.Append(CPPDEFINES = ['GTEST_HAS_TR1_TUPLE=0'])
    # clone() library function is NOT available on android-x86
    if gtest_env['OS_CONF'] == 'android' and gtest_env['CPU'] == 'x86':
        gtest_env.Append(CPPDEFINES = ['GTEST_HAS_CLONE=0'])
    # Microsoft Visual Studio 2012 has a different _VARIADIC_MAX default value.
    # See: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
    if gtest_env['OS_CONF'] == 'windows' and (gtest_env['MSVC_VERSION'] == '11.0' or gtest_env['MSVC_VERSION'] == '11.0Exp'):
        gtest_env.Append(CPPDEFINES = ['_VARIADIC_MAX=10'])
    #we compile with no rtti and we are not using exceptions. 
    gtest_env.Append(CPPDEFINES = ['GTEST_HAS_RTTI=0'])
    # we replace AllJoyn's include CPPPATH options.  AllJoyn includes stlport that will cause the 
    # gTest code to not compile as expected at this time
    gtest_env.Append(CPPPATH = [ gtest_src_base ])
    if gtest_dir != '/usr':
        gtest_env.Append(CPPPATH = [ gtest_env.Dir('$GTEST_DIR/include') ])

    gtest_obj = gtest_env.StaticObject(target = 'gtest-all', source = [ '%s/src/gtest-all.cc' % gtest_src_base ])
    gtest_env.StaticLibrary(target = 'gtest', source = gtest_obj)
    
    test_src = gtest_env.Glob('*.cc')

    unittest_env = env.Clone()

    if unittest_env['BR'] == 'on':
        # Build apps with bundled daemon support
        unittest_env.Prepend(LIBS = [unittest_env['ajrlib']])

    unittest_env.Prepend(LIBS = unittest_env['ALLJOYN_C_LIB_STATIC'])

    gtest_dir = unittest_env['GTEST_DIR']
    if gtest_dir != '/usr':
        unittest_env.Append(CPPPATH = [gtest_dir + '/include'])

    if unittest_env['OS_GROUP'] == 'windows':
        unittest_env.Append(CXXFLAGS = ['/EHsc'])


    #we compile with no rtti and we are not using exceptions.
    unittest_env.Append(CPPDEFINES = ['GTEST_HAS_RTTI=0'])

    if unittest_env['OS_CONF'] == 'android':
        # used by gtest to prevent use of wcscasecmp and set GTEST_HAS_STD_WSTRING=0
        unittest_env.Append(CPPDEFINES = ['ANDROID'])

    # tr1::tuple is not avalible for android or darwin
    if unittest_env['OS_CONF'] in [ 'android', 'darwin' ]:
        unittest_env.Append(CPPDEFINES = ['GTEST_HAS_TR1_TUPLE=0'])
    if unittest_env['OS_CONF'] == 'android' and unittest_env['CPU'] == 'x86':
        unittest_env.Append(CPPDEFINES = ['GTEST_HAS_CLONE=0'])
    if unittest_env['OS_CONF'] == 'windows' and unittest_env['MSVC_VERSION'] == '11.0':
        unittest_env.Append(CPPDEFINES = ['_VARIADIC_MAX=10'])
    #gtest library file is placed on folder above the the object files.
    unittest_env.Append(LIBPATH = ['./'])
    unittest_env.Prepend(LIBS = ['gtest'])
    
    objs = [ unittest_env.Object(test_src) ]

    unittest_prog = unittest_env.Program('ajctest', objs)
    unittest_env.Install('$C_TESTDIR/bin', unittest_prog)
    
    #install gtest utilities
    unittest_env.Install('$C_TESTDIR/bin', Dir('test_report').srcnode())
