diff --git a/cargo/private/BUILD.bazel b/cargo/private/BUILD.bazel index 2b7e6de7dd..6a1aab653c 100644 --- a/cargo/private/BUILD.bazel +++ b/cargo/private/BUILD.bazel @@ -1,13 +1,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load(":runfiles_enabled.bzl", "runfiles_enabled_build_setting") bzl_library( name = "bzl_lib", srcs = glob(["**/*.bzl"]), visibility = ["//:__subpackages__"], ) - -runfiles_enabled_build_setting( - name = "runfiles_enabled", - visibility = ["//visibility:public"], -) diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl index f1e1654672..28f9100073 100644 --- a/cargo/private/cargo_build_script.bzl +++ b/cargo/private/cargo_build_script.bzl @@ -23,7 +23,6 @@ load( "find_toolchain", _name_to_crate_name = "name_to_crate_name", ) -load(":runfiles_enabled.bzl", "is_runfiles_enabled", "runfiles_enabled_attr") # Reexport for cargo_build_script_wrapper.bzl name_to_crate_name = _name_to_crate_name @@ -206,6 +205,23 @@ def _rlocationpath(file, workspace_name): return "{}/{}".format(workspace_name, file.short_path) def _create_runfiles_dir(ctx, script): + """Create a runfiles directory to represent `CARGO_MANIFEST_DIR`. + + Due to the inability to forcibly generate runfiles directories for use as inputs + to actions, this function creates a custom runfiles directory that can more + consistently be relied upon as an input. For more details see: + https://github.com/bazelbuild/bazel/issues/15486 + + If runfiles directories can ever be more directly treated as an input this function + can be retired. + + Args: + ctx (ctx): The rule's context object + script (Target): The `cargo_build_script.script` target. + + Returns: + File: The directory created + """ runfiles_dir = ctx.actions.declare_directory("{}.cargo_runfiles".format(ctx.label.name)) # External repos always fall into the `../` branch of `_rlocationpath`. @@ -264,13 +280,10 @@ def _cargo_build_script_impl(ctx): if not workspace_name: workspace_name = ctx.workspace_name - if not is_runfiles_enabled(ctx.attr): - runfiles_dir = _create_runfiles_dir(ctx, ctx.attr.script) - script_data.append(depset([runfiles_dir])) - manifest_dir = "{}/{}/{}".format(runfiles_dir.path, workspace_name, ctx.label.package) - else: - script_data.append(ctx.attr.script[DefaultInfo].default_runfiles.files) - manifest_dir = "{}.runfiles/{}/{}".format(script.path, workspace_name, ctx.label.package) + # Create a custom runfiles directory to represent `CARGO_MANIFEST_DIR`. + runfiles_dir = _create_runfiles_dir(ctx, ctx.attr.script) + script_data.append(depset([runfiles_dir])) + manifest_dir = "{}/{}/{}".format(runfiles_dir.path, workspace_name, ctx.label.package) streams = struct( stdout = ctx.actions.declare_file(ctx.label.name + ".stdout.log"), @@ -569,9 +582,7 @@ cargo_build_script = rule( executable = True, default = Label("//cargo/private/runfiles_maker"), ), - } | runfiles_enabled_attr( - default = Label("//cargo/private:runfiles_enabled"), - ), + }, fragments = ["cpp"], toolchains = [ str(Label("//rust:toolchain_type")), diff --git a/cargo/private/runfiles_enabled.bzl b/cargo/private/runfiles_enabled.bzl deleted file mode 100644 index c11b112e0f..0000000000 --- a/cargo/private/runfiles_enabled.bzl +++ /dev/null @@ -1,167 +0,0 @@ -"""A small utility module dedicated to detecting whether or not the `--enable_runfiles` and `--windows_enable_symlinks` flag are enabled -""" - -load("@bazel_skylib//lib:selects.bzl", "selects") -load("@bazel_skylib//rules:common_settings.bzl", "bool_setting") - -RunfilesEnabledInfo = provider( - doc = "A singleton provider that contains the raw value of a build setting", - fields = { - "value": "The value of the build setting in the current configuration. " + - "This value may come from the command line or an upstream transition, " + - "or else it will be the build setting's default.", - }, -) - -def _runfiles_enabled_setting_impl(ctx): - return RunfilesEnabledInfo(value = ctx.attr.value) - -runfiles_enabled_setting = rule( - implementation = _runfiles_enabled_setting_impl, - doc = "A bool-typed build setting that cannot be set on the command line", - attrs = { - "value": attr.bool( - doc = "A boolean value", - mandatory = True, - ), - }, -) - -_RUNFILES_ENABLED_ATTR_NAME = "_runfiles_enabled" - -def runfiles_enabled_attr(default): - return { - _RUNFILES_ENABLED_ATTR_NAME: attr.label( - doc = "A flag representing whether or not runfiles are enabled.", - providers = [RunfilesEnabledInfo], - default = default, - cfg = "exec", - ), - } - -def runfiles_enabled_build_setting(name, **kwargs): - """Define a build setting identifying if runfiles are enabled. - - Args: - name (str): The name of the build setting - **kwargs: Additional keyword arguments for the target. - """ - native.config_setting( - name = "{}__enable_runfiles".format(name), - values = {"enable_runfiles": "true"}, - ) - - native.config_setting( - name = "{}__disable_runfiles".format(name), - values = {"enable_runfiles": "false"}, - ) - - bool_setting( - name = "{}__always_true".format(name), - build_setting_default = True, - ) - - native.config_setting( - name = "{}__always_true_setting".format(name), - flag_values = {":{}__always_true".format(name): "True"}, - ) - - native.config_setting( - name = "{}__always_false_setting".format(name), - flag_values = {":{}__always_true".format(name): "False"}, - ) - - # There is no way to query a setting that is unset. By utilizing constant - # settings, we can filter to a fallback setting where no known value is - # defined. - native.alias( - name = "{}__unset_runfiles".format(name), - actual = select({ - ":{}__disable_runfiles".format(name): ":{}__always_false_setting".format(name), - ":{}__enable_runfiles".format(name): ":{}__always_false_setting".format(name), - "//conditions:default": ":{}__always_true_setting".format(name), - }), - ) - - selects.config_setting_group( - name = "{}__windows_enable_runfiles".format(name), - match_all = [ - ":{}__enable_runfiles".format(name), - "@platforms//os:windows", - ], - ) - - selects.config_setting_group( - name = "{}__windows_disable_runfiles".format(name), - match_all = [ - ":{}__disable_runfiles".format(name), - "@platforms//os:windows", - ], - ) - - selects.config_setting_group( - name = "{}__windows_unset_runfiles".format(name), - match_all = [ - ":{}__unset_runfiles".format(name), - "@platforms//os:windows", - ], - ) - - native.alias( - name = "{}__unix".format(name), - actual = select({ - "@platforms//os:windows": ":{}__always_false_setting".format(name), - "//conditions:default": ":{}__always_true_setting".format(name), - }), - ) - - selects.config_setting_group( - name = "{}__unix_enable_runfiles".format(name), - match_all = [ - ":{}__enable_runfiles".format(name), - ":{}__unix".format(name), - ], - ) - - selects.config_setting_group( - name = "{}__unix_disable_runfiles".format(name), - match_all = [ - ":{}__disable_runfiles".format(name), - ":{}__unix".format(name), - ], - ) - - selects.config_setting_group( - name = "{}__unix_unset_runfiles".format(name), - match_all = [ - ":{}__unset_runfiles".format(name), - ":{}__unix".format(name), - ], - ) - - runfiles_enabled_setting( - name = name, - value = select({ - ":{}__windows_enable_runfiles".format(name): True, - ":{}__windows_disable_runfiles".format(name): False, - ":{}__windows_unset_runfiles".format(name): False, - ":{}__unix_enable_runfiles".format(name): True, - ":{}__unix_disable_runfiles".format(name): False, - ":{}__unix_unset_runfiles".format(name): True, - "//conditions:default": True, - }), - **kwargs - ) - -def is_runfiles_enabled(attr): - """Determine whether or not runfiles are enabled. - - Args: - attr (struct): A rule's struct of attributes (`ctx.attr`) - Returns: - bool: The enable_runfiles value. - """ - - runfiles_enabled = getattr(attr, _RUNFILES_ENABLED_ATTR_NAME, None) - - return runfiles_enabled[RunfilesEnabledInfo].value if runfiles_enabled else True