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 0c635c2a..1647d643 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", "//zig/config/use_standalone_translate_c:all_files", 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__"], +) diff --git a/zig/private/BUILD.bazel b/zig/private/BUILD.bazel index 6fbfc971..ae0379cb 100644 --- a/zig/private/BUILD.bazel +++ b/zig/private/BUILD.bazel @@ -102,8 +102,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 260b22bc..ccb6abca 100644 --- a/zig/private/common/translate_c.bzl +++ b/zig/private/common/translate_c.bzl @@ -120,16 +120,16 @@ def _builtin_translate_c(*, ctx, zigtoolchaininfo, global_args, compilation_cont transitive = transitive_inputs, ), outputs = [zig_out], - arguments = [zigtoolchaininfo.zig_exe_path, "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}, 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, + tools = [zigtoolchaininfo.zig_exe, zigtoolchaininfo.zig_lib], toolchain = "//zig:toolchain_type", ) diff --git a/zig/private/common/zig_build.bzl b/zig/private/common/zig_build.bzl index cf4b42e3..b07660de 100644 --- a/zig/private/common/zig_build.bzl +++ b/zig/private/common/zig_build.bzl @@ -466,11 +466,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, }, ) @@ -511,7 +511,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}", @@ -548,7 +548,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}", @@ -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}", @@ -583,7 +583,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}", @@ -602,7 +602,7 @@ def zig_build_impl(ctx, *, kind): ctx.actions.run( outputs = [static_lib], inputs = [test_artifact], - executable = zigtoolchaininfo.zig_exe_path, + executable = zigtoolchaininfo.zig_exe, arguments = ["build-lib", global_args, lib_args], mnemonic = "ZigBuildLib", progress_message = "zig build-lib %{label}", @@ -639,7 +639,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}", @@ -653,7 +653,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}", @@ -677,7 +677,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}", @@ -729,7 +729,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 0437c6e9..74feb665 100644 --- a/zig/private/common/zig_docs.bzl +++ b/zig/private/common/zig_docs.bzl @@ -187,8 +187,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..59521193 100644 --- a/zig/private/repo/BUILD.bazel +++ b/zig/private/repo/BUILD.bazel @@ -6,7 +6,6 @@ bzl_library( visibility = ["//zig:__subpackages__"], deps = [ "//zig/private/common:zig_cache", - "@bazel_skylib//lib:paths", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], ) diff --git a/zig/private/repo/zig_repository.bzl b/zig/private/repo/zig_repository.bzl index 51bb2cc2..d9c63f30 100644 --- a/zig/private/repo/zig_repository.bzl +++ b/zig/private/repo/zig_repository.bzl @@ -77,9 +77,7 @@ 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 @@ -89,8 +87,8 @@ 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_h = "lib/zig.h", + zig_lib = "lib", zig_version = {zig_version}, zig_cache = {zig_cache}, ) diff --git a/zig/private/zig_toolchain.bzl b/zig/private/zig_toolchain.bzl index e8ce9564..1b6ce408 100644 --- a/zig/private/zig_toolchain.bzl +++ b/zig/private/zig_toolchain.bzl @@ -1,9 +1,20 @@ """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") +def _zig_bootstrap_transition_impl(_, __): + return { + "//zig/settings:bootstrapped": False, + } + +_zig_bootstrap_transition = transition( + implementation = _zig_bootstrap_transition_impl, + inputs = [], + outputs = [ + "//zig/settings:bootstrapped", + ], +) + DOC = """\ Defines a Zig compiler toolchain. @@ -32,21 +43,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 +68,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 +90,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, ) @@ -167,6 +144,7 @@ def _zig_toolchain_impl(ctx): zig_toolchain = rule( implementation = _zig_toolchain_impl, + cfg = _zig_bootstrap_transition, attrs = ATTRS, doc = DOC, ) 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), ), ) diff --git a/zig/settings/BUILD.bazel b/zig/settings/BUILD.bazel index 14962998..043cb492 100644 --- a/zig/settings/BUILD.bazel +++ b/zig/settings/BUILD.bazel @@ -71,6 +71,12 @@ repeatable_string_flag( visibility = ["//visibility:private"], ) +bool_flag( + name = "bootstrapped", + build_setting_default = False, + visibility = ["//zig/config/bootstrapped:__pkg__"], +) + bool_flag( name = "use_standalone_translate_c", build_setting_default = False,