#===============================================================================
#
# Multibuild build script
#
# GENERAL DESCRIPTION
#    Build script
#
# Copyright (c) Samsung Electronics Co, Ltd 2016. All rights reserved.
#
#===============================================================================
import os
Import('env')

env = env.Clone()

def get_multibuild_variable(local_name):
    var = None
    if None != os.getenv(local_name):
        var = os.getenv(local_name).strip()
    return var

def get_multibuild_array(local_name):
    arr = get_multibuild_variable(local_name)
    if None != arr:
        return arr.split()
    return []

multibuild_build_tool = os.getenv("LOCAL_BUILD_TOOL", "arm").strip()
multibuild_scl_path = get_multibuild_variable("LOCAL_SCL_PATH")
multibuild_proc = os.getenv("LOCAL_QSEE_PROC", "scorpion").strip()
multibuild_ssg_or_core = os.getenv("LOCAL_APPS_DIR", "core").strip()
multibuild_bsp_defines = get_multibuild_array("LOCAL_BSP_DEFINES")
multibuild_bsp_cflags = []
multibuild_bsp_cpp_defines = []
for s in multibuild_bsp_defines:
    if not s.startswith('-D'):
        multibuild_bsp_cflags.append(s.replace('"','\\"'))
    else:
        multibuild_bsp_cpp_defines.append(s.replace('-D', '').replace('"','\\"'))
multibuild_imprint_tool_path = os.getenv("LOCAL_IMPRINT_TOOL", "echo No imprint for").strip()
#------------------------------------------------------------------------------
# Check if we need to load this script or just bail-out
#------------------------------------------------------------------------------
# alias - First alias is always the target then the other possible aliases
aliases = [
  'multibuild', 'all'
]
env.InitImageVars(
  alias_list = aliases,       # list of aliases, unique name index [0]
  proc = multibuild_proc,          # proc settings
  config = 'apps',            # config settings
  build_tags = ['APPS_PROC',  # list of build tags for sub lib scripts
                'MULTIBUILD_IMAGE'
               ],
)

if not env.CheckAlias():
  Return()
#------------------------------------------------------------------------------
# Configure and load in USES and path variables
#------------------------------------------------------------------------------
env.LoadToolScript('${BUILD_ROOT}/' + multibuild_ssg_or_core + '/bsp/build/scripts/secure_app_builder.py')
env.InitBuildConfig()
env.Append(OUT_DIR = os.getcwd())

env.Append(LINKCOM = "&& " + multibuild_imprint_tool_path + " $TARGET")

sconspath = env.subst('${BUILD_ROOT}/' + multibuild_ssg_or_core + '/securemsm/trustzone/qsapps/multibuild/build/SConscript')
env.Replace(SRC_SCONS_ROOT = sconspath.split('SConscript')[0])
SConscript(sconspath, exports='env',)
