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