rules_flex

April 18, 2026 ยท View on GitHub

Bazel rules for Flex, the Fast Lexical Analyzer.

flex

load("@rules_flex//flex:flex.bzl", "flex")

flex(name, src, flex_options, language, output_src_ext)

Generate C/C++ source code for a Flex lexical analyzer.

This rule exists for special cases where the build needs to perform further modification of the generated .c / .h before compilation. Most users will find the flex_cc_library rule more convenient.

The output groups cc_srcs and cc_hdrs provide access to the generated {name}.c / {name}.cc sources and (if available) the {name}.h header.

Example

load("@rules_flex//flex:flex.bzl", "flex")

flex(
    name = "hello",
    src = "hello.l",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcA Flex source file.

Unless language is set, the source's file extension will determine whether Flex operates in C or C++ mode:
  • Inputs with file extension .l generate outputs {name}.c and {name}.h.
  • Inputs with file extension .ll, .l++, .lxx, or .lpp generate output {name}.cc. This is equivalent to invoking Flex as flex++.
The C++ output depends on FlexLexer.h, which is part of the Flex source distribution and may be obtained from the Flex toolchain.
Labelrequired
flex_optionsAdditional options to pass to the flex command.

These will be added to the command args immediately before the source file.
List of stringsoptional[]
languageWhich language to generate the lexer in.Stringoptional""
output_src_extOverride the file extension used for the generated source file.

By default the extension is auto-detected according to the language attribute, which is itself auto-detected according to the input file extension.
Stringoptional""

flex_cc_library

load("@rules_flex//flex:flex.bzl", "flex_cc_library")

flex_cc_library(name, deps, src, defines, flex_options, include_prefix, language, linkstatic,
                local_defines, output_src_ext, strip_include_prefix)

Generate a C/C++ library for a Flex lexical analyzer.

Example

load("@rules_flex//flex:flex.bzl", "flex_cc_library")

flex_cc_library(
    name = "hello_lib",
    src = "hello.l",
)

cc_binary(
    name = "hello",
    srcs = ["hello_main.c"],
    deps = [":hello_lib"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsA list of other C/C++ libraries that the library depends on.

See cc_library.deps for more details.
List of labelsoptional[]
srcA Flex source file.

Unless language is set, the source's file extension will determine whether Flex operates in C or C++ mode:
  • Inputs with file extension .l generate outputs {name}.c and {name}.h.
  • Inputs with file extension .ll, .l++, .lxx, or .lpp generate output {name}.cc. This is equivalent to invoking Flex as flex++.
The C++ output depends on FlexLexer.h, which is part of the Flex source distribution and may be obtained from the Flex toolchain.
Labelrequired
definesList of defines to add to the compile line of this and all dependent targets.

See cc_library.defines for more details.
List of stringsoptional[]
flex_optionsAdditional options to pass to the flex command.

These will be added to the command args immediately before the source file.
List of stringsoptional[]
include_prefixA prefix to add to the path of the generated header.

See cc_library.include_prefix for more details.
Stringoptional""
languageWhich language to generate the lexer in.Stringoptional""
linkstaticDisable creation of a shared library output.

See cc_library.linkstatic for more details.
BooleanoptionalFalse
local_definesList of defines to add to the compile line of this target.

See cc_library.local_defines for more details.
List of stringsoptional[]
output_src_extOverride the file extension used for the generated source file.

By default the extension is auto-detected according to the language attribute, which is itself auto-detected according to the input file extension.
Stringoptional""
strip_include_prefixA prefix to strip from the path of the generated header.

See cc_library.strip_include_prefix for more details.
Stringoptional""

flex_toolchain_info

load("@rules_flex//flex:flex.bzl", "flex_toolchain_info")

flex_toolchain_info(name, flex_env, flex_lexer_h, flex_tool)

Provides ToolchainInfo and TemplateVariableInfo for the Flex toolchain.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
flex_envAdditional environment variables to set when running flex_tool.Dictionary: String -> Stringoptional{}
flex_lexer_hLabel of FlexLexer.h, used for generated C++ lexers.Labelrequired
flex_toolA FilesToRunProvider for the flex binary.Labelrequired

FlexToolchainInfo

load("@rules_flex//flex:flex.bzl", "FlexToolchainInfo")

FlexToolchainInfo(all_files, flex_tool, flex_env, flex_lexer_h)

Provider for a Flex toolchain.

FIELDS

NameDescription
all_filesA depset containing all files comprising this Flex toolchain.
flex_toolA FilesToRunProvider for the flex binary.
flex_envAdditional environment variables to set when running flex_tool.
flex_lexer_hA File for the FlexLexer.h header.

flex_register_toolchains

load("@rules_flex//flex:flex.bzl", "flex_register_toolchains")

flex_register_toolchains(version, extra_copts)

A helper function for Flex toolchains registration.

This workspace macro will create a flex_repository named flex_v{version} and register it as a Bazel toolchain.

PARAMETERS

NameDescriptionDefault Value
versionA supported version of Flex."2.6.4"
extra_coptsAdditional C compiler options to use when building Flex.[]

flex_toolchain

load("@rules_flex//flex:flex.bzl", "flex_toolchain")

flex_toolchain(ctx)

Returns the current FlexToolchainInfo.

PARAMETERS

NameDescriptionDefault Value
ctxA rule context, where the rule has a toolchain dependency on FLEX_TOOLCHAIN_TYPE.none

RETURNS

A FlexToolchainInfo.

flex_repository

load("@rules_flex//flex:flex.bzl", "flex_repository")

flex_repository(name, extra_copts, extra_linkopts, patch_strip, patches, version)

Repository rule for Flex.

The resulting repository will have a //bin:flex executable target.

Example

load("@rules_flex//flex:flex.bzl", "flex_repository")

flex_repository(
    name = "flex_v2.6.4",
    version = "2.6.4",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
extra_coptsAdditional C compiler options to use when building Flex.List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building Flex.List of stringsoptional[]
patch_stripStrip the specified number of leading components from patch file names.Integeroptional0
patchesA mapping from Flex versions to lists of patch files to apply, relative to the root of the Flex source repository. Each patch should be in standard unified diff format.Dictionary: String -> List of stringsoptional{"2.6.4": ["//flex/patches:0001-fix-noline-for-top-directives.patch"]}
versionA supported version of Flex.Stringrequired

flex_toolchain_repository

load("@rules_flex//flex:flex.bzl", "flex_toolchain_repository")

flex_toolchain_repository(name, flex_repository)

Toolchain repository rule for Flex toolchains.

Toolchain repositories add a layer of indirection so that Bazel can resolve toolchains without downloading additional dependencies.

The resulting repository will have the following targets:

  • //bin:flex (an alias into the underlying [flex_repository] (#flex_repository))
  • //:toolchain, which can be registered with Bazel.

Example

load(
    "@rules_flex//flex:flex.bzl",
    "flex_repository",
    "flex_toolchain_repository",
)

flex_repository(
    name = "flex_v2.6.4",
    version = "2.6.4",
)

flex_toolchain_repository(
    name = "flex",
    flex_repository = "@flex_v2.6.4",
)

register_toolchains("@flex//:toolchain")

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
flex_repositoryThe name of a flex_repository.Stringrequired

flex_repository_ext

flex_repository_ext = use_extension("@rules_flex//flex/extensions:flex_repository_ext.bzl", "flex_repository_ext")
flex_repository_ext.repository(name, extra_copts, extra_linkopts, version)

Module extension for declaring dependencies on Flex.

The resulting repository will have the following targets:

  • //bin:flex (an alias into the underlying [flex_repository] (#flex_repository))
  • //:toolchain, which can be registered with Bazel.

Example

flex = use_extension(
    "@rules_flex//flex/extensions:flex_repository_ext.bzl",
    "flex_repository_ext",
)

flex.repository(name = "flex", version = "2.6.4")
use_repo(flex, "flex")
register_toolchains("@flex//:toolchain")

TAG CLASSES

repository

Attributes

NameDescriptionTypeMandatoryDefault
nameAn optional name for the repository.

The name must be unique within the set of names registered by this extension. If unset, the repository name will default to "flex_v{version}".
Nameoptional""
extra_coptsAdditional C compiler options to use when building Flex.List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building Flex.List of stringsoptional[]
versionA supported version of Flex.Stringoptional"2.6.4"

flex_toolchains_ext

flex_toolchains_ext = use_extension("@rules_flex//flex/extensions:flex_toolchains_ext.bzl", "flex_toolchains_ext")
flex_toolchains_ext.toolchain(name, flex_env, flex_lexer_h, flex_tool)

Module extension for declaring Flex toolchains with custom target binaries.

The resulting repository will have one subdirectory per named module tag, which contains a :toolchain target that can be registered with Bazel.

Example

flex_toolchains = use_extension(
    "@rules_flex//flex/extensions:flex_toolchains_ext.bzl",
    "flex_toolchain_ext",
)

flex_toolchains.toolchain(
    name = "custom",
    flex_tool = "//custom_flex:flex",
    flex_lexer_h = "//custom_flex:flex_lexer_h",
)
use_repo(flex_toolchains, "flex_toolchains")
register_toolchains("@flex_toolchains//custom:toolchain")

TAG CLASSES

toolchain

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the toolchain repository to create.Namerequired
flex_envAdditional environment variables to set when running flex_tool.Dictionary: String -> Stringoptional{}
flex_lexer_hLabel of FlexLexer.h, used for generated C++ lexers.Labelrequired
flex_toolThe label of an flex executable target.Labelrequired