#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# ThingsManager Unit Test build script
##
import os
Import('env')

if env.get('RELEASE'):
        env.AppendUnique(CCFLAGS = ['-Os'])
        env.AppendUnique(CPPDEFINES = ['NDEBUG'])
else:
        env.AppendUnique(CCFLAGS = ['-g'])

if env.get('LOGGING'):
        env.AppendUnique(CPPDEFINES = ['TB_LOG'])

# Add third party libraries
lib_env = env.Clone()
SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')

target_os = env.get('TARGET_OS')
if target_os == 'linux':
        # Verify that 'google unit test' library is installed.  If not,
        # get it and install it
        SConscript(env.get('SRC_DIR') + '/extlibs/gtest/SConscript')

        # Verify that 'hippomocks' mocking code is installed.  If not,
        # get it and install it
        SConscript(env.get('SRC_DIR') + '/extlibs/hippomocks.scons')

ThingsManager_gtest_env = lib_env.Clone()

######################################################################
#unit test setting
######################################################################
src_dir = lib_env.get('SRC_DIR')
gtest_dir = src_dir + '/extlibs/gtest/gtest-1.7.0'

######################################################################
# Build flags
######################################################################
gtest = File(gtest_dir + '/lib/.libs/libgtest.a')
gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a')

ThingsManager_gtest_env.AppendUnique(
        CPPPATH = [
                src_dir + '/extlibs/hippomocks-master',
                src_dir + '/extlibs/gtest/gtest-1.7.0/include',
                '../sdk/inc',
                '../../../extlibs/timer'
        ])

if target_os not in ['windows', 'winrt']:
        ThingsManager_gtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall'])
        if target_os != 'android':
                ThingsManager_gtest_env.AppendUnique(CXXFLAGS = ['-pthread'])
                ThingsManager_gtest_env.AppendUnique(LIBS = ['pthread'])

ThingsManager_gtest_env.PrependUnique(LIBS = [
    'libTGMSDKLibrary',
    'oc',
    'octbstack',
    'oc_logger',
    'oc_logger_core',
    'connectivity_abstraction',
    gtest,
    gtest_main])

ThingsManager_gtest_env.AppendUnique(LIBS = ['dl'])

######################################################################
# Build Test
######################################################################
ThingsManager_gtest_src = env.Glob('./*.cpp')

ThingsManagerTest = ThingsManager_gtest_env.Program('ThingsManagerTest', ThingsManager_gtest_src)
Alias("ThingsManagerTest", ThingsManagerTest)
env.AppendTarget('ThingsManagerTest')

if env.get('TEST') == '1':
    target_os = env.get('TARGET_OS')
    if target_os == 'linux':
        from tools.scons.RunTest import *
        run_test(ThingsManager_gtest_env, '',
                'service/things-manager/unittests/ThingsManagerTest')