rules_m4

April 4, 2026 ยท View on GitHub

Bazel rules for the m4 macro expander.

m4

load("@rules_m4//m4:m4.bzl", "m4")

m4(name, srcs, data, freeze_state, m4_options, output, reload_state)

Perform macro expansion to produce an output file.

This rule blocks the of execution shell commands (such as syscmd) by default. To enable expansion of a file containing shell commands, set the m4_syscmd target feature.

Example

load("@rules_m4//m4:m4.bzl", "m4")

m4(
    name = "m4_example.txt",
    srcs = ["m4_example.in.txt"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcsList of source files to macro-expand.List of labelsrequired
dataList of additional files to m4.List of labelsoptional[]
freeze_stateOptional output file for GNU M4 frozen state. Must have extension .m4f.Label; nonconfigurableoptionalNone
m4_optionsAdditional options to pass to the m4 command.

These will be added to the command args immediately before the source files.
List of stringsoptional[]
outputFile to write output to. If unset, defaults to the rule name.Label; nonconfigurableoptionalNone
reload_stateOptional input file for GNU M4 frozen state. Must have extension .m4f.LabeloptionalNone

m4_toolchain_info

load("@rules_m4//m4:m4.bzl", "m4_toolchain_info")

m4_toolchain_info(name, m4_env, m4_tool)

Provides ToolchainInfo and TemplateVariableInfo for the M4 toolchain.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
m4_envAdditional environment variables to set when running m4_tool.Dictionary: String -> Stringoptional{}
m4_toolA FilesToRunProvider for the m4 binary.Labelrequired

M4ToolchainInfo

load("@rules_m4//m4:m4.bzl", "M4ToolchainInfo")

M4ToolchainInfo(all_files, m4_tool, m4_env)

Provider for an m4 toolchain.

FIELDS

NameDescription
all_filesA depset containing all files comprising this m4 toolchain.
m4_toolA FilesToRunProvider for the m4 binary.
m4_envAdditional environment variables to set when running m4_tool.

m4_register_toolchains

load("@rules_m4//m4:m4.bzl", "m4_register_toolchains")

m4_register_toolchains(version, extra_copts)

A helper function for m4 toolchains registration.

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

PARAMETERS

NameDescriptionDefault Value
versionA supported version of GNU M4."1.4.18"
extra_coptsAdditional C compiler options to use when building GNU M4.[]

m4_toolchain

load("@rules_m4//m4:m4.bzl", "m4_toolchain")

m4_toolchain(ctx)

Returns the current M4ToolchainInfo.

PARAMETERS

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

RETURNS

An M4ToolchainInfo.

m4_repository

load("@rules_m4//m4:m4.bzl", "m4_repository")

m4_repository(name, extra_copts, extra_http_mirrors, extra_linkopts, http_mirrors, version)

Repository rule for GNU M4.

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

Example

load("@rules_m4//m4:m4.bzl", "m4_repository")

m4_repository(
    name = "m4_v1.4.18",
    version = "1.4.18",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
extra_coptsAdditional C compiler options to use when building GNU M4.List of stringsoptional[]
extra_http_mirrorsAdditional HTTP mirrors of the GNU M4 source archives.

These mirrors will be appended to the list of default GNU mirrors.
List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building GNU M4.List of stringsoptional[]
http_mirrorsIf set then this value will be used instead of the default HTTP mirror list.

The extra_http_mirrors attribute will be appended to this list.
List of stringsoptional[]
versionA supported version of GNU M4.Stringrequired

m4_toolchain_repository

load("@rules_m4//m4:m4.bzl", "m4_toolchain_repository")

m4_toolchain_repository(name, m4_repository)

Toolchain repository rule for m4 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:m4 (an alias into the underlying m4_repository)
  • //:toolchain, which can be registered with Bazel.

Example

load("@rules_m4//m4:m4.bzl", "m4_repository", "m4_toolchain_repository")

m4_repository(
    name = "m4_v1.4.18",
    version = "1.4.18",
)

m4_toolchain_repository(
    name = "m4",
    m4_repository = "@m4_v1.4.18",
)

register_toolchains("@m4//:toolchain")

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
m4_repositoryThe name of an m4_repository.Stringrequired

m4_repository_ext

m4_repository_ext = use_extension("@rules_m4//m4/extensions:m4_repository_ext.bzl", "m4_repository_ext")
m4_repository_ext.repository(name, extra_copts, extra_http_mirrors, extra_linkopts, http_mirrors,
                             version)

Module extension for declaring dependencies on GNU M4.

The resulting repository will have the following targets:

  • //bin:m4 (an alias into the underlying m4_repository)
  • //:toolchain, which can be registered with Bazel.

Example

m4 = use_extension(
    "@rules_m4//m4/extensions:m4_repository_ext.bzl",
    "m4_repository_ext",
)

m4.repository(name = "m4", version = "1.4.18")
use_repo(m4, "m4")
register_toolchains("@m4//: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 "m4_v{version}".
Nameoptional""
extra_coptsAdditional C compiler options to use when building GNU M4.List of stringsoptional[]
extra_http_mirrorsAdditional HTTP mirrors of the GNU M4 source archives.

These mirrors will be appended to the list of default GNU mirrors.
List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building GNU M4.List of stringsoptional[]
http_mirrorsIf set then this value will be used instead of the default HTTP mirror list.

The extra_http_mirrors attribute will be appended to this list.
List of stringsoptional[]
versionA supported version of GNU M4.Stringoptional"1.4.18"

m4_toolchains_ext

m4_toolchains_ext = use_extension("@rules_m4//m4/extensions:m4_toolchains_ext.bzl", "m4_toolchains_ext")
m4_toolchains_ext.toolchain(name, m4_env, m4_tool)

Module extension for declaring M4 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

m4_toolchains = use_extension(
    "@rules_m4//m4/extensions:m4_toolchains_ext.bzl",
    "m4_toolchain_ext",
)

m4_toolchains.toolchain(name = "custom", m4_tool = "//custom_m4:m4")
use_repo(m4_toolchains, "m4_toolchains")
register_toolchains("@m4_toolchains//custom:toolchain")

TAG CLASSES

toolchain

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the toolchain repository to create.Namerequired
m4_envAdditional environment variables to set when running m4_tool.Dictionary: String -> Stringoptional{}
m4_toolThe label of an m4 executable target.Labelrequired