FindFLEX.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # If flex is found on the system, the module provides the macro:
  10. # FLEX_TARGET(Name FlexInput FlexOutput [COMPILE_FLAGS <string>])
  11. # which creates a custom command to generate the <FlexOutput> file from
  12. # the <FlexInput> file. If COMPILE_FLAGS option is specified, the next
  13. # parameter is added to the flex command line. Name is an alias used to
  14. # get details of this custom command. Indeed the macro defines the
  15. # following variables:
  16. # FLEX_${Name}_DEFINED - true is the macro ran successfully
  17. # FLEX_${Name}_OUTPUTS - the source file generated by the custom rule, an
  18. # alias for FlexOutput
  19. # FLEX_${Name}_INPUT - the flex source file, an alias for ${FlexInput}
  20. #
  21. # Flex scanners oftenly use tokens defined by Bison: the code generated
  22. # by Flex depends of the header generated by Bison. This module also
  23. # defines a macro:
  24. # ADD_FLEX_BISON_DEPENDENCY(FlexTarget BisonTarget)
  25. # which adds the required dependency between a scanner and a parser
  26. # where <FlexTarget> and <BisonTarget> are the first parameters of
  27. # respectively FLEX_TARGET and BISON_TARGET macros.
  28. #
  29. #====================================================================
  30. # Example:
  31. #
  32. # find_package(BISON)
  33. # find_package(FLEX)
  34. #
  35. # BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
  36. # FLEX_TARGET(MyScanner lexer.l ${CMAKE_CURRENT_BIANRY_DIR}/lexer.cpp)
  37. # ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)
  38. #
  39. # include_directories(${CMAKE_CURRENT_BINARY_DIR})
  40. # add_executable(Foo
  41. # Foo.cc
  42. # ${BISON_MyParser_OUTPUTS}
  43. # ${FLEX_MyScanner_OUTPUTS}
  44. # )
  45. #====================================================================
  46. # Copyright (c) 2006, Tristan Carel
  47. # All rights reserved.
  48. # Redistribution and use in source and binary forms, with or without
  49. # modification, are permitted provided that the following conditions are met:
  50. #
  51. # * Redistributions of source code must retain the above copyright
  52. # notice, this list of conditions and the following disclaimer.
  53. # * Redistributions in binary form must reproduce the above copyright
  54. # notice, this list of conditions and the following disclaimer in the
  55. # documentation and/or other materials provided with the distribution.
  56. # * Neither the name of the University of California, Berkeley nor the
  57. # names of its contributors may be used to endorse or promote products
  58. # derived from this software without specific prior written permission.
  59. #
  60. # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  61. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  62. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  63. # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
  64. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  66. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  67. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  68. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  69. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  70. # $Id$
  71. FIND_PROGRAM(FLEX_EXECUTABLE flex DOC "path to the flex executable")
  72. MARK_AS_ADVANCED(FLEX_EXECUTABLE)
  73. FIND_LIBRARY(FL_LIBRARY NAMES fl
  74. DOC "path to the fl library")
  75. MARK_AS_ADVANCED(FL_LIBRARY)
  76. SET(FLEX_LIBRARIES ${FL_LIBRARY})
  77. IF(FLEX_EXECUTABLE)
  78. EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version
  79. OUTPUT_VARIABLE FLEX_version_output
  80. ERROR_VARIABLE FLEX_version_error
  81. RESULT_VARIABLE FLEX_version_result
  82. OUTPUT_STRIP_TRAILING_WHITESPACE)
  83. IF(NOT ${FLEX_version_result} EQUAL 0)
  84. MESSAGE(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_error}")
  85. ELSE()
  86. STRING(REGEX REPLACE "^flex (.*)$" "\\1"
  87. FLEX_VERSION "${FLEX_version_output}")
  88. ENDIF()
  89. #============================================================
  90. # FLEX_TARGET (public macro)
  91. #============================================================
  92. #
  93. MACRO(FLEX_TARGET Name Input Output)
  94. SET(FLEX_TARGET_usage "FLEX_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
  95. IF(${ARGC} GREATER 3)
  96. IF(${ARGC} EQUAL 5)
  97. IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
  98. SET(FLEX_EXECUTABLE_opts "${ARGV4}")
  99. SEPARATE_ARGUMENTS(FLEX_EXECUTABLE_opts)
  100. ELSE()
  101. MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
  102. ENDIF()
  103. ELSE()
  104. MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
  105. ENDIF()
  106. ENDIF()
  107. ADD_CUSTOM_COMMAND(OUTPUT ${Output}
  108. COMMAND ${FLEX_EXECUTABLE}
  109. ARGS ${FLEX_EXECUTABLE_opts} -o${Output} ${Input}
  110. DEPENDS ${Input}
  111. COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}"
  112. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  113. SET(FLEX_${Name}_DEFINED TRUE)
  114. SET(FLEX_${Name}_OUTPUTS ${Output})
  115. SET(FLEX_${Name}_INPUT ${Input})
  116. SET(FLEX_${Name}_COMPILE_FLAGS ${FLEX_EXECUTABLE_opts})
  117. ENDMACRO(FLEX_TARGET)
  118. #============================================================
  119. #============================================================
  120. # ADD_FLEX_BISON_DEPENDENCY (public macro)
  121. #============================================================
  122. #
  123. MACRO(ADD_FLEX_BISON_DEPENDENCY FlexTarget BisonTarget)
  124. IF(NOT FLEX_${FlexTarget}_OUTPUTS)
  125. MESSAGE(SEND_ERROR "Flex target `${FlexTarget}' does not exists.")
  126. ENDIF()
  127. IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
  128. MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
  129. ENDIF()
  130. SET_SOURCE_FILES_PROPERTIES(${FLEX_${FlexTarget}_OUTPUTS}
  131. PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
  132. ENDMACRO(ADD_FLEX_BISON_DEPENDENCY)
  133. #============================================================
  134. ENDIF(FLEX_EXECUTABLE)
  135. INCLUDE(FindPackageHandleStandardArgs)
  136. FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX DEFAULT_MSG FLEX_EXECUTABLE)
  137. # FindFLEX.cmake ends here