#===============================================================================
#
# teetest Build Script
#
# GENERAL DESCRIPTION
#    build script
#
# Copyright (c) 2015-2017 Qualcomm Technologies, Inc.
# All Rights Reserved.
# Confidential and Proprietary - Qualcomm Technologies, Inc.
#
#-------------------------------------------------------------------------------
#
#  $Header: //components/rel/ssg.tz/1.10/securemsm/trustzone/qsapps/teetest/teetest/src/SConscript#2 $
#  $DateTime: 2020/05/24 12:37:44 $
#  $Author: pwbldsvc $
#  $Change: 24013351 $
#                      EDIT HISTORY FOR FILE
#
#  This section contains comments describing changes made to the module.
#  Notice that changes are listed in reverse chronological order.
#
#===============================================================================
import os
Import('env')
env = env.Clone()
base_env = env.Clone()

#we don't build teetest for targets where TZ is 32 bit
#however, when building for targets where TZ is 64 bit, we 
#build both 32 and 64 bit teetest
if env['TZ_EXEC_MODE'] == 'aarch32':
  Return()


  
aliases = ['uefitzt', 'tzbsptest', 'allapps','teetest']
if env["CHIPSET"] not in [ "sdm670" , 'sm7150']:
    # build is currently broken for sdm670
    aliases.append("deploy")

for teetest in ['teetest', 'teetest64']:  
  env = base_env.Clone()
  
  if teetest == 'teetest':
    proc = 'scorpion'
    build_tags = ['APPS_PROC', 'TEETEST_IMAGE']
  else:
    proc = 'A53_64'
    build_tags = ['APPS_PROC', 'TEETEST64_IMAGE']
  
  env.InitImageVars(
    alias_list = [teetest] + aliases,
    proc = proc,
    config = 'apps',
    build_tags = build_tags,
  )
  
  is64bit = not (env['PROC'] == 'scorpion')
  arch_source_dir = 'aarch64' if is64bit else 'aarch32'
  
  if not env.CheckAlias(aliases + [teetest]):
    continue
  
  if env.StandaloneSdk():
    env.Tool('secure_tee_builder',toolpath=['$SDK_ROOT/scripts'])
  else:
    env.LoadToolScript('${BUILD_ROOT}/ssg/bsp/build/scripts/secure_tee_builder.py')
  env.InitBuildConfig()

  env.Replace(OUT_DIR = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/' + teetest + '/build')
  env.Replace(LIB_OUT_DIR = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/' + teetest + '/build/${SHORT_BUILDPATH}')

  includes = [
     "./",
     "${CHIPSET}",
     '${BUILD_ROOT}/ssg/securemsm/trustzone/qsee/include/',
     '${BUILD_ROOT}/ssg/securemsm/trustzone/qsee/mink/include/',
     arch_source_dir,
     "${COREBSP_ROOT}/api/kernel/libstd/stringl",
  ]

  #----------------------------------------------------------------------------
  # Disable the optimizer for the MINK PVC integration. This code is too darn
  # hard to follow otherwise. This can be disabled from commandline as well,
  # but that is a good idea only if you are building teetest; not when you
  # are compiling everything with tzbsptest
  #----------------------------------------------------------------------------
  env.Replace(ARMCC_OPT = "")

  if not is64bit:
    env.Append(CCFLAGS = " -mfpu=none -DTEETEST32 ")
  else:
    env.Append(CCFLAGS = " -mgeneral-regs-only -mno-implicit-float -fno-vectorize-loops -DTEETEST64 ")

  # Skip HDMI tests for targets that will fail due to tzbsp_mmss_dp_is_clk_on() returning false
  if env['CHIPSET'] not in ['msm8998', 'sdm660', 'sdm845']:  
     env.Append(CPPFLAGS = ' -DTEETEST_SKIP_HDMI -DTEETEST_GIC_V1 ')
  else: 
     env.Append(CPPFLAGS = ' -DTEETEST_GIC_V3 ')

  if env.StandaloneSdk():
    libs = ['$SDK_ROOT/libs/$APP_EXEC_MODE/tee_libstd.lib']
  else:
    libs = [
      env.SConscript('$BUILD_ROOT/ssg/securemsm/trustzone/qsee/mink/libstd/build/SConscript',exports='env')
    ]
     
  #----------------------------------------------------------------------------
  # App core Objects
  #----------------------------------------------------------------------------   
  aarch_sources = env.Glob(os.path.join(arch_source_dir, '*.[cs]'), strings=True)
  common_sources = env.Glob('*.[c]', strings=True)

  sources = common_sources + aarch_sources

  teetest_units = env.SecureTEEBuilder(
    sources = sources,
    includes = includes,
    image = 'teetest64' if is64bit else 'teetest',
    deploy_sources = [sources, 
                      'SConscript',
                      env.Glob('*.h'),
                      env.Glob(os.path.join(arch_source_dir,'*.h')),
                      env.Glob(os.path.join(arch_source_dir,'*.inc')),
                      env.subst('${CHIPSET}/teetest_hwio.h'),
                      '../build/teetest.ld',
                      '../build/teetest64.ld',
                     ],
    deploy_variants = env.GetDefaultPublicVariants(),
    user_libs=libs
  )

  op = env.Alias(aliases + [teetest], teetest_units)
