############################ begin_copyright_notice ############################
### Copyright (C) 2015-2026 Intel Corporation
###
### SPDX-License-Identifier: MIT
############################ end_copyright_notice ##############################

cmake_minimum_required(VERSION 3.16)
cmake_policy(SET CMP0091 NEW)   # required for setting MSVC type

project (GTPin_Examples)

set ( CMAKE_CXX_STANDARD 17 )
set ( CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" )

if (WIN32)
     set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX" )
elseif (UNIX)
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror" )
endif ()

###### setup architecture ######
#ARCH is not specified setting it to intel64
if (NOT DEFINED ARCH)
    set ( ARCH "intel64")
endif()
message( STATUS "ARCH = ${ARCH}")

if (WIN32)
    add_definitions(-DTARGET_WINDOWS)
elseif ( UNIX ) #linux
    add_definitions(-DTARGET_LINUX)
endif()

###### setup GTPin kit ######
if ( DEFINED ENV{GTPIN_KIT} )
    message( STATUS "GTPIN kit used form env: $ENV{GTPIN_KIT}" )
    set ( GTPIN_KIT $ENV{GTPIN_KIT} )
    #get_filename_component ( GTPIN_KIT ENV{GTPIN_KIT} ABSOLUTE )
elseif ( DEFINED GTPIN_KIT )
    message( STATUS "GTPIN kit used: ${GTPIN_KIT}" )
else ()
    message( FATAL_ERROR "Missing GTPin kit" )
endif ()

set ( INSTALL_TRG ${CMAKE_BINARY_DIR}/${ARCH} )
if (WIN32)
    set (RUNTIME "RUNTIME") # used for install only dll's on windows
endif ()

set ( EXAMPLES          funtime latency itrace  memorytrace perftrace opcodeprof simdprof kernel_weight my_gtpin_tool null funlatency edge_count error_tool)
list (APPEND EXAMPLES   hlif_bcount hlif_ecount hlif_opcodeprof bounds_check memory_check simdprof_hlif cachelineprof hlif_nothing hli_skeleton)

set ( EXTERNAL_EXAMPLES funtime latency itrace  memorytrace perftrace opcodeprof simdprof kernel_weight my_gtpin_tool null funlatency edge_count error_tool)
list (APPEND EXTERNAL_EXAMPLES  hlif_bcount hlif_ecount hlif_opcodeprof bounds_check memory_check simdprof_hlif cachelineprof hlif_nothing hli_skeleton)


include_directories ( utils )
include_directories ( ${GTPIN_KIT}/Include )
include_directories ( ${GTPIN_KIT}/Include/api )
include_directories ( ${GTPIN_KIT}/Include/ged/${ARCH} )
include_directories ( ${GTPIN_KIT}/common )
link_directories    ( ${GTPIN_KIT}/Lib/${ARCH} )

set (UTILS
            utils/gtpin_crt_utils.cpp
            utils/knob_parser.cpp
            utils/gen_send_decoder.cpp
            utils/kernel_exec_config.cpp
            utils/gtpin_tool_utils.cpp
            utils/opcodeprof_utils.cpp
            )
add_library( gtpintool_utils STATIC ${UTILS} )
target_link_libraries ( gtpintool_utils gtpin ged )

set_property(TARGET gtpintool_utils PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library( funtime SHARED funtime.cpp )
add_library( latency SHARED latency.cpp )
add_library( itrace SHARED itrace.cpp kernel_weight.cpp )
add_library( memorytrace SHARED memorytrace.cpp kernel_weight.cpp )
add_library( perftrace SHARED perftrace.cpp kernel_weight.cpp )
add_library( opcodeprof SHARED opcodeprof.cpp)
add_library( simdprof SHARED simdprof.cpp )
add_library( kernel_weight SHARED kernel_weight.cpp )
add_library( my_gtpin_tool SHARED my_gtpin_tool.cpp )
add_library( null SHARED null.c )
add_library( hlif_bcount SHARED hlif_bcount.cpp )
add_library( hlif_ecount SHARED hlif_ecount.cpp )
add_library( hlif_nothing SHARED hlif_nothing.cpp )
add_library( hli_skeleton SHARED hli_skeleton.cpp )
add_library( bounds_check SHARED bounds_check.cpp )
add_library( memory_check SHARED memory_check.cpp )
add_library( cachelineprof SHARED cachelineprof.cpp )
add_library( hlif_opcodeprof SHARED hlif_opcodeprof.cpp )
add_library( simdprof_hlif SHARED simdprof_hlif.cpp )
add_library( funlatency SHARED funlatency.cpp )
add_library( edge_count SHARED edge_count.cpp )
add_library( error_tool SHARED error_tool.cpp )

target_compile_definitions(kernel_weight PUBLIC KERNEL_WEIGHT_STANDALONE)

# set required link libraries
foreach (trg ${EXAMPLES} )
    target_link_libraries ( ${trg} gtpintool_utils )
    target_link_libraries ( ${trg} gtpin ged )
    target_sources( ${trg} PUBLIC ${RESOURCE_FILE} )
endforeach (trg)

# remove auto generated 'lib' prefix for shared object
if (NOT WIN32)
    set_property( TARGET ${EXAMPLES} PROPERTY PREFIX "")
endif ()

if( ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" )
    set_property ( TARGET ${EXAMPLES}
                   PROPERTY INSTALL_RPATH "$ORIGIN/../../Lib/${ARCH}" )
endif()

install ( TARGETS ${EXAMPLES} ${RUNTIME} DESTINATION ${INSTALL_TRG} )
