# 
# CodeXL SConstruct Template
#

import os
from CXL_init import *

###################################################
# Initialize CXL command line variables
#
# Note: DO NOT MODIFY THIS SECTION. Please see CXL_init.
#
CXL_vars = Variables(None)

# Initial CXL_vars scons construction variables
initCXLVars(CXL_vars)

###################################################
#
# Additional SConstruct variables can be added here
#
# Note: * These variables can be configured from the commandline arguments.
# 	* These will be included in the help "scons -h".
# 	* Customizable

# // Add more variables here

###################################################
# Initialize CXL_env 
#
# Note: DO NOT MODIFY THIS SECTION. Please see CXL_init.
#
CXL_env = Environment( 
	variables = CXL_vars, 
	ENV = {'PATH':os.environ['PATH']})

# CXL build initialization
initCXLBuild (CXL_env)

###################################################
# Initialize External library stuff
# 
# Note: * This section is customizable. 
#
initGtk (CXL_env)
initQt4 (CXL_env)
initTinyXml (CXL_env)
initQScintilla (CXL_env)
initQCustomPlot (CXL_env)
initStdc(CXL_env)
initJava(CXL_env)
initBoost(CXL_env)

# Set the debug/release variant data
if (CXL_env['CXL_build'] == 'debug'):
    obj_variant_dir    = 'variant_debug'
else:
    obj_variant_dir    = 'variant_release'

# Generate Help 
Help(CXL_vars.GenerateHelpText(CXL_env))


###################################################
# Specify Depe!h!,<Mouse>C!k!,
#
# Note: This section is customizable.
#

CXL_lib_common_amd_deps = (
)

initCommonLibAmd (CXL_env, CXL_lib_common_amd_deps)

###################################################
# Any custom builders for this project
#
# UIC, MOC, RCC (for Qt)
# Custom ones are required because the default Qt ones are for Qt3, and
# there are no Qt4 ones which come with the scons rpm.
# rgorton note (7-Aug-2012):
# These need to be here - if they are in the CXL_init.py script, the values
# of SOURCES and TARGET are null, I think because we are outside of scons
# at that point.  Spent multiple hours puzzling over that.
#
uic_build = Builder(action = CXL_env['CXL_uic_tool'] + ' ${SOURCES[0]} -o $TARGET',
                    prefix='ui_', suffix='.h', src_suffix='.ui')
CXL_env.Append(BUILDERS = {'UicBld' : uic_build})

moc_build = Builder(action = CXL_env['CXL_moc_tool'] + ' ${SOURCES[0]} -o $TARGET',
                    prefix='moc_', suffix='.cpp', src_suffix='.h')
CXL_env.Append(BUILDERS = {'MocBld' : moc_build})

rcc_build = Builder(action = CXL_env['CXL_rcc_tool'] + ' ${SOURCES[0]} -name $TARGET.filebase -o $TARGET',
                    prefix='rcc_', suffix='.cpp', src_suffix='.qrc')
CXL_env.Append(BUILDERS = {'RccBld' : rcc_build})

#
# The documentation is generated from Doxygen
# We do not have an effective way to use the Doxygen builder tool, so 
# we will sort of spoof it, and always run it
doxy_build = Builder(action = CXL_env['CXL_doxy_tool'] + ' ${SOURCES[0]}')
CXL_env.Append(BUILDERS = {'DoxyBld' : doxy_build})

##############################################################
# Print out all environment variables of CXL_env if verbose is specified
if CXL_env['CXL_build_verbose'] != 0 :
	print CXL_env.Dump()


##############################################################
# Export the CXL_env to all SConscripts
Export( 'CXL_env ')


##############################################################
# NOTE [Richard Gorton] :
# Specifically express _dynamic_ dependencies here.  It is possible to have
# the individual components do this, but the relevant context would need to be
# exported globally, and then consumed.  That is, we would need to export the
# BaseTools_Obj, via a global name, and then the downstream SCons files would
# need to import it and write the dependency rules.
# There is no need to write dependency rules for items which solely use the
# promotion model.

# No dynamic dependencies at all

############################################
#
# Framework Section
#
FrameworkComponents = []

BaseTools_Obj = SConscript('../Common/Src/AMDTBaseTools/SConscript', variant_dir=obj_variant_dir+'/AMDTBaseTools', duplicate=0)
FrameworkComponents += BaseTools_Obj
Assertion_Obj = SConscript('../Common/Src/AMDTAssertionHandlers/SConscript', variant_dir=obj_variant_dir+'/AMDTAssertionHandlers', duplicate=0)
FrameworkComponents += Assertion_Obj

OSWrappers_Obj = SConscript('../Common/Src/AMDTOSWrappers/SConscript', variant_dir=obj_variant_dir+'/AMDTOSWrappers', duplicate=0)
CXL_env.Depends(OSWrappers_Obj, BaseTools_Obj)
FrameworkComponents += OSWrappers_Obj

OSAPIWrappers_Obj = SConscript('../Common/Src/AMDTOSAPIWrappers/SConscript', variant_dir=obj_variant_dir+'/AMDTOSAPIWrappers', duplicate=0)
CXL_env.Depends(OSAPIWrappers_Obj, BaseTools_Obj + OSWrappers_Obj)
FrameworkComponents += OSAPIWrappers_Obj

APIClasses_Obj = SConscript('../Common/Src/AMDTAPIClasses/SConscript', variant_dir=obj_variant_dir+'/AMDTAPIClasses', duplicate=0)
CXL_env.Depends(APIClasses_Obj, OSWrappers_Obj + OSAPIWrappers_Obj + BaseTools_Obj)
FrameworkComponents += APIClasses_Obj

AppComponents_Obj = SConscript('../Common/Src/AMDTApplicationComponents/SConscript', variant_dir=obj_variant_dir+'/AMDTApplicationComponents', duplicate=0)
CXL_env.Depends(AppComponents_Obj, APIClasses_Obj + OSWrappers_Obj + OSAPIWrappers_Obj + BaseTools_Obj)
FrameworkComponents += AppComponents_Obj

AMDTRemoteClient_Obj = SConscript('Remote/AMDTRemoteClient/SConscript', variant_dir=obj_variant_dir+'/AMDTRemoteClient', duplicate=0)
CXL_env.Depends(AMDTRemoteClient_Obj, OSWrappers_Obj + BaseTools_Obj) 
FrameworkComponents += AMDTRemoteClient_Obj

AppFramework_Obj = SConscript('AMDTApplicationFramework/SConscript', variant_dir=obj_variant_dir+'/AMDTApplicationFramework', duplicate=0)
CXL_env.Depends(AppFramework_Obj, APIClasses_Obj + OSWrappers_Obj + OSAPIWrappers_Obj + BaseTools_Obj + AMDTRemoteClient_Obj)
FrameworkComponents += AppFramework_Obj

# This depends upon everything built to date
Application_Obj = SConscript('../Common/Src/AMDTApplication/SConscript', variant_dir=obj_variant_dir+'/AMDTApplication', duplicate=0)
CXL_env.Depends(Application_Obj, FrameworkComponents)
FrameworkComponents += Application_Obj

# This depends upon everything built to date
Application_Obj = SConscript('App/SConscript', variant_dir=obj_variant_dir+'/AMDCodeXL', duplicate=0)
CXL_env.Depends(Application_Obj, FrameworkComponents)
FrameworkComponents += Application_Obj

SharedProf_Obj = SConscript('Components/AMDTSharedProfiling/SConscript', variant_dir=obj_variant_dir+'/AMDTSharedProfiling', duplicate=0)
CXL_env.Depends(SharedProf_Obj, FrameworkComponents)
FrameworkComponents += SharedProf_Obj

CXL_env.Depends(FrameworkComponents, 
	CXL_env['CXL_Images_install'] + 
	CXL_env['CXL_Legal_install'] +
	CXL_env['CXL_ReleaseDocs_install']
)

AMDTRemoteAgent_Obj = SConscript('Remote/AMDTRemoteAgent/SConscript', variant_dir=obj_variant_dir+'/AMDTRemoteAgent', duplicate=0)
CXL_env.Depends(AMDTRemoteAgent_Obj, OSWrappers_Obj + BaseTools_Obj) 
CXL_env.Depends(OSWrappers_Obj, BaseTools_Obj)
FrameworkComponents += AMDTRemoteAgent_Obj

############################################
#
# GPUDebugging Plugin Section
#
GpuDebuggingPlugins = []
GpuD_ProcessDbg_Obj = SConscript('Components/GpuDebugging/AMDTProcessDebugger/SConscript', variant_dir=obj_variant_dir+'/AMDTProcessDebugger', duplicate=1) 
CXL_env.Depends(GpuD_ProcessDbg_Obj, APIClasses_Obj + OSWrappers_Obj + BaseTools_Obj)
GpuDebuggingPlugins += GpuD_ProcessDbg_Obj

GpuD_RmtDbgSrv_Obj = SConscript('Components/GpuDebugging/AMDTRemoteDebuggingServer/SConscript', variant_dir=obj_variant_dir+'/AMDTRemoteDebuggingServer', duplicate=1) 
CXL_env.Depends(GpuD_RmtDbgSrv_Obj, GpuD_ProcessDbg_Obj + APIClasses_Obj + OSWrappers_Obj + BaseTools_Obj)
GpuDebuggingPlugins += GpuD_RmtDbgSrv_Obj

GpuD_ApiFunctions_Obj = SConscript('Components/GpuDebugging/AMDTApiFunctions/SConscript', variant_dir=obj_variant_dir+'/AMDTApiFunctions', duplicate=1) 
CXL_env.Depends(GpuD_ApiFunctions_Obj, GpuD_ProcessDbg_Obj + APIClasses_Obj + OSWrappers_Obj + BaseTools_Obj)
GpuDebuggingPlugins += GpuD_ApiFunctions_Obj

GpuD_ServerUtils_Obj = SConscript('Components/GpuDebugging/AMDTServerUtilities/SConscript', variant_dir=obj_variant_dir+'/AMDTServerUtilities', duplicate=1) 
CXL_env.Depends(GpuD_ServerUtils_Obj, APIClasses_Obj + OSWrappers_Obj + BaseTools_Obj)
GpuDebuggingPlugins += GpuD_ServerUtils_Obj
GDbg_Server_Core = GpuD_ServerUtils_Obj + APIClasses_Obj + OSWrappers_Obj + BaseTools_Obj

GpuD_OCL_Obj = SConscript('Components/GpuDebugging/AMDTOpenCLServer/SConscript', variant_dir=obj_variant_dir+'/AMDTOpenCLServer', duplicate=1) 
CXL_env.Depends(GpuD_OCL_Obj, GDbg_Server_Core)
GpuDebuggingPlugins += GpuD_OCL_Obj

# The original library is build as libOpenCLTemp.so. from there it is copied to libOpenCL.so.1 (which can not be created)
# a link from libOpenCL.so to libOpenCL.so.1 is then created. 
# Those two files are needed since some applications are linked to libOpenCL.so and some to libOpenCL.so.1
# In Linux the real file is usually the most "detailed" file -> yyy.so.1 and not the yyy.so hence the steps
CXL_env.AddPostAction (GpuD_OCL_Obj, "( cd " + CXL_env['CXL_spies_dir'] + " && mv libOpenCLTemp.so libOpenCL.so.1 && ln -s -f libOpenCL.so.1 libOpenCL.so )")

GpuD_OpenGL_Obj = SConscript('Components/GpuDebugging/AMDTOpenGLServer/SConscript', variant_dir=obj_variant_dir+'/AMDTOpenGLServer', duplicate=1) 
CXL_env.Depends(GpuD_OpenGL_Obj, GDbg_Server_Core)
GpuDebuggingPlugins += GpuD_OpenGL_Obj

# The original library is build as libGLTemp.so. from there it is copied to libGL.so.1 (which can not be created)
# a link from libGL.so to libGL.so.1 is then created. Same logic applies here as in the CL spy
CXL_env.AddPostAction (GpuD_OpenGL_Obj, "( cd " + CXL_env['CXL_spies_dir'] + " && mv libGLTemp.so libGL.so.1 && ln -s -f libGL.so.1 libGL.so )")

GpuD_Components_Obj = SConscript('Components/GpuDebugging/AMDTGpuDebuggingComponents/SConscript', variant_dir=obj_variant_dir+'/AMDTGpuDebuggingComponents', duplicate=1) 
CXL_env.Depends(GpuD_Components_Obj,
                GpuD_ApiFunctions_Obj
                + GpuD_ProcessDbg_Obj
                + AppComponents_Obj
                + APIClasses_Obj
                + OSWrappers_Obj
                + BaseTools_Obj)
GpuDebuggingPlugins += GpuD_Components_Obj

GpuDebugging_Obj = SConscript('Components/GpuDebugging/AMDTGpuDebugging/SConscript', variant_dir=obj_variant_dir+'/AMDTGpuDebugging', duplicate=1) 
# All framework excepting Application and SharedProfiling
CXL_env.Depends(GpuDebugging_Obj,
                # Gpu Debug items
                GpuD_Components_Obj
                + GpuD_ApiFunctions_Obj
                + GpuD_ProcessDbg_Obj
                # Framework items
                + AppFramework_Obj
                + AppComponents_Obj
                + APIClasses_Obj
                + Assertion_Obj
                + OSWrappers_Obj
                + BaseTools_Obj)
GpuDebuggingPlugins += GpuDebugging_Obj

# Make GpuDebuggingPlugins depending on Framework
CXL_env.Depends(GpuDebuggingPlugins , FrameworkComponents) 

############################################
#
# GPUProfiling Plugin Section
#

GpuProfilingPlugins = []

GpuProf_Obj = SConscript('Components/GpuProfiling/AMDTGpuProfiling/SConscript', variant_dir=obj_variant_dir+'/AMDTGpuProfiling', duplicate=1)
CXL_env.Depends(GpuProf_Obj, 
                SharedProf_Obj
                + AppFramework_Obj
                + AppComponents_Obj
                + APIClasses_Obj
                + OSWrappers_Obj
                + BaseTools_Obj
                )

GpuProfilingPlugins += GpuProf_Obj

# Make GpuProfilingPlugins depending on Framework
CXL_env.Depends(GpuProfilingPlugins , FrameworkComponents) 

############################################
#
# Kernel Analyzer Section
#

KernelAnalyzer = []
KernelAnalyzerBackend_Obj = SConscript('Components/ShaderAnalyzer/AMDTBackEnd/SConscript', variant_dir=obj_variant_dir+'/AMDTBackEnd', duplicate=0)
CXL_env.Depends(KernelAnalyzerBackend_Obj, 
                OSWrappers_Obj
                + BaseTools_Obj)
KernelAnalyzer += KernelAnalyzerBackend_Obj

KernelAnalyzerFrontEnd_Obj = SConscript('Components/ShaderAnalyzer/AMDTKernelAnalyzer/SConscript', variant_dir=obj_variant_dir+'/AMDTKernelAnalyzer', duplicate=1)
CXL_env.Depends(KernelAnalyzerFrontEnd_Obj, 
                BaseTools_Obj
                + OSWrappers_Obj
		+ APIClasses_Obj
		+ AppFramework_Obj
		+ KernelAnalyzerBackend_Obj)
KernelAnalyzer += KernelAnalyzerFrontEnd_Obj

############################################
#
# AMDTTeaPot Section
#
AMDTTeaPot = []
AMDTTeaPotLib_Obj = SConscript('Examples/AMDTTeaPot/AMDTTeaPotLib/SConscript', variant_dir=obj_variant_dir+'/AMDTTeaPotLib', duplicate=0) 
AMDTTeaPot += AMDTTeaPotLib_Obj

AMDTTeaPot_Obj  = SConscript('Examples/AMDTTeaPot/AMDTTeaPot/SConscript', variant_dir=obj_variant_dir+'/AMDTTeaPot', duplicate=0) 
CXL_env.Depends(AMDTTeaPot_Obj, AMDTTeaPotLib_Obj)
AMDTTeaPot += AMDTTeaPot_Obj 
# copy the teapot libraries to the correct place 
SConscript('Examples/AMDTTeaPot/AMDTTeaPot/Copy_SConscript', variant_dir=obj_variant_dir+'/AMDTTeaPot', duplicate=0)
#CXL_env.AddPostAction (AMDTTeaPot_Obj, SConscript('Examples/AMDTTeaPot/AMDTTeaPot/Copy_SConscript') )

############################################
#
# AMDTSystemInformationHelper Section
#
AMDTSystemInformationHelper = []
AMDTSystemInformationHelper_Obj  = SConscript('Utils/AMDTSystemInformationHelper/SConscript', variant_dir=obj_variant_dir+'/AMDTSystemInformationHelper', duplicate=0) 

CXL_env.Depends(AMDTSystemInformationHelper_Obj, APIClasses_Obj + OSWrappers_Obj)

AMDTSystemInformationHelper += AMDTSystemInformationHelper_Obj


############################################
#
# WebHelp content
# The generation of the HTML files is performed by the WordToHelp tool in the Jenkins CodeXL-Doc job.
# Here we only copy the quick start guide. The extraction of the HTML files from thier tar is performed 
# in the shell script \main\CodeXL\Util\linux\JenkinsCodeXLFullBuild
#

# Copy the quick start quide
os.system('mkdir ../Output_x86_64/release/bin/Help/')  
os.system('cp -u ../CodeXL/Help/CodeXL_Quick_Start_Guide.pdf ../Output_x86_64/release/bin/Help/')
 


############################################

CodeXL_Full = \
    GpuDebuggingPlugins + \
    GpuProfilingPlugins + \
    AMDTSystemInformationHelper + \
    AMDTTeaPot + \
    KernelAnalyzer

Default(CodeXL_Full)
#Feature group build
Alias( target='Framework'   , source=(FrameworkComponents))
Alias( target='GpuProfiling', source=(GpuProfilingPlugins))
Alias( target='GpuDebugging', source=(GpuDebuggingPlugins))
Alias( target='KernelAnalyzer', source=(KernelAnalyzer))
Alias( target='Teapot', source=(AMDTTeaPot))
Alias( target='install'     , source=(CodeXL_Full))
Alias( target='SysInfoHelper'     , source=(AMDTSystemInformationHelper))

#Per project build support
#FrameworkComponents
Alias( target='AMDTBaseTools'   , source=(BaseTools_Obj))
Alias( target='AMDTOSWrappers'   , source=(OSWrappers_Obj))
Alias( target='AMDTOSAPIWrappers'   , source=(OSAPIWrappers_Obj))
Alias( target='AMDTAPIClasses'   , source=(APIClasses_Obj))
Alias( target='AMDTApplicationComponents'   , source=(AppComponents_Obj))
Alias( target='AMDTRemoteClient'   , source=(AMDTRemoteClient_Obj))
Alias( target='AMDTApplicationFramework'   , source=(AppFramework_Obj))
Alias( target='AMDCodeXL'   , source=(Application_Obj))
Alias( target='AMDTSharedProfiling'   , source=(SharedProf_Obj))
Alias( target='AMDTRemoteAgent'   , source=(AMDTRemoteAgent_Obj))
#GPUDebugging
Alias( target='AMDTProcessDebugger'   , source=(GpuD_ProcessDbg_Obj))
Alias( target='AMDTRemoteDebuggingServer'   , source=(GpuD_RmtDbgSrv_Obj))
Alias( target='AMDTApiFunctions'   , source=(GpuD_ApiFunctions_Obj))
Alias( target='AMDTServerUtilities'   , source=(GpuD_ServerUtils_Obj))
Alias( target='AMDTOpenCLServer'   , source=(GpuD_OCL_Obj))
Alias( target='AMDTOpenGLServer'   , source=(GpuD_OpenGL_Obj))
Alias( target='AMDTGpuDebuggingComponents'   , source=(GpuD_Components_Obj))
Alias( target='AMDTGpuDebugging'   , source=(GpuDebugging_Obj))

#GPUProfiling
Alias( target='AMDTGpuProfiling'   , source=(GpuProf_Obj))
#KernelAnalyzer
Alias( target='AMDTBackend'   , source=(KernelAnalyzerBackend_Obj))
Alias( target='AMDTKernelAnalyzer'   , source=(KernelAnalyzerFrontEnd_Obj))
