| Jan Schär | 82900a7 | 2025-04-14 11:11:37 +0000 | [diff] [blame] | 1 | def _oci_os_image_impl(ctx): |
| 2 | inputs = [] |
| 3 | arguments = [] |
| Jan Schär | 07e6905 | 2025-05-12 16:34:15 +0000 | [diff] [blame] | 4 | |
| 5 | inputs.append(ctx.file.product_info) |
| 6 | arguments += ["-product_info", ctx.file.product_info.path] |
| 7 | |
| Jan Schär | 82900a7 | 2025-04-14 11:11:37 +0000 | [diff] [blame] | 8 | 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 | |
| 43 | oci_os_image = rule( |
| 44 | implementation = _oci_os_image_impl, |
| 45 | doc = """ |
| 46 | Build an OS image OCI artifact. |
| 47 | """, |
| 48 | attrs = { |
| Jan Schär | 07e6905 | 2025-05-12 16:34:15 +0000 | [diff] [blame] | 49 | "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är | 82900a7 | 2025-04-14 11:11:37 +0000 | [diff] [blame] | 56 | "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 | ) |