# Attempt to obtain git revision / dirty status
set(CELERITY_VERSION_GIT_REVISION "unknown")
set(CELERITY_VERSION_GIT_IS_DIRTY 0)

find_package(Git QUIET)
if(GIT_FOUND)
  execute_process(
    COMMAND "${GIT_EXECUTABLE}" rev-parse --short HEAD
    WORKING_DIRECTORY "${CELERITY_SOURCE_DIR}"
    RESULT_VARIABLE EXIT_CODE
    OUTPUT_VARIABLE GIT_REVISION
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  if(EXIT_CODE EQUAL 0)
    set(CELERITY_VERSION_GIT_REVISION ${GIT_REVISION})
    execute_process(
      COMMAND "${GIT_EXECUTABLE}" diff --quiet HEAD
      WORKING_DIRECTORY "${CELERITY_SOURCE_DIR}"
      RESULT_VARIABLE CELERITY_VERSION_GIT_IS_DIRTY
      ERROR_QUIET
    )
  endif()
endif()

set(MESSAGE_GIT_DIRTY)
if(CELERITY_VERSION_GIT_IS_DIRTY)
  set(MESSAGE_GIT_DIRTY "-dirty")
endif()
message(VERBOSE "Celerity git revision is ${CELERITY_VERSION_GIT_REVISION}${MESSAGE_GIT_DIRTY}")

configure_file("${CELERITY_SOURCE_DIR}/src/version.cc.in" src/version.cc @ONLY)
