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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| srcs | List of source files to macro-expand. | List of labels | required | |
| data | List of additional files to m4. | List of labels | optional | [] |
| freeze_state | Optional output file for GNU M4 frozen state. Must have extension .m4f. | Label; nonconfigurable | optional | None |
| m4_options | Additional options to pass to the m4 command.These will be added to the command args immediately before the source files. | List of strings | optional | [] |
| output | File to write output to. If unset, defaults to the rule name. | Label; nonconfigurable | optional | None |
| reload_state | Optional input file for GNU M4 frozen state. Must have extension .m4f. | Label | optional | None |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| m4_env | Additional environment variables to set when running m4_tool. | Dictionary: String -> String | optional | {} |
| m4_tool | A FilesToRunProvider for the m4 binary. | Label | required |
M4ToolchainInfo
load("@rules_m4//m4:m4.bzl", "M4ToolchainInfo")
M4ToolchainInfo(all_files, m4_tool, m4_env)
Provider for an m4 toolchain.
FIELDS
| Name | Description |
|---|---|
| all_files | A depset containing all files comprising this m4 toolchain. |
| m4_tool | A FilesToRunProvider for the m4 binary. |
| m4_env | Additional 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
| Name | Description | Default Value |
|---|---|---|
| version | A supported version of GNU M4. | "1.4.18" |
| extra_copts | Additional 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
| Name | Description | Default Value |
|---|---|---|
| ctx | A 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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this repository. | Name | required | |
| extra_copts | Additional C compiler options to use when building GNU M4. | List of strings | optional | [] |
| extra_http_mirrors | Additional HTTP mirrors of the GNU M4 source archives. These mirrors will be appended to the list of default GNU mirrors. | List of strings | optional | [] |
| extra_linkopts | Additional linker options to use when building GNU M4. | List of strings | optional | [] |
| http_mirrors | If 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 strings | optional | [] |
| version | A supported version of GNU M4. | String | required |
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 underlyingm4_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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this repository. | Name | required | |
| m4_repository | The name of an m4_repository. | String | required |
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 underlyingm4_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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | An 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}". | Name | optional | "" |
| extra_copts | Additional C compiler options to use when building GNU M4. | List of strings | optional | [] |
| extra_http_mirrors | Additional HTTP mirrors of the GNU M4 source archives. These mirrors will be appended to the list of default GNU mirrors. | List of strings | optional | [] |
| extra_linkopts | Additional linker options to use when building GNU M4. | List of strings | optional | [] |
| http_mirrors | If 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 strings | optional | [] |
| version | A supported version of GNU M4. | String | optional | "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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | The name of the toolchain repository to create. | Name | required | |
| m4_env | Additional environment variables to set when running m4_tool. | Dictionary: String -> String | optional | {} |
| m4_tool | The label of an m4 executable target. | Label | required |