import os

# 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 'Unable to initialize QTEE SDK at ${}: {} '.format(sdk_env_name, sdk_dir)

  #re-raise the exception
  raise
  Exit(1)

# This example builds both 32bit and 64 bit versions of the minkipc app
for arch in ['aarch32', 'aarch64']:
  env = qtee_sdk_env.Clone()

  # Initialize the environment for the target architecture
  # note: for off-target builds aarchXX is mapped to x86-XX
  env.InitArch(arch)

  # In this example, the output artifacts will be installed to
  # /path/to/this/SConstruct/out/${PROC}/
  out_dir = os.path.join(Dir('.').srcnode().abspath, 'outSvc', env['PROC'])
  env.Replace(OUT_DIR=out_dir)

  # Call the SMCInvoke Example Service TA SConscript to build it
  env.SConscript('./TA_SVC/src/SConscript', exports='env')

  # Call the SMCInvoke Example TA SConscript to build it
  out_dir = os.path.join(Dir('.').srcnode().abspath, 'out', env['PROC'])
  env.Replace(OUT_DIR=out_dir)
  env.SConscript('./TA/src/SConscript', exports='env')

  # Call the SMCInvoke Example CA SConscript if building off-target
  if env.OfftargetTesting():
    env.SConscript('./CA/src/SConscript', exports='env')

