1# - Find bison executable and provides macros to generate custom build rules
2# The module defined the following variables:
3#  BISON_EXECUTABLE - path to the bison program
4#  BISON_VERSION - version of bison
5#  BISON_FOUND - true if the program was found
6# If bison is found, the module defines the macros:
7#  BISON_TARGET(<Name> <YaccInput> <CodeOutput> [VERBOSE <file>]
8#              [COMPILE_FLAGS <string>])
9# which will create  a custom rule to generate  a parser. <YaccInput> is
10# the path to  a yacc file. <CodeOutput> is the name  of the source file
11# generated by bison.  A header file is also  be generated, and contains
12# the  token  list.  If  COMPILE_FLAGS  option is  specified,  the  next
13# parameter is  added in the bison  command line.  if  VERBOSE option is
14# specified, <file> is created  and contains verbose descriptions of the
15# grammar and parser. The macro defines a set of variables:
16#  BISON_${Name}_DEFINED - true is the macro ran successfully
17#  BISON_${Name}_INPUT - The input source file, an alias for <YaccInput>
18#  BISON_${Name}_OUTPUT_SOURCE - The source file generated by bison
19#  BISON_${Name}_OUTPUT_HEADER - The header file generated by bison
20#  BISON_${Name}_OUTPUTS - The sources files generated by bison
21#  BISON_${Name}_COMPILE_FLAGS - Options used in the bison command line
22#
23# Example:
24# FIND_PACKAGE(BISON)
25# BISON_TARGET(MyParser parser.y ${PROJECT_BINARY_DIR}/parser.cpp)
26# ADD_EXECUTABLE(Foo main.cpp ${BISON_MyParser_OUTPUTS})
27#
28
29# Copyright (c) 2006, Tristan Carel
30# All rights reserved.
31# Redistribution and use in source and binary forms, with or without
32# modification, are permitted provided that the following conditions are met:
33#
34#     * Redistributions of source code must retain the above copyright
35#       notice, this list of conditions and the following disclaimer.
36#     * Redistributions in binary form must reproduce the above copyright
37#       notice, this list of conditions and the following disclaimer in the
38#       documentation and/or other materials provided with the distribution.
39#     * Neither the name of the University of California, Berkeley nor the
40#       names of its contributors may be used to endorse or promote products
41#       derived from this software without specific prior written permission.
42#
43# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
44# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
46# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
47# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53
54# $Id:: FindBISON.cmake 3 2006-11-03 02:42:02Z ken                            $
55
56SET(BISON_FOUND FALSE)
57
58FIND_PROGRAM(BISON_EXECUTABLE bison DOC "path to the bison executable")
59MARK_AS_ADVANCED(BISON_EXECUTABLE)
60
61IF(BISON_EXECUTABLE)
62  SET(BISON_FOUND TRUE)
63
64  EXECUTE_PROCESS(COMMAND ${BISON_EXECUTABLE} --version
65    OUTPUT_VARIABLE BISON_version_output
66    ERROR_VARIABLE BISON_version_error
67    RESULT_VARIABLE BISON_version_result
68    OUTPUT_STRIP_TRAILING_WHITESPACE)
69  IF(NOT ${BISON_version_result} EQUAL 0)
70    MESSAGE(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
71  ELSE(NOT ${BISON_version_result} EQUAL 0)
72    STRING(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
73      BISON_VERSION "${BISON_version_output}")
74  ENDIF(NOT ${BISON_version_result} EQUAL 0)
75
76  # internal macro
77  MACRO(BISON_TARGET_option_verbose Name BisonOutput filename)
78    LIST(APPEND BISON_TARGET_cmdopt "--verbose")
79    GET_FILENAME_COMPONENT(BISON_TARGET_output_path "${BisonOutput}" PATH)
80    GET_FILENAME_COMPONENT(BISON_TARGET_output_name "${BisonOutput}" NAME_WE)
81    ADD_CUSTOM_COMMAND(OUTPUT ${filename}
82      COMMAND ${CMAKE_COMMAND}
83      ARGS -E copy
84      "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
85      "${filename}"
86      DEPENDS
87      "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
88      COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
89      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
90    SET(BISON_${Name}_VERBOSE_FILE ${filename})
91    LIST(APPEND BISON_TARGET_extraoutputs
92      "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
93  ENDMACRO(BISON_TARGET_option_verbose)
94
95  # internal macro
96  MACRO(BISON_TARGET_option_extraopts Options)
97    SET(BISON_TARGET_extraopts "${Options}")
98    SEPARATE_ARGUMENTS(BISON_TARGET_extraopts)
99    LIST(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
100  ENDMACRO(BISON_TARGET_option_extraopts)
101
102  MACRO(BISON_TARGET Name BisonInput BisonOutput)
103    SET(BISON_TARGET_output_header "")
104    SET(BISON_TARGET_command_opt "")
105    SET(BISON_TARGET_outputs "${BisonOutput}")
106    IF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
107      MESSAGE(SEND_ERROR "Usage")
108    ELSE(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
109      # Parsing parameters
110      IF(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
111	IF("${ARGV3}" STREQUAL "VERBOSE")
112	  BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
113	ENDIF("${ARGV3}" STREQUAL "VERBOSE")
114	IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
115	  BISON_TARGET_option_extraopts("${ARGV4}")
116	ENDIF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
117      ENDIF(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
118      IF(${ARGC} EQUAL 7)
119	IF("${ARGV5}" STREQUAL "VERBOSE")
120	  BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
121	ENDIF("${ARGV5}" STREQUAL "VERBOSE")
122	IF("${ARGV5}" STREQUAL "COMPILE_FLAGS")
123	  BISON_TARGET_option_extraopts("${ARGV6}")
124	ENDIF("${ARGV5}" STREQUAL "COMPILE_FLAGS")
125      ENDIF(${ARGC} EQUAL 7)
126
127      # Header's name generated by bison (see option -d)
128      LIST(APPEND BISON_TARGET_cmdopt "-d")
129      STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
130      STRING(REPLACE "c" "h" _fileext ${_fileext})
131      STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
132	BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
133      LIST(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
134
135      ADD_CUSTOM_COMMAND(OUTPUT ${BISON_TARGET_outputs}
136        ${BISON_TARGET_extraoutputs}
137        COMMAND ${BISON_EXECUTABLE}
138        ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
139        DEPENDS ${ARGV1}
140        COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
141        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
142
143      # define target variables
144      SET(BISON_${Name}_DEFINED TRUE)
145      SET(BISON_${Name}_INPUT ${ARGV1})
146      SET(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
147      SET(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
148      SET(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
149
150    ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
151  ENDMACRO(BISON_TARGET)
152
153ENDIF(BISON_EXECUTABLE)
154
155
156IF(NOT BISON_FOUND)
157  IF(NOT BISON_FIND_QUIETLY)
158    MESSAGE(STATUS "BISON was not found.")
159  ELSE(NOT BISON_FIND_QUIETLY)
160    IF(BISON_FIND_REQUIRED)
161      MESSAGE(FATAL_ERROR "BISON was not found.")
162    ENDIF(BISON_FIND_REQUIRED)
163  ENDIF(NOT BISON_FIND_QUIETLY)
164ENDIF(NOT BISON_FOUND)
165
166# FindBISON.cmake ends here
167