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