FindBISON.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # Copyright 2009 Kitware, Inc.
  37. # Copyright 2006 Tristan Carel
  38. #
  39. # Distributed under the OSI-approved BSD License (the "License");
  40. # see accompanying file Copyright.txt for details.
  41. #
  42. # This software is distributed WITHOUT ANY WARRANTY; without even the
  43. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  44. # See the License for more information.
  45. #=============================================================================
  46. # (To distribute this file outside of CMake, substitute the full
  47. # License text for the above reference.)
  48. find_program(BISON_EXECUTABLE NAMES bison win_bison DOC "path to the bison executable")
  49. mark_as_advanced(BISON_EXECUTABLE)
  50. if(BISON_EXECUTABLE)
  51. # the bison commands should be executed with the C locale, otherwise
  52. # the message (which are parsed) may be translated
  53. set(_Bison_SAVED_LC_ALL "$ENV{LC_ALL}")
  54. set(ENV{LC_ALL} C)
  55. execute_process(COMMAND ${BISON_EXECUTABLE} --version
  56. OUTPUT_VARIABLE BISON_version_output
  57. ERROR_VARIABLE BISON_version_error
  58. RESULT_VARIABLE BISON_version_result
  59. OUTPUT_STRIP_TRAILING_WHITESPACE)
  60. set(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
  61. if(NOT ${BISON_version_result} EQUAL 0)
  62. message(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
  63. else()
  64. # Bison++
  65. if("${BISON_version_output}" MATCHES "^bison\\+\\+")
  66. string(REGEX REPLACE "^bison\\+\\+ Version ([^,]+).*" "\\1"
  67. BISON_VERSION "${BISON_version_output}")
  68. # GNU Bison
  69. elseif("${BISON_version_output}" MATCHES "^bison[^+]")
  70. string(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
  71. BISON_VERSION "${BISON_version_output}")
  72. elseif("${BISON_version_output}" MATCHES "^GNU Bison ")
  73. string(REGEX REPLACE "^GNU Bison (version )?([^\n]+).*" "\\2"
  74. BISON_VERSION "${BISON_version_output}")
  75. endif()
  76. endif()
  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()
  95. # internal macro
  96. macro(BISON_TARGET_option_extraopts Options)
  97. set(BISON_TARGET_extraopts "${Options}")
  98. separate_arguments(BISON_TARGET_extraopts)
  99. list(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
  100. endmacro()
  101. #============================================================
  102. # BISON_TARGET (public macro)
  103. #============================================================
  104. #
  105. macro(BISON_TARGET Name BisonInput BisonOutput)
  106. set(BISON_TARGET_output_header "")
  107. set(BISON_TARGET_cmdopt "")
  108. set(BISON_TARGET_outputs "${BisonOutput}")
  109. if(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
  110. message(SEND_ERROR "Usage")
  111. else()
  112. # Parsing parameters
  113. if(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
  114. if("${ARGV3}" STREQUAL "VERBOSE")
  115. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
  116. endif()
  117. if("${ARGV3}" STREQUAL "COMPILE_FLAGS")
  118. BISON_TARGET_option_extraopts("${ARGV4}")
  119. endif()
  120. endif()
  121. if(${ARGC} EQUAL 7)
  122. if("${ARGV5}" STREQUAL "VERBOSE")
  123. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
  124. endif()
  125. if("${ARGV5}" STREQUAL "COMPILE_FLAGS")
  126. BISON_TARGET_option_extraopts("${ARGV6}")
  127. endif()
  128. endif()
  129. # Header's name generated by bison (see option -d)
  130. list(APPEND BISON_TARGET_cmdopt "-d")
  131. string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
  132. string(REPLACE "c" "h" _fileext ${_fileext})
  133. string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
  134. BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
  135. list(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
  136. add_custom_command(OUTPUT ${BISON_TARGET_outputs}
  137. ${BISON_TARGET_extraoutputs}
  138. COMMAND ${BISON_EXECUTABLE}
  139. ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
  140. DEPENDS ${ARGV1}
  141. COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
  142. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  143. # define target variables
  144. set(BISON_${Name}_DEFINED TRUE)
  145. set(BISON_${Name}_INPUT ${ARGV1})
  146. set(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
  147. set(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
  148. set(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
  149. endif()
  150. endmacro()
  151. #
  152. #============================================================
  153. endif()
  154. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  155. FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS BISON_EXECUTABLE
  156. VERSION_VAR BISON_VERSION)
  157. # FindBISON.cmake ends here