#******************************************************************
#
# 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# rcs_common (primitiveResource and expiryTimer) build script
##
import os

# SConscript file for Local PKI google tests
gtest_env = SConscript('#extlibs/gtest/SConscript')
lib_env = gtest_env.Clone()

# Add third party libraries
SConscript('#service/third_party_libs.scons', exports = 'lib_env')
rcs_common_env = lib_env.Clone()
target_os = rcs_common_env.get('TARGET_OS')
release = rcs_common_env.get('RELEASE')
src_dir = rcs_common_env.get('SRC_DIR')

######################################################################
# Build flags
######################################################################
rcs_common_env.AppendUnique(CPPPATH = [
    rcs_common_env.get('SRC_DIR')+'/extlibs',
    '../../include',
    'primitiveResource/include'])

rcs_common_env.AppendUnique(CPPPATH = [
    'expiryTimer/include',
    'expiryTimer/src'])

rcs_common_env.AppendUnique(LIBPATH = [rcs_common_env.get('BUILD_DIR')])

if target_os not in ['windows']:
    rcs_common_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall'])

if target_os not in ['darwin', 'ios', 'windows']:
    rcs_common_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined'])
    rcs_common_env.AppendUnique(LIBS = ['dl'])

if target_os == 'android':
    rcs_common_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
    rcs_common_env.PrependUnique(LIBS = ['gnustl_shared', 'log'])

rcs_common_env.AppendUnique(LIBS = ['oc'])

if not release:
    rcs_common_env.AppendUnique(CXXFLAGS = ['--coverage'])
    rcs_common_env.PrependUnique(LIBS = ['gcov'])

if rcs_common_env.get('SECURED') == '1':
	if rcs_common_env.get('WITH_TCP') == True:
		rcs_common_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto'])
######################################################################
# Source files and Targets
######################################################################
TIMER_SRC_DIR = 'expiryTimer/src/'
RESOURCE_SRC = 'primitiveResource/src/'
rcs_common_src = [
        TIMER_SRC_DIR + 'ExpiryTimerImpl.cpp',
        TIMER_SRC_DIR + 'ExpiryTimer.cpp',
        RESOURCE_SRC + 'PresenceSubscriber.cpp',
        RESOURCE_SRC + 'PrimitiveResource.cpp',
        RESOURCE_SRC + 'RCSException.cpp',
        RESOURCE_SRC + 'RCSAddress.cpp',
        RESOURCE_SRC + 'RCSResourceAttributes.cpp',
        RESOURCE_SRC + 'RCSRepresentation.cpp'
        ]

rcs_common_static = rcs_common_env.StaticLibrary('rcs_common', rcs_common_src)
if target_os not in ['ios']:
    rcs_common_shared = rcs_common_env.SharedLibrary('rcs_common', rcs_common_src)
    rcs_common_env.InstallTarget([rcs_common_static,rcs_common_shared], 'rcs_common')
    rcs_common_env.UserInstallTargetLib([rcs_common_static,rcs_common_shared], 'rcs_common')
else:
    rcs_common_env.InstallTarget([rcs_common_static], 'rcs_common')
    rcs_common_env.UserInstallTargetLib([rcs_common_static], 'rcs_common')

######################################################################
# Build Test Expiry Timer and primitive Resource
######################################################################
if target_os in ['linux']:
	rcs_common_test_env = rcs_common_env.Clone();

	rcs_common_test_env.PrependUnique(CPPPATH = [
		rcs_common_test_env.get('SRC_DIR')+'/extlibs/hippomocks-master',
		'utils/include'
		])

	rcs_common_test_env.PrependUnique(LIBS = [
		'octbstack',
		'oc_logger',
		'connectivity_abstraction',
		'coap',
		'rcs_common'])

	rcs_common_test_src = [
		rcs_common_test_env.Glob('primitiveResource/unittests/*.cpp'),
		'expiryTimer/unittests/ExpiryTimerTest.cpp'
		]

	rcs_common_test = rcs_common_test_env.Program('rcs_common_test', rcs_common_test_src)
	Alias("rcs_common_test", rcs_common_test)
	rcs_common_test_env.AppendTarget('rcs_common_test')

	if rcs_common_test_env.get('TEST') == '1':
		from tools.scons.RunTest import run_test
		run_test(rcs_common_test_env,
# TODO: fix test on linux
			'',
#			'service_resource-encapsulation_src_common_rcs_common_test.memcheck',
			'service/resource-encapsulation/src/common/rcs_common_test')
