FindBISON.cmake 9.4 KB

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