cmake_minimum_required(VERSION 3.16)

project(TileTrace CXX C)

if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  message(FATAL_ERROR "Do not support non-Linux now!")
endif()

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(DEFAULT_BUILD_TYPE "Debug")
if(NOT CMAKE_BUILD_TYPE)
  message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}")
endif()

set(USE_MPI ON)

# set(DETAIED_TIMER ON)

if(USE_MPI)
  find_package(MPI REQUIRED)
  if(NOT MPI_FOUND)
    message(FATAL_ERROR "MPI is NOT found!")
  endif()
  # set(OTF2XX_WITH_MPI ON)
  add_definitions(-DOTF2XX_WITH_MPI=ON)
endif()

find_package(Dyninst REQUIRED)
if(NOT Dyninst_FOUND)
    message(FATAL_ERROR "Dyninst is NOT found!")
endif()
message(STATUS "Dyninst: '${DYNINST_INCLUDE_DIR}'!")
message(STATUS "Dyninst: '${DYNINST_LIBRARIES}'!")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third_party/otf2xx/cmake")
add_subdirectory(third_party/otf2xx)

set(
    UTILS_FILES
    src/utils/Utils.cpp
    src/utils/Base.cpp
    src/utils/LocalData.cpp
    src/utils/DataStruct.cpp
)

set(
    READER_FILES
    src/reader/TraceReader.cpp  
    src/reader/mpiprof/MpiProfReader.cpp
    src/reader/otf2/OTF2TraceReader.cpp
)

set(
    STATIC_FILES
    src/static/StructureAnalysis.cpp
)

set(
    PREPRCS_FILES
    src/preprocess/InteractionPattern.cpp
    src/preprocess/Tiling.cpp
)

set(
    TRACE_FILES
    src/trace/Event.cpp  
    src/trace/LocalTrace.cpp  
    src/trace/TiledTrace.cpp
    src/trace/TiledTraceGroup.cpp
)

set(
    TILE_FILES
)

set(
    REPLAY_FILES
    src/replay/TraceReplay.cpp
    src/replay/InteractionDataFetch.cpp
)

add_library(
  libtiletrace 
  STATIC 
  OBJECT
  ${UTILS_FILES}
  ${READER_FILES}
  ${PREPRCS_FILES}
  ${TRACE_FILES}
  ${TILE_FILES}
  ${REPLAY_FILES}
  ${STATIC_FILES}
)

target_include_directories(libtiletrace PUBLIC ${PROJECT_SOURCE_DIR}/include/)
target_include_directories(libtiletrace PUBLIC ${PROJECT_SOURCE_DIR}/third_party)
target_include_directories(libtiletrace PUBLIC ${PROJECT_SOURCE_DIR}/third_party/otf2xx/include)

if(USE_MPI)
  message(STATUS "TileTrace using MPI.")
  target_include_directories(libtiletrace PRIVATE ${MPI_INCLUDE_DIRS})
  target_link_libraries(libtiletrace PRIVATE MPI::MPI_CXX)
  target_compile_definitions(libtiletrace PRIVATE TT_USE_MPI DETAIED_TIMER)
endif()

target_link_libraries(libtiletrace PUBLIC otf2xx::otf2xx)


# For static analysis
target_include_directories(libtiletrace PUBLIC ${DYNINST_INCLUDE_DIR})
# target_link_libraries(libtiletrace PUBLIC ${DYNINST_LIBRARIES})
# target_link_libraries(libtiletrace PUBLIC ${Boost_LIBRARIES})

# 链接Dyninst相关库
target_link_libraries(libtiletrace
    PUBLIC
    dyninstAPI
    parseAPI
    instructionAPI
    symtabAPI
    # symLite 
    # dynDwarf 
    # dynElf 
    # common 
    # unwind
    # # Dyninst依赖的基础库
    # ${Boost_SYSTEM_LIBRARY}
    # ${Boost_THREAD_LIBRARY}
    # tbb
    # elf
    # dwarf
    # iberty
)

add_subdirectory(test)