#===============================================================================
#
# Monitor build script
#
# GENERAL DESCRIPTION
#    build script
#
# Copyright (c) 2013 by QUALCOMM, Incorporated.
# All Rights Reserved.
# QUALCOMM Proprietary/GTDR
#
#-------------------------------------------------------------------------------
#
#  $Header: //components/rel/tz.bf/3.0.c3/trustzone_images/core/bsp/monitor/build/SConscript#1 $
#  $DateTime: 2015/03/17 22:35:05 $
#  $Author: pwbldsvc $
#  $Change: 7687830 $
#                      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
# --------   ---     ---------------------------------------------------------
# 11/14/13   pre     Initial revision
#===============================================================================
import os
import string
import re
Import('env')
env = env.Clone()

#------------------------------------------------------------------------------
# Check if we need to load this script or just bail-out
#------------------------------------------------------------------------------
# alias - First alias is always the target then the other possibles aliases
aliases = ['monitor', 'mon', 'tz', 'tz_with_test', 'tzbsp_no_xpu']

env.InitImageVars(
  alias_list=aliases,           # aliases
  proc='A53_64',              # proc
  config='apps',                # config type, proc_name
  plat='boot',
  target='MON_IMG_${BUILD_ID}',# target (elf, image file name)
  build_tags = ['APPS_PROC','MONITOR_IMAGE'],
  tools = ['${BUILD_ROOT}/core/bsp/build/scripts/bin_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/hex_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/scl_builder.py',
           'buildspec_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/kernel_builders.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/mbn_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/devcfg_builder.py',
           '${BUILD_ROOT}/tools/build/scons/gensecimage/gensecimage_builder.py',]
)

if not env.CheckAlias():
   Return()

intersect_list = list(set( SConscript.BuildTargets) & set(aliases))
if len(intersect_list) == 0:
   Return()

if len(intersect_list) != 1:
   Return()

# Set this to the correct image_name
image_name = intersect_list[0]

#------------------------------------------------------------------------------
# Init default values for this PROC/Image
#------------------------------------------------------------------------------
env.Replace(USES_RUMI = 'yes')
env.Replace(USES_DM = 'yes')

#===============================================================================
# Monitor build rules
#===============================================================================

#------------------------------------------------------------------------------
# Configure and load in USES and path variables
#------------------------------------------------------------------------------
env.InitBuildConfig()

#---------------------------------------------------------------------------
# Load in the tools scripts
#---------------------------------------------------------------------------
env.LoadToolScript('arm', toolpath = ['${BUILD_SCRIPTS_ROOT}'])
env.LoadToolScript('apps_defs', toolpath = ['${BUILD_SCRIPTS_ROOT}'])

#---------------------------------------------------------------------------
# Override some of the bootloader settings so we build "almost" like a
# bootloader
#---------------------------------------------------------------------------
env.Replace(ARMCC_OPT = "${ARM_OPT_SPACE} ${ARM_OPT_2}")

#----------------------------------------------------------------------------
# Enable Stack protection
#----------------------------------------------------------------------------
env.Append(CCFLAGS = " -fstack-protector ")

#----------------------------------------------------------------------------
# Compiler Warnings as Fatal
#----------------------------------------------------------------------------
if env['MSM_ID'] in ['8994']:
   env.Append(CCFLAGS = " -Werror ")

#------------------------------------------------------------------------------
# Add extension flags for MONITOR
#------------------------------------------------------------------------------
if env['BUILD_VER'] == "":
   env.Replace(BUILD_VER = '0')

env.Append(CPPDEFINES = [])

#-------------------------------------------------------------------------------
# Libraries Section
#-------------------------------------------------------------------------------
core_libs, core_objs = env.LoadAUSoftwareUnits('core')
monitor_units = [core_objs, core_libs]

build_libs, build_objs = env.LoadAUSoftwareUnits('build')
monitor_units += [build_objs, build_libs]

# ARM Libs (this needs to be last, otherwise linker will use
# ARMlibs functions which we want to override)
arm_libs = [
   File(env.SubstRealPath('${ARMLIB}/armlib/c_o.l')),
   # If these are used VFP registers will need to be saved/restored
#   File(env.SubstRealPath('${ARMLIB}/armlib/g_ov.l')),
#   File(env.SubstRealPath('${ARMLIB}/armlib/m_ov.l'))
]

# Extend core library to include standard libraries
core_libs.extend(arm_libs)

#----------------------------------------------------------------------------
# Begin building Monitor
#----------------------------------------------------------------------------
env.Replace(TARGET_NAME = 'mon')
env.Replace(MONITOR_ROOT = '${COREBSP_ROOT}/securemsm/trustzone/monitor')

if env['MSM_ID'] in ['8994']:
  env.Append(LINKFLAGS = ' --entry=MON_COLD_ENTRY ')
  if env['BUILD_ID'] in ['BAWAANAAA']:
    env.Append(LINKFLAGS = ' --entry=0x6E60080 ')
elif env['MSM_ID'] in ['8996']:
  env.Append(LINKFLAGS = ' --entry=MON_COLD_ENTRY ')
elif env['MSM_ID'] in ['8916']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
elif env['MSM_ID'] in ['8936']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
elif env['MSM_ID'] in ['8929']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
else:
  raise RuntimeError, "!!Check Monitor Entry Point, compare with 8916, 8994, 8996"

#----------------------------------------------------------------------------
# Generate Scatter Load File (SCL)
#----------------------------------------------------------------------------
if env['BUILD_ID'] in ['BAWAANAAA']:
  monitor_scl = env.SclBuilder('${SHORT_BUILDPATH}/${TARGET_NAME}',
                             '${MONITOR_ROOT}/msm8992/src/monitor.scl')
else:
  monitor_scl = env.SclBuilder('${SHORT_BUILDPATH}/${TARGET_NAME}',
                             '${MONITOR_ROOT}/${CHIPSET}/src/monitor.scl')

monitor_units += monitor_scl

#----------------------------------------------------------------------------
# Generate MONITOR ELF
#----------------------------------------------------------------------------
libs_path = env['INSTALL_LIBPATH']

monitor_elf = env.Program('${SHORT_BUILDPATH}/${TARGET_NAME}',
                          source=[core_objs, build_objs],
                          LIBS=[core_libs, build_libs], LIBPATH=libs_path)

monitor_units += monitor_elf

env.Depends(monitor_elf, monitor_scl)

monitor_map = env.SideEffect(
   env.subst('${SHORT_BUILDPATH}/${TARGET_NAME}.map'), monitor_elf)

monitor_sym = env.SideEffect(
   env.subst('${SHORT_BUILDPATH}/${TARGET_NAME}.sym'), monitor_elf)

env.Clean(monitor_elf, monitor_map)
env.Clean(monitor_elf, monitor_sym)

#----------------------------------------------------------------------------
# Install generated files for debugging
#----------------------------------------------------------------------------
install_dbg_files = Install(env.subst("${MONITOR_ROOT}"),
                            [monitor_elf, monitor_map, monitor_sym])

monitor_units += install_dbg_files

#============================================================================
# Define units that will be built
#============================================================================
for alias in aliases:
   env.Alias(alias, monitor_units)

1dc40151ff73d765c6da3399c0c1ab1dfa3eff1e
#===============================================================================
#
# Monitor build script
#
# GENERAL DESCRIPTION
#    build script
#
# Copyright (c) 2013 by QUALCOMM, Incorporated.
# All Rights Reserved.
# QUALCOMM Proprietary/GTDR
#
#-------------------------------------------------------------------------------
#
#  $Header: //components/rel/tz.bf/3.0.c3/trustzone_images/core/bsp/monitor/build/SConscript#1 $
#  $DateTime: 2015/03/17 22:35:05 $
#  $Author: pwbldsvc $
#  $Change: 7687830 $
#                      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
# --------   ---     ---------------------------------------------------------
# 11/14/13   pre     Initial revision
#===============================================================================
import os
import string
import re
Import('env')
env = env.Clone()

#------------------------------------------------------------------------------
# Check if we need to load this script or just bail-out
#------------------------------------------------------------------------------
# alias - First alias is always the target then the other possibles aliases
aliases = ['monitor', 'mon', 'tz', 'tz_with_test', 'tzbsp_no_xpu']

env.InitImageVars(
  alias_list=aliases,           # aliases
  proc='A53_64',              # proc
  config='apps',                # config type, proc_name
  plat='boot',
  target='MON_IMG_${BUILD_ID}',# target (elf, image file name)
  build_tags = ['APPS_PROC','MONITOR_IMAGE'],
  tools = ['${BUILD_ROOT}/core/bsp/build/scripts/bin_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/hex_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/scl_builder.py',
           'buildspec_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/kernel_builders.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/mbn_builder.py',
           '${BUILD_ROOT}/core/bsp/build/scripts/devcfg_builder.py',
           '${BUILD_ROOT}/tools/build/scons/gensecimage/gensecimage_builder.py',]
)

if not env.CheckAlias():
   Return()

intersect_list = list(set( SConscript.BuildTargets) & set(aliases))
if len(intersect_list) == 0:
   Return()

if len(intersect_list) != 1:
   Return()

# Set this to the correct image_name
image_name = intersect_list[0]

#------------------------------------------------------------------------------
# Init default values for this PROC/Image
#------------------------------------------------------------------------------
env.Replace(USES_RUMI = 'yes')
env.Replace(USES_DM = 'yes')

#===============================================================================
# Monitor build rules
#===============================================================================

#------------------------------------------------------------------------------
# Configure and load in USES and path variables
#------------------------------------------------------------------------------
env.InitBuildConfig()

#---------------------------------------------------------------------------
# Load in the tools scripts
#---------------------------------------------------------------------------
env.LoadToolScript('arm', toolpath = ['${BUILD_SCRIPTS_ROOT}'])
env.LoadToolScript('apps_defs', toolpath = ['${BUILD_SCRIPTS_ROOT}'])

#---------------------------------------------------------------------------
# Override some of the bootloader settings so we build "almost" like a
# bootloader
#---------------------------------------------------------------------------
env.Replace(ARMCC_OPT = "${ARM_OPT_SPACE} ${ARM_OPT_2}")

#----------------------------------------------------------------------------
# Enable Stack protection
#----------------------------------------------------------------------------
env.Append(CCFLAGS = " -fstack-protector ")

#----------------------------------------------------------------------------
# Compiler Warnings as Fatal
#----------------------------------------------------------------------------
if env['MSM_ID'] in ['8994']:
   env.Append(CCFLAGS = " -Werror ")

#------------------------------------------------------------------------------
# Add extension flags for MONITOR
#------------------------------------------------------------------------------
if env['BUILD_VER'] == "":
   env.Replace(BUILD_VER = '0')

env.Append(CPPDEFINES = [])

#-------------------------------------------------------------------------------
# Libraries Section
#-------------------------------------------------------------------------------
core_libs, core_objs = env.LoadAUSoftwareUnits('core')
monitor_units = [core_objs, core_libs]

build_libs, build_objs = env.LoadAUSoftwareUnits('build')
monitor_units += [build_objs, build_libs]

# ARM Libs (this needs to be last, otherwise linker will use
# ARMlibs functions which we want to override)
arm_libs = [
   File(env.SubstRealPath('${ARMLIB}/armlib/c_o.l')),
   # If these are used VFP registers will need to be saved/restored
#   File(env.SubstRealPath('${ARMLIB}/armlib/g_ov.l')),
#   File(env.SubstRealPath('${ARMLIB}/armlib/m_ov.l'))
]

# Extend core library to include standard libraries
core_libs.extend(arm_libs)

#----------------------------------------------------------------------------
# Begin building Monitor
#----------------------------------------------------------------------------
env.Replace(TARGET_NAME = 'mon')
env.Replace(MONITOR_ROOT = '${COREBSP_ROOT}/securemsm/trustzone/monitor')

if env['MSM_ID'] in ['8994']:
  env.Append(LINKFLAGS = ' --entry=MON_COLD_ENTRY ')
  if env['BUILD_ID'] in ['BAWAANAAA']:
    env.Append(LINKFLAGS = ' --entry=0x6E60080 ')
elif env['MSM_ID'] in ['8996']:
  env.Append(LINKFLAGS = ' --entry=MON_COLD_ENTRY ')
elif env['MSM_ID'] in ['8916']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
elif env['MSM_ID'] in ['8936']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
elif env['MSM_ID'] in ['8929']:
  env.Append(LINKFLAGS = ' --entry=CPU_RESET_ENTRY ')
else:
  raise RuntimeError, "!!Check Monitor Entry Point, compare with 8916, 8994, 8996"

#----------------------------------------------------------------------------
# Generate Scatter Load File (SCL)
#----------------------------------------------------------------------------
if env['BUILD_ID'] in ['BAWAANAAA']:
  monitor_scl = env.SclBuilder('${SHORT_BUILDPATH}/${TARGET_NAME}',
                             '${MONITOR_ROOT}/msm8992/src/monitor.scl')
else:
  monitor_scl = env.SclBuilder('${SHORT_BUILDPATH}/${TARGET_NAME}',
                             '${MONITOR_ROOT}/${CHIPSET}/src/monitor.scl')

monitor_units += monitor_scl

#----------------------------------------------------------------------------
# Generate MONITOR ELF
#----------------------------------------------------------------------------
libs_path = env['INSTALL_LIBPATH']

monitor_elf = env.Program('${SHORT_BUILDPATH}/${TARGET_NAME}',
                          source=[core_objs, build_objs],
                          LIBS=[core_libs, build_libs], LIBPATH=libs_path)

monitor_units += monitor_elf

env.Depends(monitor_elf, monitor_scl)

monitor_map = env.SideEffect(
   env.subst('${SHORT_BUILDPATH}/${TARGET_NAME}.map'), monitor_elf)

monitor_sym = env.SideEffect(
   env.subst('${SHORT_BUILDPATH}/${TARGET_NAME}.sym'), monitor_elf)

env.Clean(monitor_elf, monitor_map)
env.Clean(monitor_elf, monitor_sym)

#----------------------------------------------------------------------------
# Install generated files for debugging
#----------------------------------------------------------------------------
install_dbg_files = Install(env.subst("${MONITOR_ROOT}"),
                            [monitor_elf, monitor_map, monitor_sym])

monitor_units += install_dbg_files

#============================================================================
# Define units that will be built
#============================================================================
for alias in aliases:
   env.Alias(alias, monitor_units)
