blob: 1b24b18cf7c3f38c1536e059499d2029510adbda [file] [log] [blame]
Lorenz Brun30167f52021-03-17 17:49:01 +01001# 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
17load("//metropolis/node/build:def.bzl", "build_static_transition")
18
19def _static_binary_tarball_impl(ctx):
20 layer_spec = ctx.actions.declare_file(ctx.label.name + ".prototxt")
21 if len(ctx.attr.executable) != 1:
22 fail("executable arg can only contain one file/label")
23 executable_label = ctx.attr.executable[0]
24 executable = executable_label[DefaultInfo].files_to_run.executable
25 runfiles = executable_label[DefaultInfo].default_runfiles
26 files = []
27 for file in runfiles.files.to_list():
28 layer_path = file.short_path
29
30 # Weird shenanigans with external repos
31 if layer_path.startswith("../"):
32 layer_path = "external/" + layer_path[3:]
33 files.append(struct(
34 path = layer_path,
35 src = file.path,
36 ))
37 ctx.actions.write(layer_spec, proto.encode_text(struct(file = files)))
38
39 layer_out = ctx.actions.declare_file(ctx.label.name + ".tar")
40 ctx.actions.run(
41 outputs = [layer_out],
42 inputs = [layer_spec, executable] + runfiles.files.to_list(),
43 tools = [ctx.executable._container_binary],
44 executable = ctx.executable._container_binary,
45 arguments = ["-out", layer_out.path, "-spec", layer_spec.path],
46 )
47
48 return [DefaultInfo(files = depset([layer_out]), runfiles = ctx.runfiles(files = [layer_out]))]
49
50static_binary_tarball = rule(
51 implementation = _static_binary_tarball_impl,
52 doc = """
53 Build a tarball from a binary given in `executable` and its runfiles. Everything will be put under
54 /app with the same filesystem layout as if run under `bazel run`. So if your executable works under bazel run,
55 it will work when packaged with this rule with the exception of runfile manifests, which this rule currently
56 doesn't support.
57 """,
58 attrs = {
59 "executable": attr.label(
60 mandatory = True,
61 executable = True,
62 allow_single_file = True,
63 cfg = build_static_transition,
64 ),
65 "_container_binary": attr.label(
66 default = Label("//build/static_binary_tarball"),
67 cfg = "exec",
68 executable = True,
69 allow_files = True,
70 ),
71
72 # Allow for transitions to be attached to this rule.
73 "_whitelist_function_transition": attr.label(
74 default = "@bazel_tools//tools/whitelists/function_transition_whitelist",
75 ),
76 },
77)