# TOP LEVEL CMAKE FILE
# -----------------------------------------------------------------------------
# Disable in-source builds to prevent source tree corruption.
# -----------------------------------------------------------------------------
if(" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
    message(FATAL_ERROR "FATAL: In-source builds are not allowed.
    You should create a separate directory for build files.")
endif()

# -----------------------------------------------------------------------------
# Minimum supported versions
# -----------------------------------------------------------------------------
include(cmake/MCDSMinDepVersions.cmake)
cmake_minimum_required(VERSION "${MIN_VER_CMAKE}" FATAL_ERROR)

# -----------------------------------------------------------------------------
# Configure CMake policies
# -----------------------------------------------------------------------------
#list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# -----------------------------------------------------------------------------
# Project definition MCDS:
# -----------------------------------------------------------------------------
project(mcds_all
    VERSION 5.0.1
    DESCRIPTION "MCDS libraries"
    HOMEPAGE_URL "https://bitbucket.vih.infineon.com/projects/ATVDT/repos/trace_mcds_rework/browse"
    LANGUAGES C CXX
)
set(MCDS_LIBS_COPYRIGHT "(c) Infineon Technologies AG 2025")

# -----------------------------------------------------------------------------
# Component versions and generated version header
# -----------------------------------------------------------------------------
include(cmake/MCDSComponentVersions.cmake)
configure_file(
    cmake/templates/mcds_version.h.in
    ${CMAKE_BINARY_DIR}/generated/mcds_version.h
)
include_directories("${CMAKE_BINARY_DIR}/generated")

# -----------------------------------------------------------------------------
# Break in case of popular CMake configuration mistakes
# -----------------------------------------------------------------------------
if(NOT CMAKE_SIZEOF_VOID_P GREATER 0)
    message(FATAL_ERROR "CMake fails to determine the bitness of the target
    platform. Please check your CMake and compiler installation. If you are
    cross-compiling then ensure that your CMake toolchain file correctly sets
    the compiler details.")
endif()

# -----------------------------------------------------------------------------
# Add-ons
# -----------------------------------------------------------------------------
include(GNUInstallDirs)
include(cmake/MCDSUtils.cmake)
include(cmake/MCDSDetectCXXCompiler.cmake)

set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
include(CTest)

# -----------------------------------------------------------------------------
# Library type (shared / static)
# -----------------------------------------------------------------------------
option(BUILD_SHARED_LIBS "Build shared libraries" ON)

if(BUILD_SHARED_LIBS)
  set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
endif()

# -----------------------------------------------------------------------------
# Project build components
# -----------------------------------------------------------------------------
option(MCDS_BUILD_MTV "Builds MCDS Trace Viewer if enabled" OFF)
option(MCDS_BUILD_PYTHON "Builds MCDS Python wrapper if enabled" OFF)
option(MCDS_BUILD_DEMOS "Builds demos binaries if enabled" OFF)
option(MCDS_BUILD_TEST "Builds test binaries if enabled" OFF)
option(MCDS_BUILD_DOCS "Build MCDS API reference documentation using doxygen if enabled" OFF)

# -----------------------------------------------------------------------------
# Find relevant dependencies
# -----------------------------------------------------------------------------
find_package(tas_client_api CONFIG REQUIRED)
find_package(elf_disasm CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(nlohmann_json_schema_validator CONFIG REQUIRED)
find_package(capstone CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
if(MCDS_BUILD_MTV)
    find_package(fltk CONFIG REQUIRED)
endif()

# -----------------------------------------------------------------------------
# Global project properties
# -----------------------------------------------------------------------------
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# -----------------------------------------------------------------------------
# CMake Sub-projects:
# -----------------------------------------------------------------------------
# Sub-project define the relevant targets
add_subdirectory(mcds)
# build mcds_other only on windows
if (WIN32)
    add_subdirectory(mcds_other)
endif()

# Python wrapper
add_subdirectory(python)

# -----------------------------------------------------------------------------
# common source files:
# -----------------------------------------------------------------------------
# need to be included after sub-project so the relevant target definitions are
# available
add_subdirectory(apps)
add_subdirectory(docs)
add_subdirectory(src)
if(MCDS_BUILD_TEST)
    add_subdirectory(tests)
endif()
add_subdirectory(test)

# -----------------------------------------------------------------------------
# Install
# -----------------------------------------------------------------------------
# export targets
install(EXPORT McdsLibsTargets
    FILE McdsLibsTargets.cmake
    NAMESPACE McdsLibs::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/McdsLibs
        COMPONENT libraries
)

# export targets to be used from a build directory
export(EXPORT McdsLibsTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/McdsLibsTargets.cmake"
    NAMESPACE McdsLibs::
)

# Package definition
# provides configure_package_config_file() which enables creation of relocatable packages.
include(CMakePackageConfigHelpers)

# Package configuration file
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/McdsLibsConfig.cmake.in
    "${CMAKE_CURRENT_BINARY_DIR}/McdsLibsConfig.cmake"
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/McdsLibs
)

# Package version file
# If no specific version set, it takes PROJECT_VERSION
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/McdsLibsConfigVersion.cmake"
    COMPATIBILITY SameMajorVersion
)

install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/McdsLibsConfig.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/McdsLibsConfigVersion.cmake"
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/McdsLibs
        COMPONENT libraries
)

# Public headers (single install point for all mcds_libs headers)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/mcds_libs
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    COMPONENT headers
)
# Generated version header (included by the public API headers)
install(FILES ${CMAKE_BINARY_DIR}/generated/mcds_version.h
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mcds_libs
    COMPONENT headers
)

if(EXISTS "${CMAKE_SOURCE_DIR}/apps/mcds_lib_simple_demo")
  install(FILES
      "${CMAKE_SOURCE_DIR}/apps/mcds_lib_simple_demo/CMakeLists.txt"
      "${CMAKE_SOURCE_DIR}/apps/mcds_lib_simple_demo/mcds_lib_simple_demo.cpp"
      DESTINATION demo/
          COMPONENT demos
  )
endif()

# -----------------------------------------------------------------------------
# Package Generator
# -----------------------------------------------------------------------------
if(WIN32)
    set(CPACK_GENERATOR ZIP)
elseif(UNIX)
    set(CPACK_GENERATOR TGZ STGZ)
endif()

set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
set(CPACK_PACKAGE_DESCRIPTION "Multi-Core Debug Solution (MCDS)")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VENDOR "Infineon Technologies AG")
set(CPACK_PACKAGE_CONTACT "das.support@infineon.com")
set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}_V${mcds_all_VERSION_MAJOR}_${mcds_all_VERSION_MINOR}_${mcds_all_VERSION_PATCH}")

include(CPack)

cpack_add_component(applications
    DISPLAY_NAME "MCDS Applications"
    DESCRIPTION "Native MCDS tools"
    GROUP Runtime
)

cpack_add_component(libraries
    DISPLAY_NAME "MCDS API DLLs"
    DESCRIPTION "Shared libraries for MCDS tool development"
    GROUP Runtime
)

cpack_add_component(headers
    DISPLAY_NAME "MCDS API header files"
    DESCRIPTION "C/C++ header files for MCDS tool development"
    GROUP Runtime
)

cpack_add_component(demos
    DISPLAY_NAME "MCDS API demos"
    DESCRIPTION "C/C++ project demos for MCDS tool development"
    GROUP Runtime
)
