rules.md

May 26, 2026 ยท View on GitHub

Rules to build and run Zig code.

Note, all Zig targets implicitly depend on an automatically generated Zig module called bazel_builtin that exposes Bazel specific information such as the current target name or current repository name.

zig_binary

load("@rules_zig//zig:defs.bzl", "zig_binary")

zig_binary(name, deps, srcs, data, compiler_runtime, copts, csrcs, emit_asm, emit_llvm_bc,
           emit_llvm_ir, env, extra_docs, extra_srcs, linker_script, linkopts, main,
           strip_debug_symbols, zigopts)

Builds a Zig binary.

The target can be built using bazel build, corresponding to zig build-exe, and executed using bazel run, corresponding to zig run.

Documentation can be generated by requesting the zig_docs output group, e.g. using the command bazel build //my:target --output_groups=zig_docs.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_binary")

zig_binary(
    name = "my-binary",
    main = "main.zig",
    srcs = [
        "utils.zig",  # to support `@import("utils.zig")`.
    ],
    deps = [
        ":my-module",  # to support `@import("my-module")`.
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of other modules or C/C++ libraries that the library target depends upon.List of labelsoptional[]
srcsOther Zig source files required to build the target, e.g. files imported using @import.List of labelsoptional[]
dataFiles required by the target during runtime.List of labelsoptional[]
compiler_runtimeWhether to include Zig compiler runtime symbols in the generated output. The default behavior is to include them in executables and shared libraries.Stringoptional"default"
coptsC compiler flags required to build the C sources of the target. Subject to location expansion.List of stringsoptional[]
csrcsC source files required to build the target.List of labelsoptional[]
emit_asmEmit the assembly in the asm output group.BooleanoptionalFalse
emit_llvm_bcEmit the LLVM bytecode in the llvm_bc output group.BooleanoptionalFalse
emit_llvm_irEmit the LLVM IR in the llvm_ir output group.BooleanoptionalFalse
envAdditional environment variables to set when executed by bazel run. Subject to location expansion. NOTE: The environment variables are not set when you run the target outside of Bazel (for example, by manually executing the binary in bazel-bin/).Dictionary: String -> Stringoptional{}
extra_docsOther files required to generate documentation, e.g. guides referenced using //!zig-autodoc-guide:.List of labelsoptional[]
extra_srcsOther files required to build the target, e.g. files embedded using @embedFile.List of labelsoptional[]
linker_scriptCustom linker script for the target.

Note, as of Zig version 0.15.1 linker-scripts require the LLVM/LLD backend to be enabled, see https://github.com/ziglang/zig/issues/25069. Set zigopts=["-fllvm", "-flld"] to that end.
LabeloptionalNone
linkoptsAdditional list of flags passed to the linker. Subject to location expansion.List of stringsoptional[]
mainThe main source file.

If not set, deps must contain exactly one Zig module dependency which will be used as the root module. Note that in that case, 'srcs', 'extra_srcs' and 'csrcs' must also be empty as they are taken from the root module instead.
LabeloptionalNone
strip_debug_symbolsWhether to force passing '-fstrip' to the zig compiler to remove debug symbols. Bazel's --strip flag is also supported globally.BooleanoptionalFalse
zigoptsAdditional list of flags passed to the zig compiler. Subject to location expansion.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_c_library

load("@rules_zig//zig:defs.bzl", "zig_c_library")

zig_c_library(name, data, cdeps, import_name)

Defines a Zig C module.

A Zig C module is a Zig module whose C headers dependencies have been translated to zig using translate-c, and which only output defines the module's entry point.

This rule performs the translate-c step but does not perform compilation of Zig by itself. Instead, modules are compiled at the use-site. Zig performs whole program compilation.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_c_library")

zig_c_library(
    name = "my-module",
    cdeps = [
        ":cc-library",
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dataFiles required by the module during runtime.List of labelsoptional[]
cdepsC dependencies to translate their headers from.List of labelsrequired
import_nameThe import name of the module.Stringoptional""

zig_configure

load("@rules_zig//zig:defs.bzl", "zig_configure")

zig_configure(name, actual, bootstrapped, extra_toolchains, host_mode, host_threaded,
              host_use_cc_common_link, host_zigopt, mode, target, threaded, use_cc_common_link,
              use_standalone_translate_c, zig_version, zigopt)

Transitions a target and its dependencies to a different configuration.

Settings like the build mode, e.g. ReleaseSafe, or the target platform, can be set on the command-line on demand, e.g. using --@rules_zig//zig/settings:mode=release_safe. By default, target build mode is auto: Bazel dbg and fastbuild compilation modes map to Zig Debug, and Bazel opt maps to Zig ReleaseFast.

However, you may wish to always build a given target in a particular configuration, or you may wish to build a given target in multiple configurations in a single build, e.g. to generate a multi-platform release bundle.

Use this rule to that end.

You can read more about Bazel configurations and transitions here.

EXAMPLE

load(
    "@rules_zig//zig:defs.bzl",
    "zig_binary",
    "zig_configure",
)

zig_static_library(
    name = "library",
    main = "library.zig",
    tags = ["manual"],  # optional, exclude from `bazel build //...`.
)

zig_configure(
    name = "library_debug",
    actual = ":library",
    mode = "debug",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
actualThe target to transition.Labelrequired
bootstrappedWhether to use a bootstrapped Zig compiler. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:bootstrapped. 0 means do not use a bootstrapped Zig compiler. 1 means use a bootstrapped Zig compiler.Integeroptional-1
extra_toolchainsAdditional toolchains to consider during toolchain resolution for the transitioned target.List of labelsoptional[]
host_modeThe build mode setting for the host configuration, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
host_threadedThe threaded setting for the host configuration, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
host_use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries in the host configuration. Possible values: [-1, 0, 1]. -1 means use current host configuration value for //zig/settings:host_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
host_zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions when building for the host configuration.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]
modeThe build mode setting, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
targetThe target platform, expects a label to a Bazel target platform used to select a zig_target_toolchain instance.LabeloptionalNone
threadedThe threaded setting, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:experimental_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
use_standalone_translate_cWhether to use a registered standalone translate-c toolchain for C translation instead of the built-in zig translate-c tool. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:use_standalone_translate_c. 0 means do not use the standalone translate-c toolchain. 1 means use the standalone translate-c toolchain.Integeroptional-1
zig_versionThe Zig SDK version, must be registered using the zig module extension.Stringoptional""
zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_configure_binary

load("@rules_zig//zig:defs.bzl", "zig_configure_binary")

zig_configure_binary(name, actual, bootstrapped, extra_toolchains, host_mode, host_threaded,
                     host_use_cc_common_link, host_zigopt, mode, target, threaded, use_cc_common_link,
                     use_standalone_translate_c, zig_version, zigopt)

Transitions a target and its dependencies to a different configuration.

Settings like the build mode, e.g. ReleaseSafe, or the target platform, can be set on the command-line on demand, e.g. using --@rules_zig//zig/settings:mode=release_safe. By default, target build mode is auto: Bazel dbg and fastbuild compilation modes map to Zig Debug, and Bazel opt maps to Zig ReleaseFast.

However, you may wish to always build a given target in a particular configuration, or you may wish to build a given target in multiple configurations in a single build, e.g. to generate a multi-platform release bundle.

Use this rule to that end.

You can read more about Bazel configurations and transitions here.

EXAMPLE

load(
    "@rules_zig//zig:defs.bzl",
    "zig_binary",
    "zig_configure_binary",
)

zig_binary(
    name = "binary",
    main = "main.zig",
    tags = ["manual"],  # optional, exclude from `bazel build //...`.
)

zig_configure_binary(
    name = "binary_debug",
    actual = ":binary",
    mode = "debug",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
actualThe target to transition.Labelrequired
bootstrappedWhether to use a bootstrapped Zig compiler. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:bootstrapped. 0 means do not use a bootstrapped Zig compiler. 1 means use a bootstrapped Zig compiler.Integeroptional-1
extra_toolchainsAdditional toolchains to consider during toolchain resolution for the transitioned target.List of labelsoptional[]
host_modeThe build mode setting for the host configuration, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
host_threadedThe threaded setting for the host configuration, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
host_use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries in the host configuration. Possible values: [-1, 0, 1]. -1 means use current host configuration value for //zig/settings:host_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
host_zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions when building for the host configuration.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]
modeThe build mode setting, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
targetThe target platform, expects a label to a Bazel target platform used to select a zig_target_toolchain instance.LabeloptionalNone
threadedThe threaded setting, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:experimental_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
use_standalone_translate_cWhether to use a registered standalone translate-c toolchain for C translation instead of the built-in zig translate-c tool. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:use_standalone_translate_c. 0 means do not use the standalone translate-c toolchain. 1 means use the standalone translate-c toolchain.Integeroptional-1
zig_versionThe Zig SDK version, must be registered using the zig module extension.Stringoptional""
zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_configure_test

load("@rules_zig//zig:defs.bzl", "zig_configure_test")

zig_configure_test(name, actual, bootstrapped, extra_toolchains, host_mode, host_threaded,
                   host_use_cc_common_link, host_zigopt, mode, target, threaded, use_cc_common_link,
                   use_standalone_translate_c, zig_version, zigopt)

Transitions a target and its dependencies to a different configuration.

Settings like the build mode, e.g. ReleaseSafe, or the target platform, can be set on the command-line on demand, e.g. using --@rules_zig//zig/settings:mode=release_safe. By default, target build mode is auto: Bazel dbg and fastbuild compilation modes map to Zig Debug, and Bazel opt maps to Zig ReleaseFast.

However, you may wish to always build a given target in a particular configuration, or you may wish to build a given target in multiple configurations in a single build, e.g. to generate a multi-platform release bundle.

Use this rule to that end.

You can read more about Bazel configurations and transitions here.

EXAMPLE

load(
    "@rules_zig//zig:defs.bzl",
    "zig_test",
    "zig_configure_test",
)

zig_test(
    name = "test",
    main = "test.zig",
    tags = ["manual"],  # optional, exclude from `bazel build //...`.
)

zig_configure_test(
    name = "test_debug",
    actual = ":test",
    mode = "debug",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
actualThe target to transition.Labelrequired
bootstrappedWhether to use a bootstrapped Zig compiler. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:bootstrapped. 0 means do not use a bootstrapped Zig compiler. 1 means use a bootstrapped Zig compiler.Integeroptional-1
extra_toolchainsAdditional toolchains to consider during toolchain resolution for the transitioned target.List of labelsoptional[]
host_modeThe build mode setting for the host configuration, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
host_threadedThe threaded setting for the host configuration, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
host_use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries in the host configuration. Possible values: [-1, 0, 1]. -1 means use current host configuration value for //zig/settings:host_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
host_zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions when building for the host configuration.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]
modeThe build mode setting, corresponds to the -O Zig compiler flag. auto follows Bazel's --compilation_mode.Stringoptional""
targetThe target platform, expects a label to a Bazel target platform used to select a zig_target_toolchain instance.LabeloptionalNone
threadedThe threaded setting, corresponds to the -fsingle-threaded Zig compiler flag.Stringoptional""
use_cc_common_linkWhether to use cc_common.link to link zig binaries, tests and shared libraries. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:experimental_use_cc_common_link. 0 means do not use cc_common.link (use zig build-exe instead). 1 means use cc_common.link.Integeroptional-1
use_standalone_translate_cWhether to use a registered standalone translate-c toolchain for C translation instead of the built-in zig translate-c tool. Possible values: [-1, 0, 1]. -1 means use current configuration value for //zig/settings:use_standalone_translate_c. 0 means do not use the standalone translate-c toolchain. 1 means use the standalone translate-c toolchain.Integeroptional-1
zig_versionThe Zig SDK version, must be registered using the zig module extension.Stringoptional""
zigoptAdditional list of flags passed to the zig compiler for all Zig compile actions.

The flags specified by this setting do not override those specified via the zigopts attribute of zig_* rules. Instead, they are prepended to the command line before module specific flags.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_library

load("@rules_zig//zig:defs.bzl", "zig_library")

zig_library(name, deps, srcs, data, extra_srcs, import_name, main, zigopts)

Defines a Zig library.

A Zig library is a collection of Zig sources with a main source file that defines the module's entry point.

This rule does not perform compilation by itself. Instead, modules are compiled at the use-site. Zig performs whole program compilation.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_library")

zig_library(
    name = "my-module",
    main = "main.zig",
    srcs = [
        "utils.zig",  # to support `@import("utils.zig")`.
    ],
    deps = [
        ":other-module",  # to support `@import("other-module")`.
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther modules required when building the module.List of labelsoptional[]
srcsOther Zig source files required when building the module, e.g. files imported using @import.List of labelsoptional[]
dataFiles required by the module during runtime.List of labelsoptional[]
extra_srcsOther files required when building the module, e.g. files embedded using @embedFile.List of labelsoptional[]
import_nameThe import name of the module.Stringoptional""
mainThe main source file.Labelrequired
zigoptsAdditional list of flags passed to the zig compiler for this module.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_shared_library

load("@rules_zig//zig:defs.bzl", "zig_shared_library")

zig_shared_library(name, deps, srcs, data, compiler_runtime, copts, csrcs, emit_asm, emit_llvm_bc,
                   emit_llvm_ir, extra_docs, extra_srcs, linker_script, linkopts, main,
                   shared_lib_name, strip_debug_symbols, zigopts)

Builds a Zig shared library, produces a shared or dynamic library.

The target can be built using bazel build, corresponding to zig build-lib.

Suitable as a dependency to C/C++ targets.

Documentation can be generated by requesting the zig_docs output group, e.g. using the command bazel build //my:target --output_groups=zig_docs.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_shared_library")

zig_shared_library(
    name = "my-library",
    main = "main.zig",
    srcs = [
        "utils.zig",  # to support `@import("utils.zig")`.
    ],
    deps = [
        ":my-module",  # to support `@import("my-module")`.
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of other modules or C/C++ libraries that the library target depends upon.List of labelsoptional[]
srcsOther Zig source files required to build the target, e.g. files imported using @import.List of labelsoptional[]
dataFiles required by the target during runtime.List of labelsoptional[]
compiler_runtimeWhether to include Zig compiler runtime symbols in the generated output. The default behavior is to include them in executables and shared libraries.Stringoptional"default"
coptsC compiler flags required to build the C sources of the target. Subject to location expansion.List of stringsoptional[]
csrcsC source files required to build the target.List of labelsoptional[]
emit_asmEmit the assembly in the asm output group.BooleanoptionalFalse
emit_llvm_bcEmit the LLVM bytecode in the llvm_bc output group.BooleanoptionalFalse
emit_llvm_irEmit the LLVM IR in the llvm_ir output group.BooleanoptionalFalse
extra_docsOther files required to generate documentation, e.g. guides referenced using //!zig-autodoc-guide:.List of labelsoptional[]
extra_srcsOther files required to build the target, e.g. files embedded using @embedFile.List of labelsoptional[]
linker_scriptCustom linker script for the target.

Note, as of Zig version 0.15.1 linker-scripts require the LLVM/LLD backend to be enabled, see https://github.com/ziglang/zig/issues/25069. Set zigopts=["-fllvm", "-flld"] to that end.
LabeloptionalNone
linkoptsAdditional list of flags passed to the linker. Subject to location expansion.List of stringsoptional[]
mainThe main source file.

If not set, deps must contain exactly one Zig module dependency which will be used as the root module. Note that in that case, 'srcs', 'extra_srcs' and 'csrcs' must also be empty as they are taken from the root module instead.
LabeloptionalNone
shared_lib_name-Stringoptional""
strip_debug_symbolsWhether to force passing '-fstrip' to the zig compiler to remove debug symbols. Bazel's --strip flag is also supported globally.BooleanoptionalFalse
zigoptsAdditional list of flags passed to the zig compiler. Subject to location expansion.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_static_library

load("@rules_zig//zig:defs.bzl", "zig_static_library")

zig_static_library(name, deps, srcs, data, compiler_runtime, copts, csrcs, emit_asm, emit_bin,
                   emit_llvm_bc, emit_llvm_ir, extra_docs, extra_srcs, linker_script, linkopts, main,
                   strip_debug_symbols, zigopts)

Builds a Zig library, produces a static archive.

The target can be built using bazel build, corresponding to zig build-lib.

Suitable as a dependency to C/C++ targets.

Documentation can be generated by requesting the zig_docs output group, e.g. using the command bazel build //my:target --output_groups=zig_docs.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_static_library")

zig_static_library(
    name = "my-library",
    main = "main.zig",
    srcs = [
        "utils.zig",  # to support `@import("utils.zig")`.
    ],
    deps = [
        ":my-module",  # to support `@import("my-module")`.
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of other modules or C/C++ libraries that the library target depends upon.List of labelsoptional[]
srcsOther Zig source files required to build the target, e.g. files imported using @import.List of labelsoptional[]
dataFiles required by the target during runtime.List of labelsoptional[]
compiler_runtimeWhether to include Zig compiler runtime symbols in the generated output. The default behavior is to include them in executables and shared libraries.Stringoptional"default"
coptsC compiler flags required to build the C sources of the target. Subject to location expansion.List of stringsoptional[]
csrcsC source files required to build the target.List of labelsoptional[]
emit_asmEmit the assembly in the asm output group.BooleanoptionalFalse
emit_binEmit the binary.BooleanoptionalTrue
emit_llvm_bcEmit the LLVM bytecode in the llvm_bc output group.BooleanoptionalFalse
emit_llvm_irEmit the LLVM IR in the llvm_ir output group.BooleanoptionalFalse
extra_docsOther files required to generate documentation, e.g. guides referenced using //!zig-autodoc-guide:.List of labelsoptional[]
extra_srcsOther files required to build the target, e.g. files embedded using @embedFile.List of labelsoptional[]
linker_scriptCustom linker script for the target.

Note, as of Zig version 0.15.1 linker-scripts require the LLVM/LLD backend to be enabled, see https://github.com/ziglang/zig/issues/25069. Set zigopts=["-fllvm", "-flld"] to that end.
LabeloptionalNone
linkoptsAdditional list of flags passed to the linker. Subject to location expansion.List of stringsoptional[]
mainThe main source file.

If not set, deps must contain exactly one Zig module dependency which will be used as the root module. Note that in that case, 'srcs', 'extra_srcs' and 'csrcs' must also be empty as they are taken from the root module instead.
LabeloptionalNone
strip_debug_symbolsWhether to force passing '-fstrip' to the zig compiler to remove debug symbols. Bazel's --strip flag is also supported globally.BooleanoptionalFalse
zigoptsAdditional list of flags passed to the zig compiler. Subject to location expansion.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]

zig_test

load("@rules_zig//zig:defs.bzl", "zig_test")

zig_test(name, deps, srcs, data, compiler_runtime, copts, csrcs, emit_asm, emit_llvm_bc,
         emit_llvm_ir, env, env_inherit, extra_docs, extra_srcs, linker_script, linkopts, main,
         strip_debug_symbols, test_runner, zigopts)

Builds a Zig test.

The target can be executed using bazel test, corresponding to zig test.

Documentation can be generated by requesting the zig_docs output group, e.g. using the command bazel build //my:target --output_groups=zig_docs.

EXAMPLE

load("@rules_zig//zig:defs.bzl", "zig_test")

zig_test(
    name = "my-test",
    main = "test.zig",
    srcs = [
        "utils.zig",  # to support `@import("utils.zig")`.
    ],
    deps = [
        ":my-module",  # to support `@import("my-module")`.
    ],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe list of other modules or C/C++ libraries that the library target depends upon.List of labelsoptional[]
srcsOther Zig source files required to build the target, e.g. files imported using @import.List of labelsoptional[]
dataFiles required by the target during runtime.List of labelsoptional[]
compiler_runtimeWhether to include Zig compiler runtime symbols in the generated output. The default behavior is to include them in executables and shared libraries.Stringoptional"default"
coptsC compiler flags required to build the C sources of the target. Subject to location expansion.List of stringsoptional[]
csrcsC source files required to build the target.List of labelsoptional[]
emit_asmEmit the assembly in the asm output group.BooleanoptionalFalse
emit_llvm_bcEmit the LLVM bytecode in the llvm_bc output group.BooleanoptionalFalse
emit_llvm_irEmit the LLVM IR in the llvm_ir output group.BooleanoptionalFalse
envAdditional environment variables to set when executed by bazel run or bazel test. Subject to location expansion.Dictionary: String -> Stringoptional{}
env_inheritEnvironment variables to inherit from external environment when executed by bazel test.List of stringsoptional[]
extra_docsOther files required to generate documentation, e.g. guides referenced using //!zig-autodoc-guide:.List of labelsoptional[]
extra_srcsOther files required to build the target, e.g. files embedded using @embedFile.List of labelsoptional[]
linker_scriptCustom linker script for the target.

Note, as of Zig version 0.15.1 linker-scripts require the LLVM/LLD backend to be enabled, see https://github.com/ziglang/zig/issues/25069. Set zigopts=["-fllvm", "-flld"] to that end.
LabeloptionalNone
linkoptsAdditional list of flags passed to the linker. Subject to location expansion.List of stringsoptional[]
mainThe main source file.

If not set, deps must contain exactly one Zig module dependency which will be used as the root module. Note that in that case, 'srcs', 'extra_srcs' and 'csrcs' must also be empty as they are taken from the root module instead.
LabeloptionalNone
strip_debug_symbolsWhether to force passing '-fstrip' to the zig compiler to remove debug symbols. Bazel's --strip flag is also supported globally.BooleanoptionalFalse
test_runnerOptional Zig file to specify a custom test runnerLabeloptionalNone
zigoptsAdditional list of flags passed to the zig compiler. Subject to location expansion.

This is an advanced feature that can conflict with attributes, build settings, and other flags defined by the toolchain itself. Use this at your own risk of hitting undefined behaviors.
List of stringsoptional[]