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