FindFLEX.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #.rst:
  2. # FindFLEX
  3. # --------
  4. #
  5. # Find flex executable and provides a macro to generate custom build rules
  6. #
  7. #
  8. #
  9. # The module defines the following variables:
  10. #
  11. # ::
  12. #
  13. # FLEX_FOUND - true is flex executable is found
  14. # FLEX_EXECUTABLE - the path to the flex executable
  15. # FLEX_VERSION - the version of flex
  16. # FLEX_LIBRARIES - The flex libraries
  17. # FLEX_INCLUDE_DIRS - The path to the flex headers
  18. #
  19. #
  20. #
  21. # The minimum required version of flex can be specified using the
  22. # standard syntax, e.g. find_package(FLEX 2.5.13)
  23. #
  24. #
  25. #
  26. # If flex is found on the system, the module provides the macro:
  27. #
  28. # ::
  29. #
  30. # FLEX_TARGET(Name FlexInput FlexOutput
  31. # [COMPILE_FLAGS <string>]
  32. # [DEFINES_FILE <string>]
  33. # )
  34. #
  35. # which creates a custom command to generate the <FlexOutput> file from
  36. # the <FlexInput> file. If COMPILE_FLAGS option is specified, the next
  37. # parameter is added to the flex command line. If flex is configured to
  38. # output a header file, the DEFINES_FILE option may be used to specify its
  39. # name. Name is an alias used to get details of this custom command.
  40. # Indeed the macro defines the following variables:
  41. #
  42. # ::
  43. #
  44. # FLEX_${Name}_DEFINED - true is the macro ran successfully
  45. # FLEX_${Name}_OUTPUTS - the source file generated by the custom rule, an
  46. # alias for FlexOutput
  47. # FLEX_${Name}_INPUT - the flex source file, an alias for ${FlexInput}
  48. # FLEX_${Name}_OUTPUT_HEADER - the header flex output, if any.
  49. #
  50. #
  51. #
  52. # Flex scanners oftenly use tokens defined by Bison: the code generated
  53. # by Flex depends of the header generated by Bison. This module also
  54. # defines a macro:
  55. #
  56. # ::
  57. #
  58. # ADD_FLEX_BISON_DEPENDENCY(FlexTarget BisonTarget)
  59. #
  60. # which adds the required dependency between a scanner and a parser
  61. # where <FlexTarget> and <BisonTarget> are the first parameters of
  62. # respectively FLEX_TARGET and BISON_TARGET macros.
  63. #
  64. # ::
  65. #
  66. # ====================================================================
  67. # Example:
  68. #
  69. #
  70. #
  71. # ::
  72. #
  73. # find_package(BISON)
  74. # find_package(FLEX)
  75. #
  76. #
  77. #
  78. # ::
  79. #
  80. # BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
  81. # FLEX_TARGET(MyScanner lexer.l ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp)
  82. # ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)
  83. #
  84. #
  85. #
  86. # ::
  87. #
  88. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  89. # add_executable(Foo
  90. # Foo.cc
  91. # ${BISON_MyParser_OUTPUTS}
  92. # ${FLEX_MyScanner_OUTPUTS}
  93. # )
  94. # ====================================================================
  95. #=============================================================================
  96. # Copyright 2009 Kitware, Inc.
  97. # Copyright 2006 Tristan Carel
  98. #
  99. # Distributed under the OSI-approved BSD License (the "License");
  100. # see accompanying file Copyright.txt for details.
  101. #
  102. # This software is distributed WITHOUT ANY WARRANTY; without even the
  103. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  104. # See the License for more information.
  105. #=============================================================================
  106. # (To distribute this file outside of CMake, substitute the full
  107. # License text for the above reference.)
  108. find_program(FLEX_EXECUTABLE NAMES flex win_flex DOC "path to the flex executable")
  109. mark_as_advanced(FLEX_EXECUTABLE)
  110. find_library(FL_LIBRARY NAMES fl
  111. DOC "Path to the fl library")
  112. find_path(FLEX_INCLUDE_DIR FlexLexer.h
  113. DOC "Path to the flex headers")
  114. mark_as_advanced(FL_LIBRARY FLEX_INCLUDE_DIR)
  115. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  116. set(FLEX_INCLUDE_DIRS ${FLEX_INCLUDE_DIR})
  117. set(FLEX_LIBRARIES ${FL_LIBRARY})
  118. if(FLEX_EXECUTABLE)
  119. execute_process(COMMAND ${FLEX_EXECUTABLE} --version
  120. OUTPUT_VARIABLE FLEX_version_output
  121. ERROR_VARIABLE FLEX_version_error
  122. RESULT_VARIABLE FLEX_version_result
  123. OUTPUT_STRIP_TRAILING_WHITESPACE)
  124. if(NOT ${FLEX_version_result} EQUAL 0)
  125. if(FLEX_FIND_REQUIRED)
  126. message(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}")
  127. else()
  128. message("Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}\nFLEX_VERSION will not be available")
  129. endif()
  130. else()
  131. # older versions of flex printed "/full/path/to/executable version X.Y"
  132. # newer versions use "basename(executable) X.Y"
  133. get_filename_component(FLEX_EXE_NAME_WE "${FLEX_EXECUTABLE}" NAME_WE)
  134. get_filename_component(FLEX_EXE_EXT "${FLEX_EXECUTABLE}" EXT)
  135. string(REGEX REPLACE "^.*${FLEX_EXE_NAME_WE}(${FLEX_EXE_EXT})?\"? (version )?([0-9]+[^ ]*)( .*)?$" "\\3"
  136. FLEX_VERSION "${FLEX_version_output}")
  137. unset(FLEX_EXE_EXT)
  138. unset(FLEX_EXE_NAME_WE)
  139. endif()
  140. #============================================================
  141. # FLEX_TARGET (public macro)
  142. #============================================================
  143. #
  144. macro(FLEX_TARGET Name Input Output)
  145. set(FLEX_TARGET_outputs "${Output}")
  146. set(FLEX_EXECUTABLE_opts "")
  147. set(FLEX_TARGET_PARAM_OPTIONS)
  148. set(FLEX_TARGET_PARAM_ONE_VALUE_KEYWORDS
  149. COMPILE_FLAGS
  150. DEFINES_FILE
  151. )
  152. set(FLEX_TARGET_PARAM_MULTI_VALUE_KEYWORDS)
  153. cmake_parse_arguments(
  154. FLEX_TARGET_ARG
  155. "${FLEX_TARGET_PARAM_OPTIONS}"
  156. "${FLEX_TARGET_PARAM_ONE_VALUE_KEYWORDS}"
  157. "${FLEX_TARGET_MULTI_VALUE_KEYWORDS}"
  158. ${ARGN}
  159. )
  160. set(FLEX_TARGET_usage "FLEX_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>] [DEFINES_FILE <string>]")
  161. if(NOT "${FLEX_TARGET_ARG_UNPARSED_ARGUMENTS}" STREQUAL "")
  162. message(SEND_ERROR ${FLEX_TARGET_usage})
  163. else()
  164. if(NOT "${FLEX_TARGET_ARG_COMPILE_FLAGS}" STREQUAL "")
  165. set(FLEX_EXECUTABLE_opts "${FLEX_TARGET_ARG_COMPILE_FLAGS}")
  166. separate_arguments(FLEX_EXECUTABLE_opts)
  167. endif()
  168. if(NOT "${FLEX_TARGET_ARG_DEFINES_FILE}" STREQUAL "")
  169. list(APPEND FLEX_TARGET_outputs "${FLEX_TARGET_ARG_DEFINES_FILE}")
  170. list(APPEND FLEX_EXECUTABLE_opts --header-file=${FLEX_TARGET_ARG_DEFINES_FILE})
  171. endif()
  172. add_custom_command(OUTPUT ${FLEX_TARGET_outputs}
  173. COMMAND ${FLEX_EXECUTABLE} ${FLEX_EXECUTABLE_opts} -o${Output} ${Input}
  174. VERBATIM
  175. DEPENDS ${Input}
  176. COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}"
  177. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  178. set(FLEX_${Name}_DEFINED TRUE)
  179. set(FLEX_${Name}_OUTPUTS ${Output})
  180. set(FLEX_${Name}_INPUT ${Input})
  181. set(FLEX_${Name}_COMPILE_FLAGS ${FLEX_EXECUTABLE_opts})
  182. if("${FLEX_TARGET_ARG_DEFINES_FILE}" STREQUAL "")
  183. set(FLEX_${Name}_OUTPUT_HEADER "")
  184. else()
  185. set(FLEX_${Name}_OUTPUT_HEADER ${FLEX_TARGET_ARG_DEFINES_FILE})
  186. endif()
  187. endif()
  188. endmacro()
  189. #============================================================
  190. #============================================================
  191. # ADD_FLEX_BISON_DEPENDENCY (public macro)
  192. #============================================================
  193. #
  194. macro(ADD_FLEX_BISON_DEPENDENCY FlexTarget BisonTarget)
  195. if(NOT FLEX_${FlexTarget}_OUTPUTS)
  196. message(SEND_ERROR "Flex target `${FlexTarget}' does not exist.")
  197. endif()
  198. if(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
  199. message(SEND_ERROR "Bison target `${BisonTarget}' does not exist.")
  200. endif()
  201. set_source_files_properties(${FLEX_${FlexTarget}_OUTPUTS}
  202. PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
  203. endmacro()
  204. #============================================================
  205. endif()
  206. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  207. FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX REQUIRED_VARS FLEX_EXECUTABLE
  208. VERSION_VAR FLEX_VERSION)
  209. # FindFLEX.cmake ends here