Skip to content

Commit a301fad

Browse files
Fix the opt-transitions we use to get fully optimized builds + stable test artifacts (#694)
### Changes are visible to end-users: no ### Test plan existing tests --------- Co-authored-by: aspect-marvin[bot] <[email protected]>
1 parent 2ad0cea commit a301fad

File tree

6 files changed

+30
-68
lines changed

6 files changed

+30
-68
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ common:release --build_tag_filters=release
3232
common:release --stamp
3333
common:release --compilation_mode=opt
3434
common:release --@rules_rust//rust/settings:lto=fat
35+
common:release --@rules_rust//:extra_rustc_flags=-Cpanic=abort
3536

3637
# Speed up local development by using the non-hermetic CPP toolchain.
3738
common:nollvm --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0

bazel/rust/defs.bzl

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
load("@aspect_bazel_lib//lib:expand_template.bzl", _expand_template = "expand_template")
44
load("@rules_rust//rust:defs.bzl", _rust_binary = "rust_binary", _rust_library = "rust_library", _rust_proc_macro = "rust_proc_macro", _rust_test = "rust_test")
5+
load("@with_cfg.bzl", "with_cfg")
56

67
_default_platform = select({
78
# Non-Linux binaries should just build with their default platforms
89
"//conditions:default": None,
910
})
1011

11-
def rust_binary(name, rustc_env_files = [], version_key = "", crate_features = [], **kwargs):
12+
rust_opt_binary, _rust_opt_binary_internal = with_cfg(_rust_binary).set(
13+
"compilation_mode",
14+
"opt",
15+
).set(
16+
Label("@rules_rust//:extra_rustc_flags"),
17+
[
18+
"-Cstrip=symbols",
19+
"-Ccodegen-units=1",
20+
"-Cpanic=abort",
21+
],
22+
# Avoid rules_rust trying to instrument this binary
23+
).set("collect_code_coverage", "false").build()
24+
25+
def rust_binary(name, rustc_env_files = [], version_key = "", crate_features = [], platform = _default_platform, **kwargs):
1226
"""
1327
Macro for rust_binary defaults.
1428
@@ -32,46 +46,22 @@ def rust_binary(name, rustc_env_files = [], version_key = "", crate_features = [
3246
)
3347
rustc_env_files = rustc_env_files + [rustc_env_file]
3448

35-
platform = kwargs.pop("platform", _default_platform)
36-
37-
_rust_binary(
49+
# Note that we use symbol stripping to
50+
# try and make these artifacts reproducibly sized for the
51+
# container_structure tests.
52+
rust_opt_binary(
3853
name = name,
3954
rustc_env_files = rustc_env_files,
40-
rustc_flags = select({
41-
"//bazel/release:is_opt": [
42-
"-Ccodegen-units=1",
43-
"-Copt-level=3",
44-
"-Cpanic=abort",
45-
"-Cstrip=symbols",
46-
],
47-
# Default/test configuration. Note that we use symbol stripping to
48-
# try and make these artifacts reproducibly sized for the
49-
# container_structure tests.
50-
#
51-
# TODO: Can we have a CI config condition here?
52-
"//conditions:default": [
53-
"-Ccodegen-units=1",
54-
"-Copt-level=3",
55-
"-Cstrip=symbols",
56-
],
57-
}),
5855
crate_features = crate_features + ["bazel"],
5956
platform = platform,
6057
**kwargs
6158
)
6259

63-
def rust_test(name, crate_features = [], **kwargs):
64-
platform = kwargs.pop("platform", _default_platform)
65-
60+
def rust_test(name, crate_features = [], platform = _default_platform, **kwargs):
6661
_rust_test(
6762
name = name,
6863
crate_features = crate_features + ["bazel"],
6964
platform = platform,
70-
rustc_flags = select({
71-
"//conditions:default": [
72-
"-Copt-level=0",
73-
],
74-
}),
7565
**kwargs
7666
)
7767

@@ -99,22 +89,10 @@ def rust_library(name, rustc_env_files = [], version_key = "", crate_features =
9989
)
10090
stamp = -1 # workaround https://github.com/bazelbuild/rules_rust/pull/3503
10191
rustc_env_files = rustc_env_files + [rustc_env_file]
92+
10293
_rust_library(
10394
name = name,
10495
rustc_env_files = rustc_env_files,
105-
rustc_flags = select({
106-
"//bazel/release:is_opt": [
107-
"-Ccodegen-units=1",
108-
"-Copt-level=3",
109-
"-Cpanic=abort",
110-
"-Cstrip=symbols",
111-
],
112-
"//conditions:default": [
113-
"-Ccodegen-units=1",
114-
"-Copt-level=3",
115-
"-Cstrip=symbols",
116-
],
117-
}),
11896
crate_features = crate_features + ["bazel"],
11997
stamp = stamp,
12098
**kwargs
@@ -131,19 +109,6 @@ def rust_proc_macro(name, crate_features = [], **kwargs):
131109
"""
132110
_rust_proc_macro(
133111
name = name,
134-
rustc_flags = select({
135-
"//bazel/release:is_opt": [
136-
"-Ccodegen-units=1",
137-
"-Copt-level=3",
138-
"-Cpanic=abort",
139-
"--release",
140-
],
141-
"//conditions:default": [
142-
"-Ccodegen-units=1",
143-
"-Copt-level=3",
144-
"-Cstrip=debuginfo",
145-
],
146-
}),
147112
crate_features = crate_features + ["bazel"],
148113
**kwargs
149114
)

bazel/rust/multi_platform_rust_binaries.bzl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ load("@bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
44
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
55
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_files")
66
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar", "pkg_zip")
7-
load("@with_cfg.bzl", "with_cfg")
87
load("//bazel/release:hashes.bzl", "hashes")
98

10-
opt_filegroup, _opt_filegroup_internal = with_cfg(native.filegroup).set("compilation_mode", "opt").build()
11-
129
TARGET_TRIPLES = [
1310
("x86_64_unknown_linux_gnu", "linux_x86_64"),
1411
("aarch64_unknown_linux_gnu", "linux_aarch64"),
@@ -106,14 +103,14 @@ def multi_platform_rust_binaries(name, target, name_scheme = TARGET_NAMING_SCHEM
106103
mac_bins.extend(bin_outs)
107104
mac_pkged.extend(pkged_outs)
108105

109-
opt_filegroup(
106+
native.filegroup(
110107
name = name,
111108
srcs = linux_bins + mac_bins,
112109
tags = kwargs.get("tags", []) + ["release"],
113110
visibility = kwargs.get("visibility", []),
114111
)
115112

116-
opt_filegroup(
113+
native.filegroup(
117114
name = "{}.packaged".format(name),
118115
srcs = linux_pkged + mac_pkged,
119116
tags = kwargs.get("tags", []) + ["release"],

py/tests/py_venv_image_layer/my_app_amd64_layers_listing.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,9 +2514,9 @@ files:
25142514
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/
25152515
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/
25162516
- -rwxr-xr-x 0 0 0 2801 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/activate
2517-
- -rwxr-xr-x 0 0 0 1372152 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python
2518-
- -rwxr-xr-x 0 0 0 1372152 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3
2519-
- -rwxr-xr-x 0 0 0 1372152 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3.9
2517+
- -rwxr-xr-x 0 0 0 734360 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python
2518+
- -rwxr-xr-x 0 0 0 734360 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3
2519+
- -rwxr-xr-x 0 0 0 734360 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3.9
25202520
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/lib/
25212521
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/lib/python3.9/
25222522
- -rwxr-xr-x 0 0 0 348 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/pyvenv.cfg

py/tests/py_venv_image_layer/my_app_arm64_layers_listing.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,9 +2495,9 @@ files:
24952495
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/
24962496
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/
24972497
- -rwxr-xr-x 0 0 0 2801 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/activate
2498-
- -rwxr-xr-x 0 0 0 1231448 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python
2499-
- -rwxr-xr-x 0 0 0 1231448 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3
2500-
- -rwxr-xr-x 0 0 0 1231448 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3.9
2498+
- -rwxr-xr-x 0 0 0 701600 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python
2499+
- -rwxr-xr-x 0 0 0 701600 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3
2500+
- -rwxr-xr-x 0 0 0 701600 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/bin/python3.9
25012501
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/lib/
25022502
- drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/lib/python3.9/
25032503
- -rwxr-xr-x 0 0 0 349 Jan 1 2023 ./py/tests/py_venv_image_layer/my_app_bin.runfiles/_main/py/tests/py_venv_image_layer/.my_app_bin/pyvenv.cfg

py/tools/venv_bin/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ load("//bazel/release:release.bzl", "release")
22
load("//bazel/rust:defs.bzl", "rust_binary")
33
load("//bazel/rust:multi_platform_rust_binaries.bzl", "multi_platform_rust_binaries")
44

5-
# TODO(#497): transition to --nocollect_code_coverage to avoid rules_rust trying to instrument this binary
65
rust_binary(
76
name = "venv",
87
srcs = [

0 commit comments

Comments
 (0)