Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
[Backport 5.3.9104] bazel: transition oci_image to opt/release mode f…
Browse files Browse the repository at this point in the history
…or Rust (#62049)

bazel: transition oci_image to opt/release mode for Rust (#61740)

Our rust binaries (e.g. scip-ctags/syntect_server) were being built in some mix of opt & fastbuild mode[1]. Unlike with Go where there is no release/debug mode flag, Rust requires opting into optimized release builds. We can do that automagically when building any oci_image target with the power of ✨ transitions ✨

This has the side-effect of our Go binaries no longer being stripped & containing debug symbols, see bazel-contrib/rules_go#3917

Also to note, [in Cargo.toml we opt into debug symbols in release mode](https://sourcegraph.com/github.com/sourcegraph/sourcegraph@nsc/bazel-release-mode-rust/-/blob/docker-images/syntax-highlighter/Cargo.toml?L67%3A11-70%3A9). Is this preserved by this PR for bazel[2]?

[1] `strings` on the binaries showed the 3rd-party crates having `k8-opt` filepath names e.g.
```
$ / # strings syntect_server | grep k8-
/tmp/bazel-working-directory/__main__/bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/crate_index__onig_sys-69.8.1/onig_sys_build_script_.runfiles/crate_index__onig_sys-69.8.1
```
but the final binaries (and the 1st-party crates) themselves were being built in fastbuild mode. See https://github.com/sourcegraph/devx-support/issues/790 for original point of contact

[2] It seems like it may be preserved, but I dont know how reliable this is for Rust binaries
```
$ file bazel-bin/docker-images/syntax-highlighter/scip-ctags
bazel-bin/docker-images/syntax-highlighter/scip-ctags: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.0.0, with debug_info, not stripped
```

## Test plan

Tested for sourcegraph/scip-ctags image:
```
/ $ strings scip-ctags | grep "Could not parse file"
/ $ echo $?
1
```

(cherry picked from commit ce6a366)

Co-authored-by: Noah S-C <[email protected]>
  • Loading branch information
sourcegraph-release-bot and Strum355 authored Apr 22, 2024
1 parent 1b0e5c7 commit e2cf4ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
10 changes: 7 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "platforms",
sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
sha256 = "5eda539c841265031c2f82d8ae7a3a6490bd62176e0c038fc469eabf91f6149b",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.9/platforms-0.0.9.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.9/platforms-0.0.9.tar.gz",
],
)

load("@platforms//host:extension.bzl", "host_platform_repo")

host_platform_repo(name = "host_platform")

http_archive(
name = "bazel_skylib",
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
Expand Down
13 changes: 6 additions & 7 deletions dev/oci_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def oci_image(name, **kwargs):
oci_image_cross(
name = name,
image = ":" + name + "_underlying",
platforms = select({
"@platforms//os:macos": [Label("@zig_sdk//platform:linux_amd64")],
"//conditions:default": [],
platform = select({
"@platforms//os:macos": Label("@zig_sdk//platform:linux_amd64"),
"//conditions:default": Label("@platforms//host"),
}),
visibility = kwargs.pop("visibility", ["//visibility:public"]),
)
Expand All @@ -45,13 +45,12 @@ oci_image_cross = rule(
attrs = {
"image": attr.label(cfg = transition(
implementation = lambda settings, attr: [
{"//command_line_option:platforms": str(platform)}
for platform in attr.platforms
{"//command_line_option:platforms": str(attr.platform), "//command_line_option:compilation_mode": "opt"},
],
inputs = [],
outputs = ["//command_line_option:platforms"],
outputs = ["//command_line_option:platforms", "//command_line_option:compilation_mode"],
)),
"platforms": attr.label_list(),
"platform": attr.label(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
Expand Down

0 comments on commit e2cf4ee

Please sign in to comment.