#===========================================================================
#  Copyright (c) 2011-2016, 2017 QUALCOMM Technologies Incorporated.
#  All Rights Reserved.
#  Qualcomm Confidential and Proprietary
#===========================================================================
#
#
# GENERAL DESCRIPTION
#    build script
#                      EDIT HISTORY FOR FILE
#
#  This section contains comments describing changes made to the module.
#  Notice that changes are listed in reverse chronological order.
#
# when       who     what, where, why
# --------   ---     ---------------------------------------------------------
#
#===============================================================================

#-------------------------------------------------------------------------------
# Source PATH
#-------------------------------------------------------------------------------
import os

Import('env')
env = env.Clone()

env.Replace(LIB_OUT_DIR = '${BUILD_ROOT}/ssg/bsp/trustzone/qsapps/mldap/build/${SHORT_BUILDPATH}')
#------------------------------------------------------------------------------
# We need to specify "neon" to generate SIMD instructions in 32-bit mode
#------------------------------------------------------------------------------
if env['PROC'] == 'scorpion':
  env.Append(CCFLAGS = " -mfpu=neon ")
os.environ['USE_ENCAPSULATED_TID'] = "FALSE"
os.environ['USE_QSEE'] = "TRUE"
os.environ['USE_DRKV2'] = "TRUE"

env.Append(CPPDEFINES=['USE_QSEE'])
env.Append(CPPDEFINES=['USE_DRKV2'])
env.Append(CPPDEFINES=['CONFIG_MSM8998'])

env.Append(CFLAGS = ' -fstack-protector -fstack-protector-all')
env.Append(CCFLAGS = " -Wno-error -Wno-unused-value ")

includes = ['#../../ssg/securemsm/trustzone/qsapps/sampleapp/inc',
            '#../../core/api/kernel/libstd/stringl',
            '#../../ssg/securemsm/trustzone/qsapps/smplserv/inc',
            '#../../ssg/securemsm/sse/qsee/SecureUI/inc',
            '#../../core/api/boot/qfprom',
            '#../../ssg/securemsm/secmath/shared/inc',
            '#../../ssg/securemsm/secrsa/shared/inc',
            '#../../ssg/api/securemsm/trustzone/gp',
            '#../../ssg/securemsm/trustzone/qsapps/libs/applib/qsee/src',
            '#../../ssg/securemsm/sse/qsee/SecureUILib/include',
            '#../../ssg/securemsm/sse/qsee/SecureTouch/drTs/include',
            '#../../ssg/securemsm/trustzone/qsapps/libs/biometric/inc',
            '#../../core/kernel/smmu/ACv3.0/common/inc/',
            '#../../ssg/securemsm/trustzone/qsee/mink/include',
            '#../../ssg/securemsm/trustzone/qsapps/common',
            '#../../ssg/securemsm/trustzone/qsapps/libs/sp_iris/inc',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/x509',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/crypto/include',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/qsee',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/base64',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/wb',
            '#../../ssg/securemsm/trustzone/qsapps/mldap/src/include',
]
#----------------------------------------------------------------------------
# App core Objects
#----------------------------------------------------------------------------
#compile idl files
idl_files = env.Glob("../idl/*.idl")
env.LoadToolScript("${BUILD_ROOT}/ssg/bsp/build/scripts/idlcompiler.py")
idl_output_dir = "../inc"
for idl_file in idl_files:
  t = os.path.join(idl_output_dir, os.path.basename(os.path.splitext(str(idl_file))[0] + ".h"))
  env.MINKIDL(source=idl_file, target=t)

sources = [
        'qsee/qsee_agent_main.c',
        'CryptoPlatform.c',
        'CommandHandler.c',
        'qsee/CryptoPlatformQSEE.c',
        'qsee/SfsFileOperations.c',
        'common/ServiceName.c',
        'common/TLV.c',

        'crypto/sec_alloc.c',
        'crypto/bn/bn_wrapper.c',
        'crypto/rsa/rsa_wrapper.c',
        'crypto/mem_clr.c',

        'x509/asn1.c',
        'x509/asn1rsa.c',
        'x509/asn1ec.c',
        'x509/asn1gen.c',
        'x509/x509v3.c',
        'x509/da_cert_parcer.c',
        'x509/da_cert_gencer.c',

        'base64/base64.c',

        'crypto/bn/bn_lib.c',
        'crypto/bn/bn_ctx.c',
        'crypto/bn/bn_rand.c',
        'crypto/bn/bn_asm.c',
        'crypto/bn/bn_word.c',
        'crypto/bn/bn_add.c',
        'crypto/bn/bn_mul.c',
        'crypto/bn/bn_mod.c',
        'crypto/bn/bn_div.c',
        'crypto/bn/bn_exp.c',
        'crypto/bn/bn_sqr.c',
        'crypto/bn/bn_shift.c',
        'crypto/bn/bn_mont.c',
        'crypto/bn/bn_recp.c',
        'crypto/bn/bn_gcd.c',
        'crypto/bn/bn_prime.c',
        'crypto/rand/rand_lib.c',
        'crypto/ec/ec_key.c',
        'crypto/ec/ec_lib.c',
        'crypto/ec/ec_curve.c',
        'crypto/ec/ec_cvt.c',
        'crypto/ec/ec_mult.c',
        'crypto/ec/ecp_mont.c',
        'crypto/ec/ecp_smpl.c',
        'crypto/rsa/rsa_gen.c',
        'crypto/rsa/rsa.c',
        'crypto/hash/sha512.c',
]
deploy_sources = []
deploy_sources.extend(sources)

libs = [
  File(env.SubstRealPath('${MUSLPATH}/lib/libc.a')),
  File(env.SubstRealPath('${LLVMLIB}/libclang_rt.builtins-arm.a'))
]

target_name = 'mldap'
app_name = 'mldap'

#-------------------------------------------------------------------------------
# Add metadata to image
#-------------------------------------------------------------------------------
md = {
   'appName':    app_name,
   'privileges': ['default',
                  'I2C',
                  'OEMUnwrapKeys',
                  'TestSandbox',
                  'CertValidate',
                  'SPI',
                  'SPCOM',
                  'TLMM',
                  'SecureDisplay',
                  'DebugPolicySource',
                  'IntMask',
                  'OEMBuf',
                  'TransNSAddr',
                  'NSSystemReg',
                  'HdcpEncryption',
                  'RTICReport',
                 ],
  'heapSize': 0x1B8000,
  'stackSize' : 0x10000,
  'storageFilesNoPersist': True,
  'totalStorageFiles': 2000
}

deploy_header_files = env.Glob('../inc/*') + env.Glob('../idl/*')
mldap_units = env.SecureAppBuilder(
  sources = sources,
  includes = includes,
  metadata = md,
  image = target_name,
  user_libs = libs,
  deploy_sources = deploy_sources + ['SConscript'] + deploy_header_files,
  deploy_variants = env.GetDefaultPublicVariants()
)
for image in env['IMAGE_ALIASES']:
  op = env.Alias(image, mldap_units)
