Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for injecting build stamps with link options #186

Merged
merged 1 commit into from
Feb 22, 2017

Conversation

mikedanese
Copy link
Contributor

for a workspace with BUILD_SCM_VERSION=10 defined as a volatile status and a go binary rule:

go_binary(
    ....
    stamp = True,
    linkstamp = "my/pkg/version",
)

The go binary will be linked with the option -X my/pkg/version.BUILD_SCM_VERSION

@bazel-io
Copy link
Member

bazel-io commented Nov 3, 2016

Can one of the admins verify this patch?

@pmbethe09
Copy link
Member

How is this different from the 'x_defs' field already on the go_binary?

@mikedanese
Copy link
Contributor Author

@pmbethe09 currently workspace status is not available from skylark: bazelbuild/bazel#1054 so it's not possible to use stamp info from the 'x_defs' field.

go/def.bzl Outdated
for f in stamp_input:
cmds += [
"while read -r stamp || [[ -n $stamp ]]; do",
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to do this in skylark and less bash-like which to me is a hard to read?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like it's not currently possible to access from skylark, per bazelbuild/bazel#1054.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would adding a comment explaining what these lines of shell are doing help?

"reads workspace status file, converting "KEY value" lines to "-X KEY=$linkstamp.value" arguments to the go linker."

go/def.bzl Outdated
for f in stamp_input:
cmds += [
"while read -r stamp || [[ -n $stamp ]]; do",
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like it's not currently possible to access from skylark, per bazelbuild/bazel#1054.

go/def.bzl Outdated

stamp_input = []
if ctx.attr.stamp and ctx.attr.linkstamp:
stamp_input = [ctx.info_file, ctx.version_file]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are ctx.info_file and ctx.version_file? they don't seem to be documented on https://www.bazel.io/versions/master/docs/skylark/lib/ctx.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not documented because they are alpha still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go/def.bzl Outdated
for f in stamp_input:
cmds += [
"while read -r stamp || [[ -n $stamp ]]; do",
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would adding a comment explaining what these lines of shell are doing help?

"reads workspace status file, converting "KEY value" lines to "-X KEY=$linkstamp.value" arguments to the go linker."

go/def.bzl Outdated
cmds += [
"while read -r stamp || [[ -n $stamp ]]; do",
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
"STAMP_XDEFS+=(-X $(echo %s.${stamp} | tr ' ' '='));" % ctx.attr.linkstamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if linkstamp is empty, this results in -X .BUILD_TIMESTAMP=1234 - is what what we want?
also, is it legal for values to have spaces? do we need to add quotes around the values?

go/def.bzl Outdated
@@ -558,7 +574,8 @@ go_library = rule(
go_binary = rule(
go_binary_impl,
attrs = go_library_attrs + _crosstool_attrs + {
"stamp": attr.bool(default = False),
"stamp": attr.bool(default = True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe update the root README.md explaining what stamp and linkstamp do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this if/when we decide we like the design? What's the policy on compatibility? I'd like someone from go team to weigh in on this before publicizing, but it unblocks us now.

@pmbethe09
Copy link
Member

Jenkins, test this please.

@pmbethe09
Copy link
Member

Jenkins, test this please.

@mikedanese
Copy link
Contributor Author

I haven't had a chance to get back to this yet.

go/def.bzl Outdated
]

stamp_input = []
if ctx.attr.stamp and ctx.attr.linkstamp:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we maybe fail if stamp is true but linkstamp isn't set? otherwise it might be confusing why stamp isn't working.

(alternately, do we need both? or just use nonempty linkstamp as the setting?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped stamp.

@pmbethe09
Copy link
Member

Jenkins, test this please.

@pmbethe09
Copy link
Member

seems to be failing on linux:
"
bazel-out/local-fastbuild/bin/examples/stamped_bin/bazel-out/local-fastbuild/bin/examples/stamped_bin/stamped_bin.a.GoLinkFile.params: 10: bazel-out/local-fastbuild/bin/examples/stamped_bin/bazel-out/local-fastbuild/bin/examples/stamped_bin/stamped_bin.a.GoLinkFile.params: Syntax error: "(" unexpected
____Building complete.
"

go/def.bzl Outdated
"while read -r stamp || [[ -n $stamp ]]; do",
# reads workspace status file, converting "KEY value" lines to "-X KEY=$linkstamp.value" arguments to the go linker.
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
"STAMP_XDEFS+=(-X \"$(echo \"%s.${stamp}\" | tr ' ' '=')\");" % ctx.attr.linkstamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we can't use shell arrays here.

maybe just build up a string? there might be subtle quoting or escaping issues, but I'm not sure of a better way.

go/def.bzl Outdated
for f in stamp_input:
cmds += [
"while read -r stamp || [[ -n $stamp ]]; do",
# reads workspace status file, converting "KEY value" lines to "-X KEY=$linkstamp.value" arguments to the go linker.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is wrong - it's -X $linkstamp.KEY=value, right?

go/def.bzl Outdated
"STAMP_XDEFS=()",
]

stamp_input = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider renaming this to stamp_inputs to make it clear it's a list.

go/def.bzl Outdated
"while read -r stamp || [[ -n $stamp ]]; do",
# reads workspace status file, converting "KEY value" lines to "-X KEY=$linkstamp.value" arguments to the go linker.
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
"STAMP_XDEFS+=(-X \"$(echo \"%s.${stamp}\" | tr ' ' '=')\");" % ctx.attr.linkstamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the grep? It matches any line with whitespace, right? I'd imagine in some cases, the value for some key would be empty, and a line might not have any space. Maybe use sed instead?

echo $stamp | sed -e 's%^\([^ ]\+\) \?\(.*\)$%-Xa/b.\1=\2%'

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add documentation on the linkstamp attribute to README.md.

@mikedanese
Copy link
Contributor Author

@jayconrod thanks for finishing this off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants