-
Notifications
You must be signed in to change notification settings - Fork 638
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
Conversation
Can one of the admins verify this patch? |
How is this different from the 'x_defs' field already on the go_binary? |
@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", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Jenkins, test this please. |
Jenkins, test this please. |
I haven't had a chance to get back to this yet. |
1de13f7
to
2d45df0
Compare
go/def.bzl
Outdated
] | ||
|
||
stamp_input = [] | ||
if ctx.attr.stamp and ctx.attr.linkstamp: |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dropped stamp.
cf5d35c
to
4ca8724
Compare
Jenkins, test this please. |
seems to be failing on linux: |
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, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 = [] |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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%'
There was a problem hiding this 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.
@jayconrod thanks for finishing this off! |
for a workspace with BUILD_SCM_VERSION=10 defined as a volatile status and a go binary rule:
The go binary will be linked with the option
-X my/pkg/version.BUILD_SCM_VERSION