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