FindFLEX.cmake 7.0 KB

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