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>
This commit is contained in:
Chandler Carruth
2024-06-18 00:33:05 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 64398cee20
commit b51dc7f8e2
17 changed files with 742 additions and 26 deletions
+21 -18
View File
@@ -9,6 +9,7 @@
#include <optional>
#include "common/command_line.h"
#include "common/version.h"
#include "common/vlog.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/ScopeExit.h"
@@ -398,24 +399,7 @@ The linked file name. The output is always a linked binary.
};
struct Driver::Options {
static constexpr CommandLine::CommandInfo Info = {
.name = "carbon",
// TODO: Set up more detailed version information and use that here.
.version = R"""(
Carbon Language toolchain -- version 0.0.0
)""",
.help = R"""(
This is the unified Carbon Language toolchain driver. Its subcommands provide
all of the core behavior of the toolchain, including compilation, linking, and
developer tools. Each of these has its own subcommand, and you can pass a
specific subcommand to the `help` subcommand to get details about its usage.
)""",
.help_epilogue = R"""(
For questions, issues, or bug reports, please use our GitHub project:
https://github.com/carbon-language/carbon-lang
)""",
};
static const CommandLine::CommandInfo Info;
enum class Subcommand : int8_t {
Compile,
@@ -453,6 +437,25 @@ For questions, issues, or bug reports, please use our GitHub project:
LinkOptions link_options;
};
// Note that this is not constexpr so that it can include information generated
// in separate translation units and potentially overridden at link time in the
// version string.
const CommandLine::CommandInfo Driver::Options::Info = {
.name = "carbon",
.version = Version::ToolchainInfo,
.help = R"""(
This is the unified Carbon Language toolchain driver. Its subcommands provide
all of the core behavior of the toolchain, including compilation, linking, and
developer tools. Each of these has its own subcommand, and you can pass a
specific subcommand to the `help` subcommand to get details about its usage.
)""",
.help_epilogue = R"""(
For questions, issues, or bug reports, please use our GitHub project:
https://github.com/carbon-language/carbon-lang
)""",
};
auto Driver::ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
-> CommandLine::ParseResult {
return CommandLine::Parse(