FindBISON.cmake 7.0 KB

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