Skip to content

Commit e1d3d89

Browse files
committed
Make it work
1 parent 65715a5 commit e1d3d89

File tree

6 files changed

+90
-36
lines changed

6 files changed

+90
-36
lines changed

rust/private/repository_utils.bzl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,35 @@ def BUILD_for_clippy(target_triple):
137137
system = triple_to_system(target_triple)
138138
return _build_file_for_clippy_template.format(binary_ext = system_to_binary_ext(system))
139139

140+
_build_file_for_llvm_tools = """\
141+
filegroup(
142+
name = "llvm_cov_bin",
143+
srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"],
144+
visibility = ["//visibility:public"],
145+
)
146+
147+
filegroup(
148+
name = "llvm_profdata_bin",
149+
srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"],
150+
visibility = ["//visibility:public"],
151+
)
152+
"""
153+
154+
def BUILD_for_llvm_tools(target_triple):
155+
"""Emits a BUILD file the llvm-tools binaries.
156+
157+
Args:
158+
target_triple (str): The triple of the target platform
159+
160+
Returns:
161+
str: The contents of a BUILD file
162+
"""
163+
system = triple_to_system(target_triple)
164+
return _build_file_for_llvm_tools.format(
165+
binary_ext = system_to_binary_ext(system),
166+
target_triple = target_triple,
167+
)
168+
140169
_build_file_for_stdlib_template = """\
141170
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
142171
@@ -189,6 +218,8 @@ rust_toolchain(
189218
rustfmt = {rustfmt_label},
190219
cargo = "@{workspace_name}//:cargo",
191220
clippy_driver = "@{workspace_name}//:clippy_driver_bin",
221+
llvm_cov = {llvm_cov_label},
222+
llvm_profdata = {llvm_profdata_label},
192223
rustc_lib = "@{workspace_name}//:rustc_lib",
193224
rustc_srcs = {rustc_srcs},
194225
binary_ext = "{binary_ext}",
@@ -211,6 +242,7 @@ def BUILD_for_rust_toolchain(
211242
include_rustc_srcs,
212243
default_edition,
213244
include_rustfmt,
245+
include_llvm_tools,
214246
stdlib_linkflags = None):
215247
"""Emits a toolchain declaration to match an existing compiler and stdlib.
216248
@@ -222,6 +254,7 @@ def BUILD_for_rust_toolchain(
222254
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.
223255
default_edition (str): Default Rust edition.
224256
include_rustfmt (bool): Whether rustfmt is present in the toolchain.
257+
include_llvm_tools (bool): Whether llvm-tools are present in the toolchain.
225258
stdlib_linkflags (list, optional): Overriden flags needed for linking to rust
226259
stdlib, akin to BAZEL_LINKLIBS. Defaults to
227260
None.
@@ -240,6 +273,11 @@ def BUILD_for_rust_toolchain(
240273
rustfmt_label = "None"
241274
if include_rustfmt:
242275
rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"".format(workspace_name = workspace_name)
276+
llvm_cov_label = "None"
277+
llvm_profdata_label = "None"
278+
if include_llvm_tools:
279+
llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"".format(workspace_name = workspace_name)
280+
llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"".format(workspace_name = workspace_name)
243281

244282
return _build_file_for_rust_toolchain_template.format(
245283
toolchain_name = name,
@@ -254,6 +292,8 @@ def BUILD_for_rust_toolchain(
254292
exec_triple = exec_triple,
255293
target_triple = target_triple,
256294
rustfmt_label = rustfmt_label,
295+
llvm_cov_label = llvm_cov_label,
296+
llvm_profdata_label = llvm_profdata_label,
257297
)
258298

259299
_build_file_for_toolchain_template = """\
@@ -369,12 +409,13 @@ filegroup(
369409
)""",
370410
)
371411

372-
def load_rust_stdlib(ctx, target_triple):
412+
def load_rust_stdlib(ctx, target_triple, include_llvm_tools):
373413
"""Loads a rust standard library and yields corresponding BUILD for it
374414
375415
Args:
376416
ctx (repository_ctx): A repository_ctx.
377417
target_triple (str): The rust-style target triple of the tool
418+
include_llvm_tools (bool): Whether to include LLVM tools in the toolchain
378419
379420
Returns:
380421
str: The BUILD file contents for this stdlib, and a toolchain decl to match
@@ -408,6 +449,7 @@ def load_rust_stdlib(ctx, target_triple):
408449
workspace_name = ctx.attr.name,
409450
default_edition = ctx.attr.edition,
410451
include_rustfmt = not (not ctx.attr.rustfmt_version),
452+
include_llvm_tools = include_llvm_tools,
411453
)
412454

413455
return stdlib_build_file + toolchain_build_file
@@ -449,6 +491,8 @@ def load_llvm_tools(ctx, target_triple):
449491
version = ctx.attr.version,
450492
)
451493

494+
return BUILD_for_llvm_tools(target_triple)
495+
452496
def check_version_valid(version, iso_date, param_prefix = ""):
453497
"""Verifies that the provided rust version and iso_date make sense.
454498

rust/private/rust.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ def _rust_test_common(ctx, toolchain, output):
410410
getattr(ctx.attr, "env", {}),
411411
data,
412412
)
413+
env["RUST_LLVM_COV"] = toolchain.llvm_cov.path
414+
env["RUST_LLVM_PROFDATA"] = toolchain.llvm_profdata.path
413415
providers.append(testing.TestEnvironment(env))
414416

415417
return providers

rust/private/rustc.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ def construct_arguments(
792792
rustc_flags.add("proc_macro")
793793

794794
if ctx.configuration.coverage_enabled:
795-
rustc_flags.add("-Cinstrument-coverage")
795+
rustc_flags.add("--codegen=instrument-coverage")
796796

797797
# Make bin crate data deps available to tests.
798798
for data in getattr(attr, "data", []):
@@ -979,8 +979,12 @@ def rustc_compile_action(
979979
),
980980
)
981981

982+
coverage_runfiles = []
983+
if toolchain.llvm_cov and ctx.configuration.coverage_enabled and crate_info.is_test:
984+
coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata]
985+
982986
runfiles = ctx.runfiles(
983-
files = getattr(ctx.files, "data", []),
987+
files = getattr(ctx.files, "data", []) + coverage_runfiles,
984988
collect_data = True,
985989
)
986990

rust/repositories.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ def _rust_toolchain_repository_impl(ctx):
163163
build_components.append(load_rustfmt(ctx))
164164

165165
# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
166-
if ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22"):
167-
load_llvm_tools(ctx, ctx.attr.exec_triple)
166+
include_llvm_tools = ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22")
167+
if include_llvm_tools:
168+
build_components.append(load_llvm_tools(ctx, ctx.attr.exec_triple))
168169

169170
for target_triple in [ctx.attr.exec_triple] + ctx.attr.extra_target_triples:
170-
build_components.append(load_rust_stdlib(ctx, target_triple))
171+
build_components.append(load_rust_stdlib(ctx, target_triple, include_llvm_tools))
171172

172173
# extra_target_triples contains targets such as wasm, which don't have rustc_dev components
173174
if ctx.attr.dev_components and target_triple not in ctx.attr.extra_target_triples:

rust/toolchain.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ def _rust_toolchain_impl(ctx):
465465
dylib_ext = ctx.attr.dylib_ext,
466466
exec_triple = ctx.attr.exec_triple,
467467
libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library),
468+
llvm_cov = ctx.file.llvm_cov,
469+
llvm_profdata = ctx.file.llvm_profdata,
468470
os = ctx.attr.os,
469471
rust_doc = sysroot.rustdoc,
470472
rust_lib = sysroot.rust_std, # `rust_lib` is deprecated and only exists for legacy support.
@@ -535,6 +537,16 @@ rust_toolchain = rule(
535537
),
536538
mandatory = True,
537539
),
540+
"llvm_cov": attr.label(
541+
doc = "The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item.",
542+
allow_single_file = True,
543+
cfg = "exec",
544+
),
545+
"llvm_profdata": attr.label(
546+
doc = "The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item.",
547+
allow_single_file = True,
548+
cfg = "exec",
549+
),
538550
"llvm_tools": attr.label(
539551
doc = "LLVM tools that are shipped with the Rust toolchain.",
540552
allow_files = True,

util/collect_coverage.sh

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
set -euo pipefail
4-
set -x
54

6-
echo here!
7-
8-
# export LLVM_PROFILE_FILE="${COVERAGE_DIR}/%h-%p-%m.profraw"
9-
output_file=$COVERAGE_DIR/_cc_coverage.dat
10-
11-
/Users/ksmiley/dev/rules_rust/examples/bazel-examples/external/rust_darwin_aarch64/lib/rustlib/aarch64-apple-darwin/bin/llvm-profdata \
12-
merge --sparse \
13-
"${COVERAGE_DIR}"/*.profraw \
14-
-output "${output_file}.data"
15-
16-
# object_param=""
17-
# while read -r line; do
18-
# if [[ ${line: -24} == "runtime_objects_list.txt" ]]; then
19-
# while read -r line_runtime_object; do
20-
# if [[ -e "${RUNFILES_DIR}/${TEST_WORKSPACE}/${line_runtime_object}" ]]; then
21-
# object_param+=" -object ${RUNFILES_DIR}/${TEST_WORKSPACE}/${line_runtime_object}"
22-
# fi
23-
# done < "${line}"
24-
# fi
25-
# done < "${COVERAGE_MANIFEST}"
26-
27-
28-
# /Users/ksmiley/dev/rules_rust/examples/bazel-examples/external/rust_darwin_aarch64/lib/rustlib/aarch64-apple-darwin/bin/llvm-cov export -instr-profile "${output_file}.data" -format=lcov \
29-
# -ignore-filename-regex='.*external/.+' \
30-
# -ignore-filename-regex='/tmp/.+' \
31-
# ${object_param} | sed 's#/proc/self/cwd/##' > "${output_file}"
32-
33-
# exit 1
5+
if [[ -n "${VERBOSE_COVERAGE:-}" ]]; then
6+
set -x
7+
fi
8+
9+
readonly profdata_file=$COVERAGE_DIR/coverage.profdata
10+
11+
"$RUNFILES_DIR/$TEST_WORKSPACE/$RUST_LLVM_PROFDATA" \
12+
merge \
13+
--sparse "$COVERAGE_DIR"/*.profraw \
14+
-output "$profdata_file"
15+
16+
"$RUNFILES_DIR/$TEST_WORKSPACE/$RUST_LLVM_COV" \
17+
export \
18+
-format=lcov \
19+
-instr-profile "$profdata_file" \
20+
-ignore-filename-regex='.*external/.+' \
21+
-ignore-filename-regex='/tmp/.+' \
22+
-path-equivalence="$ROOT",. \
23+
"$RUNFILES_DIR/$TEST_WORKSPACE/$TEST_BINARY" \
24+
| sed 's#/proc/self/cwd/##' > "$COVERAGE_OUTPUT_FILE"

0 commit comments

Comments
 (0)