FindFLEX.cmake 7.1 KB

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