root/cmake/FindBISON.cmake

Revision 214fc9d7a7c8ed9ee8fd8b8c51b19287374157e8, 7.3 kB (checked in by Philip Herron <redbrain@…>, 2 years ago)

basic cmake support

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