diff --git a/rs_bindings_from_cc/bazel_support/compile_rust.bzl b/rs_bindings_from_cc/bazel_support/compile_rust.bzl index d3efc8b20..e210115a1 100644 --- a/rs_bindings_from_cc/bazel_support/compile_rust.bzl +++ b/rs_bindings_from_cc/bazel_support/compile_rust.bzl @@ -5,45 +5,12 @@ """Utility module for sharing logic between rules and aspects that generate Rust bindings from C++. """ -load("@rules_rust//:version.bzl", RUST_VERSION = "VERSION") - -# buildifier: disable=bzl-visibility -load("@rules_rust//rust/private:providers.bzl", "DepVariantInfo") - # buildifier: disable=bzl-visibility load( - "@rules_rust//rust/private:rustc.bzl", - "rustc_compile_action", + "@rules_rust//rust/private:common.bzl", + "rust_common", ) -load("@bazel_skylib//lib:structs.bzl", "structs") - -def _version_parts(version): - major, minor = version.split(".")[0:2] - return (int(major), int(minor)) - -def _rust_version_ge(version): - """Checks if the rust version as at least the given major.minor version.""" - return _version_parts(RUST_VERSION) >= _version_parts(version) - -def _get_crate_info(providers): - for provider in providers: - if hasattr(provider, "name"): - return provider - fail("Couldn't find a CrateInfo in the list of providers") - -def _get_dep_info(providers): - for provider in providers: - if hasattr(provider, "direct_crates"): - return provider - fail("Couldn't find a DepInfo in the list of providers") - -def _get_cc_info(providers): - for provider in providers: - if hasattr(provider, "linking_context"): - return provider - fail("Couldn't find a CcInfo in the list of providers") - def compile_rust(ctx, attr, src, extra_srcs, deps, crate_name, include_coverage, force_all_deps_direct, allow_lto = True, aliases = {}): """Compiles a Rust source file. @@ -62,70 +29,23 @@ def compile_rust(ctx, attr, src, extra_srcs, deps, crate_name, include_coverage, Returns: A DepVariantInfo provider. """ - toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"] - output_hash = repr(hash(src.path)) + emit_rmeta = True - lib_name = "{prefix}{name}-{lib_hash}{extension}".format( - prefix = "lib", - name = crate_name, - lib_hash = output_hash, - extension = ".rlib", - ) - - rmeta_name = "{prefix}{name}-{lib_hash}{extension}".format( - prefix = "lib", - name = crate_name, - lib_hash = output_hash, - extension = ".rmeta", - ) - - lib = ctx.actions.declare_file(lib_name) - - rmeta = ctx.actions.declare_file(rmeta_name) - - # TODO(b/336367148): We should inherit almost nothing from `attr`, but for now, at least, we - # should omit the rustc_flags. - attr_args = structs.to_dict(attr) - attr_args["rustc_flags"] = [] - - if _rust_version_ge("0.67"): - srcs = [src] + extra_srcs - else: - srcs = depset([src] + extra_srcs) - - providers = rustc_compile_action( + return rust_common.compile_rust( ctx = ctx, - attr = struct(**attr_args), - toolchain = toolchain, - crate_info_dict = dict( - name = crate_name, - type = "rlib", - root = src, - srcs = srcs, - deps = deps.to_list(), - proc_macro_deps = [], - aliases = aliases, - output = lib, - metadata = rmeta, - edition = "2018", - is_test = False, - rustc_env = {}, - compile_data = depset([]), - compile_data_targets = depset([]), - owner = ctx.label, - ), + attr = attr, + src = src, + extra_srcs = extra_srcs, + deps = deps, + edition = "2018", + emit_rmeta = emit_rmeta, + crate_name = crate_name, + aliases = aliases, + include_coverage = include_coverage, + allow_lto = allow_lto, + force_all_deps_direct = force_all_deps_direct, # LINT.IfChange rust_flags = ["-Zallow-features=custom_inner_attributes,impl_trait_in_assoc_type,register_tool,negative_impls,extern_types,arbitrary_self_types,allocator_api,cfg_sanitize"], # LINT.ThenChange(//docs/overview/unstable_features.md) - output_hash = output_hash, - force_all_deps_direct = force_all_deps_direct, - include_coverage = include_coverage, - ) - - return DepVariantInfo( - crate_info = _get_crate_info(providers), - dep_info = _get_dep_info(providers), - cc_info = _get_cc_info(providers), - build_info = None, )