#===============================================================================
#  Copyright (c) 2011-2020 QUALCOMM Technologies Incorporated.
#  All Rights Reserved.
#  Qualcomm Confidential and Proprietary
#===============================================================================

#-------------------------------------------------------------------------------
# Source PATH
#-------------------------------------------------------------------------------
import os
import re
Import('env')
env = env.Clone()

if not hasattr(env, 'IMAGE_ALIASES'):
  aliases = []
  aliases = ['tima_attestation',]
  env.Replace(OUT_DIR = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/tz_atn/build')
  env.Replace(LIB_OUT_DIR = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/tz_atn/build/${SHORT_BUILDPATH}')

if env.has_key('USES_NO_CP'):
  env.Append(CCFLAGS = ' -DUSES_NO_CP ')

env.Append(CCFLAGS = " -DCONFIG_QSEE")
env.Append(CCFLAGS = ' -Wno-pointer-sign ')
env.Append(CCFLAGS = ' -Wno-implicit-function-declaration ')
env.Append(CCFLAGS = ' -Wno-strlcpy-strlcat-size ')
env.Append(CCFLAGS = ' -Wno-string-plus-int ')
env.Append(CCFLAGS = ' -Wno-unused-parameter ')
env.Append(CCFLAGS = ' -Wno-missing-field-initializers ')

new_ta_build = os.getenv('SUPPORT_NEW_TA_BUILD', 'false')
if new_ta_build in ['true']:
  env.Append(LINKFLAGS= ' -no-threads ')

#------------------------------------------------------------------------------
# We need to specify "neon" to generate SIMD instructions in 32-bit mode
#------------------------------------------------------------------------------
if env['PROC'] == 'scorpion':
  env.Append(CCFLAGS = " -mfpu=neon ")

env.Append(CPPDEFINES = 'TIMA_ATTESTATION=1')

includes = [
   '#../../core/api/boot/qfprom',
   '#../../core/api/kernel/libstd/stringl',
   '#../../core/api/services',
   '#../../ssg/api/securemsm/trustzone/qsee',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_attestation/inc',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_atn_common/tima_common/shared/inc',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_atn_common/tz_common/public',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_atn_common/tz_platform/public/tl',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_atn_common/tz_platform/vendor/QSEE/tl',
   '#../../ssg/securemsm/trustzone/qsapps/tima/tima_atn_common/uclib',
   '#../../ssg/securemsm/tz_iccc_common/public', # ICCC
]

#----------------------------------------------------------------------------
# App core Objects
#----------------------------------------------------------------------------
sources = [
        '../../tima_attestation/src/app_main.c',
        '../../tima_attestation/src/tima_attestation.c',
        '../../tima_atn_common/tima_common/shared/src/dashboard.c',
        '../../tima_atn_common/tz_platform/vendor/QSEE/tl/QSEE_TZ_Vendor.c',
        '../../tima_atn_common/tz_common/public/init_tl.c', # dummy source for init_tl.h
        '../../../../../tz_iccc_common/tl/tz_iccc_common_tl.c', # ICCC
]

target_name = 'tima_atn'
app_name = 'tima_atn'

print("[Attestation] TA build for CHIPSET : " + env['CHIPSET'])
#-------------------------------------------------------------------------------
# Add ICCC flag for new chipsets that support not V4 but legacy ICCC
#-------------------------------------------------------------------------------
if env['CHIPSET'] in ['rennell']:
  env.Append(CCFLAGS = " -DCONFIG_READ_VBHASH_WITH_ICCC")

#-------------------------------------------------------------------------------
# Add metadata to image
#-------------------------------------------------------------------------------
if env['CHIPSET'] in ['sm8250','saipan','bitra','lahaina','kodiak']: # if env['CHIPSET'] in ['sm8250','sm7250','sm7225','sm8350']:
  md = {
    'appName':    app_name,
    'privileges': ['default',
                   'System',
                   'WhitelistBypass',
                   'HwFuse',
                   'OEMBuf',
                   'ICCCReadData' # ICCC v4 TA-to-TA
                  ],
    'heapSize':  0x10000,
  }
  env.Append(CCFLAGS = " -DCONFIG_READ_VBHASH_WITH_ICCC")
else:
  md = {
    'appName':    app_name,
    'privileges': ['default',
                   'System',    
                   'WhitelistBypass',
                   'HwFuse',
                   'OEMBuf',
                  ],
   'heapSize':  0x10000,
  }

deploy_header_files = env.Glob('../inc/*')

timaatn_units = env.SecureAppBuilder(
  sources = sources,
  includes = includes,
  metadata = md,
  image = target_name,
  deploy_sources = sources + ['SConscript'] + deploy_header_files
)
  
if hasattr(env, 'IMAGE_ALIASES'):
  for image in env['IMAGE_ALIASES']:
    env.Alias(image, timaatn_units)
else:
  env.Alias(aliases, timaatn_units)