#******************************************************************
#
# Copyright 2017 Intel Mobile Communications GmbH 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
##
# LifX Plugin build script
##

import os
import os.path

Import('env')

target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')
bridging_path = os.path.join(src_dir, 'bridging')

lifx_env = env.Clone()

print "Reading LifX Plugin script"

######################################################################
# Build flags
######################################################################

def maskFlags(flags):
    flags = [flags.replace('-Wl,--no-undefined', '' ) for flags in flags]
    return flags

lifx_env.PrependUnique(CPPPATH = [ os.path.join(src_dir, 'resource', 'c_common', 'oic_malloc', 'include'),
                              os.path.join(src_dir, 'resource', 'c_common', 'oic_string', 'include'),
                              os.path.join(src_dir, 'resource', 'c_common'),
                              os.path.join(src_dir, 'resource', 'oc_logger', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'logger', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'include'),
                              os.path.join(src_dir, 'resource', 'csdk', 'stack', 'include'),
                              os.path.join(src_dir, 'resource', 'include'),
                              os.path.join(src_dir, 'extlibs', 'cjson'),
                              os.path.join(src_dir, 'extlibs', 'tinycbor', 'src'),
                              os.path.join(src_dir, 'extlibs', 'rapidjson', 'rapidjson', 'include', 'rapidjson')
                              ])
lifx_env.AppendUnique(CPPPATH = [ os.path.join(bridging_path, 'include'),
                             os.path.join(bridging_path, 'plugins', 'lifx_plugin', 'lifx_objects')
                             ])

if target_os not in ['arduino', 'windows']:
    lifx_env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])

if target_os in ['darwin','ios']:
    lifx_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])

if 'g++' in lifx_env.get('CXX'):
    lifx_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-Wextra', '-Werror'])

lifx_env.AppendUnique(RPATH = [lifx_env.get('BUILD_DIR')])
lifx_env.AppendUnique(LIBPATH = [lifx_env.get('BUILD_DIR')])

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

lifx_env['LINKFLAGS'] = maskFlags(env['LINKFLAGS'])
lifx_env.AppendUnique(LINKFLAGS = ['-Wl,--allow-shlib-undefined'])
lifx_env.AppendUnique(LINKFLAGS = ['-Wl,--whole-archive', lifx_env.get('BUILD_DIR') +'libmpmcommon.a','-Wl,-no-whole-archive'])

lifx_env.AppendUnique(LIBS = ['m',
                               'octbstack',
                               'ocsrm',
                               'connectivity_abstraction',
                               'coap',
                               'curl' ])

#####################################################################
# Source files and Target(s)
######################################################################
lifx_src = [
         os.path.join(bridging_path, 'plugins', 'lifx_plugin', 'lifxResource.cpp'),
         os.path.join(bridging_path, 'plugins', 'lifx_plugin', 'lifx_objects', 'lifx.cpp'),
         ]

lifx_env.AppendUnique(LIFX_SRC = lifx_src)
lifxlib = lifx_env.SharedLibrary('lifxplugin', lifx_env.get('LIFX_SRC'))
lifx_env.InstallTarget(lifxlib, 'lifxplugin')
lifx_env.UserInstallTargetLib(lifxlib, 'lifxplugin')
