FindFLEX.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # - Find flex executable and provides a macro to generate custom build rules
  2. #
  3. # The module defines the following variables:
  4. # FLEX_FOUND - true is flex executable is found
  5. # FLEX_EXECUTABLE - the path to the flex executable
  6. # FLEX_VERSION - the version of flex
  7. # FLEX_LIBRARIES - The flex libraries
  8. #
  9. # The minimum required version of flex can be specified using the
  10. # standard syntax, e.g. FIND_PACKAGE(FLEX 2.5.13)
  11. #
  12. #
  13. # If flex is found on the system, the module provides the macro:
  14. # FLEX_TARGET(Name FlexInput FlexOutput [COMPILE_FLAGS <string>])
  15. # which creates a custom command to generate the <FlexOutput> file from
  16. # the <FlexInput> file. If COMPILE_FLAGS option is specified, the next
  17. # parameter is added to the flex command line. Name is an alias used to
  18. # get details of this custom command. Indeed the macro defines the
  19. # following variables:
  20. # FLEX_${Name}_DEFINED - true is the macro ran successfully
  21. # FLEX_${Name}_OUTPUTS - the source file generated by the custom rule, an
  22. # alias for FlexOutput
  23. # FLEX_${Name}_INPUT - the flex source file, an alias for ${FlexInput}
  24. #
  25. # Flex scanners oftenly use tokens defined by Bison: the code generated
  26. # by Flex depends of the header generated by Bison. This module also
  27. # defines a macro:
  28. # ADD_FLEX_BISON_DEPENDENCY(FlexTarget BisonTarget)
  29. # which adds the required dependency between a scanner and a parser
  30. # where <FlexTarget> and <BisonTarget> are the first parameters of
  31. # respectively FLEX_TARGET and BISON_TARGET macros.
  32. #
  33. # ====================================================================
  34. # Example:
  35. #
  36. # find_package(BISON)
  37. # find_package(FLEX)
  38. #
  39. # BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
  40. # FLEX_TARGET(MyScanner lexer.l ${CMAKE_CURRENT_BIANRY_DIR}/lexer.cpp)
  41. # ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)
  42. #
  43. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  44. # add_executable(Foo
  45. # Foo.cc
  46. # ${BISON_MyParser_OUTPUTS}
  47. # ${FLEX_MyScanner_OUTPUTS}
  48. # )
  49. # ====================================================================
  50. #=============================================================================
  51. # Copyright 2009 Kitware, Inc.
  52. # Copyright 2006 Tristan Carel
  53. #
  54. # Distributed under the OSI-approved BSD License (the "License");
  55. # see accompanying file Copyright.txt for details.
  56. #
  57. # This software is distributed WITHOUT ANY WARRANTY; without even the
  58. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  59. # See the License for more information.
  60. #=============================================================================
  61. # (To distribute this file outside of CMake, substitute the full
  62. # License text for the above reference.)
  63. FIND_PROGRAM(FLEX_EXECUTABLE 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. MARK_AS_ADVANCED(FL_LIBRARY)
  68. SET(FLEX_LIBRARIES ${FL_LIBRARY})
  69. IF(FLEX_EXECUTABLE)
  70. EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version
  71. OUTPUT_VARIABLE FLEX_version_output
  72. ERROR_VARIABLE FLEX_version_error
  73. RESULT_VARIABLE FLEX_version_result
  74. OUTPUT_STRIP_TRAILING_WHITESPACE)
  75. IF(NOT ${FLEX_version_result} EQUAL 0)
  76. IF(FLEX_FIND_REQUIRED)
  77. MESSAGE(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}")
  78. ELSE()
  79. MESSAGE("Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}\nFLEX_VERSION will not be available")
  80. ENDIF()
  81. ELSE()
  82. STRING(REGEX REPLACE "^flex (.*)$" "\\1"
  83. FLEX_VERSION "${FLEX_version_output}")
  84. ENDIF()
  85. #============================================================
  86. # FLEX_TARGET (public macro)
  87. #============================================================
  88. #
  89. MACRO(FLEX_TARGET Name Input Output)
  90. SET(FLEX_TARGET_usage "FLEX_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
  91. IF(${ARGC} GREATER 3)
  92. IF(${ARGC} EQUAL 5)
  93. IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
  94. SET(FLEX_EXECUTABLE_opts "${ARGV4}")
  95. SEPARATE_ARGUMENTS(FLEX_EXECUTABLE_opts)
  96. ELSE()
  97. MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
  98. ENDIF()
  99. ELSE()
  100. MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
  101. ENDIF()
  102. ENDIF()
  103. ADD_CUSTOM_COMMAND(OUTPUT ${Output}
  104. COMMAND ${FLEX_EXECUTABLE}
  105. ARGS ${FLEX_EXECUTABLE_opts} -o${Output} ${Input}
  106. DEPENDS ${Input}
  107. COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}"
  108. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  109. SET(FLEX_${Name}_DEFINED TRUE)
  110. SET(FLEX_${Name}_OUTPUTS ${Output})
  111. SET(FLEX_${Name}_INPUT ${Input})
  112. SET(FLEX_${Name}_COMPILE_FLAGS ${FLEX_EXECUTABLE_opts})
  113. ENDMACRO(FLEX_TARGET)
  114. #============================================================
  115. #============================================================
  116. # ADD_FLEX_BISON_DEPENDENCY (public macro)
  117. #============================================================
  118. #
  119. MACRO(ADD_FLEX_BISON_DEPENDENCY FlexTarget BisonTarget)
  120. IF(NOT FLEX_${FlexTarget}_OUTPUTS)
  121. MESSAGE(SEND_ERROR "Flex target `${FlexTarget}' does not exists.")
  122. ENDIF()
  123. IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
  124. MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
  125. ENDIF()
  126. SET_SOURCE_FILES_PROPERTIES(${FLEX_${FlexTarget}_OUTPUTS}
  127. PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
  128. ENDMACRO(ADD_FLEX_BISON_DEPENDENCY)
  129. #============================================================
  130. ENDIF(FLEX_EXECUTABLE)
  131. INCLUDE(FindPackageHandleStandardArgs)
  132. FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX REQUIRED_VARS FLEX_EXECUTABLE
  133. VERSION_VAR FLEX_VERSION)
  134. # FindFLEX.cmake ends here