1 # - Find flex executable and provides a macro to generate custom build rules
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
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}
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
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.
29 #====================================================================
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)
39 # include_directories(${CMAKE_CURRENT_BINARY_DIR})
42 # ${BISON_MyParser_OUTPUTS}
43 # ${FLEX_MyScanner_OUTPUTS}
45 #====================================================================
47 # Copyright (c) 2006, Tristan Carel
48 # All rights reserved.
49 # Redistribution and use in source and binary forms, with or without
50 # modification, are permitted provided that the following conditions are met:
52 # * Redistributions of source code must retain the above copyright
53 # notice, this list of conditions and the following disclaimer.
54 # * Redistributions in binary form must reproduce the above copyright
55 # notice, this list of conditions and the following disclaimer in the
56 # documentation and/or other materials provided with the distribution.
57 # * Neither the name of the University of California, Berkeley nor the
58 # names of its contributors may be used to endorse or promote products
59 # derived from this software without specific prior written permission.
61 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
62 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
65 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
70 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 # $Id: FindFLEX.cmake,v 1.1 2009-08-13 04:11:23 lowman Exp $
74 FIND_PROGRAM(FLEX_EXECUTABLE flex DOC "path to the flex executable")
75 MARK_AS_ADVANCED(FLEX_EXECUTABLE)
77 FIND_LIBRARY(FL_LIBRARY NAMES fl
78 DOC "path to the fl library")
79 MARK_AS_ADVANCED(FL_LIBRARY)
80 SET(FLEX_LIBRARIES ${FL_LIBRARY})
84 EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version
85 OUTPUT_VARIABLE FLEX_version_output
86 ERROR_VARIABLE FLEX_version_error
87 RESULT_VARIABLE FLEX_version_result
88 OUTPUT_STRIP_TRAILING_WHITESPACE)
89 IF(NOT ${FLEX_version_result} EQUAL 0)
90 MESSAGE(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_error}")
92 STRING(REGEX REPLACE "^flex (.*)$" "\\1"
93 FLEX_VERSION "${FLEX_version_output}")
96 #============================================================
97 # FLEX_TARGET (public macro)
98 #============================================================
100 MACRO(FLEX_TARGET Name Input Output)
101 SET(FLEX_TARGET_usage "FLEX_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
102 IF(${ARGC} GREATER 3)
104 IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
105 SET(FLEX_EXECUTABLE_opts "${ARGV4}")
106 SEPARATE_ARGUMENTS(FLEX_EXECUTABLE_opts)
108 MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
111 MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
115 ADD_CUSTOM_COMMAND(OUTPUT ${Output}
116 COMMAND ${FLEX_EXECUTABLE}
117 ARGS ${FLEX_EXECUTABLE_opts} -o${Output} ${Input}
119 COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}"
120 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
122 SET(FLEX_${Name}_DEFINED TRUE)
123 SET(FLEX_${Name}_OUTPUTS ${Output})
124 SET(FLEX_${Name}_INPUT ${Input})
125 SET(FLEX_${Name}_COMPILE_FLAGS ${FLEX_EXECUTABLE_opts})
126 ENDMACRO(FLEX_TARGET)
127 #============================================================
130 #============================================================
131 # ADD_FLEX_BISON_DEPENDENCY (public macro)
132 #============================================================
134 MACRO(ADD_FLEX_BISON_DEPENDENCY FlexTarget BisonTarget)
136 IF(NOT FLEX_${FlexTarget}_OUTPUTS)
137 MESSAGE(SEND_ERROR "Flex target `${FlexTarget}' does not exists.")
140 IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
141 MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
144 SET_SOURCE_FILES_PROPERTIES(${FLEX_${FlexTarget}_OUTPUTS}
145 PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
146 ENDMACRO(ADD_FLEX_BISON_DEPENDENCY)
147 #============================================================
149 ENDIF(FLEX_EXECUTABLE)
151 INCLUDE(FindPackageHandleStandardArgs)
152 FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX DEFAULT_MSG FLEX_EXECUTABLE)
154 # FindFLEX.cmake ends here