| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| Jan Schär | 06341a6 | 2025-07-09 08:02:35 +0000 | [diff] [blame^] | 17 | def _binary_tarball_impl(ctx): |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 18 | layer_spec = ctx.actions.declare_file(ctx.label.name + ".prototxt") |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 19 | executable = ctx.attr.executable[DefaultInfo].files_to_run.executable |
| 20 | runfiles = ctx.attr.executable[DefaultInfo].default_runfiles |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 21 | files = [] |
| 22 | for file in runfiles.files.to_list(): |
| 23 | layer_path = file.short_path |
| 24 | |
| 25 | # Weird shenanigans with external repos |
| 26 | if layer_path.startswith("../"): |
| 27 | layer_path = "external/" + layer_path[3:] |
| 28 | files.append(struct( |
| 29 | path = layer_path, |
| 30 | src = file.path, |
| 31 | )) |
| 32 | ctx.actions.write(layer_spec, proto.encode_text(struct(file = files))) |
| 33 | |
| 34 | layer_out = ctx.actions.declare_file(ctx.label.name + ".tar") |
| 35 | ctx.actions.run( |
| 36 | outputs = [layer_out], |
| 37 | inputs = [layer_spec, executable] + runfiles.files.to_list(), |
| 38 | tools = [ctx.executable._container_binary], |
| 39 | executable = ctx.executable._container_binary, |
| 40 | arguments = ["-out", layer_out.path, "-spec", layer_spec.path], |
| 41 | ) |
| 42 | |
| 43 | return [DefaultInfo(files = depset([layer_out]), runfiles = ctx.runfiles(files = [layer_out]))] |
| 44 | |
| Jan Schär | 06341a6 | 2025-07-09 08:02:35 +0000 | [diff] [blame^] | 45 | binary_tarball = rule( |
| 46 | implementation = _binary_tarball_impl, |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 47 | doc = """ |
| 48 | Build a tarball from a binary given in `executable` and its runfiles. Everything will be put under |
| 49 | /app with the same filesystem layout as if run under `bazel run`. So if your executable works under bazel run, |
| 50 | it will work when packaged with this rule with the exception of runfile manifests, which this rule currently |
| 51 | doesn't support. |
| 52 | """, |
| 53 | attrs = { |
| 54 | "executable": attr.label( |
| 55 | mandatory = True, |
| 56 | executable = True, |
| 57 | allow_single_file = True, |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 58 | cfg = "target", |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 59 | ), |
| 60 | "_container_binary": attr.label( |
| Jan Schär | 06341a6 | 2025-07-09 08:02:35 +0000 | [diff] [blame^] | 61 | default = Label("//build/binary_tarball"), |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 62 | cfg = "exec", |
| 63 | executable = True, |
| 64 | allow_files = True, |
| 65 | ), |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 66 | }, |
| 67 | ) |