# //******************************************************************
# //
# // Copyright 2015 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.
# //
# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# libocsrm (static library) build script
##

Import('env')
import os

libocsrm_env = env.Clone()

target_os = libocsrm_env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
with_upstream_libcoap = libocsrm_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
	libocsrm_env.AppendUnique(CPPPATH = ['#extlibs/libcoap/libcoap/include'])
else:
	libocsrm_env.AppendUnique(CPPPATH = ['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])

libocsrm_env.PrependUnique(CPPPATH = [
		'#/extlibs/mbedtls/mbedtls/include',
		'#/resource/csdk/logger/include',
		'#/resource/c_common/ocrandom/include',
		'#/resource/csdk/include',
		'#/resource/csdk/stack/include',
		'#/resource/csdk/stack/include/internal',
		'#/resource/oc_logger/include',
		'#/resource/csdk/connectivity/external/inc',
		'#/resource/csdk/connectivity/common/inc',
		'#/resource/csdk/connectivity/inc',
		'#/resource/csdk/connectivity/api',
		'#/resource/csdk/security/include',
		'#/resource/csdk/security/include/internal',
		'#/resource/csdk/security/provisioning/include',
		'#/extlibs/mbedtls/mbedtls/include'
		])

if target_os in ['windows', 'msys_nt']:
	libocsrm_env.AppendUnique(LIBPATH = [os.path.join(libocsrm_env.get('BUILD_DIR'), 'resource', 'oc_logger')])
    #  - warning C4200: nonstandard extension used: zero-sized array in struct/union
    #  - warning C4214: nonstandard extension used: bit field types other than int
    #    - warnings inherited from a header included from libcoap
	libocsrm_env.AppendUnique(CCFLAGS=['/wd4200', '/wd4214', '/W4', '/WX'])

if target_os in ['linux', 'android', 'tizen', 'msys_nt', 'windows'] and libocsrm_env.get('SECURED') == '1':
	SConscript('provisioning/SConscript', 'libocsrm_env')

if target_os in ['linux', 'windows', 'darwin'] and libocsrm_env.get('SECURED') == '1':
	SConscript('tool/SConscript', 'libocsrm_env')

# As in the source code, it includes arduino Time library (C++)
# It requires compile the .c with g++
if target_os == 'arduino':
	libocsrm_env.Replace(CC = libocsrm_env.get('CXX'))
	libocsrm_env.Replace(CFLAGS = libocsrm_env.get('CXXFLAGS'))

if target_os not in ['arduino', 'windows']:
	libocsrm_env.AppendUnique(CPPDEFINES  = ['WITH_POSIX', '_GNU_SOURCE'])
	libocsrm_env.AppendUnique(CFLAGS = ['-std=c99', '-Wall'])

libocsrm_env.AppendUnique(LIBS = ['coap'])

if target_os == 'arduino':
	libocsrm_env.AppendUnique(CPPDEFINES = ['NDEBUG', 'WITH_ARDUINO'])
elif target_os not in ['windows', 'msys_nt']:
	libocsrm_env.AppendUnique(CFLAGS = ['-fPIC'])
	libocsrm_env.AppendUnique(LIBS = ['m'])

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

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

if env.get('MULTIPLE_OWNER') == '1':
	libocsrm_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])

######################################################################
# Source files and Targets
######################################################################
OCSRM_SRC = 'src/'
libocsrm_src = [
	OCSRM_SRC + 'secureresourcemanager.c',
	OCSRM_SRC + 'resourcemanager.c',
	OCSRM_SRC + 'aclresource.c',
	OCSRM_SRC + 'verresource.c',
	OCSRM_SRC + 'amaclresource.c',
	OCSRM_SRC + 'pstatresource.c',
	OCSRM_SRC + 'doxmresource.c',
	OCSRM_SRC + 'credresource.c',
	OCSRM_SRC + 'pconfresource.c',
	OCSRM_SRC + 'dpairingresource.c',
	OCSRM_SRC + 'policyengine.c',
	OCSRM_SRC + 'psinterface.c',
	OCSRM_SRC + 'srmresourcestrings.c',
	OCSRM_SRC + 'srmutility.c',
	OCSRM_SRC + 'iotvticalendar.c',
	OCSRM_SRC + 'base64.c',
	OCSRM_SRC + 'directpairing.c',
	OCSRM_SRC + 'deviceonboardingstate.c'
	]

if libocsrm_env.get('SECURED') == '1':
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'oxmpincommon.c', OCSRM_SRC + 'pbkdf2.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'crlresource.c', OCSRM_SRC + 'pkix_interface.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'oxmverifycommon.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'certhelpers.c', OCSRM_SRC + 'occertutility.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'csrresource.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'rolesresource.c']
	libocsrm_src = libocsrm_src + [OCSRM_SRC + 'ocsecurity.c']

if target_os in ['windows', 'msys_nt']:
	libocsrm_src  = libocsrm_src + [OCSRM_SRC + 'strptime.c']

libocsrm_src.extend(env['cbor_files'])

# Insert a hack for Arduino, whose compiler may not support all defines expected
# by tinycbor
if target_os in ['arduino']:
	libocsrm_env.AppendUnique(CPPDEFINES = ['INT64_MAX=0x7FFFFFFFFFFFFFFF'])

libocsrm = libocsrm_env.StaticLibrary('ocsrm', libocsrm_src)

libocsrm_env.InstallTarget(libocsrm, 'ocsrm')
