# -*- Python -*-
# $Id: SConscript 4614 2010-04-09 13:11:41Z rgorton $
# SConscript for building the GpuProfiling component
# Generates all of the moc/uic files, and compiles
# the rest of the sources

import os
from CXL_init import *
Import('*')

TheLibraryName = 'CXLGpuProfiling'
GPUProf_env = CXL_env.Clone()
UseBoost(GPUProf_env)
buildDirPath = "." + '/build/' + GPUProf_env['CXL_arch']
print buildDirPath
GPUProf_env.VariantDir(buildDirPath, GPUProf_env['CXL_common_dir'] + '/Src')

UseTinyXml(GPUProf_env)
UseQScintilla(GPUProf_env)
UseQt4(GPUProf_env)

GPUProf_env.Append( LIBS=
[
    'CXLRemoteClient',
    'CXLSharedProfiling',
])

###################################################
# Initialize SconsScripts
#
# For CommonProjectHdr stuff
GPUProf_env.Append( CPPPATH = [
    ".",
    "../",
    GPUProf_env['CXL_common_dir'] + '/../',
    GPUProf_env['CXL_common_dir'] + '/Src/CommonProjectsHdr',
    GPUProf_env['CXL_common_dir'] + '/Src/TSingleton',
    GPUProf_env['CXL_common_dir'] + '/Src/ADLUtil',
    GPUProf_env['CXL_common_dir'] + '/Src/DeviceInfo',
    GPUProf_env['CXL_common_dir'] + '/Src/DynamicLibraryModule',
    GPUProf_env['CXL_common_dir'] + '/Src/AMDTMutex',
    GPUProf_env['CXL_common_dir'] + '/Lib/Ext/utf8cpp/source',
    GPUProf_env['CXL_common_dir'] + '/Lib/AMD/RCP/include',
    GPUProf_env['CXL_common_dir'] + '/Lib/AMD/GPUPerfAPI/3_2/Include',
    GPUProf_env['CXL_commonproj_dir'],
    GPUProf_env['CXL_common_dir'] + '/../CodeXL',
    GPUProf_env['CXL_common_dir'] + '/../CodeXL/Components',
    GPUProf_env['CXL_common_dir'] + '/../CodeXL/Components/AMDTSharedProfiling/inc',
    GPUProf_env['CXL_common_dir'] + '/../CodeXL/Remote',
    os.getcwd(),
    '#obj',
    ])

GPUProf_env.Prepend(CPPDEFINES=["USE_POINTER_SINGLETON"])

# the AMD_INTERNAL_BUILD is needed for HSA headers
GPUProf_env.Prepend(CPPDEFINES=["AMD_INTERNAL_BUILD"])

# GPU Profiling also uses some content which won't ever be "live"
# ADL, CAL 8.95, APPSDK 2.8
GPUProf_env.Append(CPPPATH = GPUProf_env['CXL_common_dir'] + '/Lib/AMD/ADL/include')
if GPUProf_env['CXL_os'] == 'Linux':
    GPUProf_env.Prepend(CPPDEFINES="_LINUX")
# No libpath or libraries for ADL

# Cal 8.95
cal_dir = GPUProf_env['CXL_common_dir'] + '/Lib/AMD/CAL/8.95'
cal_inc = cal_dir + '/include'
#
GPUProf_env.Append(CPPPATH = [cal_inc])

# APPSDK 3.0 - headers only.
appsdk_dir = GPUProf_env['CXL_common_dir'] + '/Lib/AMD/APPSDK/3-0'
appsdk_inc = appsdk_dir + '/include'
GPUProf_env.Prepend(CPPPATH = [appsdk_inc])

# HSA - headers only.
hsa_dir = GPUProf_env['CXL_common_dir'] + '/Lib/AMD/HSA/STGFoundation/hsa'
hsa_inc = hsa_dir + '/include' , '/opt/rocm/hsa/include/hsa' #added local install of public release HSA headers to allow both Public and Internal builds
GPUProf_env.Prepend(CPPPATH = [hsa_inc])

# compiler flags required in order to build HSA headers
GPUProf_env.Append(CPPFLAGS = '-fpermissive -Wno-error ')

GPUProf_env.Append(CPPFLAGS = '-std=c++11 -DAMDT_DEBUG_SUFFIX=\'\' -DAMDT_BUILD_SUFFIX=\'\'')

# Assume that the environment does not need to be cloned, etc.
uic_files = Split(
        'EditNameValue.ui ' +
        'FindToolBar.ui ' +
        'ListViewDialog.ui ' +
        'ProfileSetting.ui ' +
        'SummaryViewBase.ui '
        )

moc_files = Split(
        'gpBaseSessionView.h ' +
        'gpViewsCreator.h ' +
        'APITimelineItems.h ' +
        'CounterSelectionSettingPage.h ' +
        'EditNameValueDialog.h ' +
        'GeneralSettingWindow.h ' +
        'ListViewWindow.h ' +
        'OpenCLTraceSettingPage.h ' +
        'ProfileManager.h ' +
        'ProfileSettingDialog.h ' +
        'SummaryView.h ' +
        'Session.h ' +
        'TraceView.h ' +
        'TraceTable.h ' +
        'CodeViewerWindow.h ' +
        'CustomDataTypes.h ' +
        'SessionControl.h ' +
        'SessionWindow.h '
        )

# This leaves annoying artifacts (objects) in ..../Common/Src
# There is probably a good way to do that (TBD)
commonsrc_files = Split(
        buildDirPath + '/ADLUtil/ADLUtil.cpp ' +
        buildDirPath + '/DeviceInfo/DeviceInfoUtils.cpp ' +
        buildDirPath + '/DynamicLibraryModule/DynamicLibraryModule.cpp ' +
        buildDirPath + '/DynamicLibraryModule/OpenCLModule.cpp ' +
        buildDirPath + '/AMDTMutex/AMDTMutex.cpp '
        )

if (GPUProf_env['CXL_hsa'] == 'true'):
    commonsrc_files += \
    [
        buildDirPath + '/HSAUtils/HSAUtils.cpp',
        buildDirPath + '/DynamicLibraryModule/HSAModule.cpp'
    ]

gpuprof_files = Split(
        'AMDTGpuProfilerPlugin.cpp ' +
        'gpViewsCreator.cpp ' +
        'APIColorMap.cpp ' +
        'APITimelineItems.cpp ' +
        'AsyncRemoteGpuProfilingTask.cpp ' +
        'CLAPIDefs.cpp ' +
        'CLAPIFilterManager.cpp ' +
        'CLSummarizer.cpp ' +
        'CLTimelineItems.cpp ' +
        'CodeViewerWindow.cpp ' +
        'CounterGroup.cpp ' +
        'CounterManager.cpp ' +
        'CounterSelectionSettingPage.cpp ' +
        'CustomDataTypes.cpp ' +
        'EditNameValueDialog.cpp ' +
        'FindToolBarView.cpp ' +
        'GeneralSettingWindow.cpp ' +
        'GlobalSettings.cpp ' +
        'GPUProjectHandler.cpp ' +
        'GpuSessionActionsCreator.cpp ' +
        'HSAAPIDefs.cpp ' +
        'HSATimelineItems.cpp ' +
        'KernelOccupancyWindow.cpp ' +
        'ListViewWindow.cpp ' +
        'OpenCLTraceSettingPage.cpp ' +
        'ProfileManager.cpp ' +
        'ProfileParam.cpp ' +
        'ProfileProcessMonitor.cpp ' +
        'ProfileSettingData.cpp ' +
        'ProfileSettingDialog.cpp ' +
        'Project.cpp ' +
        'ProjectSettings.cpp ' +
        'SessionControl.cpp ' +
        'Session.cpp ' +
        'SessionManager.cpp ' +
        'SessionWindow.cpp ' +
        'SessionViewTabWidget.cpp ' +
        'SummaryView.cpp ' +
        'SymbolInfo.cpp ' +
        'TraceTable.cpp ' +
        'TraceView.cpp ' +
        'Util.cpp ' + 
        'gpBaseSessionView.cpp ' +
        'gpTreeHandler.cpp ' +
        'AtpUtils.cpp ' +
        'CXLAnalyzerHTMLUtils.cpp ' +
        'CXLAtpFile.cpp '
        )

# Generated files first.
# Building uic files
#   foo.ui --> ui_foo.h
uic_gen = []
for uic_file in uic_files:
    uic_gen += GPUProf_env.UicBld(uic_file)

UIC_Generated = uic_gen

# Building moc content
#   foo.h --> moc_foo.cpp
moc_gen = []
for moc_file in moc_files:
    moc_gen += GPUProf_env.MocBld(moc_file)

MOC_Generated = moc_gen
# this dependency might not be needed here, but it is needed in other
# cases; adding the dependency has trivial "downside"
GPUProf_env.Depends(MOC_Generated, UIC_Generated)

# Now the "regular" build parts
gpuprof_objs = GPUProf_env.SharedObject(gpuprof_files + commonsrc_files + MOC_Generated)
all_objs = gpuprof_objs

GPUProfiling_Lib = [all_objs]
GPUProf_env.Depends(GPUProfiling_Lib, UIC_Generated + MOC_Generated)
soFiles = GPUProf_env.SharedLibrary(TheLibraryName, GPUProfiling_Lib)

libInstall = GPUProf_env.Install(
        dir = GPUProf_env['CXL_lib_dir'],
        source = (soFiles))

Return('libInstall')
