#===============================================================================
#
# App Core
#
# GENERAL DESCRIPTION
#    build script
#
#
#-------------------------------------------------------------------------------
#
#                      EDIT HISTORY FOR FILE
#
#  This section contains schedulerents describing changes made to the module.
#  Notice that changes are listed in reverse chronological order.
#
# when       who     what, where, why
# --------   ---     ---------------------------------------------------------
# 08/14/17    dr     Port to sdm845
# 01/01/17    dr     Created
#===============================================================================

import os

try:
  Import('env')
except:
  #We're doing an out of tree build so we need to setup the env to build our TA
  
  # First, call into the QTEE SDK to get an Environment with all the tools
  try:
    # Either set $QTEE_SDK in the OS environment or pass
    # QTEE_SDK=/path/to/SDK as a SCons commandline argument
    # In this example, the SCons argument takes priority
    sdk_env_name = 'QTEE_SDK'
    sdk_dir = ARGUMENTS.get(sdk_env_name, os.environ.get(sdk_env_name))
    
    print 'Initialising QTEE SDK..'
    # Call the QTEE SDK SConscript to get a QTEE SDK SCons Environment  
    qtee_sdk_env = SConscript(os.path.join(sdk_dir,'SConstruct'))
            
  except:
    print '\nUnable to initialize QTEE SDK at ${}: {} '.format(sdk_env_name, sdk_dir)
            
    #re-raise the exception
    raise
    Exit(1)
    
  #we've gotten the env, now let's build our TA
  # Initialize the environment for the target architecture
  for arch in ['aarch32', 'aarch64']:
    env = qtee_sdk_env.Clone()
            
    # Initialize the environment for the target architecture
    env.InitArch(arch)
    
    #establish where our compiled artifacts will go
    
    
    #We can call this exact sconscript again, but this time exporting the env we just got
    #so that we aren't tabbing over everthing below this
    this_sconscript = (lambda x:x).func_code.co_filename
    env.SConscript(this_sconscript,exports='env')
  Return()


env = env.Clone()

#-------------------------------------------------------------------------------
# Compiler, object, and linker definitions
#-------------------------------------------------------------------------------

if env['PROC'] == 'scorpion':
  app_name = 'seccamdemo'
else:
  app_name = 'seccamdemo64'

env['APP_NAME'] = app_name

seccam_lib = env.SConscript('${BUILD_ROOT}/apps/securemsm/trustzone/qsapps/seccamlib/src/SConscript', exports='env')

if env.StandaloneSdk():
  sp_iris_lib = '$SDK_ROOT/libs/${APP_EXEC_MODE}/sp_iris.lib', 
else:
  sp_iris_lib = env.SConscript('${BUILD_ROOT}/ssg/securemsm/trustzone/qsapps/libs/sp_iris/build/SConscript',exports='env')

includes = [
  "${BUILD_ROOT}/apps/securemsm/trustzone/qsapps/seccamlib/inc",
  "${BUILD_ROOT}/ssg/securemsm/trustzone/qsapps/libs/sp_iris/inc",
  "$SDK_ROOT/inc/sp_iris",
]

#----------------------------------------------------------------------------
# App core Objects
#----------------------------------------------------------------------------
sources = [
  'app_main.c',
]

libs = [
    seccam_lib,
    sp_iris_lib
]
#-------------------------------------------------------------------------------
# Add metadata to image
#-------------------------------------------------------------------------------
md = {
   'appName':       app_name,
   'privileges':    ['default',
                     'SecureCamera',
                     'SPCOM',
                    ],
   'stackSize':     0x2000,
   'heapSize':      0x9000,

}

app_units = env.SecureAppBuilder(
  sources = sources,
  includes = includes,
  metadata = md,
  user_libs = libs,
  image = app_name,
  deploy_sources = [sources,
                    'SConscript'],
  deploy_variants = env.GetDefaultPublicVariants(),
  force_splitbins_on_sdk=True
)

op = env.Alias(app_name, app_units)
Return('app_units')
