| # Copyright 2022 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Wrappers for providing FuchsiaPackageResourcesInfo for rust binaries.""" |
| |
| load(":utils.bzl", "get_runfiles", "is_lib", "make_resource_struct") |
| load(":providers.bzl", "FuchsiaPackageResourcesInfo") |
| |
| def _fuchsia_wrap_rust_impl(ctx): |
| # Expect exactly one binary to be generated by the wrapped binary rule. |
| native_outputs = ctx.attr.native_target.files.to_list() |
| if len(native_outputs) != 1: |
| fail("Expected exactly 1 native output for %s, got %s" % (ctx.attr.native_target, native_outputs)) |
| |
| resources = [ |
| # The binary generated by the native rust_* rule. |
| make_resource_struct(src = native_outputs[0], dest = "bin/" + ctx.attr.bin_name), |
| ] |
| for data in ctx.attr.data: |
| resources.extend(data[FuchsiaPackageResourcesInfo].resources) |
| for file in get_runfiles(ctx.attr.native_target): |
| if is_lib(file): |
| resources.append(make_resource_struct(src = file, dest = "lib/" + file.basename)) |
| return FuchsiaPackageResourcesInfo(resources = resources) |
| |
| _fuchsia_wrap_rust_binary = rule( |
| implementation = _fuchsia_wrap_rust_impl, |
| doc = """Attaches fuchsia-specific metadata to rust_binary targets. |
| |
| This allows them to be directly included in fuchsia_component. |
| """, |
| attrs = { |
| "bin_name": attr.string( |
| doc = "The name of the executable to place under bin/.", |
| mandatory = True, |
| ), |
| "native_target": attr.label( |
| doc = "The underlying cc_* target.", |
| mandatory = True, |
| providers = [[DefaultInfo]], |
| ), |
| "data": attr.label_list( |
| doc = "Packaged files needed by this target at runtime.", |
| providers = [FuchsiaPackageResourcesInfo], |
| ), |
| }, |
| ) |
| |
| def fuchsia_wrap_rust_binary( |
| name = None, |
| bin_name = None, |
| native_binary = None, |
| sdk_root_label = "@fuchsia_sdk", |
| clang_root_label = "@fuchsia_clang", |
| **kwargs): |
| """Wrap a compiled rust binary. |
| |
| The resulting target can be used as a dep in fuchsia_component. |
| |
| Args: |
| name: This target name. |
| bin_name: The filename to place under bin/. Defaults to name. |
| native_binary: The existing binary's target label. |
| sdk_root_label: Optionally override the root label of the fuchsia sdk repo. |
| clang_root_label: Optionally override the root label of the fuchsia clang repo. |
| **kwargs: Misc arguments (like testonly and visibility) to forward to the underlying rule. |
| """ |
| |
| _fuchsia_wrap_rust_binary( |
| name = name, |
| bin_name = bin_name if bin_name != None else name, |
| native_target = native_binary, |
| data = [ |
| "%s//pkg/sysroot:dist" % sdk_root_label, |
| "%s//:dist" % clang_root_label, |
| "%s//:runtime" % clang_root_label, |
| ], |
| **kwargs |
| ) |