| Jan Schär | e6c0c32 | 2025-05-12 16:14:25 +0000 | [diff] [blame] | 1 | # Copyright The Monogon Project Authors. |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | def _product_info_impl(ctx): |
| 5 | product_info_file = ctx.actions.declare_file(ctx.label.name + ".json") |
| 6 | args = ctx.actions.args() |
| 7 | args.add("-status_file", ctx.info_file) |
| 8 | args.add("-product_info_file", product_info_file) |
| 9 | args.add("-os_release_file", ctx.outputs.out_os_release) |
| 10 | args.add("-stamp_var", ctx.attr.stamp_var) |
| 11 | args.add("-name", ctx.attr.os_name) |
| 12 | args.add("-id", ctx.attr.os_id) |
| 13 | args.add("-architecture", ctx.attr.architecture) |
| 14 | args.add("-build_flags", "-".join(ctx.attr.build_flags)) |
| Jan Schär | d4309bb | 2025-07-18 10:13:22 +0200 | [diff] [blame^] | 15 | args.add("-platform_os", ctx.attr.platform_os) |
| Jan Schär | e6c0c32 | 2025-05-12 16:14:25 +0000 | [diff] [blame] | 16 | args.add_all(ctx.attr.components, before_each = "-component") |
| 17 | ctx.actions.run( |
| 18 | mnemonic = "GenProductInfo", |
| 19 | progress_message = "Generating product info", |
| 20 | inputs = [ctx.info_file], |
| 21 | outputs = [product_info_file, ctx.outputs.out_os_release], |
| 22 | executable = ctx.executable._genproductinfo, |
| 23 | arguments = [args], |
| 24 | ) |
| 25 | return [DefaultInfo(files = depset([product_info_file]))] |
| 26 | |
| 27 | _product_info = rule( |
| 28 | implementation = _product_info_impl, |
| 29 | attrs = { |
| 30 | "os_name": attr.string(mandatory = True), |
| 31 | "os_id": attr.string(mandatory = True), |
| 32 | "stamp_var": attr.string(mandatory = True), |
| Jan Schär | d4309bb | 2025-07-18 10:13:22 +0200 | [diff] [blame^] | 33 | "platform_os": attr.string(default = "unknown"), |
| Jan Schär | e6c0c32 | 2025-05-12 16:14:25 +0000 | [diff] [blame] | 34 | "components": attr.string_list(), |
| 35 | "out_os_release": attr.output( |
| 36 | mandatory = True, |
| 37 | doc = """Output, contains the os-release file.""", |
| 38 | ), |
| 39 | "architecture": attr.string(mandatory = True), |
| 40 | "build_flags": attr.string_list(), |
| 41 | "_genproductinfo": attr.label( |
| 42 | default = ":genproductinfo", |
| 43 | cfg = "exec", |
| 44 | executable = True, |
| 45 | allow_files = True, |
| 46 | ), |
| 47 | }, |
| 48 | ) |
| 49 | |
| 50 | def _product_info_macro_impl(**kwargs): |
| 51 | _product_info( |
| 52 | architecture = select({ |
| 53 | "@platforms//cpu:x86_64": "x86_64", |
| 54 | "@platforms//cpu:aarch64": "aarch64", |
| 55 | }), |
| 56 | build_flags = select({ |
| 57 | Label(":flag_debug"): ["debug"], |
| 58 | "//conditions:default": [], |
| 59 | }) + select({ |
| 60 | Label(":flag_race"): ["race"], |
| 61 | "//conditions:default": [], |
| 62 | }), |
| 63 | **kwargs |
| 64 | ) |
| 65 | |
| 66 | product_info = macro( |
| 67 | inherit_attrs = _product_info, |
| 68 | attrs = {"architecture": None, "build_flags": None}, |
| 69 | implementation = _product_info_macro_impl, |
| 70 | ) |