# - Find etags/ctags programs and provides macros to define tag table files
# The module provides the following variables:
#  TAGS_ETAGS_EXECUTABLE - path to the tag table file generator for Emacs
#  TAGS_ETAGS_FOUND - true if the program etags is found
#  TAGS_CTAGS_EXECUTABLE - path to the tag table file generator for Vi
#  TAGS_CTAGS_FOUND - true if the program ctags is found
#  TAGS_FOUND - true if etags or ctags is found
# If etags program is found the macro
#   TAGS_ETAGS(<output-file> <source-file> [<source-file> ...])
# is defined to generate a tag table of the source files understood by emacs
# named <output-file>
#
# If ctags program is found the macro
#   TAGS_CTAGS(<output-file> <source-file> [<source-file> ...])
# is defined to generate a tag table of the source files understood by vi
# named <output-file>
#
# if etags or ctags programs are found the macro
#  TAGS_TARGET_NAME(<name>)
# is defined to generate a target named <name> which depends of all the
# tag tables previously defined.
#
# Example usage:
#  FIND_PACKAGE(TAGS)
#  IF(TAGS_ETAGS_FOUND)
#    TAGS_ETAGS(${PROJECT_SOURCE_DIR}/ETAGS foo.cc foo.h)
#  ENDIF(TAGS_ETAGS_FOUND)
#  IF(TAGS_ETAGS_FOUND)
#    TAGS_CTAGS(${PROJECT_SOURCE_DIR}/CTAGS foo.cc foo.h)
#  ENDIF(TAGS_ETAGS_FOUND)
#  IF(TAGS_FOUND)
#    TAGS_TARGET_NAME(TAGS)
#  ENDIFIF(TAGS_FOUND)
#

# Copyright (c) 2006, Tristan Carel
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Berkeley nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# $Id:: FindTAGS.cmake 145 2006-10-31 22:56:51Z tristan                       $

SET(TAGS_FOUND FALSE)

FIND_PROGRAM(TAGS_ETAGS_EXECUTABLE etags DOC "tag table generator for Emacs")
MARK_AS_ADVANCED(TAGS_ETAGS_EXECUTABLE)
IF(TAGS_ETAGS_EXECUTABLE)
  SET(TAGS_ETAGS_FOUND TRUE)
  SET(TAGS_FOUND TRUE)
ENDIF(TAGS_ETAGS_EXECUTABLE)

FIND_PROGRAM(TAGS_CTAGS_EXECUTABLE ctags DOC "tag table generator for Vi")
MARK_AS_ADVANCED(TAGS_CTAGS_EXECUTABLE)
IF(TAGS_CTAGS_EXECUTABLE)
  SET(TAGS_CTAGS_FOUND TRUE)
  SET(TAGS_FOUND TRUE)
ENDIF(TAGS_CTAGS_EXECUTABLE)

IF(TAGS_FOUND)
  MACRO(TAGS_generate type output)
    LIST(APPEND TAGS_outputs ${output})
    SET(TAGS_ETAGS_files ${ARGV})
    LIST(REMOVE_AT TAGS_ETAGS_files 0 1)
    ADD_CUSTOM_COMMAND(OUTPUT ${output}
      COMMAND ${TAGS_${type}_EXECUTABLE} -o ${output} ${TAGS_ETAGS_files}
      DEPENDS ${TAGS_ETAGS_files}
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      COMMENT "Building tag table ${output}  with ${TAGS_${type}_EXECUTABLE}")
  ENDMACRO(TAGS_generate)

  MACRO(TAGS_TARGET_NAME name)
    IF(TAGS_outputs)
      ADD_CUSTOM_TARGET(${name} DEPENDS ${TAGS_outputs})
    ENDIF(TAGS_outputs)
  ENDMACRO(TAGS_TARGET_NAME)
ENDIF(TAGS_FOUND)

IF(TAGS_ETAGS_FOUND)
  MACRO(TAGS_ETAGS output)
    TAGS_generate(ETAGS ${ARGV})
  ENDMACRO(TAGS_ETAGS output)
ENDIF(TAGS_ETAGS_FOUND)

IF(TAGS_CTAGS_FOUND)
  MACRO(TAGS_CTAGS output)
    TAGS_generate(CTAGS ${ARGV})
  ENDMACRO(TAGS_CTAGS output)
ENDIF(TAGS_CTAGS_FOUND)

# FindTAGS.cmake ends here
