-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.rs
1266 lines (1164 loc) · 43.7 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0 OR MIT
#![forbid(unsafe_code)]
// Refs:
// - https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html
// - https://llvm.org/docs/CommandGuide/llvm-profdata.html
// - https://llvm.org/docs/CommandGuide/llvm-cov.html
use std::{
collections::{BTreeSet, HashMap},
ffi::{OsStr, OsString},
io::{self, BufRead, Write},
path::Path,
time::SystemTime,
};
use anyhow::{bail, Context as _, Result};
use camino::{Utf8Path, Utf8PathBuf};
use cargo_config2::Flags;
use cargo_llvm_cov::json::{CodeCovJsonExport, CoverageKind, LlvmCovJsonExport};
use regex::Regex;
use walkdir::WalkDir;
use crate::{
cargo::Workspace,
cli::{Args, ShowEnvOptions, Subcommand},
context::Context,
metadata::Metadata,
process::ProcessBuilder,
regex_vec::{RegexVec, RegexVecBuilder},
term::Coloring,
};
#[macro_use]
mod term;
#[macro_use]
mod process;
mod cargo;
mod clean;
mod cli;
mod context;
mod demangle;
mod env;
mod fs;
mod metadata;
mod regex_vec;
fn main() {
term::init_coloring();
if let Err(e) = try_main() {
error!("{e:#}");
}
if term::error()
|| term::warn()
&& env::var_os("CARGO_LLVM_COV_DENY_WARNINGS").filter(|v| v == "true").is_some()
{
std::process::exit(1)
}
}
fn try_main() -> Result<()> {
let mut args = Args::parse()?;
term::verbose::set(args.verbose != 0);
match args.subcommand {
Subcommand::Demangle => demangle::run()?,
Subcommand::Clean => clean::run(&mut args)?,
Subcommand::ShowEnv => {
let cx = &Context::new(args)?;
let stdout = io::stdout();
let writer =
&mut ShowEnvWriter { writer: stdout.lock(), options: cx.args.show_env.clone() };
set_env(cx, writer, IsNextest(true))?; // Include envs for nextest.
writer.set("CARGO_LLVM_COV_TARGET_DIR", cx.ws.metadata.target_directory.as_str())?;
}
Subcommand::Report => {
let cx = &Context::new(args)?;
create_dirs(cx)?;
generate_report(cx)?;
}
Subcommand::Run => {
let cx = &Context::new(args)?;
clean::clean_partial(cx)?;
create_dirs(cx)?;
run_run(cx)?;
if !cx.args.cov.no_report {
generate_report(cx)?;
}
}
Subcommand::Nextest { .. } => {
let cx = &Context::new(args)?;
clean::clean_partial(cx)?;
create_dirs(cx)?;
run_nextest(cx)?;
if !cx.args.cov.no_report {
generate_report(cx)?;
}
}
Subcommand::NextestArchive => {
let cx = &Context::new(args)?;
clean::clean_partial(cx)?;
create_dirs(cx)?;
archive_nextest(cx)?;
}
Subcommand::None | Subcommand::Test => {
let cx = &Context::new(args)?;
clean::clean_partial(cx)?;
create_dirs(cx)?;
run_test(cx)?;
if !cx.args.cov.no_report {
generate_report(cx)?;
}
}
}
Ok(())
}
fn create_dirs(cx: &Context) -> Result<()> {
fs::create_dir_all(&cx.ws.target_dir)?;
if let Some(output_dir) = &cx.args.cov.output_dir {
fs::create_dir_all(output_dir)?;
if cx.args.cov.html {
fs::create_dir_all(output_dir.join("html"))?;
}
if cx.args.cov.text {
fs::create_dir_all(output_dir.join("text"))?;
}
}
if cx.args.doctests {
fs::create_dir_all(&cx.ws.doctests_dir)?;
}
Ok(())
}
trait EnvTarget {
fn set(&mut self, key: &str, value: &str) -> Result<()>;
fn unset(&mut self, key: &str) -> Result<()>;
}
impl EnvTarget for ProcessBuilder {
fn set(&mut self, key: &str, value: &str) -> Result<()> {
self.env(key, value);
Ok(())
}
fn unset(&mut self, key: &str) -> Result<()> {
self.env_remove(key);
Ok(())
}
}
struct ShowEnvWriter<W: io::Write> {
writer: W,
options: ShowEnvOptions,
}
impl<W: io::Write> EnvTarget for ShowEnvWriter<W> {
fn set(&mut self, key: &str, value: &str) -> Result<()> {
let prefix = if self.options.export_prefix { "export " } else { "" };
writeln!(self.writer, "{prefix}{key}={}", shell_escape::escape(value.into()))
.context("failed to write env to stdout")
}
fn unset(&mut self, key: &str) -> Result<()> {
if env::var_os(key).is_some() {
warn!("cannot unset environment variable `{key}`");
}
Ok(())
}
}
struct IsNextest(bool);
fn set_env(cx: &Context, env: &mut dyn EnvTarget, IsNextest(is_nextest): IsNextest) -> Result<()> {
fn push_common_flags(cx: &Context, flags: &mut Flags) {
if cx.ws.stable_coverage {
flags.push("-C");
flags.push("instrument-coverage");
} else {
flags.push("-Z");
flags.push("instrument-coverage");
if cx.ws.target_for_config.triple().contains("-windows") {
// `-C codegen-units=1` is needed to work around link error on windows
// https://github.com/rust-lang/rust/issues/85461
// https://github.com/microsoft/windows-rs/issues/1006#issuecomment-887789950
// This has been fixed in https://github.com/rust-lang/rust/pull/91470,
// but old nightly compilers still need this.
flags.push("-C");
flags.push("codegen-units=1");
}
}
// Workaround for https://github.com/rust-lang/rust/issues/91092.
// Unnecessary since https://github.com/rust-lang/rust/pull/111469.
if if cx.ws.rustc_version.nightly {
cx.ws.rustc_version.minor <= 71
} else {
cx.ws.rustc_version.minor < 71
} {
flags.push("-C");
flags.push("llvm-args=--instrprof-atomic-counter-update-all");
}
if !cx.args.cov.no_cfg_coverage {
flags.push("--cfg=coverage");
}
if cx.ws.rustc_version.nightly && !cx.args.cov.no_cfg_coverage_nightly {
flags.push("--cfg=coverage_nightly");
}
}
let llvm_profile_file_name =
if let Some(llvm_profile_file_name) = env::var("LLVM_PROFILE_FILE_NAME")? {
if !llvm_profile_file_name.ends_with(".profraw") {
bail!("extension of LLVM_PROFILE_FILE_NAME must be 'profraw'");
}
llvm_profile_file_name
} else {
let mut llvm_profile_file_name = format!("{}-%p", cx.ws.name);
if is_nextest {
// https://github.com/taiki-e/cargo-llvm-cov/issues/258
// https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
// Select the number of threads that is the same as the one nextest uses by default here.
// https://github.com/nextest-rs/nextest/blob/c54694dfe7be016993983b5dedbcf2b50d4b1a6e/nextest-runner/src/config/test_threads.rs
// https://github.com/nextest-rs/nextest/blob/c54694dfe7be016993983b5dedbcf2b50d4b1a6e/nextest-runner/src/config/config_impl.rs#L30
// TODO: should we respect custom test-threads?
// - If the number of threads specified by the user is negative or
// less or equal to available cores, it should not really be a problem
// because it does not exceed the number of available cores.
// - Even if the number of threads specified by the user is greater than
// available cores, it is expected that the number of threads that can
// write simultaneously will not exceed the number of available cores.
llvm_profile_file_name.push_str(&format!(
"-%{}m",
std::thread::available_parallelism().map_or(1, usize::from)
));
} else {
llvm_profile_file_name.push_str("-%m");
}
llvm_profile_file_name.push_str(".profraw");
llvm_profile_file_name
};
let llvm_profile_file = cx.ws.target_dir.join(llvm_profile_file_name);
let rustflags = &mut cx.ws.config.rustflags(&cx.ws.target_for_config)?.unwrap_or_default();
push_common_flags(cx, rustflags);
if cx.args.remap_path_prefix {
rustflags.push("--remap-path-prefix");
rustflags.push(format!("{}/=", cx.ws.metadata.workspace_root));
}
if cx.args.target.is_none() {
// https://github.com/dtolnay/trybuild/pull/121
// https://github.com/dtolnay/trybuild/issues/122
// https://github.com/dtolnay/trybuild/pull/123
rustflags.push("--cfg=trybuild_no_target");
}
// https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html#including-doc-tests
let rustdocflags = &mut cx.ws.config.build.rustdocflags.clone();
if cx.args.doctests {
let rustdocflags = rustdocflags.get_or_insert_with(Flags::default);
push_common_flags(cx, rustdocflags);
rustdocflags.push("-Z");
rustdocflags.push("unstable-options");
rustdocflags.push("--persist-doctests");
rustdocflags.push(cx.ws.doctests_dir.as_str());
}
match (cx.args.coverage_target_only, &cx.args.target) {
(true, Some(coverage_target)) => {
env.set(
&format!("CARGO_TARGET_{}_RUSTFLAGS", target_u_upper(coverage_target)),
&rustflags.encode_space_separated()?,
)?;
env.unset("RUSTFLAGS")?;
env.unset("CARGO_ENCODED_RUSTFLAGS")?;
}
_ => {
// First, try with RUSTFLAGS because `nextest` subcommand sometimes doesn't work well with encoded flags.
if let Ok(v) = rustflags.encode_space_separated() {
env.set("RUSTFLAGS", &v)?;
env.unset("CARGO_ENCODED_RUSTFLAGS")?;
} else {
env.set("CARGO_ENCODED_RUSTFLAGS", &rustflags.encode()?)?;
}
}
}
if let Some(rustdocflags) = rustdocflags {
// First, try with RUSTDOCFLAGS because `nextest` subcommand sometimes doesn't work well with encoded flags.
if let Ok(v) = rustdocflags.encode_space_separated() {
env.set("RUSTDOCFLAGS", &v)?;
env.unset("CARGO_ENCODED_RUSTDOCFLAGS")?;
} else {
env.set("CARGO_ENCODED_RUSTDOCFLAGS", &rustdocflags.encode()?)?;
}
}
if cx.args.include_ffi {
// https://github.com/rust-lang/cc-rs/blob/1.0.73/src/lib.rs#L2347-L2365
// Environment variables that use hyphens are not available in many environments, so we ignore them for now.
let target_u = target_u_lower(cx.ws.target_for_config.triple());
let cflags_key = &format!("CFLAGS_{target_u}");
// Use std::env instead of crate::env to match cc-rs's behavior.
// https://github.com/rust-lang/cc-rs/blob/1.0.73/src/lib.rs#L2740
let mut cflags = match std::env::var(cflags_key) {
Ok(cflags) => cflags,
Err(_) => match std::env::var("TARGET_CFLAGS") {
Ok(cflags) => cflags,
Err(_) => std::env::var("CFLAGS").unwrap_or_default(),
},
};
let cxxflags_key = &format!("CXXFLAGS_{target_u}");
let mut cxxflags = match std::env::var(cxxflags_key) {
Ok(cxxflags) => cxxflags,
Err(_) => match std::env::var("TARGET_CXXFLAGS") {
Ok(cxxflags) => cxxflags,
Err(_) => std::env::var("CXXFLAGS").unwrap_or_default(),
},
};
let clang_flags = " -fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic";
cflags.push_str(clang_flags);
cxxflags.push_str(clang_flags);
env.set(cflags_key, &cflags)?;
env.set(cxxflags_key, &cxxflags)?;
}
env.set("LLVM_PROFILE_FILE", llvm_profile_file.as_str())?;
env.set("CARGO_LLVM_COV", "1")?;
if cx.args.subcommand == Subcommand::ShowEnv {
env.set("CARGO_LLVM_COV_SHOW_ENV", "1")?;
}
Ok(())
}
fn has_z_flag(args: &[String], name: &str) -> bool {
let mut iter = args.iter().map(String::as_str);
while let Some(mut arg) = iter.next() {
if arg == "-Z" {
arg = iter.next().unwrap();
} else if let Some(a) = arg.strip_prefix("-Z") {
arg = a;
} else {
continue;
}
if let Some(rest) = arg.strip_prefix(name) {
if rest.is_empty() || rest.starts_with('=') {
return true;
}
}
}
false
}
fn run_test(cx: &Context) -> Result<()> {
let mut cargo = cx.cargo();
set_env(cx, &mut cargo, IsNextest(false))?;
cargo.arg("test");
if cx.ws.need_doctest_in_workspace && !has_z_flag(&cx.args.cargo_args, "doctest-in-workspace") {
// https://github.com/rust-lang/cargo/issues/9427
cargo.arg("-Z");
cargo.arg("doctest-in-workspace");
}
if cx.args.ignore_run_fail {
{
let mut cargo = cargo.clone();
cargo.arg("--no-run");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
cargo.stdout_to_stderr().run()?;
} else {
// Capture output to prevent duplicate warnings from appearing in two runs.
cargo.run_with_output()?;
}
}
cargo.arg("--no-fail-fast");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
if let Err(e) = cargo.run() {
warn!("{e:#}");
}
} else {
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
cargo.run()?;
}
Ok(())
}
fn archive_nextest(cx: &Context) -> Result<()> {
let mut cargo = cx.cargo();
set_env(cx, &mut cargo, IsNextest(true))?;
cargo.arg("nextest").arg("archive");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
cargo.run()?;
Ok(())
}
fn run_nextest(cx: &Context) -> Result<()> {
let mut cargo = cx.cargo();
set_env(cx, &mut cargo, IsNextest(true))?;
cargo.arg("nextest").arg("run");
if cx.args.ignore_run_fail {
{
let mut cargo = cargo.clone();
cargo.arg("--no-run");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
cargo.stdout_to_stderr().run()?;
} else {
// Capture output to prevent duplicate warnings from appearing in two runs.
cargo.run_with_output()?;
}
}
cargo.arg("--no-fail-fast");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
if let Err(e) = cargo.run() {
warn!("{e:#}");
}
} else {
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
cargo.run()?;
}
Ok(())
}
fn run_run(cx: &Context) -> Result<()> {
let mut cargo = cx.cargo();
set_env(cx, &mut cargo, IsNextest(false))?;
if cx.args.ignore_run_fail {
{
let mut cargo = cargo.clone();
cargo.arg("build");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
cargo.stdout_to_stderr().run()?;
} else {
// Capture output to prevent duplicate warnings from appearing in two runs.
cargo.run_with_output()?;
}
}
cargo.arg("run");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
if let Err(e) = cargo.run() {
warn!("{e:#}");
}
} else {
cargo.arg("run");
cargo::test_or_run_args(cx, &mut cargo);
if term::verbose() {
status!("Running", "{cargo}");
}
stdout_to_stderr(cx, &mut cargo);
cargo.run()?;
}
Ok(())
}
fn stdout_to_stderr(cx: &Context, cargo: &mut ProcessBuilder) {
if cx.args.cov.no_report
|| cx.args.cov.output_dir.is_some()
|| cx.args.cov.output_path.is_some()
{
// Do not redirect if unnecessary.
} else {
// Redirect stdout to stderr as the report is output to stdout by default.
cargo.stdout_to_stderr();
}
}
fn generate_report(cx: &Context) -> Result<()> {
merge_profraw(cx).context("failed to merge profile data")?;
let object_files = object_files(cx).context("failed to collect object files")?;
let ignore_filename_regex = ignore_filename_regex(cx);
let format = Format::from_args(cx);
format
.generate_report(cx, &object_files, ignore_filename_regex.as_deref())
.context("failed to generate report")?;
if cx.args.cov.fail_under_functions.is_some()
|| cx.args.cov.fail_under_lines.is_some()
|| cx.args.cov.fail_under_regions.is_some()
|| cx.args.cov.fail_uncovered_functions.is_some()
|| cx.args.cov.fail_uncovered_lines.is_some()
|| cx.args.cov.fail_uncovered_regions.is_some()
|| cx.args.cov.show_missing_lines
{
let format = Format::Json;
let json = format
.get_json(cx, &object_files, ignore_filename_regex.as_ref())
.context("failed to get json")?;
if let Some(fail_under_functions) = cx.args.cov.fail_under_functions {
// Handle --fail-under-functions.
let functions_percent = json
.get_coverage_percent(CoverageKind::Functions)
.context("failed to get function coverage")?;
if functions_percent < fail_under_functions {
term::error::set(true);
}
}
if let Some(fail_under_lines) = cx.args.cov.fail_under_lines {
// Handle --fail-under-lines.
let lines_percent = json
.get_coverage_percent(CoverageKind::Lines)
.context("failed to get line coverage")?;
if lines_percent < fail_under_lines {
term::error::set(true);
}
}
if let Some(fail_under_regions) = cx.args.cov.fail_under_regions {
// Handle --fail-under-regions.
let regions_percent = json
.get_coverage_percent(CoverageKind::Regions)
.context("failed to get region coverage")?;
if regions_percent < fail_under_regions {
term::error::set(true);
}
}
if let Some(fail_uncovered_functions) = cx.args.cov.fail_uncovered_functions {
// Handle --fail-uncovered-functions.
let uncovered =
json.count_uncovered_functions().context("failed to count uncovered functions")?;
if uncovered > fail_uncovered_functions {
term::error::set(true);
}
}
if let Some(fail_uncovered_lines) = cx.args.cov.fail_uncovered_lines {
// Handle --fail-uncovered-lines.
let uncovered_files = json.get_uncovered_lines(ignore_filename_regex.as_deref());
let uncovered = uncovered_files
.iter()
.fold(0_u64, |uncovered, (_, lines)| uncovered + lines.len() as u64);
if uncovered > fail_uncovered_lines {
term::error::set(true);
}
}
if let Some(fail_uncovered_regions) = cx.args.cov.fail_uncovered_regions {
// Handle --fail-uncovered-regions.
let uncovered =
json.count_uncovered_regions().context("failed to count uncovered regions")?;
if uncovered > fail_uncovered_regions {
term::error::set(true);
}
}
if cx.args.cov.show_missing_lines {
// Handle --show-missing-lines.
let uncovered_files = json.get_uncovered_lines(ignore_filename_regex.as_deref());
if !uncovered_files.is_empty() {
let stdout = io::stdout();
let mut stdout = stdout.lock();
writeln!(stdout, "Uncovered Lines:")?;
for (file, lines) in &uncovered_files {
let lines: Vec<_> = lines.iter().map(ToString::to_string).collect();
writeln!(stdout, "{file}: {}", lines.join(", "))?;
}
stdout.flush()?;
}
}
}
if cx.args.cov.open {
let path = &cx.args.cov.output_dir.as_ref().unwrap().join("html/index.html");
status!("Opening", "{path}");
open_report(cx, path)?;
}
Ok(())
}
fn open_report(cx: &Context, path: &Utf8Path) -> Result<()> {
match &cx.ws.config.doc.browser {
Some(browser) => {
cmd!(&browser.path)
.args(&browser.args)
.arg(path)
.run()
.with_context(|| format!("couldn't open report with {}", browser.path.display()))?;
}
None => opener::open(path).context("couldn't open report")?,
}
Ok(())
}
fn merge_profraw(cx: &Context) -> Result<()> {
// Convert raw profile data.
let profraw_files = glob::glob(
Utf8Path::new(&glob::Pattern::escape(cx.ws.target_dir.as_str())).join("*.profraw").as_str(),
)?
.filter_map(Result::ok)
.collect::<Vec<_>>();
if profraw_files.is_empty() {
warn!(
"not found *.profraw files in {}; this may occur if target directory is accidentally \
cleared, or running report subcommand without running any tests or binaries",
cx.ws.target_dir
);
}
let mut input_files = String::new();
for path in profraw_files {
input_files.push_str(
path.to_str().with_context(|| format!("{path:?} contains invalid utf-8 data"))?,
);
input_files.push('\n');
}
let input_files_path = &cx.ws.target_dir.join(format!("{}-profraw-list", cx.ws.name));
fs::write(input_files_path, input_files)?;
let mut cmd = cx.process(&cx.llvm_profdata);
cmd.args(["merge", "-sparse"])
.arg("-f")
.arg(input_files_path)
.arg("-o")
.arg(&cx.ws.profdata_file);
if let Some(mode) = &cx.args.cov.failure_mode {
cmd.arg(format!("-failure-mode={mode}"));
}
if let Some(flags) = &cx.llvm_profdata_flags {
cmd.args(flags.split(' ').filter(|s| !s.trim().is_empty()));
}
if term::verbose() {
status!("Running", "{cmd}");
}
cmd.stdout_to_stderr().run()?;
Ok(())
}
fn object_files(cx: &Context) -> Result<Vec<OsString>> {
fn walk_target_dir<'a>(
cx: &'a Context,
target_dir: &Utf8Path,
) -> impl Iterator<Item = walkdir::DirEntry> + 'a {
WalkDir::new(target_dir)
.into_iter()
.filter_entry(move |e| {
let p = e.path();
if p.is_dir() {
if p.file_name()
.map_or(false, |f| f == "incremental" || f == ".fingerprint" || f == "out")
{
// Ignore incremental compilation related files and output from build scripts.
return false;
}
} else if let Some(stem) = p.file_stem() {
let stem = stem.to_string_lossy();
if stem == "build-script-build" || stem.starts_with("build_script_build-") {
let p = p.parent().unwrap();
if p.parent().unwrap().file_name().unwrap() == "build" {
if cx.args.cov.include_build_script {
let dir = p.file_name().unwrap().to_string_lossy();
if !cx.build_script_re.is_match(&dir) {
return false;
}
} else {
return false;
}
}
}
}
true
})
.filter_map(Result::ok)
}
fn is_object(cx: &Context, f: &Path) -> bool {
let ext = f.extension().unwrap_or_default();
// is_executable::is_executable doesn't work well on WSL.
// https://github.com/taiki-e/cargo-llvm-cov/issues/316
// https://github.com/taiki-e/cargo-llvm-cov/issues/342
if ext == "d" || ext == "rmeta" {
return false;
}
if cx.ws.target_for_config.triple().contains("-windows")
&& (ext.eq_ignore_ascii_case("exe") || ext.eq_ignore_ascii_case("dll"))
{
return true;
}
is_executable::is_executable(f)
}
let re = Targets::new(&cx.ws).pkg_hash_re()?;
let mut files = vec![];
let mut searched_dir = String::new();
// To support testing binary crate like tests that use the CARGO_BIN_EXE
// environment variable, pass all compiled executables.
// This is not the ideal way, but the way unstable book says it is cannot support them.
// https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html#tips-for-listing-the-binaries-automatically
let mut target_dir = cx.ws.target_dir.clone();
if matches!(cx.args.subcommand, Subcommand::Nextest { archive_file: true }) {
target_dir.push("target");
}
// https://doc.rust-lang.org/nightly/cargo/guide/build-cache.html
if let Some(target) = &cx.args.target {
target_dir.push(target);
}
// https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles
let profile = match cx.args.profile.as_deref() {
None if cx.args.release => "release",
Some("release" | "bench") => "release",
None | Some("dev" | "test") => "debug",
Some(p) => p,
};
target_dir.push(profile);
for f in walk_target_dir(cx, &target_dir) {
let f = f.path();
if is_object(cx, f) {
if let Some(file_stem) = fs::file_stem_recursive(f).unwrap().to_str() {
if re.is_match(file_stem) {
files.push(make_relative(cx, f).to_owned().into_os_string());
}
}
}
}
searched_dir.push_str(target_dir.as_str());
if cx.args.doctests {
for f in glob::glob(
Utf8Path::new(&glob::Pattern::escape(cx.ws.doctests_dir.as_str()))
.join("*/rust_out")
.as_str(),
)?
.filter_map(Result::ok)
{
if is_object(cx, &f) {
files.push(make_relative(cx, &f).to_owned().into_os_string());
}
}
searched_dir.push(',');
searched_dir.push_str(cx.ws.doctests_dir.as_str());
}
// trybuild
let mut trybuild_target_dir = cx.ws.trybuild_target_dir();
if let Some(target) = &cx.args.target {
trybuild_target_dir.push(target);
}
// Currently, trybuild always use debug build.
trybuild_target_dir.push("debug");
if trybuild_target_dir.is_dir() {
let mut trybuild_targets = vec![];
for metadata in trybuild_metadata(&cx.ws, &cx.ws.metadata.target_directory)? {
for package in metadata.packages.into_values() {
for target in package.targets {
trybuild_targets.push(target.name);
}
}
}
if !trybuild_targets.is_empty() {
let re =
Regex::new(&format!("^({})(-[0-9a-f]+)?$", trybuild_targets.join("|"))).unwrap();
for entry in walk_target_dir(cx, &trybuild_target_dir) {
let path = make_relative(cx, entry.path());
if let Some(file_stem) = fs::file_stem_recursive(path).unwrap().to_str() {
if re.is_match(file_stem) {
continue;
}
}
if is_object(cx, path) {
files.push(path.to_owned().into_os_string());
}
}
searched_dir.push(',');
searched_dir.push_str(trybuild_target_dir.as_str());
}
}
// This sort is necessary to make the result of `llvm-cov show` match between macos and linux.
files.sort_unstable();
if files.is_empty() {
warn!(
"not found object files (searched directories: {searched_dir}); this may occur if \
show-env subcommand is used incorrectly (see docs or other warnings), or unsupported \
commands such as nextest archive are used",
);
}
Ok(files)
}
struct Targets {
packages: BTreeSet<String>,
targets: BTreeSet<String>,
}
impl Targets {
fn new(ws: &Workspace) -> Self {
let mut packages = BTreeSet::new();
let mut targets = BTreeSet::new();
for id in &ws.metadata.workspace_members {
let pkg = &ws.metadata.packages[id];
packages.insert(pkg.name.clone());
for t in &pkg.targets {
targets.insert(t.name.clone());
}
}
Self { packages, targets }
}
fn pkg_hash_re(&self) -> Result<RegexVec> {
let mut re = RegexVecBuilder::new("^(lib)?(", ")(-[0-9a-f]+)?$");
for pkg in &self.packages {
re.or(&pkg.replace('-', "(-|_)"));
}
for t in &self.targets {
re.or(&t.replace('-', "(-|_)"));
}
re.build()
}
}
/// Collects metadata for packages generated by trybuild. If the trybuild test
/// directory is not found, it returns an empty vector.
fn trybuild_metadata(ws: &Workspace, target_dir: &Utf8Path) -> Result<Vec<Metadata>> {
// https://github.com/dtolnay/trybuild/pull/219
let mut trybuild_dir = target_dir.join("tests").join("trybuild");
if !trybuild_dir.is_dir() {
trybuild_dir.pop();
if !trybuild_dir.is_dir() {
return Ok(vec![]);
}
}
let mut metadata = vec![];
for entry in fs::read_dir(trybuild_dir)?.filter_map(Result::ok) {
let manifest_path = &entry.path().join("Cargo.toml");
if !manifest_path.is_file() {
continue;
}
metadata.push(Metadata::new(manifest_path, ws.config.cargo())?);
}
Ok(metadata)
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Format {
/// `llvm-cov report`
None,
/// `llvm-cov export -format=text`
Json,
/// `llvm-cov export -format=lcov`
LCov,
/// `llvm-cov export -format=lcov` later converted to XML
Cobertura,
/// `llvm-cov show -format=lcov` later converted to Codecov JSON
Codecov,
/// `llvm-cov show -format=text`
Text,
/// `llvm-cov show -format=html`
Html,
}
impl Format {
fn from_args(cx: &Context) -> Self {
if cx.args.cov.json {
Self::Json
} else if cx.args.cov.lcov {
Self::LCov
} else if cx.args.cov.cobertura {
Self::Cobertura
} else if cx.args.cov.codecov {
Self::Codecov
} else if cx.args.cov.text {
Self::Text
} else if cx.args.cov.html {
Self::Html
} else {
Self::None
}
}
const fn llvm_cov_args(self) -> &'static [&'static str] {
match self {
Self::None => &["report"],
Self::Json | Self::Codecov => &["export", "-format=text"],
Self::LCov | Self::Cobertura => &["export", "-format=lcov"],
Self::Text => &["show", "-format=text"],
Self::Html => &["show", "-format=html"],
}
}
fn use_color(self, cx: &Context) -> Option<&'static str> {
if matches!(self, Self::Json | Self::LCov | Self::Html) {
// `llvm-cov export` doesn't have `-use-color` flag.
// https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-export
// Color output cannot be disabled when generating html.
return None;
}
if self == Self::Text && cx.args.cov.output_dir.is_some() {
return Some("-use-color=0");
}
match cx.args.color {
Some(Coloring::Auto) | None => None,
Some(Coloring::Always) => Some("-use-color=1"),
Some(Coloring::Never) => Some("-use-color=0"),
}
}
fn generate_report(
self,
cx: &Context,
object_files: &[OsString],
ignore_filename_regex: Option<&str>,
) -> Result<()> {
let mut cmd = cx.process(&cx.llvm_cov);
cmd.args(self.llvm_cov_args());
cmd.args(self.use_color(cx));
cmd.arg(format!("-instr-profile={}", cx.ws.profdata_file));
cmd.args(object_files.iter().flat_map(|f| [OsStr::new("-object"), f]));
if let Some(ignore_filename_regex) = ignore_filename_regex {
cmd.arg("-ignore-filename-regex");
cmd.arg(ignore_filename_regex);
}
match self {
Self::Text | Self::Html => {
cmd.args([
&format!("-show-instantiations={}", cx.args.cov.show_instantiations),
"-show-line-counts-or-regions",
"-show-expansions",
"-show-branches=count",
&format!("-Xdemangler={}", cx.current_exe.display()),
"-Xdemangler=llvm-cov",
"-Xdemangler=demangle",
]);
if let Some(output_dir) = &cx.args.cov.output_dir {
if self == Self::Html {
cmd.arg(format!("-output-dir={}", output_dir.join("html")));
} else {
cmd.arg(format!("-output-dir={}", output_dir.join("text")));
}
}
}
Self::Json | Self::LCov | Self::Cobertura | Self::Codecov => {
if cx.args.cov.summary_only {
cmd.arg("-summary-only");
}
if cx.args.cov.skip_functions {
cmd.arg("-skip-functions");
}
}
Self::None => {}
}
if let Some(flags) = &cx.llvm_cov_flags {
cmd.args(flags.split(' ').filter(|s| !s.trim().is_empty()));
}
if cx.args.cov.cobertura {
if term::verbose() {
status!("Running", "{cmd}");
}
let lcov = cmd.read()?;
// Convert to XML
let cdata = lcov2cobertura::parse_lines(
lcov.as_bytes().lines(),
&cx.ws.metadata.workspace_root,
&[],
)?;