From dc83f612308d2448c3ec3440f9574e3e80b86119 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Tue, 17 Mar 2026 11:08:14 +0100 Subject: [PATCH 1/8] Make `zig_toolchain` take mandatory labels * zig_asm * Simplify zig_toolchain Require labels as cfg exec Symlinking Zig exe and libs for zig local toolchain Stop forwarding exe paths and use file everywhere Explicit dependency on zig.h Use dirname of zig.h file for lib paths * source directory * do not use .path for zig_lib * zig_exe_file -> zig_exe * no _single_file --- zig/private/BUILD.bazel | 2 - zig/private/common/translate_c.bzl | 4 +- zig/private/common/zig_build.bzl | 20 ++--- zig/private/common/zig_docs.bzl | 4 +- zig/private/common/zig_lib_dir.bzl | 2 +- zig/private/providers/zig_toolchain_info.bzl | 20 +---- zig/private/repo/BUILD.bazel | 20 ++++- zig/private/repo/local_zig_repository.bzl | 57 ++++++++++++ zig/private/repo/toolchain_build_file.bzl | 21 +++++ zig/private/repo/zig_repository.bzl | 31 ++----- zig/private/zig_toolchain.bzl | 92 ++++++-------------- zig/private/zig_toolchain_header.bzl | 12 +-- 12 files changed, 154 insertions(+), 131 deletions(-) create mode 100644 zig/private/repo/local_zig_repository.bzl create mode 100644 zig/private/repo/toolchain_build_file.bzl diff --git a/zig/private/BUILD.bazel b/zig/private/BUILD.bazel index 7310642c..b51cd1dc 100644 --- a/zig/private/BUILD.bazel +++ b/zig/private/BUILD.bazel @@ -98,8 +98,6 @@ bzl_library( visibility = ["//zig:__subpackages__"], deps = [ "//zig/private/providers:zig_toolchain_info", - "@aspect_bazel_lib//lib:paths", - "@bazel_skylib//lib:paths", ], ) diff --git a/zig/private/common/translate_c.bzl b/zig/private/common/translate_c.bzl index 08feceb9..1d852121 100644 --- a/zig/private/common/translate_c.bzl +++ b/zig/private/common/translate_c.bzl @@ -70,13 +70,13 @@ def zig_translate_c(*, ctx, name, canonical_name = None, zigtoolchaininfo, globa transitive = transitive_inputs, ), outputs = [zig_out], - arguments = [zigtoolchaininfo.zig_exe_path, "translate-c", global_args, args], + arguments = [zigtoolchaininfo.zig_exe, "translate-c", global_args, args], mnemonic = "ZigTranslateC", progress_message = "zig translate-c %{label}", execution_requirements = {tag: "" for tag in ctx.attr.tags}, env = { "ZIG_GLOBAL_CACHE_DIR": zigtoolchaininfo.zig_cache, - "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib_path, + "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib.path, "ZIG_LOCAL_CACHE_DIR": zigtoolchaininfo.zig_cache, }, tools = zigtoolchaininfo.zig_files, diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl index dc76803f..a48ddacb 100644 --- a/zig/private/common/zig_build.bzl +++ b/zig/private/common/zig_build.bzl @@ -450,11 +450,11 @@ def zig_build_impl(ctx, *, kind): zig_build_kwargs = dict( execution_requirements = {tag: "" for tag in ctx.attr.tags}, - tools = zigtoolchaininfo.zig_files, + tools = [zigtoolchaininfo.zig_exe, zigtoolchaininfo.zig_lib], toolchain = "//zig:toolchain_type", env = { "ZIG_GLOBAL_CACHE_DIR": zigtoolchaininfo.zig_cache, - "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib_path, + "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib.path, "ZIG_LOCAL_CACHE_DIR": zigtoolchaininfo.zig_cache, }, ) @@ -496,7 +496,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [static_lib] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", global_args, args], mnemonic = "ZigBuildLib", progress_message = "zig build-lib %{label}", @@ -533,7 +533,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [bin_output] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-exe", global_args, args], mnemonic = "ZigBuildExe", progress_message = "zig build-exe %{label}", @@ -552,7 +552,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [bc] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["test", "--test-no-exec", global_args, args, test_args], mnemonic = "ZigBuildTest", progress_message = "zig test %{label}", @@ -570,7 +570,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [static_lib], inputs = [bc], - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", global_args, lib_args], mnemonic = "ZigBuildLib", progress_message = "zig build-lib %{label}", @@ -607,7 +607,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [bin_output] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["test", "--test-no-exec", global_args, args], mnemonic = "ZigBuildTest", progress_message = "zig test %{label}", @@ -621,7 +621,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = ([bin_output] if ctx.attr.emit_bin else []) + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", global_args, args], mnemonic = "ZigBuildStaticLib", progress_message = "zig build-lib %{label}", @@ -645,7 +645,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [static_lib] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", global_args, args], mnemonic = "ZigBuildLib", progress_message = "zig build-lib %{label}", @@ -697,7 +697,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [bin_output] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", "-dynamic", global_args, args], mnemonic = "ZigBuildSharedLib", progress_message = "zig build-lib -dynamic %{label}", diff --git a/zig/private/common/zig_docs.bzl b/zig/private/common/zig_docs.bzl index 81b7a4fd..184f857c 100644 --- a/zig/private/common/zig_docs.bzl +++ b/zig/private/common/zig_docs.bzl @@ -184,8 +184,8 @@ def zig_docs_impl(ctx, *, kind): ctx.actions.run( outputs = outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, - tools = zigtoolchaininfo.zig_files, + executable = zigtoolchaininfo.zig_exe, + tools = [zigtoolchaininfo.zig_exe, zigtoolchaininfo.zig_lib], arguments = arguments, mnemonic = mnemonic, progress_message = progress_message, diff --git a/zig/private/common/zig_lib_dir.bzl b/zig/private/common/zig_lib_dir.bzl index 215adf30..5dadad50 100644 --- a/zig/private/common/zig_lib_dir.bzl +++ b/zig/private/common/zig_lib_dir.bzl @@ -10,4 +10,4 @@ def zig_lib_dir(*, zigtoolchaininfo, args): zigtoolchaininfo: ZigToolchainInfo. args: Args; mutable, Append the Zig cache flags to this object. """ - args.add_all(["--zig-lib-dir", zigtoolchaininfo.zig_lib_path]) + args.add_all(["--zig-lib-dir", zigtoolchaininfo.zig_lib]) diff --git a/zig/private/providers/zig_toolchain_info.bzl b/zig/private/providers/zig_toolchain_info.bzl index 6ed9f024..50513c52 100644 --- a/zig/private/providers/zig_toolchain_info.bzl +++ b/zig/private/providers/zig_toolchain_info.bzl @@ -5,23 +5,9 @@ Information about how to invoke the Zig executable. """ FIELDS = { - "zig_exe_path": "Path to the Zig executable for the target platform.", - "zig_exe_rpath": """\ -Rlocation path to the Zig executable for the target platform. - -May be absolute if the zig_exe_path points to a locally installed Zig executable. -""", - "zig_lib_path": "Path to the Zig library directory for the target platform.", - "zig_lib_rpath": """\ -Rlocation path to the Zig library directory for the target platform. - -May be absolute if the zig_exe_path points to a locally installed Zig executable. -""", - "zig_files": """\ -Files required in runfiles to make the Zig executable available. - -May be empty if the zig_exe_path points to a locally installed Zig executable. -""", + "zig_exe": "File for the Zig executable.", + "zig_h": "File for the Zig header at the root of the Zig lib directory.", + "zig_lib": "File for the Zig lib source directory.", "zig_version": "String, The Zig toolchain's version.", "zig_cache": "String, The Zig cache directory prefix used for the global and local cache.", } diff --git a/zig/private/repo/BUILD.bazel b/zig/private/repo/BUILD.bazel index 1cf5d5af..f342de29 100644 --- a/zig/private/repo/BUILD.bazel +++ b/zig/private/repo/BUILD.bazel @@ -5,12 +5,28 @@ bzl_library( srcs = ["zig_repository.bzl"], visibility = ["//zig:__subpackages__"], deps = [ + ":toolchain_build_file", "//zig/private/common:zig_cache", - "@bazel_skylib//lib:paths", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], ) +bzl_library( + name = "local_zig_repository", + srcs = ["local_zig_repository.bzl"], + visibility = ["//zig:__subpackages__"], + deps = [ + ":toolchain_build_file", + "//zig/private/common:zig_cache", + ], +) + +bzl_library( + name = "toolchain_build_file", + srcs = ["toolchain_build_file.bzl"], + visibility = ["//zig:__subpackages__"], +) + bzl_library( name = "toolchains_repo", srcs = ["toolchains_repo.bzl"], @@ -26,6 +42,8 @@ filegroup( name = "all_files", srcs = [ ":BUILD.bazel", + ":local_zig_repository.bzl", + ":toolchain_build_file.bzl", ":toolchains_repo.bzl", ":zig_repository.bzl", ], diff --git a/zig/private/repo/local_zig_repository.bzl b/zig/private/repo/local_zig_repository.bzl new file mode 100644 index 00000000..544af29a --- /dev/null +++ b/zig/private/repo/local_zig_repository.bzl @@ -0,0 +1,57 @@ +"""Implementation of a repository rule for a host-local Zig toolchain.""" + +load( + "//zig/private/common:zig_cache.bzl", + "VAR_CACHE_PREFIX", + "VAR_CACHE_PREFIX_LINUX", + "VAR_CACHE_PREFIX_MACOS", + "VAR_CACHE_PREFIX_WINDOWS", + "env_zig_cache_prefix", +) +load("//zig/private/repo:toolchain_build_file.bzl", "render_toolchain_build") + +DOC = "Expose a host-local Zig toolchain via a synthetic repository." + +ATTRS = { + "zig_exe_path": attr.string(mandatory = True, doc = "Absolute path to the Zig executable."), + "zig_lib_path": attr.string(mandatory = True, doc = "Absolute path to the Zig lib directory."), + "zig_version": attr.string(mandatory = True, doc = "The Zig SDK version number."), + "platform": attr.string(mandatory = True, doc = "The execution platform for this Zig SDK, e.g. `x86_64-linux`."), + "translate_c": attr.label(doc = "The translate-c label.", mandatory = False), +} + +ENV = [ + VAR_CACHE_PREFIX, + VAR_CACHE_PREFIX_LINUX, + VAR_CACHE_PREFIX_MACOS, + VAR_CACHE_PREFIX_WINDOWS, +] + +def _zig_exe_name(*, platform): + return "zig.exe" if platform.find("windows") != -1 else "zig" + +def _local_zig_repository_impl(repository_ctx): + cache_prefix = env_zig_cache_prefix( + repository_ctx.os.environ, + repository_ctx.attr.platform, + ) + + zig_exe = _zig_exe_name(platform = repository_ctx.attr.platform) + repository_ctx.symlink(repository_ctx.path(repository_ctx.attr.zig_exe_path), zig_exe) + repository_ctx.symlink(repository_ctx.path(repository_ctx.attr.zig_lib_path), "lib") + repository_ctx.file( + "BUILD.bazel", + render_toolchain_build( + zig_cache = cache_prefix, + zig_exe = zig_exe, + zig_version = repository_ctx.attr.zig_version, + translate_c = repository_ctx.attr.translate_c, + ), + ) + +local_zig_repository = repository_rule( + implementation = _local_zig_repository_impl, + attrs = ATTRS, + doc = DOC, + environ = ENV, +) diff --git a/zig/private/repo/toolchain_build_file.bzl b/zig/private/repo/toolchain_build_file.bzl new file mode 100644 index 00000000..ef9f3b4a --- /dev/null +++ b/zig/private/repo/toolchain_build_file.bzl @@ -0,0 +1,21 @@ +"""Helpers to render BUILD files for Zig toolchain repositories.""" + +def render_toolchain_build(*, zig_exe, zig_version, zig_cache, translate_c): + return """\ +# Generated by zig/private/repo/toolchain_build_file.bzl + +load("@rules_zig//zig:toolchain.bzl", "zig_toolchain") + +zig_toolchain( + name = "zig_toolchain", + zig_exe = {zig_exe}, + zig_h = "lib/zig.h", + zig_lib = "lib", + zig_version = {zig_version}, + zig_cache = {zig_cache}, +) +""".format( + zig_cache = repr(zig_cache), + zig_exe = repr(zig_exe), + zig_version = repr(zig_version), + ) diff --git a/zig/private/repo/zig_repository.bzl b/zig/private/repo/zig_repository.bzl index 51bb2cc2..00df3e0b 100644 --- a/zig/private/repo/zig_repository.bzl +++ b/zig/private/repo/zig_repository.bzl @@ -9,6 +9,7 @@ load( "VAR_CACHE_PREFIX_WINDOWS", "env_zig_cache_prefix", ) +load("//zig/private/repo:toolchain_build_file.bzl", "render_toolchain_build") DOC = "Fetch and install a Zig toolchain." @@ -77,31 +78,17 @@ def _zig_repository_impl(repository_ctx): repository_ctx.attr.platform, ) - zig_exe = "zig" - if repository_ctx.attr.platform.find("windows") != -1: - zig_exe = "zig.exe" + zig_exe = "zig.exe" if repository_ctx.attr.platform.find("windows") != -1 else "zig" - build_content = """\ -# Generated by zig/private/repo/zig_repository.bzl - -load("@rules_zig//zig:toolchain.bzl", "zig_toolchain") - -zig_toolchain( - name = "zig_toolchain", - zig_exe = {zig_exe}, - zig_lib = glob(["lib/**"]), - zig_lib_path = "lib", - zig_version = {zig_version}, - zig_cache = {zig_cache}, -) -""".format( - zig_cache = repr(cache_prefix), - zig_exe = repr(zig_exe), - zig_version = repr(repository_ctx.attr.zig_version), + repository_ctx.file( + "BUILD.bazel", + render_toolchain_build( + zig_cache = cache_prefix, + zig_exe = zig_exe, + zig_version = repository_ctx.attr.zig_version, + ), ) - repository_ctx.file("BUILD.bazel", build_content) - download_args = { "url": _mirrors_urls( url = repository_ctx.attr.url, diff --git a/zig/private/zig_toolchain.bzl b/zig/private/zig_toolchain.bzl index e8ce9564..18d87e47 100644 --- a/zig/private/zig_toolchain.bzl +++ b/zig/private/zig_toolchain.bzl @@ -1,7 +1,5 @@ """Implementation of the zig_toolchain rule.""" -load("@aspect_bazel_lib//lib:paths.bzl", "to_rlocation_path") -load("@bazel_skylib//lib:paths.bzl", "paths") load("//zig/private/providers:zig_toolchain_info.bzl", "ZigToolchainInfo") DOC = """\ @@ -32,21 +30,20 @@ See https://bazel.build/extending/toolchains#defining-toolchains. ATTRS = { "zig_exe": attr.label( doc = "A hermetically downloaded Zig executable for the target platform.", - mandatory = False, + mandatory = True, allow_single_file = True, + executable = True, + cfg = "exec", ), - "zig_exe_path": attr.string( - doc = "Path to an existing Zig executable for the target platform.", - mandatory = False, - ), - "zig_lib": attr.label_list( - doc = "Files of a hermetically downloaded Zig library for the target platform.", - mandatory = False, - allow_files = True, + "zig_h": attr.label( + doc = "The Zig header at the root of the Zig library directory.", + mandatory = True, + allow_single_file = True, ), - "zig_lib_path": attr.string( - doc = "Absolute path to an existing Zig library for the target platform or a the path to a hermetically downloaded Zig library relative to the Zig executable.", - mandatory = False, + "zig_lib": attr.label( + doc = "A source directory containing the hermetic Zig library for the target platform.", + mandatory = True, + allow_single_file = True, ), "zig_version": attr.string( doc = "The Zig toolchain's version.", @@ -58,19 +55,14 @@ ATTRS = { ), } -# Avoid using non-normalized paths (workspace/../other_workspace/path) -def _to_manifest_path(ctx, file): - if file.short_path.startswith("../"): - return "external/" + file.short_path[3:] - else: - return ctx.workspace_name + "/" + file.short_path - -def _validate_zig_version(ctx, *, zig_exe_path, zig_files, zig_version): +def _validate_zig_version(ctx, *, zig_exe, zig_lib, zig_version): output = ctx.actions.declare_file(ctx.label.name + ".version_validation") + args = ctx.actions.args() + args.add_all([zig_exe, zig_version, output]) ctx.actions.run_shell( outputs = [output], - tools = zig_files, - arguments = [zig_exe_path, zig_version, output.path], + tools = [zig_exe, zig_lib], + arguments = [args], command = "\n".join([ 'actual_version="$($1 version)"', "if [[ $actual_version != $2 ]]; then", @@ -85,67 +77,39 @@ def _validate_zig_version(ctx, *, zig_exe_path, zig_files, zig_version): return output def _zig_toolchain_impl(ctx): - if ctx.attr.zig_exe and ctx.attr.zig_exe_path: - fail("Can only set one of zig_exe or zig_exe_path but both were set.") - if not ctx.attr.zig_exe and not ctx.attr.zig_exe_path: - fail("Must set one of zig_exe or zig_exe_path.") - - if ctx.attr.zig_exe and not ctx.attr.zig_lib: - fail("Must set zig_lib if zig_exe is set.") - if not ctx.attr.zig_exe and ctx.attr.zig_lib: - fail("Can only set zig_lib if zig_exe is set.") - - if not ctx.attr.zig_lib_path: - fail("Must set one of zig_lib or zig_lib_path.") - if ctx.attr.zig_lib and paths.is_absolute(ctx.attr.zig_lib_path): - fail("zig_lib_path must be relative if zig_lib is set.") - elif not ctx.attr.zig_lib and not paths.is_absolute(ctx.attr.zig_lib_path): - fail("zig_lib_path must be absolute if zig_lib is not set.") - - zig_files = [] - zig_exe_path = ctx.attr.zig_exe_path - zig_exe_rpath = zig_exe_path - zig_lib_path = ctx.attr.zig_lib_path - zig_lib_rpath = zig_lib_path + zig_exe = ctx.executable.zig_exe + zig_h = ctx.file.zig_h + zig_lib = ctx.file.zig_lib zig_version = ctx.attr.zig_version zig_cache = ctx.attr.zig_cache - if ctx.attr.zig_exe: - zig_files = ctx.attr.zig_exe.files.to_list() + ctx.files.zig_lib - zig_exe_path = _to_manifest_path(ctx, zig_files[0]) - zig_exe_rpath = to_rlocation_path(ctx, zig_files[0]) - zig_lib_path = paths.join(paths.dirname(zig_exe_path), ctx.attr.zig_lib_path) - zig_lib_rpath = paths.join(paths.dirname(zig_exe_rpath), ctx.attr.zig_lib_path) - validation = _validate_zig_version( ctx, - zig_exe_path = zig_exe_path, - zig_files = zig_files, + zig_exe = zig_exe, + zig_lib = zig_lib, zig_version = zig_version, ) # Validation actions of transitive dependencies do not seem to be picked up # by Bazel. So, we need to make the validation output an input of Zig SDK # using actions to ensure that it takes place. - zig_files.append(validation) + tool_files = [zig_exe, zig_lib, validation] # Make the $(tool_BIN) variable available in places like genrules. # See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables template_variables = platform_common.TemplateVariableInfo({ - "ZIG_BIN": zig_exe_path, + "ZIG_BIN": zig_exe.path, }) default = DefaultInfo( - files = depset(direct = zig_files), - runfiles = ctx.runfiles(files = zig_files), + files = depset(direct = tool_files), + runfiles = ctx.runfiles(files = tool_files), ) zigtoolchaininfo = ZigToolchainInfo( - zig_exe_path = zig_exe_path, - zig_exe_rpath = zig_exe_rpath, - zig_lib_path = zig_lib_path, - zig_lib_rpath = zig_lib_rpath, - zig_files = zig_files, + zig_exe = zig_exe, + zig_h = zig_h, + zig_lib = zig_lib, zig_version = zig_version, zig_cache = zig_cache, ) diff --git a/zig/private/zig_toolchain_header.bzl b/zig/private/zig_toolchain_header.bzl index 7b604c35..1c79ee1c 100644 --- a/zig/private/zig_toolchain_header.bzl +++ b/zig/private/zig_toolchain_header.bzl @@ -81,14 +81,6 @@ def _zig_toolchain_header_impl(ctx): zigtoolchaininfo = ctx.toolchains["//zig:toolchain_type"].zigtoolchaininfo zigtargetinfo = ctx.toolchains["//zig/target:toolchain_type"].zigtargetinfo - header_files = zigtoolchaininfo.zig_files - header_path = zigtoolchaininfo.zig_lib_path - for file in zigtoolchaininfo.zig_files: - if file.basename == "zig.h": - header_files = [file] - header_path = file.dirname - break - alignment = max_int_alignment(zigtargetinfo.triple.arch) defines = ["ZIG_TARGET_MAX_INT_ALIGNMENT={}".format(alignment)] if zigtargetinfo.triple.abi == "msvc": @@ -96,8 +88,8 @@ def _zig_toolchain_header_impl(ctx): cc_info = CcInfo( compilation_context = cc_common.create_compilation_context( - headers = depset(direct = header_files), - includes = depset(direct = [header_path]), + headers = depset(direct = [zigtoolchaininfo.zig_h]), + includes = depset(direct = [zigtoolchaininfo.zig_lib.path]), defines = depset(direct = defines), ), ) From c6061d4f41c3e9c30b07774bd122d44a741e62c2 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:13:15 +0000 Subject: [PATCH 2/8] from_source --- zig/private/common/translate_c.bzl | 4 +- zig/private/repo/BUILD.bazel | 11 ----- zig/private/repo/local_zig_repository.bzl | 57 ----------------------- zig/private/repo/toolchain_build_file.bzl | 2 +- zig/private/zig_toolchain.bzl | 14 ++++++ zig/settings/BUILD.bazel | 18 +++++++ 6 files changed, 35 insertions(+), 71 deletions(-) delete mode 100644 zig/private/repo/local_zig_repository.bzl diff --git a/zig/private/common/translate_c.bzl b/zig/private/common/translate_c.bzl index 1d852121..be836247 100644 --- a/zig/private/common/translate_c.bzl +++ b/zig/private/common/translate_c.bzl @@ -70,7 +70,7 @@ def zig_translate_c(*, ctx, name, canonical_name = None, zigtoolchaininfo, globa transitive = transitive_inputs, ), outputs = [zig_out], - arguments = [zigtoolchaininfo.zig_exe, "translate-c", global_args, args], + arguments = [zigtoolchaininfo.zig_exe.path, "translate-c", global_args, args], mnemonic = "ZigTranslateC", progress_message = "zig translate-c %{label}", execution_requirements = {tag: "" for tag in ctx.attr.tags}, @@ -79,7 +79,7 @@ def zig_translate_c(*, ctx, name, canonical_name = None, zigtoolchaininfo, globa "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib.path, "ZIG_LOCAL_CACHE_DIR": zigtoolchaininfo.zig_cache, }, - tools = zigtoolchaininfo.zig_files, + tools = [zigtoolchaininfo.zig_exe, zigtoolchaininfo.zig_lib], toolchain = "//zig:toolchain_type", ) diff --git a/zig/private/repo/BUILD.bazel b/zig/private/repo/BUILD.bazel index f342de29..664a9858 100644 --- a/zig/private/repo/BUILD.bazel +++ b/zig/private/repo/BUILD.bazel @@ -11,16 +11,6 @@ bzl_library( ], ) -bzl_library( - name = "local_zig_repository", - srcs = ["local_zig_repository.bzl"], - visibility = ["//zig:__subpackages__"], - deps = [ - ":toolchain_build_file", - "//zig/private/common:zig_cache", - ], -) - bzl_library( name = "toolchain_build_file", srcs = ["toolchain_build_file.bzl"], @@ -42,7 +32,6 @@ filegroup( name = "all_files", srcs = [ ":BUILD.bazel", - ":local_zig_repository.bzl", ":toolchain_build_file.bzl", ":toolchains_repo.bzl", ":zig_repository.bzl", diff --git a/zig/private/repo/local_zig_repository.bzl b/zig/private/repo/local_zig_repository.bzl deleted file mode 100644 index 544af29a..00000000 --- a/zig/private/repo/local_zig_repository.bzl +++ /dev/null @@ -1,57 +0,0 @@ -"""Implementation of a repository rule for a host-local Zig toolchain.""" - -load( - "//zig/private/common:zig_cache.bzl", - "VAR_CACHE_PREFIX", - "VAR_CACHE_PREFIX_LINUX", - "VAR_CACHE_PREFIX_MACOS", - "VAR_CACHE_PREFIX_WINDOWS", - "env_zig_cache_prefix", -) -load("//zig/private/repo:toolchain_build_file.bzl", "render_toolchain_build") - -DOC = "Expose a host-local Zig toolchain via a synthetic repository." - -ATTRS = { - "zig_exe_path": attr.string(mandatory = True, doc = "Absolute path to the Zig executable."), - "zig_lib_path": attr.string(mandatory = True, doc = "Absolute path to the Zig lib directory."), - "zig_version": attr.string(mandatory = True, doc = "The Zig SDK version number."), - "platform": attr.string(mandatory = True, doc = "The execution platform for this Zig SDK, e.g. `x86_64-linux`."), - "translate_c": attr.label(doc = "The translate-c label.", mandatory = False), -} - -ENV = [ - VAR_CACHE_PREFIX, - VAR_CACHE_PREFIX_LINUX, - VAR_CACHE_PREFIX_MACOS, - VAR_CACHE_PREFIX_WINDOWS, -] - -def _zig_exe_name(*, platform): - return "zig.exe" if platform.find("windows") != -1 else "zig" - -def _local_zig_repository_impl(repository_ctx): - cache_prefix = env_zig_cache_prefix( - repository_ctx.os.environ, - repository_ctx.attr.platform, - ) - - zig_exe = _zig_exe_name(platform = repository_ctx.attr.platform) - repository_ctx.symlink(repository_ctx.path(repository_ctx.attr.zig_exe_path), zig_exe) - repository_ctx.symlink(repository_ctx.path(repository_ctx.attr.zig_lib_path), "lib") - repository_ctx.file( - "BUILD.bazel", - render_toolchain_build( - zig_cache = cache_prefix, - zig_exe = zig_exe, - zig_version = repository_ctx.attr.zig_version, - translate_c = repository_ctx.attr.translate_c, - ), - ) - -local_zig_repository = repository_rule( - implementation = _local_zig_repository_impl, - attrs = ATTRS, - doc = DOC, - environ = ENV, -) diff --git a/zig/private/repo/toolchain_build_file.bzl b/zig/private/repo/toolchain_build_file.bzl index ef9f3b4a..2fa8108f 100644 --- a/zig/private/repo/toolchain_build_file.bzl +++ b/zig/private/repo/toolchain_build_file.bzl @@ -1,6 +1,6 @@ """Helpers to render BUILD files for Zig toolchain repositories.""" -def render_toolchain_build(*, zig_exe, zig_version, zig_cache, translate_c): +def render_toolchain_build(*, zig_exe, zig_version, zig_cache): return """\ # Generated by zig/private/repo/toolchain_build_file.bzl diff --git a/zig/private/zig_toolchain.bzl b/zig/private/zig_toolchain.bzl index 18d87e47..0f7e4717 100644 --- a/zig/private/zig_toolchain.bzl +++ b/zig/private/zig_toolchain.bzl @@ -2,6 +2,19 @@ load("//zig/private/providers:zig_toolchain_info.bzl", "ZigToolchainInfo") +def _zig_built_from_source_transition_impl(_, __): + return { + "//zig/settings:built_from_source": False, + } + +_zig_built_from_source_transition = transition( + implementation = _zig_built_from_source_transition_impl, + inputs = [], + outputs = [ + "//zig/settings:built_from_source", + ], +) + DOC = """\ Defines a Zig compiler toolchain. @@ -131,6 +144,7 @@ def _zig_toolchain_impl(ctx): zig_toolchain = rule( implementation = _zig_toolchain_impl, + cfg = _zig_built_from_source_transition, attrs = ATTRS, doc = DOC, ) diff --git a/zig/settings/BUILD.bazel b/zig/settings/BUILD.bazel index e0f8b400..c8e4cad7 100644 --- a/zig/settings/BUILD.bazel +++ b/zig/settings/BUILD.bazel @@ -77,3 +77,21 @@ filegroup( srcs = [":BUILD.bazel"], visibility = ["//zig:__pkg__"], ) + +bool_flag( + name = "built_from_source", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "built_from_source_enabled", + flag_values = {":built_from_source": "true"}, + visibility = ["//visibility:public"], +) + +config_setting( + name = "built_from_source_disabled", + flag_values = {":built_from_source": "false"}, + visibility = ["//visibility:public"], +) From 98614f802d85a579c781bc8756b3222ef1c783de Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:33:06 +0000 Subject: [PATCH 3/8] bootstrap --- zig/private/zig_toolchain.bzl | 12 ++++++------ zig/settings/BUILD.bazel | 24 ++++++------------------ 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/zig/private/zig_toolchain.bzl b/zig/private/zig_toolchain.bzl index 0f7e4717..1b6ce408 100644 --- a/zig/private/zig_toolchain.bzl +++ b/zig/private/zig_toolchain.bzl @@ -2,16 +2,16 @@ load("//zig/private/providers:zig_toolchain_info.bzl", "ZigToolchainInfo") -def _zig_built_from_source_transition_impl(_, __): +def _zig_bootstrap_transition_impl(_, __): return { - "//zig/settings:built_from_source": False, + "//zig/settings:bootstrapped": False, } -_zig_built_from_source_transition = transition( - implementation = _zig_built_from_source_transition_impl, +_zig_bootstrap_transition = transition( + implementation = _zig_bootstrap_transition_impl, inputs = [], outputs = [ - "//zig/settings:built_from_source", + "//zig/settings:bootstrapped", ], ) @@ -144,7 +144,7 @@ def _zig_toolchain_impl(ctx): zig_toolchain = rule( implementation = _zig_toolchain_impl, - cfg = _zig_built_from_source_transition, + cfg = _zig_bootstrap_transition, attrs = ATTRS, doc = DOC, ) diff --git a/zig/settings/BUILD.bazel b/zig/settings/BUILD.bazel index c8e4cad7..1c1c9b0a 100644 --- a/zig/settings/BUILD.bazel +++ b/zig/settings/BUILD.bazel @@ -71,27 +71,15 @@ repeatable_string_flag( visibility = ["//visibility:private"], ) +bool_flag( + name = "bootstrapped", + build_setting_default = False, + visibility = ["//zig/config/bootstrapped:__pkg__"], +) + # Execute `bazel run //util:update_filegroups` to update this target. filegroup( name = "all_files", srcs = [":BUILD.bazel"], visibility = ["//zig:__pkg__"], ) - -bool_flag( - name = "built_from_source", - build_setting_default = False, - visibility = ["//visibility:public"], -) - -config_setting( - name = "built_from_source_enabled", - flag_values = {":built_from_source": "true"}, - visibility = ["//visibility:public"], -) - -config_setting( - name = "built_from_source_disabled", - flag_values = {":built_from_source": "false"}, - visibility = ["//visibility:public"], -) From cb407fd01a78ed0f319b1a755886c8cc498860f5 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:34:20 +0000 Subject: [PATCH 4/8] fix --- docs/toolchains.md | 9 ++++----- zig/config/BUILD.bazel | 1 + zig/private/repo/BUILD.bazel | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/toolchains.md b/docs/toolchains.md index 60e21ad6..d5d14749 100644 --- a/docs/toolchains.md +++ b/docs/toolchains.md @@ -69,7 +69,7 @@ toolchain(
 load("@rules_zig//zig:toolchain.bzl", "zig_toolchain")
 
-zig_toolchain(name, zig_cache, zig_exe, zig_exe_path, zig_lib, zig_lib_path, zig_version)
+zig_toolchain(name, zig_cache, zig_exe, zig_h, zig_lib, zig_version)
 
Defines a Zig compiler toolchain. @@ -102,10 +102,9 @@ See https://bazel.build/extending/toolchains#defining-toolchains. | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | zig_cache | The Zig cache directory prefix. Used for both the global and local cache. | String | required | | -| zig_exe | A hermetically downloaded Zig executable for the target platform. | Label | optional | `None` | -| zig_exe_path | Path to an existing Zig executable for the target platform. | String | optional | `""` | -| zig_lib | Files of a hermetically downloaded Zig library for the target platform. | List of labels | optional | `[]` | -| zig_lib_path | Absolute path to an existing Zig library for the target platform or a the path to a hermetically downloaded Zig library relative to the Zig executable. | String | optional | `""` | +| zig_exe | A hermetically downloaded Zig executable for the target platform. | Label | required | | +| zig_h | The Zig header at the root of the Zig library directory. | Label | required | | +| zig_lib | A source directory containing the hermetic Zig library for the target platform. | Label | required | | | zig_version | The Zig toolchain's version. | String | required | | diff --git a/zig/config/BUILD.bazel b/zig/config/BUILD.bazel index a694dccb..cd085dcb 100644 --- a/zig/config/BUILD.bazel +++ b/zig/config/BUILD.bazel @@ -3,6 +3,7 @@ filegroup( name = "all_files", srcs = [ ":BUILD.bazel", + "//zig/config/bootstrapped:all_files", "//zig/config/mode:all_files", "//zig/config/threaded:all_files", ], diff --git a/zig/private/repo/BUILD.bazel b/zig/private/repo/BUILD.bazel index 664a9858..24765799 100644 --- a/zig/private/repo/BUILD.bazel +++ b/zig/private/repo/BUILD.bazel @@ -11,12 +11,6 @@ bzl_library( ], ) -bzl_library( - name = "toolchain_build_file", - srcs = ["toolchain_build_file.bzl"], - visibility = ["//zig:__subpackages__"], -) - bzl_library( name = "toolchains_repo", srcs = ["toolchains_repo.bzl"], @@ -27,6 +21,12 @@ bzl_library( ], ) +bzl_library( + name = "toolchain_build_file", + srcs = ["toolchain_build_file.bzl"], + visibility = ["//zig:__subpackages__"], +) + # Execute `bazel run //util:update_filegroups` to update this target. filegroup( name = "all_files", From c419ff31fb0464023a1eb2e2675b688abc131a09 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:34:29 +0000 Subject: [PATCH 5/8] bootstrap --- zig/config/bootstrapped/BUILD.bazel | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 zig/config/bootstrapped/BUILD.bazel diff --git a/zig/config/bootstrapped/BUILD.bazel b/zig/config/bootstrapped/BUILD.bazel new file mode 100644 index 00000000..e72a8c0b --- /dev/null +++ b/zig/config/bootstrapped/BUILD.bazel @@ -0,0 +1,20 @@ +config_setting( + name = "bootstrap_enabled", + flag_values = { + "//zig/settings:bootstrapped": "true", + }, +) + +config_setting( + name = "bootstrap_disabled", + flag_values = { + "//zig/settings:bootstrapped": "false", + }, +) + +# Execute `bazel run //util:update_filegroups` to update this target. +filegroup( + name = "all_files", + srcs = [":BUILD.bazel"], + visibility = ["//zig/config:__pkg__"], +) From ba910e90e7b65c901626ad6eb037b8c4396be3f3 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:42:21 +0000 Subject: [PATCH 6/8] no render --- zig/private/repo/toolchain_build_file.bzl | 21 ------------------ zig/private/repo/zig_repository.bzl | 26 +++++++++++++++++------ 2 files changed, 19 insertions(+), 28 deletions(-) delete mode 100644 zig/private/repo/toolchain_build_file.bzl diff --git a/zig/private/repo/toolchain_build_file.bzl b/zig/private/repo/toolchain_build_file.bzl deleted file mode 100644 index 2fa8108f..00000000 --- a/zig/private/repo/toolchain_build_file.bzl +++ /dev/null @@ -1,21 +0,0 @@ -"""Helpers to render BUILD files for Zig toolchain repositories.""" - -def render_toolchain_build(*, zig_exe, zig_version, zig_cache): - return """\ -# Generated by zig/private/repo/toolchain_build_file.bzl - -load("@rules_zig//zig:toolchain.bzl", "zig_toolchain") - -zig_toolchain( - name = "zig_toolchain", - zig_exe = {zig_exe}, - zig_h = "lib/zig.h", - zig_lib = "lib", - zig_version = {zig_version}, - zig_cache = {zig_cache}, -) -""".format( - zig_cache = repr(zig_cache), - zig_exe = repr(zig_exe), - zig_version = repr(zig_version), - ) diff --git a/zig/private/repo/zig_repository.bzl b/zig/private/repo/zig_repository.bzl index 00df3e0b..5df9dc6b 100644 --- a/zig/private/repo/zig_repository.bzl +++ b/zig/private/repo/zig_repository.bzl @@ -80,15 +80,27 @@ def _zig_repository_impl(repository_ctx): zig_exe = "zig.exe" if repository_ctx.attr.platform.find("windows") != -1 else "zig" - repository_ctx.file( - "BUILD.bazel", - render_toolchain_build( - zig_cache = cache_prefix, - zig_exe = zig_exe, - zig_version = repository_ctx.attr.zig_version, - ), + build_content = """\ +# Generated by zig/private/repo/zig_repository.bzl + +load("@rules_zig//zig:toolchain.bzl", "zig_toolchain") + +zig_toolchain( + name = "zig_toolchain", + zig_exe = {zig_exe}, + zig_h = "lib/zig.h", + zig_lib = "lib", + zig_version = {zig_version}, + zig_cache = {zig_cache}, +) +""".format( + zig_cache = repr(cache_prefix), + zig_exe = repr(zig_exe), + zig_version = repr(repository_ctx.attr.zig_version), ) + repository_ctx.file("BUILD.bazel", build_content) + download_args = { "url": _mirrors_urls( url = repository_ctx.attr.url, From fcc502c6b5862e786b5ca58ae4f6a39096f958a0 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Thu, 7 May 2026 13:45:15 +0000 Subject: [PATCH 7/8] fix --- zig/private/repo/BUILD.bazel | 8 -------- zig/private/repo/zig_repository.bzl | 1 - 2 files changed, 9 deletions(-) diff --git a/zig/private/repo/BUILD.bazel b/zig/private/repo/BUILD.bazel index 24765799..59521193 100644 --- a/zig/private/repo/BUILD.bazel +++ b/zig/private/repo/BUILD.bazel @@ -5,7 +5,6 @@ bzl_library( srcs = ["zig_repository.bzl"], visibility = ["//zig:__subpackages__"], deps = [ - ":toolchain_build_file", "//zig/private/common:zig_cache", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], @@ -21,18 +20,11 @@ bzl_library( ], ) -bzl_library( - name = "toolchain_build_file", - srcs = ["toolchain_build_file.bzl"], - visibility = ["//zig:__subpackages__"], -) - # Execute `bazel run //util:update_filegroups` to update this target. filegroup( name = "all_files", srcs = [ ":BUILD.bazel", - ":toolchain_build_file.bzl", ":toolchains_repo.bzl", ":zig_repository.bzl", ], diff --git a/zig/private/repo/zig_repository.bzl b/zig/private/repo/zig_repository.bzl index 5df9dc6b..d9c63f30 100644 --- a/zig/private/repo/zig_repository.bzl +++ b/zig/private/repo/zig_repository.bzl @@ -9,7 +9,6 @@ load( "VAR_CACHE_PREFIX_WINDOWS", "env_zig_cache_prefix", ) -load("//zig/private/repo:toolchain_build_file.bzl", "render_toolchain_build") DOC = "Fetch and install a Zig toolchain." From 93bd2c67bcede529399ea01817290fd789024d0f Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Sat, 9 May 2026 20:04:52 +0000 Subject: [PATCH 8/8] fixup --- zig/private/common/translate_c.bzl | 4 ++-- zig/private/common/zig_build.bzl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zig/private/common/translate_c.bzl b/zig/private/common/translate_c.bzl index 749618d2..d6333d93 100644 --- a/zig/private/common/translate_c.bzl +++ b/zig/private/common/translate_c.bzl @@ -265,10 +265,10 @@ def _external_translate_c(*, ctx, zigtoolchaininfo, translatectoolchaininfo, com execution_requirements = {tag: "" for tag in ctx.attr.tags}, env = { "ZIG_GLOBAL_CACHE_DIR": zigtoolchaininfo.zig_cache, - "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib_path, + "ZIG_LIB_DIR": zigtoolchaininfo.zig_lib.path, "ZIG_LOCAL_CACHE_DIR": zigtoolchaininfo.zig_cache, }, - tools = [translatectoolchaininfo.files_to_run] + zigtoolchaininfo.zig_files, + tools = [translatectoolchaininfo.files_to_run], toolchain = "//zig:toolchain_type", **actions_run_extra_kwargs ) diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl index 44473365..b07660de 100644 --- a/zig/private/common/zig_build.bzl +++ b/zig/private/common/zig_build.bzl @@ -564,7 +564,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [test_obj] + auxiliary_outputs, inputs = inputs, - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["test-obj", "--test-no-exec", global_args, args, test_args], mnemonic = "ZigBuildTest", progress_message = "zig test-obj %{label}",