Add `Cpp` `BuildInfo` implementation for Bazel.
PiperOrigin-RevId: 566626929
Change-Id: I05ffcf1326b0c62d829c5c6c6e7a6c9c001bbd18
diff --git a/src/create_embedded_tools.py b/src/create_embedded_tools.py
index 35c5092..3b4d59d 100644
--- a/src/create_embedded_tools.py
+++ b/src/create_embedded_tools.py
@@ -30,8 +30,14 @@
output_paths = [
('*MODULE.tools', lambda x: 'MODULE.bazel'),
('*tools/jdk/BUILD.tools', lambda x: 'tools/jdk/BUILD'),
- ('*tools/build_defs/repo/BUILD.repo',
- lambda x: 'tools/build_defs/repo/BUILD'),
+ (
+ '*tools/build_defs/repo/BUILD.repo',
+ lambda x: 'tools/build_defs/repo/BUILD',
+ ),
+ (
+ '*tools/build_defs/build_info/BUILD.tools',
+ lambda x: 'tools/build_defs/build_info/BUILD',
+ ),
('*tools/j2objc/BUILD.tools', lambda x: 'tools/j2objc/BUILD'),
('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
('*tools/cpp/BUILD.tools', lambda x: 'tools/cpp/BUILD'),
diff --git a/tools/build_defs/BUILD b/tools/build_defs/BUILD
index c176d2d..bf49dc2 100644
--- a/tools/build_defs/BUILD
+++ b/tools/build_defs/BUILD
@@ -1,6 +1,7 @@
filegroup(
name = "srcs",
srcs = glob(["**"]) + [
+ "//tools/build_defs/build_info:embedded_tools",
"//tools/build_defs/cc:srcs",
"//tools/build_defs/cc/tests:cc_import_tests_files",
"//tools/build_defs/cc/whitelists/parse_headers_and_layering_check:srcs",
@@ -16,6 +17,7 @@
filegroup(
name = "embedded_tools",
srcs = glob(["**"]) + [
+ "//tools/build_defs/build_info:embedded_tools",
"//tools/build_defs/cc:srcs",
"//tools/build_defs/cc/whitelists/parse_headers_and_layering_check:srcs",
"//tools/build_defs/cc/whitelists/starlark_hdrs_check:srcs",
@@ -29,6 +31,7 @@
filegroup(
name = "bzl_srcs",
srcs = [
+ "//tools/build_defs/build_info:bzl_srcs",
"//tools/build_defs/cc:action_names.bzl",
"//tools/build_defs/cc:cc_import.bzl",
"//tools/build_defs/hash:hash.bzl",
diff --git a/tools/build_defs/build_info/BUILD b/tools/build_defs/build_info/BUILD
new file mode 100644
index 0000000..6964d53
--- /dev/null
+++ b/tools/build_defs/build_info/BUILD
@@ -0,0 +1,28 @@
+# Copyright 2023 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.
+
+load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+
+package(default_visibility = ["//visibility:public"])
+
+bzl_library(
+ name = "bazel_cc_build_info_bzl",
+ srcs = ["bazel_cc_build_info.bzl"],
+ visibility = ["//visibility:private"],
+)
+
+filegroup(
+ name = "embedded_tools",
+ srcs = glob(["*"]),
+)
diff --git a/tools/build_defs/build_info/BUILD.tools b/tools/build_defs/build_info/BUILD.tools
new file mode 100644
index 0000000..9053be4
--- /dev/null
+++ b/tools/build_defs/build_info/BUILD.tools
@@ -0,0 +1,27 @@
+# Copyright 2023 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.
+
+load(":bazel_cc_build_info.bzl", "bazel_cc_build_info")
+
+package(default_visibility = ["//visibility:public"])
+
+bazel_cc_build_info(
+ name = "cc_build_info",
+)
+
+filegroup(
+ name = "bzl_srcs",
+ srcs = glob(["*.bzl"]),
+ visibility = ["//tools/build_defs:__pkg__"],
+)
diff --git a/tools/build_defs/build_info/bazel_cc_build_info.bzl b/tools/build_defs/build_info/bazel_cc_build_info.bzl
new file mode 100644
index 0000000..b70b650
--- /dev/null
+++ b/tools/build_defs/build_info/bazel_cc_build_info.bzl
@@ -0,0 +1,81 @@
+# Copyright 2023 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.
+
+"""A rule for getting transliterated build info files for C++."""
+
+def _transform_version(version_file_contents):
+ if "BUILD_TIMESTAMP" not in version_file_contents.keys():
+ fail("timestamp information is missing from workspace status file, BUILD_TIMESTAMP key not found.")
+ return {
+ "{BUILD_SCM_REVISION}": version_file_contents.get("BUILD_SCM_REVISION", "0"),
+ "{BUILD_SCM_STATUS}": version_file_contents.get("BUILD_SCM_STATUS", ""),
+ "{BUILD_TIMESTAMP}": version_file_contents["BUILD_TIMESTAMP"],
+ }
+
+def _transform_info(info_file_contents):
+ return {
+ "{BUILD_EMBED_LABEL}": info_file_contents.get("BUILD_EMBED_LABEL", ""),
+ "{BUILD_HOST}": info_file_contents.get("BUILD_HOST", "hostname"),
+ "{BUILD_USER}": info_file_contents.get("BUILD_USER", "username"),
+ }
+
+def _get_build_info_files(actions, version_template, info_template, redacted_file, stamp):
+ outputs = []
+ if stamp:
+ version_file = actions.transform_version_file(transform_func = _transform_version, template = version_template, output_file_name = "volatile_file.h")
+ info_file = actions.transform_info_file(transform_func = _transform_info, template = info_template, output_file_name = "non_volatile_file.h")
+ outputs.append(info_file)
+ outputs.append(version_file)
+ else:
+ output_redacted_file = actions.declare_file("redacted_file.h")
+ actions.symlink(output = output_redacted_file, target_file = redacted_file)
+ outputs.append(output_redacted_file)
+ return outputs
+
+def _impl(ctx):
+ output_groups = {
+ "non_redacted_build_info_files": depset(_get_build_info_files(
+ ctx.actions,
+ ctx.file._version_template,
+ ctx.file._info_template,
+ ctx.file._redacted_file,
+ True,
+ )),
+ "redacted_build_info_files": depset(_get_build_info_files(
+ ctx.actions,
+ ctx.file._version_template,
+ ctx.file._info_template,
+ ctx.file._redacted_file,
+ False,
+ )),
+ }
+ return OutputGroupInfo(**output_groups)
+
+bazel_cc_build_info = rule(
+ implementation = _impl,
+ attrs = {
+ "_version_template": attr.label(
+ default = "@//tools/build_defs/build_info/templates:volatile_file.h.template",
+ allow_single_file = True,
+ ),
+ "_info_template": attr.label(
+ default = "@//tools/build_defs/build_info/templates:non_volatile_file.h.template",
+ allow_single_file = True,
+ ),
+ "_redacted_file": attr.label(
+ default = "@//tools/build_defs/build_info/templates:redacted_file.h.template",
+ allow_single_file = True,
+ ),
+ },
+)
diff --git a/tools/build_defs/build_info/templates/BUILD b/tools/build_defs/build_info/templates/BUILD
new file mode 100644
index 0000000..0b68ae9
--- /dev/null
+++ b/tools/build_defs/build_info/templates/BUILD
@@ -0,0 +1,24 @@
+# Copyright 2023 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.
+
+exports_files(
+ [
+ "non_volatile_file.h.template",
+ "volatile_file.h.template",
+ "redacted_file.h.template",
+ ],
+ visibility = [
+ "//tools/build_defs/build_info:__pkg__",
+ ],
+)
diff --git a/tools/build_defs/build_info/templates/non_volatile_file.h.template b/tools/build_defs/build_info/templates/non_volatile_file.h.template
new file mode 100644
index 0000000..527af10
--- /dev/null
+++ b/tools/build_defs/build_info/templates/non_volatile_file.h.template
@@ -0,0 +1,3 @@
+#define BUILD_EMBED_LABEL "{BUILD_EMBED_LABEL}"
+#define BUILD_HOST "{BUILD_HOST}"
+#define BUILD_USER "{BUILD_USER}"
diff --git a/tools/build_defs/build_info/templates/redacted_file.h.template b/tools/build_defs/build_info/templates/redacted_file.h.template
new file mode 100644
index 0000000..e8b7f20
--- /dev/null
+++ b/tools/build_defs/build_info/templates/redacted_file.h.template
@@ -0,0 +1,6 @@
+#define BUILD_EMBED_LABEL "redacted"
+#define BUILD_HOST "redacted"
+#define BUILD_SCM_REVISION "0"
+#define BUILD_SCM_STATUS "redacted"
+#define BUILD_TIMESTAMP 0
+#define BUILD_USER "redacted"
diff --git a/tools/build_defs/build_info/templates/volatile_file.h.template b/tools/build_defs/build_info/templates/volatile_file.h.template
new file mode 100644
index 0000000..de63bde
--- /dev/null
+++ b/tools/build_defs/build_info/templates/volatile_file.h.template
@@ -0,0 +1,3 @@
+#define BUILD_SCM_REVISION "{BUILD_SCM_REVISION}"
+#define BUILD_SCM_STATUS "{BUILD_SCM_STATUS}"
+#define BUILD_TIMESTAMP {BUILD_TIMESTAMP}