FindBISON.cmake 6.6 KB

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