blob: 8ec116429dbdbf592c8ede5e7e622c4f450961df [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
Lorenz Brun30167f52021-03-17 17:49:01 +010017def _static_binary_tarball_impl(ctx):
18 layer_spec = ctx.actions.declare_file(ctx.label.name + ".prototxt")
Jan Schär2b9a0a02025-07-09 07:54:12 +000019 executable = ctx.attr.executable[DefaultInfo].files_to_run.executable
20 runfiles = ctx.attr.executable[DefaultInfo].default_runfiles
Lorenz Brun30167f52021-03-17 17:49:01 +010021 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
45static_binary_tarball = rule(
46 implementation = _static_binary_tarball_impl,
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är2b9a0a02025-07-09 07:54:12 +000058 cfg = "target",
Lorenz Brun30167f52021-03-17 17:49:01 +010059 ),
60 "_container_binary": attr.label(
61 default = Label("//build/static_binary_tarball"),
62 cfg = "exec",
63 executable = True,
64 allow_files = True,
65 ),
Lorenz Brun30167f52021-03-17 17:49:01 +010066 },
67)