FindFLEX — CMake 4.0.0 Documentation

# FindFLEX

Find Fast Lexical Analyzer (Flex) executable and provide a macro to generate custom build rules.

The module defines the following variables:

`FLEX_FOUND`

  True if `flex` executable is found.

`FLEX_EXECUTABLE`

  The path to the `flex` executable.

`FLEX_VERSION`

  The version of `flex`.

`FLEX_LIBRARIES`

  The `flex` libraries.

`FLEX_INCLUDE_DIRS`

  The path to the `flex` headers.

The minimum required version of `flex` can be specified using the standard CMake syntax, e.g. `find_package(FLEX 2.5.13)`.

If `flex` is found on the system, the module defines the macro:

flex_target

      flex_target(<Name> <FlexInput> <FlexOutput>
                  [OPTIONS <options>...]
                  [COMPILE_FLAGS <string>]
                  [DEFINES_FILE <string>]
                  )

which creates a custom command to generate the `<FlexOutput>` file from the `<FlexInput>` file. `<Name>` is an alias used to get details of this custom command.

The options are:

`OPTIONS <options>...`

  Added in version 4.0.

  A semicolon-separated list of flex options added to the `flex` command line.

`COMPILE_FLAGS <string>`

  Deprecated since version 4.0.

  Space-separated flex options added to the `flex` command line. A ;-list will not work. This option is deprecated in favor of `OPTIONS <options>...`.

`DEFINES_FILE <string>`

  Added in version 3.5.

  If flex is configured to output a header file, this option may be used to specify its name.

Changed in version 3.17: When `CMP0098` is set to `NEW`, `flex` runs in the `CMAKE_CURRENT_BINARY_DIR` directory.

The macro defines the following variables:

`FLEX_<Name>_DEFINED`

  True if the macro ran successfully.

`FLEX_<Name>_OUTPUTS`

  The source file generated by the custom rule, an alias for `<FlexOutput>`.

`FLEX_<Name>_INPUT`

  The flex source file, an alias for `<FlexInput>`.

`FLEX_<Name>_OUTPUT_HEADER`

  The header flex output, if any.

`FLEX_<Name>_OPTIONS`

  Added in version 4.0.

  Options used in the `flex` command line.

Flex scanners often use tokens defined by Bison: the code generated by Flex depends of the header generated by Bison. This module also defines a macro:

add_flex_bison_dependency

      add_flex_bison_dependency(<FlexTarget> <BisonTarget>)

which adds the required dependency between a scanner and a parser where `<FlexTarget>` and `<BisonTarget>` are the first parameters of respectively `flex_target` and `bison_target` macros.

## Examples

    find_package(BISON)
    find_package(FLEX)

    bison_target(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
    flex_target(MyScanner lexer.l  ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp)
    add_flex_bison_dependency(MyScanner MyParser)

    include_directories(${CMAKE_CURRENT_BINARY_DIR})
    add_executable(Foo
      Foo.cc
      ${BISON_MyParser_OUTPUTS}
      ${FLEX_MyScanner_OUTPUTS}
    )
    target_link_libraries(Foo ${FLEX_LIBRARIES})

Adding additional command-line options to the `flex` executable can be passed as a list. For example, adding the `--warn` option to report warnings, and the `--noline` (`-L`) to not generate `#line` directives.

    find_package(FLEX)

    if(FLEX_FOUND)
      flex_target(MyScanner lexer.l lexer.cpp OPTIONS --warn --noline)
    endif()

Generator expressions can be used in `OPTIONS <options...`. For example, to add the `--debug` (`-d`) option only for the `Debug` build type:

    find_package(FLEX)

    if(FLEX_FOUND)
      flex_target(MyScanner lexer.l lexer.cpp OPTIONS $<$<CONFIG:Debug>:--debug>)
    endif()