FindBISON.cmake 7.9 KB

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