#============================================================================
#
# CBSP Builders build rules
#
# GENERAL DESCRIPTION
#    Contains builder(s) to build a Secure App
#
# Copyright (c) Qualcomm Technologies, Inc.
# All Rights Reserved.
# Confidential and Proprietary - Qualcomm Technologies, Inc.
#============================================================================
import os

def exists(env):
  True

def generate(env):
  env.AddMethod(get_ta_dependency,    "GetTADependencies")
  env.AddMethod(get_commonlib_mbn,    "GetCommonlibMBN")
  env.AddMethod(get_ta_linker_script, "GetTALinkerScript")
  env.AddMethod(get_qtee_emu_libs,    "GetQteeEmuLibs")

def get_ta_dependency(env):
  armv9 = ''
  riscv64 = ''
  if env.StandaloneSdk():
    base_path = '${SDK_ROOT}/libs'
    mid_path = ''
    applib_path = ''
    cmnlib_path = ''
    end_path = '${APP_EXEC_MODE}'
  else:
    base_path = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps'
    mid_path = 'build/${SHORT_BUILDPATH}'
    applib_path = 'applib'
    cmnlib_path = 'commonlib'
    end_path = '${PROC}'

  if env.IsArmv9AppEnvStat():
    armv9 = 'armv9'

  if env.IsRiscVAppEnvStat():
    riscv64 = 'riscv64'
  if env['TZ_EXEC_MODE'] == 'riscv64':
    cmnlib = [env.File(os.path.join(base_path, cmnlib_path, mid_path, riscv64, end_path, 'libcmnlib.so'))]
    applib = [env.File(os.path.join(base_path, applib_path, mid_path, riscv64, end_path, 'applib.lib'))]
    appobj = [env.File(os.path.join(base_path, applib_path, mid_path, riscv64, end_path, 'common_applib.o'))]
  else:   
    cmnlib = [env.File(os.path.join(base_path, cmnlib_path, mid_path, armv9, end_path, 'libcmnlib.so'))]
    applib = [env.File(os.path.join(base_path, applib_path, mid_path, armv9, end_path, 'applib.lib'))]
    appobj = [env.File(os.path.join(base_path, applib_path, mid_path, armv9, end_path, 'common_applib.o'))]
    appobj_prxy_mem = []
    if env.get('OFFTARGET_DEBUG_LEAK'):
      appobj_prxy_mem.append(env.File(os.path.join(base_path, applib_path, mid_path, armv9, end_path, 'common_applib_libcHeap.o')))
    else:
      appobj_prxy_mem.append(env.File(os.path.join(base_path, applib_path, mid_path, armv9, end_path, 'prxy_mem.o')))

  #these files are distributed as binaries with the SDK, so we definitely don't want to
  #accidentally delete them when cleaning
  if env.StandaloneSdk():
    env.NoClean([applib,cmnlib,appobj,appobj_prxy_mem])
    env.Precious([applib,cmnlib,appobj,appobj_prxy_mem])

  if env.OfftargetTesting():
    # Relative path used for cmnlib linking.
    return applib, appobj + appobj_prxy_mem
  else:
    return applib + cmnlib, appobj + appobj_prxy_mem

def get_commonlib_mbn(env):
  # offtarget testing won't generate a cmnlib mbn
  if env['PROC'] == 'scorpion':
    cmnlib_mbn = '${BUILD_ROOT}/build/ms/bin/${SHORT_BUILDPATH}/cmnlib.mbn'
  elif env['PROC'] == 'A53_64':
    cmnlib_mbn = '${BUILD_ROOT}/build/ms/bin/${SHORT_BUILDPATH}/cmnlib64.mbn'
  elif env['PROC'] == 'RV64':
    cmnlib_mbn = '${BUILD_ROOT}/build/ms/bin/${SHORT_BUILDPATH}/cmnlib64.mbn'


def get_ta_linker_script(env):
  root_path = '${SDK_ROOT}/scripts/' if env.StandaloneSdk() else '${BUILD_ROOT}/ssg/bsp/build/scripts/'
  link_script = 'secureapp.ld' if env['APP_EXEC_MODE'] == 'aarch32' else 'secureapp64.ld'
  if env.get('is_image_embedded'):
    link_script = 'secureapp_aligned.ld' if env['APP_EXEC_MODE'] == 'aarch32' else 'secureapp64_aligned.ld'

  return os.path.join(root_path, link_script)

def get_qtee_emu_libs(env, use_qteeemu_release, test_svc_enabled):

  if env.StandaloneSdk():
    root_path = '${SDK_ROOT}/'
    path = os.path.join(root_path, "libs/", "${APP_EXEC_MODE}/")
    cmnlib_path = path
    config_path = os.path.join(root_path, "config/", "QTEEEmu/", "QTEEEmu_default_cfg.json")
  else:
    qteeemu_variant = 'release' if use_qteeemu_release else 'debug'
    test_variant = 'test_enabled' if test_svc_enabled else 'test_disabled'

    path = os.path.join('${BUILD_ROOT}/ssg/bsp/sdk/QTEEEmu/build/QTEEEmu/',
                        qteeemu_variant,
                        test_variant,
                        '${PROC}/',
                        '${BUILD_ID}/')

    cmnlib_path = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/commonlib/build/${SHORT_BUILDPATH}/${PROC}/'

    config_path = '${BUILD_ROOT}/ssg/securemsm/sdk/QTEEEmu/config/QTEEEmu_default_cfg.json'

  client    = [env.File(path        + 'offtargetclient.lib')]
  lib1      = [env.File(path        + 'offtargetlib1.lib')]
  lib2      = [env.File(path        + 'offtargetlib2.lib')]

  #these files are distributed as binaries with the SDK, so we definitely don't want to
  #accidentally delete them when cleaning
  if env.StandaloneSdk():
    env.NoClean([client,lib1,lib2])
    env.Precious([client,lib1,lib2])

  return client + lib1 + lib2, cmnlib_path, config_path
