Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FindBISON.cmake
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 # The minimum required version of bison can be specified using the
9 # standard CMake syntax, e.g. find_package(BISON 2.1.3)
10 #
11 # If bison is found, the module defines the macros:
12 #  BISON_TARGET(<Name> <YaccInput> <CodeOutput> [VERBOSE <file>]
13 #              [COMPILE_FLAGS <string>])
14 # which will create  a custom rule to generate  a parser. <YaccInput> is
15 # the path to  a yacc file. <CodeOutput> is the name  of the source file
16 # generated by bison.  A header file is also  be generated, and contains
17 # the  token  list.  If  COMPILE_FLAGS  option is  specified,  the  next
18 # parameter is  added in the bison  command line.  if  VERBOSE option is
19 # specified, <file> is created  and contains verbose descriptions of the
20 # grammar and parser. The macro defines a set of variables:
21 #  BISON_${Name}_DEFINED - true is the macro ran successfully
22 #  BISON_${Name}_INPUT - The input source file, an alias for <YaccInput>
23 #  BISON_${Name}_OUTPUT_SOURCE - The source file generated by bison
24 #  BISON_${Name}_OUTPUT_HEADER - The header file generated by bison
25 #  BISON_${Name}_OUTPUTS - The sources files generated by bison
26 #  BISON_${Name}_COMPILE_FLAGS - Options used in the bison command line
27 #
28 #  ====================================================================
29 #  Example:
30 #
31 #   find_package(BISON)
32 #   BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
33 #   add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS})
34 #  ====================================================================
35
36 #=============================================================================
37 # Copyright 2009 Kitware, Inc.
38 # Copyright 2006 Tristan Carel
39 #
40 # Distributed under the OSI-approved BSD License (the "License");
41 # see accompanying file Copyright.txt for details.
42 #
43 # This software is distributed WITHOUT ANY WARRANTY; without even the
44 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
45 # See the License for more information.
46 #=============================================================================
47 # (To distribute this file outside of CMake, substitute the full
48 #  License text for the above reference.)
49
50 find_program(BISON_EXECUTABLE bison DOC "path to the bison executable")
51 mark_as_advanced(BISON_EXECUTABLE)
52
53 if(BISON_EXECUTABLE)
54   # the bison commands should be executed with the C locale, otherwise
55   # the message (which are parsed) may be translated
56   set(_Bison_SAVED_LC_ALL "$ENV{LC_ALL}")
57   set(ENV{LC_ALL} C)
58
59   execute_process(COMMAND ${BISON_EXECUTABLE} --version
60     OUTPUT_VARIABLE BISON_version_output
61     ERROR_VARIABLE BISON_version_error
62     RESULT_VARIABLE BISON_version_result
63     OUTPUT_STRIP_TRAILING_WHITESPACE)
64
65   set(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
66
67   if(NOT ${BISON_version_result} EQUAL 0)
68     message(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
69   else()
70     # Bison++
71     if("${BISON_version_output}" MATCHES "^bison\\+\\+")
72       string(REGEX REPLACE "^bison\\+\\+ Version ([^,]+).*" "\\1"
73         BISON_VERSION "${BISON_version_output}")
74     # GNU Bison
75     elseif("${BISON_version_output}" MATCHES "^bison[^+]")
76       string(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
77         BISON_VERSION "${BISON_version_output}")
78     elseif("${BISON_version_output}" MATCHES "^GNU Bison ")
79       string(REGEX REPLACE "^GNU Bison (version )?([^\n]+).*" "\\2"
80         BISON_VERSION "${BISON_version_output}")
81     endif()
82   endif()
83
84   # internal macro
85   macro(BISON_TARGET_option_verbose Name BisonOutput filename)
86     list(APPEND BISON_TARGET_cmdopt "--verbose")
87     get_filename_component(BISON_TARGET_output_path "${BisonOutput}" PATH)
88     get_filename_component(BISON_TARGET_output_name "${BisonOutput}" NAME_WE)
89     add_custom_command(OUTPUT ${filename}
90       COMMAND ${CMAKE_COMMAND}
91       ARGS -E copy
92       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
93       "${filename}"
94       DEPENDS
95       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
96       COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
97       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
98     set(BISON_${Name}_VERBOSE_FILE ${filename})
99     list(APPEND BISON_TARGET_extraoutputs
100       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
101   endmacro()
102
103   # internal macro
104   macro(BISON_TARGET_option_extraopts Options)
105     set(BISON_TARGET_extraopts "${Options}")
106     separate_arguments(BISON_TARGET_extraopts)
107     list(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
108   endmacro()
109
110   #============================================================
111   # BISON_TARGET (public macro)
112   #============================================================
113   #
114   macro(BISON_TARGET Name BisonInput BisonOutput)
115     set(BISON_TARGET_output_header "")
116     set(BISON_TARGET_cmdopt "")
117     set(BISON_TARGET_outputs "${BisonOutput}")
118     if(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
119       message(SEND_ERROR "Usage")
120     else()
121       # Parsing parameters
122       if(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
123         if("${ARGV3}" STREQUAL "VERBOSE")
124           BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
125         endif()
126         if("${ARGV3}" STREQUAL "COMPILE_FLAGS")
127           BISON_TARGET_option_extraopts("${ARGV4}")
128         endif()
129       endif()
130
131       if(${ARGC} EQUAL 7)
132         if("${ARGV5}" STREQUAL "VERBOSE")
133           BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
134         endif()
135
136         if("${ARGV5}" STREQUAL "COMPILE_FLAGS")
137           BISON_TARGET_option_extraopts("${ARGV6}")
138         endif()
139       endif()
140
141       # Header's name generated by bison (see option -d)
142       list(APPEND BISON_TARGET_cmdopt "-d")
143       string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
144       string(REPLACE "c" "h" _fileext ${_fileext})
145       string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
146           BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
147       list(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
148
149       add_custom_command(OUTPUT ${BISON_TARGET_outputs}
150         ${BISON_TARGET_extraoutputs}
151         COMMAND ${BISON_EXECUTABLE}
152         ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
153         DEPENDS ${ARGV1}
154         COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
155         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
156
157       # define target variables
158       set(BISON_${Name}_DEFINED TRUE)
159       set(BISON_${Name}_INPUT ${ARGV1})
160       set(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
161       set(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
162       set(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
163
164     endif()
165   endmacro()
166   #
167   #============================================================
168
169 endif()
170
171 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
172 FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS  BISON_EXECUTABLE
173                                         VERSION_VAR BISON_VERSION)
174
175 # FindBISON.cmake ends here