FindBISON.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # If bison is found, the module defines the macros:
  9. # BISON_TARGET(<Name> <YaccInput> <CodeOutput> [VERBOSE <file>]
  10. # [COMPILE_FLAGS <string>])
  11. # which will create a custom rule to generate a parser. <YaccInput> is
  12. # the path to a yacc file. <CodeOutput> is the name of the source file
  13. # generated by bison. A header file is also be generated, and contains
  14. # the token list. If COMPILE_FLAGS option is specified, the next
  15. # parameter is added in the bison command line. if VERBOSE option is
  16. # specified, <file> is created and contains verbose descriptions of the
  17. # grammar and parser. The macro defines a set of variables:
  18. # BISON_${Name}_DEFINED - true is the macro ran successfully
  19. # BISON_${Name}_INPUT - The input source file, an alias for <YaccInput>
  20. # BISON_${Name}_OUTPUT_SOURCE - The source file generated by bison
  21. # BISON_${Name}_OUTPUT_HEADER - The header file generated by bison
  22. # BISON_${Name}_OUTPUTS - The sources files generated by bison
  23. # BISON_${Name}_COMPILE_FLAGS - Options used in the bison command line
  24. #
  25. # ====================================================================
  26. # Example:
  27. #
  28. # find_package(BISON)
  29. # BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
  30. # add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS})
  31. # ====================================================================
  32. #=============================================================================
  33. # Copyright 2009 Kitware, Inc.
  34. # Copyright 2006 Tristan Carel
  35. #
  36. # Distributed under the OSI-approved BSD License (the "License");
  37. # see accompanying file Copyright.txt for details.
  38. #
  39. # This software is distributed WITHOUT ANY WARRANTY; without even the
  40. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  41. # See the License for more information.
  42. #=============================================================================
  43. # (To distributed this file outside of CMake, substitute the full
  44. # License text for the above reference.)
  45. FIND_PROGRAM(BISON_EXECUTABLE bison DOC "path to the bison executable")
  46. MARK_AS_ADVANCED(BISON_EXECUTABLE)
  47. IF(BISON_EXECUTABLE)
  48. EXECUTE_PROCESS(COMMAND ${BISON_EXECUTABLE} --version
  49. OUTPUT_VARIABLE BISON_version_output
  50. ERROR_VARIABLE BISON_version_error
  51. RESULT_VARIABLE BISON_version_result
  52. OUTPUT_STRIP_TRAILING_WHITESPACE)
  53. IF(NOT ${BISON_version_result} EQUAL 0)
  54. MESSAGE(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
  55. ELSE()
  56. STRING(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
  57. BISON_VERSION "${BISON_version_output}")
  58. ENDIF()
  59. # internal macro
  60. MACRO(BISON_TARGET_option_verbose Name BisonOutput filename)
  61. LIST(APPEND BISON_TARGET_cmdopt "--verbose")
  62. GET_FILENAME_COMPONENT(BISON_TARGET_output_path "${BisonOutput}" PATH)
  63. GET_FILENAME_COMPONENT(BISON_TARGET_output_name "${BisonOutput}" NAME_WE)
  64. ADD_CUSTOM_COMMAND(OUTPUT ${filename}
  65. COMMAND ${CMAKE_COMMAND}
  66. ARGS -E copy
  67. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
  68. "${filename}"
  69. DEPENDS
  70. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
  71. COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
  72. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  73. SET(BISON_${Name}_VERBOSE_FILE ${filename})
  74. LIST(APPEND BISON_TARGET_extraoutputs
  75. "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
  76. ENDMACRO(BISON_TARGET_option_verbose)
  77. # internal macro
  78. MACRO(BISON_TARGET_option_extraopts Options)
  79. SET(BISON_TARGET_extraopts "${Options}")
  80. SEPARATE_ARGUMENTS(BISON_TARGET_extraopts)
  81. LIST(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
  82. ENDMACRO(BISON_TARGET_option_extraopts)
  83. #============================================================
  84. # BISON_TARGET (public macro)
  85. #============================================================
  86. #
  87. MACRO(BISON_TARGET Name BisonInput BisonOutput)
  88. SET(BISON_TARGET_output_header "")
  89. SET(BISON_TARGET_command_opt "")
  90. SET(BISON_TARGET_outputs "${BisonOutput}")
  91. IF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
  92. MESSAGE(SEND_ERROR "Usage")
  93. ELSE()
  94. # Parsing parameters
  95. IF(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
  96. IF("${ARGV3}" STREQUAL "VERBOSE")
  97. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
  98. ENDIF()
  99. IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
  100. BISON_TARGET_option_extraopts("${ARGV4}")
  101. ENDIF()
  102. ENDIF()
  103. IF(${ARGC} EQUAL 7)
  104. IF("${ARGV5}" STREQUAL "VERBOSE")
  105. BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
  106. ENDIF()
  107. IF("${ARGV5}" STREQUAL "COMPILE_FLAGS")
  108. BISON_TARGET_option_extraopts("${ARGV6}")
  109. ENDIF()
  110. ENDIF()
  111. # Header's name generated by bison (see option -d)
  112. LIST(APPEND BISON_TARGET_cmdopt "-d")
  113. STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
  114. STRING(REPLACE "c" "h" _fileext ${_fileext})
  115. STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
  116. BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
  117. LIST(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
  118. ADD_CUSTOM_COMMAND(OUTPUT ${BISON_TARGET_outputs}
  119. ${BISON_TARGET_extraoutputs}
  120. COMMAND ${BISON_EXECUTABLE}
  121. ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
  122. DEPENDS ${ARGV1}
  123. COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
  124. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  125. # define target variables
  126. SET(BISON_${Name}_DEFINED TRUE)
  127. SET(BISON_${Name}_INPUT ${ARGV1})
  128. SET(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
  129. SET(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
  130. SET(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
  131. ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
  132. ENDMACRO(BISON_TARGET)
  133. #
  134. #============================================================
  135. ENDIF(BISON_EXECUTABLE)
  136. INCLUDE(FindPackageHandleStandardArgs)
  137. FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON DEFAULT_MSG BISON_EXECUTABLE)
  138. # FindBISON.cmake ends here