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

##
# Notification Service c++ wrapper build script
##

import platform
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'])

lib_env = env.Clone()
SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
notification_env = lib_env.Clone()

target_os = env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
notification_env.AppendUnique(CPPPATH = [
    '../../include',
    'inc',
    '../common',
    '../consumer/inc',
    '../../src/common',
    '#/resource/csdk/security/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/include',
    '#/resource/oc_logger/include'
])

notification_env.PrependUnique(LIBS = [
    'oc_logger',
    'oc',
    'octbstack',
    'notification_provider',
    'resource_directory'
    ])
notification_env.AppendUnique(CXXFLAGS = ['-std=c++0x','-frtti'])

if target_os not in ['windows', 'winrt']:
    notification_env.AppendUnique(CCFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0'])

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

if target_os == 'linux':
    notification_env.AppendUnique(LIBS = ['pthread'])
    
if target_os == 'android':
    notification_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
    notification_env.PrependUnique(LIBS = ['gnustl_shared', 'log'])
    notification_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_provider_wrapper.so'])

if not env.get('RELEASE') and target_os not in ['ios']:
    notification_env.PrependUnique(LIBS = ['gcov'])
    notification_env.AppendUnique(CCFLAGS = ['--coverage'])

if env.get('WITH_CLOUD') == True:
    notification_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])

with_mq = env.get('WITH_MQ')
if 'SUB' in with_mq:
    notification_env.AppendUnique(CPPDEFINES = ['MQ_SUBSCRIBER', 'WITH_MQ'])
    print "MQ SUB support"

if 'PUB' in with_mq:
    notification_env.AppendUnique(CPPDEFINES = ['MQ_PUBLISHER', 'WITH_MQ'])
    print "MQ PUB support"

if 'BROKER' in with_mq:
    notification_env.AppendUnique(CPPDEFINES = ['MQ_BROKER', 'WITH_MQ'])
    print "MQ Broker support"
######################################################################
# Source files and Targets for Provider
######################################################################
notification_jni_provider_env = notification_env.Clone()

notificationCommonStaticObjs = [
    notification_env.Object('../common/NSMediaContents.cpp'),
    notification_env.Object('../common/NSMessage.cpp'),
    notification_env.Object('../common/NSSyncInfo.cpp'),
    notification_env.Object('../common/NSTopic.cpp'),
    notification_env.Object('../common/NSTopicsList.cpp')]

notificationCommonSharedObjs = [
    notification_env.SharedObject('../common/NSMediaContents.cpp'),
    notification_env.SharedObject('../common/NSMessage.cpp'),
    notification_env.SharedObject('../common/NSSyncInfo.cpp'),
    notification_env.SharedObject('../common/NSTopic.cpp'),
    notification_env.SharedObject('../common/NSTopicsList.cpp')]

notification_provider_src = [
    env.Glob('src/*.cpp'),notificationCommonSharedObjs]

if target_os in ['android']:
    notification_env.AppendUnique(LINKFLAGS = ['-Wl,-soname,libnotification_provider_wrapper.so'])
if target_os not in ['ios']:
    providersdk = notification_env.SharedLibrary('notification_provider_wrapper', notification_provider_src)
    notification_env.InstallTarget(providersdk, 'libnotification_provider_wrapper')
    notification_env.UserInstallTargetLib(providersdk, 'libnotification_provider_wrapper')

notification_provider_src = [
    env.Glob('src/*.cpp'),notificationCommonStaticObjs]

providersdk = notification_env.StaticLibrary('notification_provider_wrapper', notification_provider_src)
notification_env.InstallTarget(providersdk, 'libnotification_provider_wrapper')
notification_env.UserInstallTargetLib(providersdk, 'libnotification_provider_wrapper')
notification_env.UserInstallTargetHeader('inc/NSConsumer.h', 'service/notification', 'NSConsumer.h')
notification_env.UserInstallTargetHeader('inc/NSProviderService.h', 'service/notification', 'NSProviderService.h')

Export('notificationCommonStaticObjs')
Export('notificationCommonSharedObjs')

######################################################################
# Source files and Targets for Provider Jni
######################################################################
if target_os == 'android':
    notification_jni_provider_env.AppendUnique(CPPPATH = ['../../../../extlibs/boost/boost_1_58_0'])
    notification_jni_provider_env.AppendUnique(CPPPATH = ['../../../../java/jni'])
    notification_jni_provider_env.AppendUnique(CPPPATH = ['../../android/notification-service/src/main/jni/common'])
    notification_jni_provider_env.AppendUnique(CPPPATH = ['../../android/notification-service/src/main/jni/provider'])

    notification_jni_provider_env.PrependUnique(LIBS = [
        'notification_provider_wrapper'
        ])

    notificationJniCommonSharedObjs = [
        notification_jni_provider_env.SharedObject('../../android/notification-service/src/main/jni/common/JniNotificationCommon.cpp')]

    notification_provider_jni_src = [
        env.Glob('../../android/notification-service/src/main/jni/provider/*.cpp'),
        notificationJniCommonSharedObjs]

    providerJni = notification_jni_provider_env.SharedLibrary('notification_provider_jni', notification_provider_jni_src)
    notification_jni_provider_env.InstallTarget(providerJni, 'libnotification_provider_jni')
    notification_jni_provider_env.UserInstallTargetLib(providerJni, 'libnotification_provider_jni')

    Export('notificationJniCommonSharedObjs')
