root/trunk/cmake/FindBison.cmake

Revision 7, 4.4 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 # Copyright (c) 2009, Whispersoft s.r.l.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Whispersoft s.r.l. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30 # - Look for GNU Bison, the parser generator
31 # Based off a news post from Andy Cedilnik at Kitware
32 # Defines the following:
33 #  BISON_EXECUTABLE - path to the bison executable
34 #  BISON_FILE - parse a file with bison
35 #  BISON_PREFIX_OUTPUTS - Set to true to make BISON_FILE produce prefixed
36 #                         symbols in the generated output based on filename.
37 #                         So for ${filename}.y, you'll get ${filename}parse(), etc.
38 #                         instead of yyparse().
39 #  BISON_GENERATE_DEFINES - Set to true to make BISON_FILE output the matching
40 #                           .h file for a .c file. You want this if you're using
41 #                           flex.
42
43 if(NOT DEFINED BISON_PREFIX_OUTPUTS)
44   set(BISON_PREFIX_OUTPUTS FALSE)
45 endif(NOT DEFINED BISON_PREFIX_OUTPUTS)
46
47 if(NOT DEFINED BISON_GENERATE_DEFINES)
48   set(BISON_GENERATE_DEFINES FALSE)
49 endif(NOT DEFINED BISON_GENERATE_DEFINES)
50
51 if(NOT BISON_EXECUTABLE)
52   message(STATUS "Looking for bison")
53   find_program(BISON_EXECUTABLE bison)
54   if(BISON_EXECUTABLE)
55     message(STATUS "Looking for bison -- ${BISON_EXECUTABLE}")
56   endif(BISON_EXECUTABLE)
57 endif(NOT BISON_EXECUTABLE)
58
59 if(BISON_EXECUTABLE)
60   macro(BISON_FILE FILENAME)
61     get_filename_component(PATH "${FILENAME}" PATH)
62     if("${PATH}" STREQUAL "")
63       set(PATH_OPT "")
64     else("${PATH}" STREQUAL "")
65       set(PATH_OPT "/${PATH}")
66     endif("${PATH}" STREQUAL "")
67
68     get_filename_component(HEAD "${FILENAME}" NAME_WE)
69     if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
70       file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
71     endif(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}")
72
73     if(BISON_PREFIX_OUTPUTS)
74       set(PREFIX "${HEAD}")
75     else(BISON_PREFIX_OUTPUTS)
76       set(PREFIX "yy")
77     endif(BISON_PREFIX_OUTPUTS)
78
79     set(OUTFILE "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}/${HEAD}.tab.c")
80     if(BISON_GENERATE_DEFINES)
81       set(HEADER "${CMAKE_CURRENT_BINARY_DIR}${PATH_OPT}/${HEAD}.tab.h")
82       add_custom_command(
83         OUTPUT "${OUTFILE}" "${HEADER}"
84         COMMAND "${BISON_EXECUTABLE}"
85         ARGS "--name-prefix=${PREFIX}"
86         "--defines"
87         "--output-file=${OUTFILE}"
88         "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}"
89         DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
90       set_source_files_properties("${OUTFILE}" "${HEADER}"
91         PROPERTIES GENERATED TRUE)
92       set_source_files_properties("${HEADER}" PROPERTIES HEADER_FILE_ONLY TRUE)
93     else(BISON_GENERATE_DEFINES)
94       add_custom_command(
95         OUTPUT "${OUTFILE}"
96         COMMAND "${BISON_EXECUTABLE}"
97         ARGS "--name-prefix=${PREFIX}"
98         "--output-file=${OUTFILE}"
99         "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}"
100         DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
101       set_source_files_properties("${OUTFILE}" PROPERTIES GENERATED TRUE)
102     endif(BISON_GENERATE_DEFINES)
103   endmacro(BISON_FILE)
104 else(BISON_EXECUTABLE)
105   message(FATAL_ERROR "=========> Could NOT find Bison executable")
106 endif(BISON_EXECUTABLE)
Note: See TracBrowser for help on using the browser.