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'))
  print "done initializing qtee sdk"
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 skeleton app
for arch in ['aarch32', 'aarch64']:
  print "init skeleton ca",arch
  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, 'out', env['PROC'])
  env.Replace(OUT_DIR=out_dir)
  env.Replace(LIB_OUT_DIR=out_dir)
  
  # Call the skeleton TA SConscript to build it
  env.SConscript('./TA/src/SConscript', exports='env')
  
  # Call the skeleton CA SConscript if building off-target
  if env.OfftargetTesting():
    env.SConscript('./CA/src/SConscript', exports='env')
