Files
carbon-lang/common/BUILD
T
Chandler CarruthandJon Ross-Perkins b51dc7f8e2 Introduce version and build info stamping. (#4054)
This adds a defined Carbon version to the Bazel build and codebase that
can be used both to implement features like version checks and to report
a meaningful version on the command line. This replaces a hard-coded
string and a TODO in the driver.

As part of this, it adds support for defining the version in Bazel, and
special build flags for overriding relevant parts such as the
pre-release marker used. The exact structure and meaning of our version
string, including the pre-release parts, is implemented here in line
with the draft proposal:

https://docs.google.com/document/d/11S5VAPe5Pm_BZPlajWrqDDVr9qc7-7tS2VshqO0wWkk/edit?resourcekey=0-2YFC9Uvl4puuDnWlr2MmYw

This also introduces a workspace status command to the repository to
extract the git commit SHA and other information when building, and the
logic to stamp that into binaries as part of the version string when
useful. The technique used leverages weak symbols with whole archive
linking to allow a link-time override of unstamped data with stamped
data in the leaf executable. This makes building with `--stamp` a
reasonable default, especially for development builds. The CI system is
explicitly opted out of this as there it has no benefit.

Last but not least, all of these are wired into the install rules so
that we build installable packages with the version number in a
conventional place in the directory and filename.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-06-18 00:33:05 +00:00

552 lines
13 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//bazel/version:rules.bzl", "expand_version_build_info")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "bazel_working_dir",
hdrs = ["bazel_working_dir.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "benchmark_main",
srcs = ["benchmark_main.cpp"],
deps = [
":init_llvm",
"@abseil-cpp//absl/flags:parse",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "command_line",
srcs = ["command_line.cpp"],
hdrs = ["command_line.h"],
deps = [
":check",
":ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "command_line_test",
size = "small",
srcs = ["command_line_test.cpp"],
deps = [
":command_line",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "check",
srcs = [
"check_internal.cpp",
"check_internal.h",
],
hdrs = ["check.h"],
deps = [
":ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "check_test",
size = "small",
srcs = ["check_test.cpp"],
deps = [
":check",
"//testing/base:gtest_main",
"@googletest//:gtest",
],
)
cc_library(
name = "enum_base",
hdrs = ["enum_base.h"],
deps = [
"//common:ostream",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "enum_base_test_def",
testonly = 1,
textual_hdrs = ["enum_base_test.def"],
)
cc_test(
name = "enum_base_test",
size = "small",
srcs = ["enum_base_test.cpp"],
deps = [
":enum_base",
":enum_base_test_def",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
],
)
cc_library(
name = "error",
hdrs = ["error.h"],
deps = [
":check",
":ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "error_test",
size = "small",
srcs = ["error_test.cpp"],
deps = [
":error",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
],
)
cc_library(
name = "exe_path",
srcs = ["exe_path.cpp"],
hdrs = ["exe_path.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "exe_path_test",
size = "small",
srcs = ["exe_path_test.cpp"],
deps = [
":exe_path",
"//testing/base:gtest_main",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "hashing",
srcs = ["hashing.cpp"],
hdrs = ["hashing.h"],
deps = [
":check",
":ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "hashing_test",
srcs = ["hashing_test.cpp"],
deps = [
":hashing",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_binary(
name = "hashing_benchmark",
testonly = 1,
srcs = ["hashing_benchmark.cpp"],
deps = [
":benchmark_main",
":check",
":hashing",
"@abseil-cpp//absl/hash",
"@abseil-cpp//absl/random",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "hashtable_key_context",
hdrs = ["hashtable_key_context.h"],
deps = [
":hashing",
],
)
cc_library(
name = "indirect_value",
hdrs = ["indirect_value.h"],
)
cc_test(
name = "indirect_value_test",
size = "small",
srcs = ["indirect_value_test.cpp"],
deps = [
":indirect_value",
"//testing/base:gtest_main",
"@googletest//:gtest",
],
)
cc_library(
name = "init_llvm",
srcs = ["init_llvm.cpp"],
hdrs = ["init_llvm.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
# Link against this to cause `:init_llvm` to pull in all LLVM targets.
#
# Be careful when depending on this: it pulls in several hundred megabytes of
# LLVM binary size in -c fastbuild. This should only be depended on by a
# `cc_binary` or `cc_test` target, never a `cc_library`.
cc_library(
name = "all_llvm_targets",
srcs = ["all_llvm_targets.cpp"],
deps = [
":init_llvm",
"@llvm-project//llvm:AllTargetsAsmParsers",
"@llvm-project//llvm:AllTargetsCodeGens",
"@llvm-project//llvm:Support",
],
alwayslink = 1,
)
cc_library(
name = "map",
hdrs = ["map.h"],
deps = [
":check",
":hashtable_key_context",
":raw_hashtable",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "map_test",
srcs = ["map_test.cpp"],
deps = [
":map",
":raw_hashtable_test_helpers",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
],
)
cc_binary(
name = "map_benchmark",
testonly = 1,
srcs = ["map_benchmark.cpp"],
deps = [
":map",
":raw_hashtable_benchmark_helpers",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/random",
"@boost_unordered",
"@google_benchmark//:benchmark_main",
"@llvm-project//llvm:Support",
],
)
sh_test(
name = "map_benchmark_test",
# The benchmark allocates a large amount of memory.
size = "enormous",
# We configure the test to run quickly.
timeout = "short",
srcs = ["map_benchmark_test.sh"],
data = [":map_benchmark"],
)
cc_library(
name = "ostream",
hdrs = ["ostream.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "raw_hashtable",
srcs = ["raw_hashtable.cpp"],
hdrs = ["raw_hashtable.h"],
deps = [
":check",
":hashing",
":hashtable_key_context",
":raw_hashtable_metadata_group",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "raw_hashtable_metadata_group",
srcs = ["raw_hashtable_metadata_group.cpp"],
hdrs = ["raw_hashtable_metadata_group.h"],
deps = [
":check",
"@llvm-project//llvm:Support",
],
)
cc_binary(
name = "raw_hashtable_metadata_group_benchmark",
testonly = 1,
srcs = ["raw_hashtable_metadata_group_benchmark.cpp"],
deps = [
":raw_hashtable_metadata_group",
"@abseil-cpp//absl/random",
"@google_benchmark//:benchmark_main",
"@llvm-project//llvm:Support",
],
)
sh_test(
name = "raw_hashtable_metadata_group_benchmark_test",
srcs = ["raw_hashtable_metadata_group_benchmark_test.sh"],
data = [":raw_hashtable_metadata_group_benchmark"],
)
cc_library(
name = "raw_hashtable_benchmark_helpers",
testonly = 1,
srcs = ["raw_hashtable_benchmark_helpers.cpp"],
hdrs = ["raw_hashtable_benchmark_helpers.h"],
copts = [
"-O2", # Always optimize to make testing benchmarks faster.
],
deps = [
":check",
":hashing",
":raw_hashtable",
":set",
"@abseil-cpp//absl/base:no_destructor",
"@abseil-cpp//absl/hash",
"@abseil-cpp//absl/random",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "raw_hashtable_test_helpers",
testonly = 1,
hdrs = ["raw_hashtable_test_helpers.h"],
deps = [
":check",
":hashing",
":hashtable_key_context",
":ostream",
],
)
cc_library(
name = "set",
hdrs = ["set.h"],
deps = [
":check",
":hashtable_key_context",
":raw_hashtable",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "set_test",
srcs = ["set_test.cpp"],
deps = [
":raw_hashtable_test_helpers",
":set",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
],
)
cc_binary(
name = "set_benchmark",
testonly = 1,
srcs = ["set_benchmark.cpp"],
deps = [
":raw_hashtable_benchmark_helpers",
":set",
"@abseil-cpp//absl/container:flat_hash_set",
"@google_benchmark//:benchmark_main",
"@llvm-project//llvm:Support",
],
)
sh_test(
name = "set_benchmark_test",
# The benchmark allocates a large amount of memory.
size = "enormous",
# We configure the test to run quickly.
timeout = "short",
srcs = ["set_benchmark_test.sh"],
data = [":set_benchmark"],
)
cc_library(
name = "string_helpers",
srcs = ["string_helpers.cpp"],
hdrs = ["string_helpers.h"],
deps = [
":check",
":error",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "string_helpers_test",
size = "small",
srcs = ["string_helpers_test.cpp"],
deps = [
":string_helpers",
"//testing/base:gtest_main",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "struct_reflection",
hdrs = ["struct_reflection.h"],
)
cc_test(
name = "struct_reflection_test",
size = "small",
srcs = ["struct_reflection_test.cpp"],
deps = [
":struct_reflection",
"//testing/base:gtest_main",
"@googletest//:gtest",
],
)
cc_library(
name = "variant_helpers",
hdrs = ["variant_helpers.h"],
deps = [
":error",
"@llvm-project//llvm:Support",
],
)
# The base version source file only uses non-stamped parts of the version
# information so we expand it once here without any stamping.
expand_version_build_info(
name = "version_cpp_gen",
out = "version.cpp",
stamp = 0,
template = "version.tmpl.cpp",
)
# Build a nostamp version of the stamp source, but mark its definitions as weak.
# We'll include this in the library to satisfy definitions of library and test
# users, but still allow binaries that want full build stamping to depend on the
# stamp library below to override with strong, stamped definitions.
expand_version_build_info(
name = "version_nostamp_cpp_gen",
out = "version_nostamp.cpp",
stamp = 0,
substitutions = {"MAKE_WEAK": "1"},
template = "version_stamp.tmpl.cpp",
)
# Provides APIs for accessing Carbon version information.
#
# These provide full access to the major, minor, and patch version. It also
# provides an API for querying version strings that may contain detailed build
# information such as the commit SHA.
#
# By default, this provides the API and an *unstamped* implementations of
# version strings. As a consequence, depending on this library doesn't introduce
# any dependency on the commit SHA or loss of build caching.
#
# Targets that want full build info stamping in the data produced by these APIs
# should additionally depend on `:version_stamp` below -- the data these APIs
# return will be overridden in any binaries depending on that rule with the
# fully stamped details.
cc_library(
name = "version",
srcs = [
"version.cpp",
"version_nostamp.cpp",
],
hdrs = ["version.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
# Generate the fully stamped sourcefile if stamping is enabled in the build.
expand_version_build_info(
name = "version_stamp_cpp_gen",
out = "version_stamp.cpp",
template = "version_stamp.tmpl.cpp",
)
# Depend on this library to enable fully-stamped build information in the
# version API provided by `:version`. This doesn't provide the API, it injects
# an override of stamped versions of the data.
#
# Note that depending on this will significantly reduce build caching with
# `--stamp` builds. It should be used sparingly, typically in user-facing
# binaries or systems that need to render a maximally detailed version string
# with build information stamped into it.
cc_library(
name = "version_stamp",
srcs = ["version_stamp.cpp"],
deps = [
":version",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "vlog",
srcs = ["vlog_internal.h"],
hdrs = ["vlog.h"],
deps = [
":ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "vlog_test",
size = "small",
srcs = ["vlog_test.cpp"],
deps = [
":vlog",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"@googletest//:gtest",
],
)