# - Get software version string from GIT version control system.
# This module obtains a version string from GIT using the 'git describe'
# command. The version string is update at build time.
#
# For this to properly work the software expects that every release is tagged
# with a tag in the format:
#   'v{major}.{minor}.{patchlevel}'
#
# A version.h file is generated in the binary directory corrosponding the
# diretory from which this module is included.
#
# The following option may be set:
#   GGV_VERSION_FILENAME - Name of output file, default 'version.h'
#   GGV_DEFAULT_VERSION - Version to use if GIT version could not be
#                         determined, default 'unknown_dirty'
#   GGV_TAG_PATTERN - Pattern used to match tags, default 'v*'
#
# Example:
#   include(GitGetVersion)
#   add_dependencies(your_main_target git_version)
#
#=============================================================================
# Copyright (c) 2015, David Imhoff <dimhoff.devel@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#=============================================================================
#

if (GGV_RUN_INTERNAL)
  # Internal code, runs at build time to determine current version
  find_package(Git QUIET)

  set(VERSION_VCS "${GGV_DEFAULT_VERSION}")
  if (GIT_FOUND)
    execute_process(COMMAND
      "${GIT_EXECUTABLE}" describe --tags --dirty --always
        --match=${GGV_TAG_PATTERN}
      WORKING_DIRECTORY "${GGV_SOURCE_DIR}"
      RESULT_VARIABLE GIT_DESCRIBE_EXIT_CODE
      OUTPUT_VARIABLE GIT_DESCRIBE_OUT
      ERROR_QUIET
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    if (GIT_DESCRIBE_EXIT_CODE EQUAL 0)
      set(VERSION_VCS "${GIT_DESCRIBE_OUT}")
    endif (GIT_DESCRIBE_EXIT_CODE EQUAL 0)
  endif (GIT_FOUND)

  # Generate version.h
  file(WRITE ${GGV_VERSION_FILENAME}.in
"// AUTOGENERATED by GitGetVersion.cmake
\#ifndef __VERSION_H__
\#define __VERSION_H__
\#define VERSION \"${VERSION_VCS}\"
\#endif //__VERSION_H__
")
  # Replace current file only if modified
  configure_file(
    ${GGV_VERSION_FILENAME}.in
    ${GGV_VERSION_FILENAME}
    COPYONLY)
else (GGV_RUN_INTERNAL)
  # Code run at configure time to create targets
  if (NOT DEFINED GGV_VERSION_FILENAME)
    set(GGV_VERSION_FILENAME version.h)
  endif (NOT DEFINED GGV_VERSION_FILENAME)

  if (NOT DEFINED GGV_DEFAULT_VERSION)
    set(GGV_DEFAULT_VERSION unknown_dirty)
  endif (NOT DEFINED GGV_DEFAULT_VERSION)

  if (NOT DEFINED GGV_TAG_PATTERN)
    set(GGV_TAG_PATTERN v*)
  endif (NOT DEFINED GGV_TAG_PATTERN)

  add_custom_target(
    git_version
    ${CMAKE_COMMAND}
      -D "GGV_RUN_INTERNAL:bool=ON"
      -D "GGV_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
      -D "GGV_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}"
      -D "GGV_VERSION_FILENAME=${GGV_VERSION_FILENAME}"
      -D "GGV_DEFAULT_VERSION=${GGV_DEFAULT_VERSION}"
      -D "GGV_TAG_PATTERN=${GGV_TAG_PATTERN}"
      -P ${CMAKE_CURRENT_LIST_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif (GGV_RUN_INTERNAL)
