Skip to content

Commit b51dc7f

Browse files
chandlercjonmeow
andauthored
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 <[email protected]>
1 parent 64398ce commit b51dc7f

File tree

17 files changed

+742
-26
lines changed

17 files changed

+742
-26
lines changed

.bazelrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
# Exceptions. See /LICENSE for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5+
# Ensure all builds have Carbon's workspace status attached. We have carefully
6+
# factored the stamping done by this to avoid excessive build performance impact
7+
# and so enable stamping with it by default. CI and systems especially dependent
8+
# on caching should explicitly use `--nostamp`.
9+
build --workspace_status_command=./scripts/workspace_status.py
10+
build --stamp
11+
12+
# Provide aliases for configuring the release and pre-release version being
13+
# built. For documentation of these flags, see //bazel/version/BUILD.
14+
build --flag_alias=release=//bazel/version:release
15+
build --flag_alias=pre_release=//bazel/version:pre_release
16+
build --flag_alias=rc_number=//bazel/version:rc_number
17+
build --flag_alias=beta_number=//bazel/version:beta_number
18+
build --flag_alias=alpha_number=//bazel/version:alpha_number
19+
build --flag_alias=nightly_date=//bazel/version:nightly_date
20+
521
# Support running clang-tidy with:
622
# bazel build --config=clang-tidy -k //...
723
# See: https://github.com/erenon/bazel_clang_tidy

.github/workflows/tests.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ jobs:
294294
# https://discord.com/channels/655572317891461132/707150492370862090/1151605725576056934
295295
build --jobs=32
296296
297+
# Avoid any cache impact from build stamping in CI.
298+
build --nostamp
299+
297300
# General build options.
298301
build --verbose_failures
299302
test --test_output=errors

bazel/version/BUILD

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
# Exceptions. See /LICENSE for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "int_flag", "string_flag")
6+
7+
package(default_visibility = ["//toolchain/install:__pkg__"])
8+
9+
exports_files(["gen_tmpl.py"])
10+
11+
# Several flags are provided for customizing the exact version used for the
12+
# build of Carbon. Each of these is documented here, but rather than using the
13+
# label-based names in Bazel invocations (`bazel build --//bazel/version:flag`)
14+
# we suggest using the flag aliases provided in the project's `.bazelrc` and we
15+
# document the flags using those aliases. The aliases match the local flag names
16+
# here.
17+
#
18+
# For more details on the versioning scheme used by Carbon, see:
19+
# - https://docs.google.com/document/d/11S5VAPe5Pm_BZPlajWrqDDVr9qc7-7tS2VshqO0wWkk/edit?resourcekey=0-2YFC9Uvl4puuDnWlr2MmYw
20+
# TODO: Replace with path to the markdown once this lands.
21+
#
22+
# First, we provide a flag to enable a release version: `--release`. It is
23+
# disabled by default, and if enabled it must be the only version flag used.
24+
bool_flag(
25+
name = "release",
26+
build_setting_default = False,
27+
)
28+
29+
# A `--pre_release=KIND` flag where `KIND` must be one of:
30+
# - `rc` -- a release candidate version.
31+
# Example: `--pre_release=rc --rc_number=2`
32+
# - `beta` -- a beta version.
33+
# Example: `--pre_release=beta --beta_number=2`
34+
# - `alpha` -- an alpha version.
35+
# Example: `--pre_release=alpha --alpha_number=2`
36+
# - `nightly -- a nightly version.
37+
# Example: `--pre_release=nightly --nightly_date=2024.06.17`
38+
# - `dev` -- the default, a development build.
39+
# Example: `--pre_release=dev`
40+
#
41+
# This flag cannot be used along with `--release`, and for all but the `dev`
42+
# kind must be combined with one of the below flags to specify further details
43+
# of the version.
44+
string_flag(
45+
name = "pre_release",
46+
build_setting_default = "dev",
47+
values = [
48+
"rc",
49+
"beta",
50+
"alpha",
51+
"nightly",
52+
"dev",
53+
],
54+
)
55+
56+
# `--rc_number=N` sets the release candidate number to `N`. Requires `--pre_release=rc`.
57+
int_flag(
58+
name = "rc_number",
59+
build_setting_default = -1,
60+
)
61+
62+
# When `--pre_release=beta` is used, `--beta_number=N` set's the beta
63+
# pre-release number to `N`.
64+
#
65+
# An error if used without `--pre_release=beta`.
66+
int_flag(
67+
name = "beta_number",
68+
build_setting_default = -1,
69+
)
70+
71+
# When `--pre_release=alpha` is used, `--alpha_number=N` set's the alpha
72+
# pre-release number to `N`.
73+
#
74+
# An error if used without `--pre_release=alpha`.
75+
int_flag(
76+
name = "alpha_number",
77+
build_setting_default = -1,
78+
)
79+
80+
# When `--pre_release=nightly` is used, `--nightly_date=YYYY.MM.DD` set's the
81+
# nightly build date. The value for this flag must be a string with the exact
82+
# format of `YYYY.MM.DD`.
83+
#
84+
# An error if used without `--pre_release=nightly`.
85+
string_flag(
86+
name = "nightly_date",
87+
build_setting_default = "",
88+
)
89+
90+
# A config setting to observe the value of the `--stamp` command line flag
91+
# within starlark with a macro and `select`. This is a workaround suggested for
92+
# a Bazel issue: https://github.com/bazelbuild/bazel/issues/11164
93+
config_setting(
94+
name = "internal_stamp_flag_detect",
95+
values = {"stamp": "1"},
96+
visibility = ["//visibility:public"],
97+
)

bazel/version/compute_version.bzl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
# Exceptions. See /LICENSE for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
"""Compute the version string."""
6+
7+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
8+
load("//:version_base.bzl", "version_base")
9+
10+
def _validate_nightly_date(date):
11+
date_components = date.split(".", 2)
12+
if len(date_components) != 3:
13+
fail("Must provide a nightly date in 'YYYY.MM.DD' format, found '{}'.".format(date))
14+
year = date_components[0]
15+
if len(year) != 4 or not year.isdigit():
16+
fail("The nightly date year was not a sequence of four digits.")
17+
month = date_components[1]
18+
if len(month) != 2 or not month.isdigit():
19+
fail("The nightly date month was not a sequence of two digits.")
20+
day = date_components[2]
21+
if len(day) != 2 or not day.isdigit():
22+
fail("The nightly date day was not a sequence of two digits.")
23+
24+
def compute_version(ctx):
25+
"""Compute the version string.
26+
27+
Args:
28+
ctx: The context for a rule computing the version.
29+
30+
Returns:
31+
The version string.
32+
"""
33+
version = version_base
34+
35+
# See if we need to append a pre-release suffix to the version.
36+
#
37+
# TODO: We should more fully check for erroneous combinations of flags here
38+
# to help ensure users don't get surprising results.
39+
if not ctx.attr._release_flag[BuildSettingInfo].value:
40+
pre_release = ctx.attr._pre_release_flag[BuildSettingInfo].value
41+
pre_release_numbers = {
42+
"alpha": ctx.attr._alpha_number_flag[BuildSettingInfo].value,
43+
"beta": ctx.attr._beta_number_flag[BuildSettingInfo].value,
44+
"rc": ctx.attr._rc_number_flag[BuildSettingInfo].value,
45+
}
46+
if pre_release in pre_release_numbers:
47+
number = pre_release_numbers[pre_release]
48+
if number < 0:
49+
fail("Must provide a non-negative {} number when building that pre-release.".format(pre_release))
50+
version += "-{0}.{1}".format(pre_release, number)
51+
elif pre_release == "nightly":
52+
date = ctx.attr._nightly_date_flag[BuildSettingInfo].value
53+
_validate_nightly_date(date)
54+
version += "-0.nightly.{}".format(date)
55+
elif pre_release == "dev":
56+
version += "-0.dev"
57+
else:
58+
fail("Invalid pre-release flag: " + pre_release)
59+
60+
return version
61+
62+
VERSION_ATTRS = {
63+
"_alpha_number_flag": attr.label(default = ":alpha_number"),
64+
"_beta_number_flag": attr.label(default = ":beta_number"),
65+
"_nightly_date_flag": attr.label(default = ":nightly_date"),
66+
"_pre_release_flag": attr.label(default = ":pre_release"),
67+
"_rc_number_flag": attr.label(default = ":rc_number"),
68+
"_release_flag": attr.label(default = ":release"),
69+
}

bazel/version/gen_tmpl.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
3+
"""Generate a file from a template, substituting the provided key/value pairs.
4+
5+
The file format should match Python's `string.Template` substitution rules:
6+
- `$$` for a literal `$`
7+
- `$identifier` for some key `identifier` to be substituted
8+
- `${identifier}` when adjacent text would be interpreted as part of the
9+
identifier.
10+
11+
The keys must be strings that are valid identifiers: `[_A-Za-z][_A-Za-z0-9]*`
12+
13+
The values may not contain newlines or any vertical whitespace.
14+
15+
The initial key/value pairs are read from the command line using repeated
16+
`--substitute=KEY=DEFAULT-VALUE` flags.
17+
18+
Updated values for those keys will be read from any files provided to the
19+
`--status-file` flag. This flag can be given multiple times and the values will
20+
be read and updated from the files in order, meaning the last file's value will
21+
win. New keys are never read from these files. The file format parsed is Bazel's
22+
[status file format](https://bazel.build/docs/user-manual#workspace-status):
23+
each line is a single entry starting with a key using only characters `[_A-Z]`,
24+
one space character, and the rest of the line is the value. To assist with using
25+
Bazel status files, if the key parsed from the file begins with `STABLE_`, that
26+
prefix is removed. Any keys which are present in the substitutions provided on
27+
the command line will have their value updated with the string read from the
28+
file.
29+
"""
30+
31+
__copyright__ = """
32+
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
33+
Exceptions. See /LICENSE for license information.
34+
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
35+
"""
36+
37+
import argparse
38+
import sys
39+
from pathlib import Path
40+
from string import Template
41+
42+
43+
def main() -> None:
44+
parser = argparse.ArgumentParser(__doc__)
45+
parser.add_argument(
46+
"--template",
47+
metavar="FILE",
48+
type=Path,
49+
required=True,
50+
help="The template source file to use.",
51+
)
52+
parser.add_argument(
53+
"--output",
54+
metavar="FILE",
55+
type=Path,
56+
required=True,
57+
help="The output source file to produce.",
58+
)
59+
parser.add_argument(
60+
"--substitution",
61+
metavar="KEY=DEFAULT-VALUE",
62+
action="append",
63+
help="A substitution that should be supported and its default value.",
64+
)
65+
parser.add_argument(
66+
"--status-file",
67+
metavar="FILE",
68+
type=Path,
69+
action="append",
70+
default=[],
71+
help="A file of key/value updates in Bazel's status file format.",
72+
)
73+
parser.add_argument("-v", "--verbose", action="store_true")
74+
args = parser.parse_args()
75+
76+
# Collect the supported substitutions from the command line.
77+
substitutions = {}
78+
for substitution_arg in args.substitution:
79+
key, value = substitution_arg.split("=", 1)
80+
substitutions.update({key: value})
81+
82+
# Read either of the two status files provided to build up substitutions,
83+
# with the stable file last so its values override any duplicates.
84+
for status_file in args.status_file:
85+
if args.verbose:
86+
print(f"Reading status file: {status_file}", file=sys.stderr)
87+
for line in status_file.open():
88+
# Remove line endings.
89+
line = line.rstrip("\r\n")
90+
# Exactly matches our pattern
91+
(key, value) = line.split(" ", 1)
92+
key = key.removeprefix("STABLE_")
93+
if key in substitutions:
94+
if args.verbose:
95+
print(f"Parsed: '{key}': '{value}'", file=sys.stderr)
96+
substitutions.update({key: value})
97+
98+
if args.verbose:
99+
print(f"Reading template file: {args.template}", file=sys.stderr)
100+
with open(args.template) as template_file:
101+
template = template_file.read()
102+
103+
result = Template(template).substitute(substitutions)
104+
105+
if args.verbose:
106+
print(f"Writing output file: {args.output}", file=sys.stderr)
107+
with open(args.output, mode="w") as output_file:
108+
output_file.write(result)
109+
110+
111+
if __name__ == "__main__":
112+
main()

0 commit comments

Comments
 (0)