Skip to content

Instantly share code, notes, and snippets.

@robbertvanginkel
Last active June 19, 2019 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbertvanginkel/925b10198e98010d0ca7ad722b5acbef to your computer and use it in GitHub Desktop.
Save robbertvanginkel/925b10198e98010d0ca7ad722b5acbef to your computer and use it in GitHub Desktop.
rules_go caching and stamping
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "go",
srcs = ["main.go"],
x_defs = {"main.buildtime" : "{BUILD_TIMESTAMP}"}
)
cc_library(
name = "stamp",
linkstamp = "stamp.cc",
)
cc_binary(
name = "c",
srcs = ["main.cc"],
deps = [":stamp"],
)
$ bazel build --disk_cache /tmp/examplecache //:c
INFO: Invocation ID: 8ef8a212-1f96-417f-a5a2-3f0b8e537676
INFO: Analyzed target //:c (14 packages loaded, 192 targets configured).
INFO: Found 1 target...
Target //:c up-to-date:
bazel-bin/c
INFO: Elapsed time: 1.817s, Critical Path: 0.03s
INFO: 3 processes: 3 remote cache hit.
INFO: Build completed successfully, 7 total actions
$ bazel clean
INFO: Invocation ID: 95e55dc7-012e-466d-9694-6647dda1e3a0
INFO: Starting clean.
$ bazel build --disk_cache /tmp/examplecache //:c
INFO: Invocation ID: 8d31f38a-7435-485b-a4e2-c4868d7d7acd
INFO: Analyzed target //:c (14 packages loaded, 190 targets configured).
INFO: Found 1 target...
Target //:c up-to-date:
bazel-bin/c
INFO: Elapsed time: 1.668s, Critical Path: 0.03s
INFO: 3 processes: 3 remote cache hit.
INFO: Build completed successfully, 7 total actions
$ diff -C 3 *.log.txt
*** go.log.txt 2019-06-19 12:19:02.000000000 -0700
--- go_after_clean.log.txt 2019-06-19 12:19:06.000000000 -0700
***************
*** 3828,3834 ****
inputs {
path: "bazel-out/volatile-status.txt"
digest {
! hash: "4835a9b695649ddd095b9a107914ead01ce809b910da3eacb10d7c230d7c28a5"
size_bytes: 27
hash_function_name: "SHA-256"
}
--- 3828,3834 ----
inputs {
path: "bazel-out/volatile-status.txt"
digest {
! hash: "339f338a031a16b62ce2e864af7483e4bc28f9dd7fd38fa6aa796eb949ecf8ec"
size_bytes: 27
hash_function_name: "SHA-256"
}
***************
*** 5585,5591 ****
actual_outputs {
path: "bazel-out/darwin-fastbuild/bin/darwin_amd64_stripped/go"
digest {
! hash: "224388c2265d9f83b9c126a86a0b23fa58c9b940bbb1fb7dece0fe24537324a9"
size_bytes: 1661880
hash_function_name: "SHA-256"
}
--- 5585,5591 ----
actual_outputs {
path: "bazel-out/darwin-fastbuild/bin/darwin_amd64_stripped/go"
digest {
! hash: "9468c7bb6f3994f1606edf74715d1cf6900e2dd0e9a185bf31ac0f45297d874a"
size_bytes: 1661880
hash_function_name: "SHA-256"
}
$ bazel build --disk_cache /tmp/examplecache //:go
INFO: Invocation ID: 1e315f6f-6cf4-4a4d-a591-d817907922ca
INFO: Analyzed target //:go (24 packages loaded, 6583 targets configured).
INFO: Found 1 target...
Target //:go up-to-date:
bazel-bin/darwin_amd64_stripped/go
INFO: Elapsed time: 3.840s, Critical Path: 0.39s
INFO: 3 processes: 2 remote cache hit, 1 darwin-sandbox.
INFO: Build completed successfully, 7 total actions
$ bazel build --disk_cache /tmp/examplecache //:go
INFO: Invocation ID: b2703b55-900a-491d-bbcc-5de5605ac71e
INFO: Analyzed target //:go (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:go up-to-date:
bazel-bin/darwin_amd64_stripped/go
INFO: Elapsed time: 0.153s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
$ bazel clean
INFO: Invocation ID: 3c193bbe-c031-4734-985d-de2ea0c76042
INFO: Starting clean.
$ bazel build --disk_cache /tmp/examplecache //:go
INFO: Invocation ID: 8a7942c6-2371-4a47-8132-b23a05e5d37d
INFO: Analyzed target //:go (24 packages loaded, 6583 targets configured).
INFO: Found 1 target...
Target //:go up-to-date:
bazel-bin/darwin_amd64_stripped/go
INFO: Elapsed time: 1.798s, Critical Path: 0.30s
INFO: 3 processes: 2 remote cache hit, 1 darwin-sandbox.
INFO: Build completed successfully, 7 total actions
#include <stdio.h>
extern const int build_ts;
int main() {
printf("Hello from C at %d!\n", build_ts);
return 0;
}
package main
import "fmt"
var buildtime = ""
func main() {
fmt.Printf("Hello from Go at %s\n", buildtime)
}
#ifndef BUILD_TIMESTAMP
#define BUILD_TIMESTAMP 0
#endif
extern const int build_ts;
const int build_ts = BUILD_TIMESTAMP;
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz",
],
sha256 = "f04d2373bcaf8aa09bccb08a98a57e721306c8f6043a2a0ee610fd6853dcde3d",
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment