| Jan Schär | 4cfff95 | 2025-04-23 10:05:39 +0000 | [diff] [blame^] | 1 | commit 676d9d220a8a71f207701fc8568078028235aea6 |
| 2 | Author: Jan Schär <jan@monogon.tech> |
| 3 | Date: Wed Apr 30 09:30:04 2025 +0000 |
| 4 | |
| 5 | Add stampsrcs attribute |
| 6 | |
| 7 | This change adds the stampsrcs attribute, which allows providing a list |
| 8 | of files containing variable definitions, which are used in addition to |
| 9 | the stable and volatile status files and have the same format. |
| 10 | |
| 11 | See https://github.com/bazel-contrib/rules_go/issues/3507 |
| 12 | |
| 13 | diff --git a/go/private/actions/archive.bzl b/go/private/actions/archive.bzl |
| 14 | index 06212139..e89cedd6 100644 |
| 15 | --- a/go/private/actions/archive.bzl |
| 16 | +++ b/go/private/actions/archive.bzl |
| 17 | @@ -177,6 +177,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d |
| 18 | _cover = source.cover, |
| 19 | _embedsrcs = tuple(source.embedsrcs), |
| 20 | _x_defs = tuple(source.x_defs.items()), |
| 21 | + _stampsrcs = tuple(source.stampsrcs), |
| 22 | _gc_goopts = tuple(source.gc_goopts), |
| 23 | _cgo = source.cgo, |
| 24 | _cdeps = tuple(source.cdeps), |
| 25 | @@ -198,8 +199,10 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d |
| 26 | _cgo_deps = cgo_deps, |
| 27 | ) |
| 28 | x_defs = dict(source.x_defs) |
| 29 | + stampsrcs = source.stampsrcs |
| 30 | for a in direct: |
| 31 | x_defs.update(a.x_defs) |
| 32 | + stampsrcs = stampsrcs + a.stampsrcs |
| 33 | |
| 34 | # Ensure that the _cgo_export.h of the current target comes first when cgo_exports is iterated |
| 35 | # by prepending it and specifying the order explicitly. This is required as the CcInfo attached |
| 36 | @@ -213,6 +216,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d |
| 37 | libs = depset(direct = [out_lib], transitive = [a.libs for a in direct]), |
| 38 | transitive = depset([data], transitive = [a.transitive for a in direct]), |
| 39 | x_defs = x_defs, |
| 40 | + stampsrcs = stampsrcs, |
| 41 | cgo_deps = depset(transitive = [cgo_deps] + [a.cgo_deps for a in direct]), |
| 42 | cgo_exports = cgo_exports, |
| 43 | runfiles = runfiles, |
| 44 | diff --git a/go/private/actions/link.bzl b/go/private/actions/link.bzl |
| 45 | index 18dd1f94..3b236f06 100644 |
| 46 | --- a/go/private/actions/link.bzl |
| 47 | +++ b/go/private/actions/link.bzl |
| 48 | @@ -161,6 +161,7 @@ def emit_link( |
| 49 | stamp_inputs.append(info_file) |
| 50 | if stamp_x_defs_volatile: |
| 51 | stamp_inputs.append(version_file) |
| 52 | + stamp_inputs = stamp_inputs + archive.stampsrcs |
| 53 | if stamp_inputs: |
| 54 | builder_args.add_all(stamp_inputs, before_each = "-stamp") |
| 55 | |
| 56 | diff --git a/go/private/context.bzl b/go/private/context.bzl |
| 57 | index f5cd735d..42c5cf13 100644 |
| 58 | --- a/go/private/context.bzl |
| 59 | +++ b/go/private/context.bzl |
| 60 | @@ -202,6 +202,7 @@ def _merge_embed(source, embed): |
| 61 | source["cover"] = depset(transitive = [source["cover"], s.cover]) |
| 62 | source["deps"] = source["deps"] + s.deps |
| 63 | source["x_defs"].update(s.x_defs) |
| 64 | + source["stampsrcs"] = source["stampsrcs"] + s.stampsrcs |
| 65 | source["gc_goopts"] = source["gc_goopts"] + s.gc_goopts |
| 66 | source["runfiles"] = source["runfiles"].merge(s.runfiles) |
| 67 | |
| 68 | @@ -319,6 +320,7 @@ def new_go_info( |
| 69 | "embedsrcs": embedsrcs, |
| 70 | "cover": depset(attr_srcs) if coverage_instrumented else depset(), |
| 71 | "x_defs": {}, |
| 72 | + "stampsrcs": [], |
| 73 | "deps": deps, |
| 74 | "gc_goopts": _expand_opts(go, "gc_goopts", getattr(attr, "gc_goopts", [])), |
| 75 | "runfiles": _collect_runfiles(go, getattr(attr, "data", []), deps), |
| 76 | @@ -344,6 +346,8 @@ def new_go_info( |
| 77 | k = "{}.{}".format(importmap, k) |
| 78 | x_defs[k] = v |
| 79 | go_info["x_defs"] = x_defs |
| 80 | + for t in getattr(attr, "stampsrcs", []): |
| 81 | + go_info["stampsrcs"] = go_info["stampsrcs"] + t[DefaultInfo].files.to_list() |
| 82 | if not go_info["cgo"]: |
| 83 | for k in ("cdeps", "cppopts", "copts", "cxxopts", "clinkopts"): |
| 84 | if getattr(attr, k, None): |
| 85 | diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl |
| 86 | index f3f2f07c..eac3a978 100644 |
| 87 | --- a/go/private/rules/binary.bzl |
| 88 | +++ b/go/private/rules/binary.bzl |
| 89 | @@ -302,6 +302,12 @@ def _go_binary_kwargs(go_cc_aspects = []): |
| 90 | See [Defines and stamping] for examples of how to use these. |
| 91 | """, |
| 92 | ), |
| 93 | + "stampsrcs": attr.label_list( |
| 94 | + allow_files = True, |
| 95 | + doc = """Additional files containing variables which can be referenced in `x_defs`. |
| 96 | + The format of these files should be the same as the workspace status. |
| 97 | + """, |
| 98 | + ), |
| 99 | "basename": attr.string( |
| 100 | doc = """The basename of this binary. The binary |
| 101 | basename may also be platform-dependent: on Windows, we add an .exe extension. |
| 102 | diff --git a/go/private/rules/library.bzl b/go/private/rules/library.bzl |
| 103 | index 8aa020d6..fe944d3a 100644 |
| 104 | --- a/go/private/rules/library.bzl |
| 105 | +++ b/go/private/rules/library.bzl |
| 106 | @@ -147,6 +147,12 @@ go_library = rule( |
| 107 | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. |
| 108 | """, |
| 109 | ), |
| 110 | + "stampsrcs": attr.label_list( |
| 111 | + allow_files = True, |
| 112 | + doc = """Additional files containing variables which can be referenced in `x_defs`. |
| 113 | + The format of these files should be the same as the workspace status. |
| 114 | + """, |
| 115 | + ), |
| 116 | "cgo": attr.bool( |
| 117 | doc = """ |
| 118 | If `True`, the package may contain [cgo] code, and `srcs` may contain C, C++, Objective-C, and Objective-C++ files |
| 119 | diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl |
| 120 | index 4859c53e..da46ead9 100644 |
| 121 | --- a/go/private/rules/test.bzl |
| 122 | +++ b/go/private/rules/test.bzl |
| 123 | @@ -93,6 +93,7 @@ def _go_test_impl(ctx): |
| 124 | embedsrcs = [struct(files = internal_go_info.embedsrcs)], |
| 125 | deps = internal_archive.direct + [internal_archive], |
| 126 | x_defs = ctx.attr.x_defs, |
| 127 | + stampsrcs = ctx.attr.stampsrcs, |
| 128 | ), |
| 129 | name = internal_go_info.name + "_test", |
| 130 | importpath = internal_go_info.importpath + "_test", |
| 131 | @@ -326,6 +327,12 @@ _go_test_kwargs = { |
| 132 | See [Defines and stamping] for examples of how to use these. |
| 133 | """, |
| 134 | ), |
| 135 | + "stampsrcs": attr.label_list( |
| 136 | + allow_files = True, |
| 137 | + doc = """Additional files containing variables which can be referenced in `x_defs`. |
| 138 | + The format of these files should be the same as the workspace status. |
| 139 | + """, |
| 140 | + ), |
| 141 | "linkmode": attr.string( |
| 142 | default = "auto", |
| 143 | values = ["auto"] + LINKMODES, |
| 144 | @@ -661,6 +668,7 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab |
| 145 | attrs = structs.to_dict(internal_go_info) |
| 146 | attrs["deps"] = internal_deps |
| 147 | attrs["x_defs"] = x_defs |
| 148 | + attrs["stampsrcs"] = internal_go_info.stampsrcs + internal_archive.stampsrcs |
| 149 | internal_go_info = GoInfo(**attrs) |
| 150 | internal_archive = go.archive(go, internal_go_info, _recompile_suffix = ".recompileinternal", recompile_internal_deps = need_recompile_deps) |
| 151 | |
| 152 | @@ -698,6 +706,7 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab |
| 153 | cover = arc_data._cover, |
| 154 | embedsrcs = as_list(arc_data._embedsrcs), |
| 155 | x_defs = dict(arc_data._x_defs), |
| 156 | + stampsrcs = as_list(arc_data._stampsrcs), |
| 157 | deps = deps, |
| 158 | gc_goopts = as_list(arc_data._gc_goopts), |
| 159 | runfiles = arc_data.runfiles, |
| 160 | @@ -722,6 +731,7 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab |
| 161 | libs = depset(direct = [arc_data.file], transitive = [a.libs for a in deps]), |
| 162 | transitive = depset(direct = [arc_data], transitive = [a.transitive for a in deps]), |
| 163 | x_defs = go_info.x_defs, |
| 164 | + stampsrcs = go_info.stampsrcs, |
| 165 | cgo_deps = depset(transitive = [arc_data._cgo_deps] + [a.cgo_deps for a in deps]), |
| 166 | cgo_exports = depset(transitive = [a.cgo_exports for a in deps]), |
| 167 | runfiles = go_info.runfiles, |