Files
carbon-lang/common/version.tmpl.cpp
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

52 lines
1.7 KiB
C++

// 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
#include "common/version.h"
#include <string_view>
namespace Carbon {
// A simplistic string-to-integer routine that is consteval for compile-time
// extracting specific components of the version from the string form. We use
// `std::string_view` for its broader `constexpr` API.
static consteval auto ToInt(std::string_view str) -> int {
int result = 0;
while (true) {
result += str.front() - '0';
str.remove_prefix(1);
if (str.empty()) {
break;
}
result *= 10;
}
return result;
}
static consteval auto MajorVersion(std::string_view str) -> int {
return ToInt(str.substr(0, str.find('.')));
}
static consteval auto MinorVersion(std::string_view str) -> int {
str.remove_prefix(str.find('.') + 1);
return ToInt(str.substr(0, str.find('.')));
}
static consteval auto PatchVersion(std::string_view str) -> int {
str.remove_prefix(str.find('.') + 1);
str.remove_prefix(str.find('.') + 1);
// Note that searching for `-` may find the end of the string if there is no
// pre-release component, but that produces the correct result here.
return ToInt(str.substr(0, str.find('-')));
}
// The major, minor, and patch versions are always provided and stable. They
// don't depend on build stamping or introduce caching issues. Provide normal
// strong definitions.
constexpr int Version::Major = MajorVersion("$VERSION");
constexpr int Version::Minor = MinorVersion("$VERSION");
constexpr int Version::Patch = PatchVersion("$VERSION");
} // namespace Carbon