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
25 # BISON_TARGET(MyParser parser.y ${PROJECT_BINARY_DIR}/parser.cpp)
26 # ADD_EXECUTABLE(Foo main.cpp ${BISON_MyParser_OUTPUTS})
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:
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.
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.
54 # $Id:: FindBISON.cmake 3 2006-11-03 02:42:02Z ken $
56 SET(BISON_FOUND FALSE)
58 FIND_PROGRAM(BISON_EXECUTABLE bison DOC "path to the bison executable")
59 MARK_AS_ADVANCED(BISON_EXECUTABLE)
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)
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} -E copy
83 "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
86 "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
87 COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
88 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
89 SET(BISON_${Name}_VERBOSE_FILE ${filename})
90 LIST(APPEND BISON_TARGET_extraoutputs
91 "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
92 ENDMACRO(BISON_TARGET_option_verbose)
95 MACRO(BISON_TARGET_option_extraopts Options)
96 SET(BISON_TARGET_extraopts "${Options}")
97 SEPARATE_ARGUMENTS(BISON_TARGET_extraopts)
98 LIST(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
99 ENDMACRO(BISON_TARGET_option_extraopts)
101 MACRO(BISON_TARGET Name BisonInput BisonOutput)
102 SET(BISON_TARGET_output_header "")
103 SET(BISON_TARGET_cmdopt "")
104 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)
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)
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)
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}")
135 ADD_CUSTOM_COMMAND(OUTPUT ${BISON_TARGET_outputs}
136 ${BISON_TARGET_extraoutputs}
137 COMMAND ${BISON_EXECUTABLE} ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
139 COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
140 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
142 # define target variables
143 SET(BISON_${Name}_DEFINED TRUE)
144 SET(BISON_${Name}_INPUT ${ARGV1})
145 SET(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
146 SET(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
147 SET(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
149 ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
150 ENDMACRO(BISON_TARGET)
152 ENDIF(BISON_EXECUTABLE)
156 IF(NOT BISON_FIND_QUIETLY)
157 MESSAGE(STATUS "BISON was not found.")
158 ELSE(NOT BISON_FIND_QUIETLY)
159 IF(BISON_FIND_REQUIRED)
160 MESSAGE(FATAL_ERROR "BISON was not found.")
161 ENDIF(BISON_FIND_REQUIRED)
162 ENDIF(NOT BISON_FIND_QUIETLY)
163 ENDIF(NOT BISON_FOUND)
165 # FindBISON.cmake ends here