blob: 8b688a7e177cac2a6dfe9091c6fb07eb942a0da6 [file] [log] [blame]
Jan Schär82900a72025-04-14 11:11:37 +00001def _oci_os_image_impl(ctx):
2 inputs = []
3 arguments = []
Jan Schär07e69052025-05-12 16:34:15 +00004
5 inputs.append(ctx.file.product_info)
6 arguments += ["-product_info", ctx.file.product_info.path]
7
Jan Schär82900a72025-04-14 11:11:37 +00008 for name, label in ctx.attr.srcs.items():
9 files = label[DefaultInfo].files.to_list()
10 if len(files) != 1:
11 fail("payload {} does not have exactly one file: {}", name, files)
12 file = files[0]
13 inputs.append(file)
14 arguments += [
15 "-payload_name",
16 name,
17 "-payload_file",
18 file.path,
19 ]
20
21 arguments += ["-compression_level", str(ctx.attr.compression_level)]
22 runfiles = None
23 if ctx.attr.compression_level == 0:
24 # When not compressing, the inputs are referenced by symlinks.
25 runfiles = ctx.runfiles(files = inputs)
26
27 output = ctx.actions.declare_directory(ctx.label.name)
28 arguments += ["-out", output.path]
29
30 ctx.actions.run(
31 mnemonic = "MkOCI",
32 executable = ctx.executable._mkoci,
33 arguments = arguments,
34 inputs = inputs,
35 outputs = [output],
36 )
37
38 return [DefaultInfo(
39 files = depset([output]),
40 runfiles = runfiles,
41 )]
42
43oci_os_image = rule(
44 implementation = _oci_os_image_impl,
45 doc = """
46 Build an OS image OCI artifact.
47 """,
48 attrs = {
Jan Schär07e69052025-05-12 16:34:15 +000049 "product_info": attr.label(
50 doc = """
51 Product info of the OS in JSON format.
52 """,
53 mandatory = True,
54 allow_single_file = True,
55 ),
Jan Schär82900a72025-04-14 11:11:37 +000056 "srcs": attr.string_keyed_label_dict(
57 doc = """
58 Payloads to include in the OCI artifact.
59 The key defines the name of the payload.
60 The value is a label which must contain one file.
61 """,
62 mandatory = True,
63 allow_files = True,
64 ),
65 "compression_level": attr.int(
66 default = 2,
67 doc = """
68 The compression level to use for payloads,
69 1 is the fastest, 4 gives the smallest results.
70 0 disables compression, payloads are symlinked.
71 """,
72 ),
73
74 # Tool
75 "_mkoci": attr.label(
76 default = Label("//osbase/build/mkoci"),
77 executable = True,
78 cfg = "exec",
79 ),
80 },
81)