FindBISON.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 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. STRING(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
  65. BISON_VERSION "${BISON_version_output}")
  66. ENDIF()
  67. # internal macro
  68. MACRO(BISON_TARGET_option_verbose Name BisonOutput filename)
  69. LIST(APPEND BISON_TARGET_cmdopt "--verbose")
  70. GET_FILENAME_COMPONENT(BISON_TARGET_output_path "${BisonOutput}" PATH)
  71. GET_FILENAME_COMPONENT(BISON_TARGET_output_name "${BisonOutput}" NAME_WE)
  72. ADD_CUSTOM_COMMAND(OUTPUT ${filename}
  73. COMMAND ${CMAKE_COMMAND}
  74. ARGS -E copy
  75. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
  76. "${filename}"
  77. DEPENDS
  78. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
  79. COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
  80. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  81. SET(BISON_${Name}_VERBOSE_FILE ${filename})
  82. LIST(APPEND BISON_TARGET_extraoutputs
  83. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
  84. ENDMACRO(BISON_TARGET_option_verbose)
  85. # internal macro
  86. MACRO(BISON_TARGET_option_extraopts Options)
  87. SET(BISON_TARGET_extraopts "${Options}")
  88. SEPARATE_ARGUMENTS(BISON_TARGET_extraopts)
  89. LIST(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
  90. ENDMACRO(BISON_TARGET_option_extraopts)
  91. #============================================================
  92. # BISON_TARGET (public macro)
  93. #============================================================
  94. #
  95. MACRO(BISON_TARGET Name BisonInput BisonOutput)
  96. SET(BISON_TARGET_output_header "")
  97. SET(BISON_TARGET_cmdopt "")
  98. SET(BISON_TARGET_outputs "${BisonOutput}")
  99. IF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
  100. MESSAGE(SEND_ERROR "Usage")
  101. ELSE()
  102. # Parsing parameters
  103. IF(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
  104. IF("${ARGV3}" STREQUAL "VERBOSE")
  105. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
  106. ENDIF()
  107. IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
  108. BISON_TARGET_option_extraopts("${ARGV4}")
  109. ENDIF()
  110. ENDIF()
  111. IF(${ARGC} EQUAL 7)
  112. IF("${ARGV5}" STREQUAL "VERBOSE")
  113. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
  114. ENDIF()
  115. IF("${ARGV5}" STREQUAL "COMPILE_FLAGS")
  116. BISON_TARGET_option_extraopts("${ARGV6}")
  117. ENDIF()
  118. ENDIF()
  119. # Header's name generated by bison (see option -d)
  120. LIST(APPEND BISON_TARGET_cmdopt "-d")
  121. STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
  122. STRING(REPLACE "c" "h" _fileext ${_fileext})
  123. STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
  124. BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
  125. LIST(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
  126. ADD_CUSTOM_COMMAND(OUTPUT ${BISON_TARGET_outputs}
  127. ${BISON_TARGET_extraoutputs}
  128. COMMAND ${BISON_EXECUTABLE}
  129. ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
  130. DEPENDS ${ARGV1}
  131. COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
  132. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  133. # define target variables
  134. SET(BISON_${Name}_DEFINED TRUE)
  135. SET(BISON_${Name}_INPUT ${ARGV1})
  136. SET(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
  137. SET(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
  138. SET(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
  139. ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
  140. ENDMACRO(BISON_TARGET)
  141. #
  142. #============================================================
  143. ENDIF(BISON_EXECUTABLE)
  144. INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  145. FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS BISON_EXECUTABLE
  146. VERSION_VAR BISON_VERSION)
  147. # FindBISON.cmake ends here