|
| 1 | +# Copyright 2025 The TensorStore Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Version of run_binary from bazel-skylib that may avoid a separate exec build.""" |
| 15 | + |
| 16 | +# The `value` is a list of Label objects indicating which platform constraints are satisfied. |
| 17 | +PlatformConstraintsInfo = provider( |
| 18 | + "List of satisfied platform constraints.", |
| 19 | + fields = ["value"], |
| 20 | +) |
| 21 | + |
| 22 | +# This should include all constraints that are needed to identify cases where the target and exec |
| 23 | +# platforms are incompatible. |
| 24 | +PLATFORM_CONSTRAINTS = [ |
| 25 | + "@platforms//cpu:x86_64", |
| 26 | + "@platforms//cpu:arm64", |
| 27 | + "@platforms//cpu:ppc64le", |
| 28 | + "@platforms//os:windows", |
| 29 | + "@platforms//os:linux", |
| 30 | + "@platforms//os:macos", |
| 31 | + "@platforms//os:ios", |
| 32 | + "@platforms//os:freebsd", |
| 33 | + "@platforms//os:android", |
| 34 | +] |
| 35 | + |
| 36 | +def _platform_constraints_impl(ctx): |
| 37 | + return PlatformConstraintsInfo(value = [ |
| 38 | + constraint.label |
| 39 | + for constraint in ctx.attr.constraints |
| 40 | + if ctx.target_platform_has_constraint(constraint[platform_common.ConstraintValueInfo]) |
| 41 | + ]) |
| 42 | + |
| 43 | +# The platform_constraints rule serves to populate a `PlatformConstraintsInfo` object for either the |
| 44 | +# exec or target platform. |
| 45 | +_platform_constraints = rule( |
| 46 | + implementation = _platform_constraints_impl, |
| 47 | + attrs = { |
| 48 | + "constraints": attr.label_list( |
| 49 | + mandatory = True, |
| 50 | + providers = [platform_common.ConstraintValueInfo], |
| 51 | + ), |
| 52 | + }, |
| 53 | +) |
| 54 | + |
| 55 | +def _run_binary_impl(ctx): |
| 56 | + exec_constraints = ctx.attr.exec_platform_constraints[PlatformConstraintsInfo].value |
| 57 | + target_constraints = ctx.attr.target_platform_constraints[PlatformConstraintsInfo].value |
| 58 | + tool_cfg = "target" if exec_constraints == target_constraints else "exec" |
| 59 | + tool_attr_name = "tool_" + tool_cfg |
| 60 | + tool_as_list = [getattr(ctx.attr, tool_attr_name)] |
| 61 | + |
| 62 | + # The implementation below is derived from bazel-skylib. |
| 63 | + args = [ |
| 64 | + ctx.expand_location(a, tool_as_list) |
| 65 | + for a in ctx.attr.args |
| 66 | + ] |
| 67 | + envs = { |
| 68 | + k: ctx.expand_location(v, tool_as_list) |
| 69 | + for k, v in ctx.attr.env.items() |
| 70 | + } |
| 71 | + ctx.actions.run( |
| 72 | + outputs = ctx.outputs.outs, |
| 73 | + inputs = ctx.files.srcs, |
| 74 | + tools = [getattr(ctx.executable, tool_attr_name)], |
| 75 | + executable = getattr(ctx.executable, tool_attr_name), |
| 76 | + arguments = args, |
| 77 | + mnemonic = "RunBinary", |
| 78 | + use_default_shell_env = False, |
| 79 | + env = ctx.configuration.default_shell_env | envs, |
| 80 | + ) |
| 81 | + return DefaultInfo( |
| 82 | + files = depset(ctx.outputs.outs), |
| 83 | + runfiles = ctx.runfiles(files = ctx.outputs.outs), |
| 84 | + ) |
| 85 | + |
| 86 | +_run_binary = rule( |
| 87 | + implementation = _run_binary_impl, |
| 88 | + attrs = { |
| 89 | + "tool_exec": attr.label( |
| 90 | + executable = True, |
| 91 | + allow_files = True, |
| 92 | + mandatory = True, |
| 93 | + cfg = "exec", |
| 94 | + ), |
| 95 | + "tool_target": attr.label( |
| 96 | + executable = True, |
| 97 | + allow_files = True, |
| 98 | + mandatory = True, |
| 99 | + cfg = "target", |
| 100 | + ), |
| 101 | + "env": attr.string_dict(), |
| 102 | + "srcs": attr.label_list( |
| 103 | + allow_files = True, |
| 104 | + ), |
| 105 | + "outs": attr.output_list( |
| 106 | + mandatory = True, |
| 107 | + ), |
| 108 | + "args": attr.string_list(), |
| 109 | + "exec_platform_constraints": attr.label( |
| 110 | + mandatory = True, |
| 111 | + cfg = "exec", |
| 112 | + providers = [PlatformConstraintsInfo], |
| 113 | + ), |
| 114 | + "target_platform_constraints": attr.label( |
| 115 | + mandatory = True, |
| 116 | + cfg = "target", |
| 117 | + providers = [PlatformConstraintsInfo], |
| 118 | + ), |
| 119 | + }, |
| 120 | +) |
| 121 | + |
| 122 | +def run_binary( |
| 123 | + name, |
| 124 | + tool, |
| 125 | + env = {}, |
| 126 | + srcs = [], |
| 127 | + outs = [], |
| 128 | + args = [], |
| 129 | + platform_constraints = PLATFORM_CONSTRAINTS, |
| 130 | + **kwargs): |
| 131 | + """Runs a binary as a build step, avoiding separate exec build if possible. |
| 132 | +
|
| 133 | + Equivalent to the `run_binary` rule from bazel-skylib, except that the |
| 134 | + `tool` is built in the *target* configuration rather than the *exec* |
| 135 | + configuration if the target platform is the same as the exec platform. |
| 136 | +
|
| 137 | + In builds where many of the dependencies of `tool` are also needed in the |
| 138 | + `target` configuration, this avoids building those dependencies twice. |
| 139 | +
|
| 140 | + Args: |
| 141 | + name: Rule name |
| 142 | + tool: Label specifying the tool binary to run. |
| 143 | + env: Additional environment variables to set. |
| 144 | + srcs: Source files required. |
| 145 | + outs: Outputs. |
| 146 | + args: Command-line arguments for the tool. |
| 147 | + platform_constraints: List of platform constraints to check compatibility |
| 148 | + between exec and target configuration. |
| 149 | + **kwargs: Additional attributes common to all rules. |
| 150 | + """ |
| 151 | + constraints_rule_name = name + "_platform_constraints" |
| 152 | + _platform_constraints( |
| 153 | + name = constraints_rule_name, |
| 154 | + constraints = platform_constraints, |
| 155 | + visibility = ["//visibility:private"], |
| 156 | + ) |
| 157 | + _run_binary( |
| 158 | + name = name, |
| 159 | + tool_exec = tool, |
| 160 | + tool_target = tool, |
| 161 | + exec_platform_constraints = constraints_rule_name, |
| 162 | + target_platform_constraints = constraints_rule_name, |
| 163 | + env = env, |
| 164 | + srcs = srcs, |
| 165 | + outs = outs, |
| 166 | + args = args, |
| 167 | + **kwargs |
| 168 | + ) |
0 commit comments