Skip to content

Commit 693c0f3

Browse files
committed
Fix full LTO in standalone and Linux builds
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a52f212e-fee3-4245-a5fc-e5be4cdf2cdf
1 parent ca38751 commit 693c0f3

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

misc/bazel/rust.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_rust//rust:defs.bzl", "rust_binary")
22
load("@semmle_code//buildutils-internal:glibc_symbols_check.bzl", "glibc_symbols_check")
33
load("@semmle_code//buildutils-internal:lipo.bzl", "universal_binary")
4-
load("@semmle_code//buildutils-internal:transitions.bzl", "forward_binary_from_transition", "get_transition_attrs")
4+
load("//misc/bazel:transitions.bzl", "forward_binary_from_transition", "get_transition_attrs")
55

66
def _full_lto_transition_impl(_settings, _attr):
77
return {"@rules_rust//rust/settings:lto": "fat"}
@@ -27,6 +27,9 @@ def codeql_rust_binary(
2727
rust_label_name = "single_arch/" + name
2828
binary_dep = ":" + rust_label_name
2929
if full_lto:
30+
# rustc must consume the LLVM bitcode because the C++ linker may use an
31+
# incompatible LLVM version.
32+
kwargs["experimental_use_cc_common_link"] = 0
3033
lto_label_name = "full_lto/" + name
3134
_full_lto_binary(
3235
name = lto_label_name,

misc/bazel/transitions.bzl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load("@bazel_skylib//lib:paths.bzl", "paths")
2+
3+
def forward_binary_from_transition(ctx):
4+
binary = ctx.attr.dep[0]
5+
default_info = binary[DefaultInfo]
6+
original_executable = default_info.files_to_run.executable
7+
if not original_executable:
8+
fail("Cannot transition a target that is not executable")
9+
10+
(_, extension) = paths.split_extension(original_executable.basename)
11+
new_executable = ctx.actions.declare_file(ctx.label.name + extension)
12+
inputs = [original_executable]
13+
command = "cp %s %s" % (original_executable.path, new_executable.path)
14+
15+
providers = []
16+
if OutputGroupInfo in binary:
17+
pdb_file = getattr(binary[OutputGroupInfo], "pdb_file", None)
18+
if pdb_file:
19+
(pdb_file,) = pdb_file.to_list()
20+
linked_pdb_file = ctx.actions.declare_file(ctx.label.name + ".pdb")
21+
ctx.actions.symlink(target_file = pdb_file, output = linked_pdb_file)
22+
inputs.append(linked_pdb_file)
23+
providers.append(binary[OutputGroupInfo])
24+
25+
ctx.actions.run_shell(
26+
inputs = inputs,
27+
outputs = [new_executable],
28+
command = command,
29+
)
30+
files = depset(direct = [new_executable])
31+
runfiles = default_info.default_runfiles.merge(ctx.runfiles([new_executable]))
32+
providers.append(
33+
DefaultInfo(
34+
files = files,
35+
runfiles = runfiles,
36+
executable = new_executable,
37+
),
38+
)
39+
return providers
40+
41+
def get_transition_attrs(transition_rule):
42+
return {
43+
"_allowlist_function_transition": attr.label(
44+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
45+
),
46+
"dep": attr.label(mandatory = True, cfg = transition_rule),
47+
}

0 commit comments

Comments
 (0)