| Serge Bazanski | 8999faa | 2023-11-20 12:42:13 +0100 | [diff] [blame] | 1 | load( |
| 2 | "@io_bazel_rules_go//go:def.bzl", |
| Serge Bazanski | 8999faa | 2023-11-20 12:42:13 +0100 | [diff] [blame] | 3 | "go_context", |
| Serge Bazanski | 8999faa | 2023-11-20 12:42:13 +0100 | [diff] [blame] | 4 | ) |
| 5 | |
| 6 | def _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 | |
| 40 | go_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 Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 62 | cfg = "exec", |
| Serge Bazanski | 8999faa | 2023-11-20 12:42:13 +0100 | [diff] [blame] | 63 | 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 | ) |