blob: d0e439b765fdee2f3cab4be7d3ab54a5a21c180a [file] [log] [blame]
Serge Bazanski8999faa2023-11-20 12:42:13 +01001load(
2 "@io_bazel_rules_go//go:def.bzl",
3 "GoLibrary",
4 "go_context",
5 "go_library",
6)
7
8def _go_version_library_impl(ctx):
9 output = ctx.actions.declare_file(ctx.attr.name + "_generated.go")
10
11 ctx.actions.run(
12 mnemonic = "GenVersion",
13 progress_message = "Generating version file",
14 inputs = [ctx.info_file],
15 outputs = [output],
16 executable = ctx.executable._genversion,
17 arguments = [
18 "-importpath",
19 ctx.attr.importpath,
20 "-product",
21 ctx.attr.product,
22 "-status_file",
23 ctx.info_file.path,
24 "-out_file",
25 output.path,
26 ],
27 )
28
29 go = go_context(ctx)
30 source_files = [output]
31 library = go.new_library(
32 go,
33 srcs = source_files,
34 )
35 source = go.library_to_source(go, ctx.attr, library, False)
36 providers = [library, source]
37 output_groups = {
38 "go_generated_srcs": source_files,
39 }
40 return providers + [OutputGroupInfo(**output_groups)]
41
42go_version_library = rule(
43 doc = """
44 Generate a Go library target which can be further embedded/depended upon
45 by other Go code. This library contains a Version proto field which will
46 be automatically populated with version based on build state data.
47 """,
48 implementation = _go_version_library_impl,
49 attrs = {
50 "importpath": attr.string(
51 mandatory = True,
52 ),
53 "product": attr.string(
54 mandatory = True,
55 doc = """
56 Name of Monogon product that for which this version library will
57 be generated. This must correspond to the product name as used in
58 Git tags, which in turn is used to extract a release version
59 during a build.
60 """,
61 ),
62 "_genversion": attr.label(
63 default = Label("//version/stampgo"),
64 cfg = "host",
65 executable = True,
66 allow_files = True,
67 ),
68 "_go_context_data": attr.label(
69 default = "@io_bazel_rules_go//:go_context_data",
70 ),
71 "deps": attr.label_list(
72 default = [
73 "@org_golang_google_protobuf//proto",
74 "//version/spec",
75 ],
76 ),
77 },
78 toolchains = ["@io_bazel_rules_go//go:toolchain"],
79)