Skip to content

Commit

Permalink
add support for injecting build stamps with link options
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedanese committed Jan 10, 2017
1 parent f65dfc8 commit cf5d35c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
19 changes: 19 additions & 0 deletions examples/stamped_bin/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package(
default_visibility = ["//visibility:public"],
)

load("//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "stamped_bin",
srcs = ["main.go"],
linkstamp = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp",
deps = [
":stamp",
],
)

go_library(
name = "stamp",
srcs = ["stamp.go"],
)
30 changes: 30 additions & 0 deletions examples/stamped_bin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2016 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"

"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp"
)

func main() {
fmt.Println(stamp.BUILD_TIMESTAMP)
if stamp.BUILD_TIMESTAMP == "fail" {
os.Exit(1)
}
}
18 changes: 18 additions & 0 deletions examples/stamped_bin/stamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright 2016 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package stamp

var BUILD_TIMESTAMP = "fail"
24 changes: 21 additions & 3 deletions go/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def emit_go_link_action(ctx, importmap, transitive_libs, cgo_deps, lib,
('../' * out_depth) + ctx.file.go_tool.path,
"tool", "link", "-L", ".",
"-o", _go_importpath(ctx),
'"${STAMP_XDEFS[@]}"',
]

if x_defs:
Expand All @@ -389,9 +390,25 @@ def emit_go_link_action(ctx, importmap, transitive_libs, cgo_deps, lib,
# Avoided -s on OSX but but it requires dsymutil to be on $PATH.
# TODO(yugui) Remove this workaround once rules_go stops supporting XCode 7.2
# or earlier.
cmds += ["export PATH=$PATH:/usr/bin"]
cmds += [
"export PATH=$PATH:/usr/bin",
"export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
"declare -a STAMP_XDEFS",
]

stamp_input = []
if ctx.attr.linkstamp:
stamp_input = [ctx.info_file, ctx.version_file]
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.
"if echo $stamp | grep -qe '^.*[[:space:]].*$'; then",
"STAMP_XDEFS+=(-X \"$(echo \"%s.${stamp}\" | tr ' ' '=')\");" % ctx.attr.linkstamp,
"fi",
"done < " + f.path,
]
cmds += [
"cd " + out_dir,
' '.join(link_cmd),
"mv -f " + _go_importpath(ctx) + " " + ("../" * out_depth) + executable.path,
Expand All @@ -401,7 +418,7 @@ def emit_go_link_action(ctx, importmap, transitive_libs, cgo_deps, lib,

ctx.action(
inputs = (list(transitive_libs) + [lib] + list(cgo_deps) +
ctx.files.toolchain + ctx.files._crosstool),
ctx.files.toolchain + ctx.files._crosstool) + stamp_input,
outputs = [executable],
executable = f,
mnemonic = "GoLink",
Expand Down Expand Up @@ -558,7 +575,7 @@ go_library = rule(
go_binary = rule(
go_binary_impl,
attrs = go_library_attrs + _crosstool_attrs + {
"stamp": attr.bool(default = False),
"linkstamp": attr.string(default = ""),
"x_defs": attr.string_dict(),
},
executable = True,
Expand All @@ -576,6 +593,7 @@ go_test = rule(
),
cfg = "host",
),
"linkstamp": attr.string(default = ""),
"x_defs": attr.string_dict(),
},
executable = True,
Expand Down

0 comments on commit cf5d35c

Please sign in to comment.