diff --git a/misc/bazel/rust.bzl b/misc/bazel/rust.bzl index 97afee4ba1c1..f9d41d1079bb 100644 --- a/misc/bazel/rust.bzl +++ b/misc/bazel/rust.bzl @@ -1,17 +1,44 @@ load("@rules_rust//rust:defs.bzl", "rust_binary") load("@semmle_code//buildutils-internal:glibc_symbols_check.bzl", "glibc_symbols_check") load("@semmle_code//buildutils-internal:lipo.bzl", "universal_binary") +load("@semmle_code//buildutils-internal:transitions.bzl", "forward_binary_from_transition", "get_transition_attrs") + +def _full_lto_transition_impl(_settings, _attr): + return {"@rules_rust//rust/settings:lto": "fat"} + +_full_lto_transition = transition( + implementation = _full_lto_transition_impl, + inputs = [], + outputs = ["@rules_rust//rust/settings:lto"], +) + +_full_lto_binary = rule( + implementation = forward_binary_from_transition, + attrs = get_transition_attrs(_full_lto_transition), +) def codeql_rust_binary( name, + full_lto = False, target_compatible_with = None, visibility = None, symbols_test = True, **kwargs): rust_label_name = "single_arch/" + name + binary_dep = ":" + rust_label_name + if full_lto: + # rustc must consume the LLVM bitcode because the C++ linker may use an + # incompatible LLVM version. + kwargs["experimental_use_cc_common_link"] = 0 + lto_label_name = "full_lto/" + name + _full_lto_binary( + name = lto_label_name, + dep = binary_dep, + ) + binary_dep = ":" + lto_label_name universal_binary( name = name, - dep = ":" + rust_label_name, + dep = binary_dep, target_compatible_with = target_compatible_with, visibility = visibility, ) diff --git a/misc/bazel/semmle_code_stub/buildutils-internal/transitions.bzl b/misc/bazel/semmle_code_stub/buildutils-internal/transitions.bzl new file mode 100644 index 000000000000..60bca5e5d179 --- /dev/null +++ b/misc/bazel/semmle_code_stub/buildutils-internal/transitions.bzl @@ -0,0 +1,21 @@ +def forward_binary_from_transition(ctx): + default_info = ctx.attr.dep[0][DefaultInfo] + original_executable = default_info.files_to_run.executable + executable = ctx.actions.declare_file(ctx.label.name) + ctx.actions.symlink( + output = executable, + target_file = original_executable, + is_executable = True, + ) + return [DefaultInfo( + executable = executable, + runfiles = default_info.default_runfiles, + )] + +def get_transition_attrs(transition_rule): + return { + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), + "dep": attr.label(mandatory = True, cfg = transition_rule), + } diff --git a/rust/extractor/BUILD.bazel b/rust/extractor/BUILD.bazel index 52b551f6335d..d3ae10fe39e8 100644 --- a/rust/extractor/BUILD.bazel +++ b/rust/extractor/BUILD.bazel @@ -11,6 +11,7 @@ codeql_rust_binary( "src/qltest_cargo.mustache", "src/nightly-toolchain/rust-toolchain.toml", ], + full_lto = True, proc_macro_deps = all_crate_deps( proc_macro = True, ) + [