############################ begin_copyright_notice ############################
### Copyright (C) 2021-2023 Intel Corporation
###
### SPDX-License-Identifier: MIT
############################ end_copyright_notice ##############################

cmake_minimum_required(VERSION 3.16)
project (GTREPLAY_EXAMPLES)


###### 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)
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX"  )
elseif ( UNIX ) #linux
    add_definitions(-DTARGET_LINUX)
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mavx2 -pthread")
else ()
    message( FATAL_ERROR "OS not supported" )
endif()

if ( WIN32 )
    set( LINK_LIBS "WS2_32"  )

else ()
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads)
    set( LINK_LIBS "dl ${CMAKE_THREAD_LIBS_INIT}")
    string(STRIP ${LINK_LIBS} LINK_LIBS)
endif ()

###### setup GTReplay kit ######
if ( DEFINED $ENV{GTPIN_KIT} )
    message( STATUS "GTPIN kit used form env: $ENV{GTPIN_KIT}" )
    set ( GTPIN_KIT $ENV{GTPIN_KIT} )
    set ( GTREPLAY_KIT $ENV{GTPIN_KIT}/GTReplay )
    string(REGEX REPLACE "\\\\" "/" GTPIN_KIT "${GTPIN_KIT}")
    string(REGEX REPLACE "\\\\" "/" GTREPLAY_KIT "${GTREPLAY_KIT}")
elseif ( DEFINED GTPIN_KIT )
    set ( GTREPLAY_KIT ${GTPIN_KIT}/GTReplay )
    string(REGEX REPLACE "\\\\" "/" GTPIN_KIT "${GTPIN_KIT}")
    string(REGEX REPLACE "\\\\" "/" GTREPLAY_KIT "${GTREPLAY_KIT}")
    message( STATUS "GTPIN kit used: ${GTPIN_KIT}" )
else ()
    message( FATAL_ERROR "Missing GTPin kit" )
endif ()


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

set ( GTREPLAY_EXAMPLES icount imix ops mem toggle bbltrace)

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

set (UTILS
            ${GTREPLAY_KIT}/utils/knob_parser.cpp
            ${GTREPLAY_KIT}/utils/gtreplay_assert.cpp
)
add_library( gtreplay_utils ${UTILS} )
set_property(TARGET gtreplay_utils PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library( icount SHARED icount.cpp)
add_library( bbltrace SHARED bbltrace.cpp)
add_library( imix SHARED imix.cpp)
add_library( ops SHARED ops.cpp)
add_library( mem SHARED mem.cpp mem_model.cpp ${GTPIN_KIT}/Examples/utils/gen_send_decoder.cpp)
add_library( toggle SHARED toggle.cpp)

set_property(TARGET icount PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET bbltrace PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET imix PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET ops PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET mem PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET toggle PROPERTY POSITION_INDEPENDENT_CODE ON)

foreach (trg ${GTREPLAY_EXAMPLES} )
    target_link_libraries ( ${trg} gtracereplay gtreplay_utils ged)
endforeach (trg)

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

set ( CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../../Lib/${ARCH}" )
set ( CMAKE_BUILD_WITH_INSTALL_RPATH ON)

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

install ( TARGETS ${GTREPLAY_EXAMPLES} ${RUNTIME} DESTINATION ${INSTALL_LIB_TRG} )
