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

fix: substitute build metadata (git commit) in bazel builds #7378

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ build --copt=-DGRPC_BAZEL_BUILD
# inside a docker image.
build --experimental_convenience_symlinks=ignore

# Use host-OS-specific config lines from bazelrc files.
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK, this file is not loaded if google-cloud-cpp is built as part of a larger project. That is not a blocker for this change, but maybe we should document the limitations somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added such a note.

[Indeed, if this code was part of a bigger project they may not be using bazel or git, so they'd have to figure out how to generate their own build identifier.]

build --enable_platform_specific_config=true

# Specify a binary that Bazel runs before each build. The program can report
# information about the status of the workspace, such as the current source
# control revision.
build:linux --workspace_status_command=bazel/workspace_status_command.sh

# Clang Sanitizers, use with (for example):
#
# --client_env=CXX=clang++ --client_env=CC=clang --config asan
Expand Down
25 changes: 25 additions & 0 deletions bazel/workspace_status_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
#
# Copyright 2021 Google LLC
#
# 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.

# This program should print zero or more key/value pairs to standard
# output, one entry on each line, and then exit with zero (otherwise
# the build fails). The key names can be anything but they may only use
# upper case letters and underscores. The first space after the key
# name separates it from the value. The value is the rest of the line
# (including additional whitespaces). Neither the key nor the value may
# span multiple lines. Keys must not be duplicated.

echo "STABLE_GIT_COMMIT $(git rev-parse --short HEAD || echo "unknown")"
5 changes: 3 additions & 2 deletions google/cloud/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ genrule(
srcs = ["internal/build_info.cc.in"],
outs = ["internal/build_info.cc"],
cmd = """
V=$$(git rev-parse --short HEAD 2>/dev/null || echo "unknown");
V=$$(sed -n -e 's/^STABLE_GIT_COMMIT *//p' bazel-out/stable-status.txt)
sed -e "s;@CMAKE_CXX_FLAGS@;$(CC_FLAGS);" \
-e "s;\\$${CMAKE_CXX_FLAGS_.*};$(COMPILATION_MODE);" \
-e "s;@GOOGLE_CLOUD_CPP_GIT_HEAD@;$${V};" < $< > $@
-e "s;@GOOGLE_CLOUD_CPP_BUILD_METADATA@;$${V:-unknown};" < $< > $@
""",
stamp = True,
toolchains = [
"@bazel_tools//tools/cpp:current_cc_toolchain",
"@bazel_tools//tools/cpp:cc_flags",
Expand Down
1 change: 1 addition & 0 deletions google/cloud/spanner/spanner_version_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ TEST(SpannerVersionTest, Format) {
/// @test Verify the version does not contain build info for release builds.
TEST(SpannerVersionTest, NoBuildInfoInRelease) {
if (!google::cloud::internal::build_metadata().empty()) {
EXPECT_THAT(google::cloud::internal::build_metadata(), Not(HasSubstr("@")));
EXPECT_THAT(VersionString(),
HasSubstr("+" + google::cloud::internal::build_metadata()));
return;
Expand Down