[Release] Webkit2-efl-123997_0.11.46
[framework/web/webkit-efl.git] / Source / cmake / FindFLEX.cmake
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
47 #=============================================================================
48 # Copyright 2009 Kitware, Inc.
49 # Copyright 2006 Tristan Carel
50 #
51 # Distributed under the OSI-approved BSD License (the "License");
52 # see accompanying file Copyright.txt for details.
53 #
54 # This software is distributed WITHOUT ANY WARRANTY; without even the
55 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
56 # See the License for more information.
57 #=============================================================================
58 # (To distributed this file outside of CMake, substitute the full
59 #  License text for the above reference.)
60
61 FIND_PROGRAM(FLEX_EXECUTABLE flex DOC "path to the flex executable")
62 MARK_AS_ADVANCED(FLEX_EXECUTABLE)
63
64 FIND_LIBRARY(FL_LIBRARY NAMES fl
65   DOC "path to the fl library")
66 MARK_AS_ADVANCED(FL_LIBRARY)
67 SET(FLEX_LIBRARIES ${FL_LIBRARY})
68
69 IF(FLEX_EXECUTABLE)
70
71   EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version
72     OUTPUT_VARIABLE FLEX_version_output
73     ERROR_VARIABLE FLEX_version_error
74     RESULT_VARIABLE FLEX_version_result
75     OUTPUT_STRIP_TRAILING_WHITESPACE)
76   IF(NOT ${FLEX_version_result} EQUAL 0)
77     MESSAGE(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_error}")
78   ELSE()
79     STRING(REGEX REPLACE "^flex (.*)$" "\\1"
80       FLEX_VERSION "${FLEX_version_output}")
81   ENDIF()
82
83   #============================================================
84   # FLEX_TARGET (public macro)
85   #============================================================
86   #
87   MACRO(FLEX_TARGET Name Input Output)
88     SET(FLEX_TARGET_usage "FLEX_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
89     IF(${ARGC} GREATER 3)
90       IF(${ARGC} EQUAL 5)
91         IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
92           SET(FLEX_EXECUTABLE_opts  "${ARGV4}")
93           SEPARATE_ARGUMENTS(FLEX_EXECUTABLE_opts)
94         ELSE()
95           MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
96         ENDIF()
97       ELSE()
98         MESSAGE(SEND_ERROR ${FLEX_TARGET_usage})
99       ENDIF()
100     ENDIF()
101
102     ADD_CUSTOM_COMMAND(OUTPUT ${Output}
103       COMMAND ${FLEX_EXECUTABLE}
104       ARGS ${FLEX_EXECUTABLE_opts} -o${Output} ${Input}
105       DEPENDS ${Input}
106       COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}"
107       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
108
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
117   #============================================================
118   # ADD_FLEX_BISON_DEPENDENCY (public macro)
119   #============================================================
120   #
121   MACRO(ADD_FLEX_BISON_DEPENDENCY FlexTarget BisonTarget)
122
123     IF(NOT FLEX_${FlexTarget}_OUTPUTS)
124       MESSAGE(SEND_ERROR "Flex target `${FlexTarget}' does not exists.")
125     ENDIF()
126
127     IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
128       MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
129     ENDIF()
130
131     SET_SOURCE_FILES_PROPERTIES(${FLEX_${FlexTarget}_OUTPUTS}
132       PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
133   ENDMACRO(ADD_FLEX_BISON_DEPENDENCY)
134   #============================================================
135
136 ENDIF(FLEX_EXECUTABLE)
137
138 INCLUDE(FindPackageHandleStandardArgs)
139 FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX DEFAULT_MSG FLEX_EXECUTABLE)
140
141 # FindFLEX.cmake ends here