gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/arch.bzl (about)

     1  """Wrappers for architecture-specific rules."""
     2  
     3  load("//tools/bazeldefs:defs.bzl", _amd64_config = "amd64_config", _arch_config = "arch_config", _arm64_config = "arm64_config", _select_arch = "select_arch", _transition_allowlist = "transition_allowlist")
     4  
     5  # Export arch rules.
     6  select_arch = _select_arch
     7  transition_allowlist = _transition_allowlist
     8  
     9  def _arch_transition_impl(settings, attr):
    10      return {
    11          "arm64": _arm64_config(settings, attr),
    12          "amd64": _amd64_config(settings, attr),
    13      }
    14  
    15  arch_transition = transition(
    16      implementation = _arch_transition_impl,
    17      inputs = [],
    18      outputs = _arch_config,
    19  )
    20  
    21  def _arch_genrule_impl(ctx):
    22      """Runs a command with inputs from multiple architectures.
    23  
    24      The command will be run multiple times, with the provided
    25      template rendered using the architecture for the output.
    26      """
    27      outputs = []
    28      for (arch, src) in ctx.split_attr.src.items():
    29          # Calculate the template for this output file.
    30          output = ctx.actions.declare_file(ctx.attr.template % arch)
    31          outputs.append(output)
    32  
    33          # Copy the specific generated source.
    34          input_files = src[DefaultInfo].files
    35          ctx.actions.run_shell(
    36              inputs = input_files,
    37              outputs = [output],
    38              command = "cp %s %s" % (
    39                  " ".join([f.path for f in input_files.to_list()]),
    40                  output.path,
    41              ),
    42          )
    43      return [DefaultInfo(
    44          files = depset(outputs),
    45      )]
    46  
    47  arch_genrule = rule(
    48      implementation = _arch_genrule_impl,
    49      attrs = {
    50          "src": attr.label(
    51              doc = "Sources for the genrule.",
    52              cfg = arch_transition,
    53          ),
    54          "template": attr.string(
    55              doc = "Template for the output files.",
    56              mandatory = True,
    57          ),
    58          "_allowlist_function_transition": attr.label(
    59              default = _transition_allowlist,
    60          ),
    61      },
    62  )