# ------------------- Source files --------------------

add_library(
  PugsUtils
  BuildInfo.cpp
  BacktraceManager.cpp
  ConsoleManager.cpp
  Demangle.cpp
  Exceptions.cpp
  FPEManager.cpp
  Messenger.cpp
  Partitioner.cpp
  PugsUtils.cpp
  RevisionInfo.cpp
  SignalManager.cpp)

# --------------- get git revision info ---------------

# Generates revision header file
add_custom_target(
  PugsGitRevison
  ALL
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
)

set_source_files_properties(
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
  PROPERTIES
  GENERATED TRUE
  HEADER_FILE_ONLY TRUE
  )

# --------- check git revision info at build ----------

add_custom_command(TARGET PugsGitRevison
  COMMAND ${CMAKE_COMMAND} -E remove -f
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
  COMMENT ""
  )

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
  PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
  COMMENT ""
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision
  VERBATIM
  )

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision
  PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -DPUGS_VERSION=${PUGS_VERSION} -DPUGS_SOURCE_DIR=${PUGS_SOURCE_DIR} -P ${PUGS_SOURCE_DIR}/cmake/GetPugsGitRevision.cmake
  COMMENT "Check pugs git status"
  VERBATIM
  )

# ----------------- get version info ------------------

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pugs_version.hpp.in"
  "${CMAKE_CURRENT_BINARY_DIR}/pugs_version.hpp"
  @ONLY)


list(
  APPEND
  SOURCES
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_git_revision.hpp
  ${CMAKE_CURRENT_BINARY_DIR}/pugs_version.hpp
  ${PUGS_BINARY_DIR}/pugs_config.hpp
  )

# Additional dependencies
add_dependencies(
  PugsUtils
  PugsGitRevison)

# ---------------- Checks for includes ----------------

# Checks for FPE
include(CheckIncludeFile)
check_include_file(fenv.h PUGS_HAS_FENV_H)

# Checks for host system name
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
  set(SYSTEM_IS_LINUX 1)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  set(SYSTEM_IS_DARWIN 1)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
  set(SYSTEM_IS_WINDOWS 1)
else()
  message(WARNMSG "Your system is ${CMAKE_HOST_SYSTEM_NAME} which may not be managed!")
endif()

# Generates pugs_config.hpp
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pugs_config.hpp.in"
  "${CMAKE_CURRENT_BINARY_DIR}/pugs_config.hpp"
  @ONLY)

# Generates pugs_build_info.hpp
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pugs_build_info.hpp.in"
  "${CMAKE_CURRENT_BINARY_DIR}/pugs_build_info.hpp"
  @ONLY)

# ------------------- Installation --------------------
# temporary version workaround
if(${CMAKE_VERSION} VERSION_LESS "3.13.0")
  install(TARGETS PugsUtils
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib)
endif()
