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

Support for stamp in Go binaries #233

Closed
zombiezen opened this issue Dec 16, 2016 · 7 comments
Closed

Support for stamp in Go binaries #233

zombiezen opened this issue Dec 16, 2016 · 7 comments

Comments

@zombiezen
Copy link

While the stamp attribute is there in go_binary, it seems like it doesn't do anything. I would expect it to use the -X flag, but I'm not sure what import path it would use (linker docs).

@pmbethe09
Copy link
Member

It would seem like a good idea for the 'stamp' attribute to function similarly to the same on other *_binary rules.

@twpayne
Copy link
Contributor

twpayne commented Jan 11, 2017

Would it make sense to specify the import path in a separate attribute to the rule? In addition, it would be nice if part of the value could come from the workspace status command. Something like:

go_binary(
    name = "foo",
    stamp = 1,
    stamp_var = "github.com/my/project.buildStamp",
    stamp_value = "$(BUILD_SCM_REVISION) $(BUILD_SCM_STATUS)",
)

This would pass -X "github.com/my/project.buildStamp=revision status" to go build.

@twpayne
Copy link
Contributor

twpayne commented Jan 16, 2017

In case it helps others, while this issue is still open, I'm using a hacky workaround to get the BUILD_SCM_REVISION from tools/buildstamp/get_workspace_status into my Go binary by generating a Go source file containing the string:

genrule(
    name = "genrule-version.go",
    outs = ["version.go"],
    cmd = "BUILD_SCM_REVISION=$$(awk '/^BUILD_SCM_REVISION/ { print $$2 }' bazel-out/volatile-status.txt); echo -en \"package main\n\nvar Version = \\\"$$BUILD_SCM_REVISION\\\"\n\" > $@",
    stamp = 1,
)

@pmbethe09
Copy link
Member

pmbethe09 commented Jan 17, 2017 via email

@twpayne
Copy link
Contributor

twpayne commented Jan 18, 2017

Sounds reasonable :)

What should the rule look like? I'm thinking of something like:

go_buildstamp(
  name = "buildstamp",
  out = "buildstamp.go",  # name of generated file
  package_name = "main",  # optional, could default to "main" or the basename of the directory
)

This would generate buildstamp.go that could then be included as a normal Go source file. The generated file could be something like:

package main

const (
    BuildEmbedLabel  = "..."
    BuildUser        = "..."
    BuildHost        = "..."
    BuildTimestamp   = "..."
    BuildSCMRevision = "..."
    BuildSCMStatus   = "..."
)

I'd be happy to have a go at implementing this if it looks reasonable to you.

@pmbethe09
Copy link
Member

pmbethe09 commented Jan 18, 2017 via email

@zombiezen
Copy link
Author

I looked into writing a PR for doing this using the linker options approach, but it seems that it is blocked on bazelbuild/bazel#1054, as Skylark has no access to the workspace status.

For future implementors, I discovered you can use go tool link -X main.NameOfVar=foo to set variables in package main. Note that they must be variables, not constants, for -X to work. Furthermore, the linker will no-op if the variable doesn't exist, so it would be safe to always pass the flags.

jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 14, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 15, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 15, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 15, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 15, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 15, 2017
If the stamp attribute is true in go_binary,
bazel-out/volatile-status.txt and stable-status.txt will be
concatenated and written into the binary using a -X linker flag.

Developers can access stamp information by adding a dependency on
@io_bazel_rules_go//buildstamp:go_default_library.

Fixes bazelbuild#233
jayconrod pushed a commit to mikedanese/rules_go that referenced this issue Feb 21, 2017
jayconrod pushed a commit to mikedanese/rules_go that referenced this issue Feb 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants