Skip to content

Commit

Permalink
Support setting file name of LLVM_PROFILE_FILE (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored Jan 18, 2024
1 parent 59cba33 commit 1b26281
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Support setting file name of `LLVM_PROFILE_FILE`. ([#340](https://github.com/taiki-e/cargo-llvm-cov/pull/340))

## [0.6.1] - 2024-01-13

- Support `--target` option for `cargo llvm-cov nextest --archive-file`. ([#334](https://github.com/taiki-e/cargo-llvm-cov/pull/334))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ You can override these environment variables to change cargo-llvm-cov's behavior
- `LLVM_PROFDATA` -- Override the path to `llvm-profdata`. See `LLVM_COV` environment variable for more.
- `LLVM_COV_FLAGS` -- A space-separated list of additional flags to pass to all `llvm-cov` invocations that cargo-llvm-cov performs. See [LLVM documentation](https://llvm.org/docs/CommandGuide/llvm-cov.html) for available options.
- `LLVM_PROFDATA_FLAGS` -- A space-separated list of additional flags to pass to all `llvm-profdata` invocations that cargo-llvm-cov performs. See [LLVM documentation](https://llvm.org/docs/CommandGuide/llvm-profdata.html) for available options.
- `LLVM_PROFILE_FILE_NAME` -- Override the file name (the final component of the path) of the `LLVM_PROFILE_FILE`. See [LLVM documentation](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program) for available syntax.

See also [environment variables that Cargo reads](https://doc.rust-lang.org/nightly/cargo/reference/environment-variables.html#environment-variables-cargo-reads). cargo-llvm-cov respects many of them.

Expand Down
57 changes: 32 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,28 +210,37 @@ fn set_env(cx: &Context, env: &mut dyn EnvTarget, IsNextest(is_nextest): IsNexte
}
}

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");
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();
Expand Down Expand Up @@ -621,9 +630,7 @@ fn open_report(cx: &Context, path: &Utf8Path) -> Result<()> {
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(format!("{}-*.profraw", cx.ws.name))
.as_str(),
Utf8Path::new(&glob::Pattern::escape(cx.ws.target_dir.as_str())).join("*.profraw").as_str(),
)?
.filter_map(Result::ok)
.collect::<Vec<_>>();
Expand Down

0 comments on commit 1b26281

Please sign in to comment.