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