Skip to content

Commit

Permalink
test(pgo): determine llvm-profdata existence at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Nov 30, 2024
1 parent fb887c8 commit 4e1de0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ jobs:
- run: rustup target add ${{ matrix.other }}
- run: rustup component add rustc-dev llvm-tools-preview rust-docs
if: startsWith(matrix.rust, 'nightly')
- name: Append rustup toolchain target bins to PATH
run: echo "$(dirname $(rustc --print target-libdir))/bin" >> "$GITHUB_PATH"
if: matrix.os == 'ubuntu-latest'
- run: sudo apt update -y && sudo apt install lldb gcc-multilib libsecret-1-0 libsecret-1-dev -y
if: matrix.os == 'ubuntu-latest'
- run: rustup component add rustfmt || echo "rustfmt not available"
Expand Down
31 changes: 21 additions & 10 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,30 @@ fn check_command(command_path: &Path, args: &[&str]) -> Result<(), CheckCommandE
}

fn has_command(command: &str) -> bool {
match check_command(Path::new(command), &["--version"]).as_ref() {
Ok(()) => true,
Err(e @ CheckCommandError::CannotExecute { program, .. }) => {
// * hg is not installed on GitHub macOS or certain constrained
// environments like Docker. Consider installing it if Cargo
// gains more hg support, but otherwise it isn't critical.
// * lldb is not pre-installed on Ubuntu and Windows, so skip.
if is_ci() && !matches!(program.as_str(), "hg" | "lldb") {
panic!("{e}")
let cmd = Path::new(command);
let mut err = None;
for arg in ["--version", "-h"] {
match check_command(cmd, &[arg]) {
Ok(()) => return true,
Err(e) => {
err = Some(e);
}
}
}

match err.unwrap() {
// Always bail out if a command is not found on CI, except:
//
// * hg is not installed on GitHub macOS or certain constrained
// environments like Docker. Consider installing it if Cargo
// gains more hg support, but otherwise it isn't critical.
// * lldb is not pre-installed on Ubuntu and Windows, so skip.
CheckCommandError::CannotExecute { program, .. }
if !is_ci() || matches!(program.as_str(), "hg" | "lldb") =>
{
false
}
Err(e @ CheckCommandError::UnsuccessfulTermination { .. }) => {
e => {
panic!("{e}");
}
}
Expand Down
28 changes: 3 additions & 25 deletions tests/testsuite/pgo.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
//! Test if PGO works.

use std::path::PathBuf;
use std::process::Command;

use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::str;

fn llvm_profdata() -> Option<PathBuf> {
let output = Command::new("rustc")
.arg("--print=target-libdir")
.output()
.expect("rustc to run");
assert!(output.status.success());
let mut libdir = PathBuf::from(String::from_utf8(output.stdout).unwrap());
assert!(libdir.pop());
let mut bin = libdir.join("bin").join("llvm-profdata");
bin.exists().then(|| bin.clone()).or_else(|| {
bin.set_extension("exe");
bin.exists().then_some(bin)
})
}

#[cargo_test]
// macOS may emit different LLVM PGO warnings.
// Windows LLVM has different requirements.
#[cfg_attr(not(target_os = "linux"), ignore = "linux only")]
#[cfg_attr(not(target_os = "linux"), cargo_test, ignore = "linux only")]
#[cfg_attr(target_os = "linux", cargo_test(requires = "llvm-profdata"))]
fn pgo_works() {
let Some(llvm_profdata) = llvm_profdata() else {
return;
};

let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -77,7 +55,7 @@ fn pgo_works() {
.with_process_builder(cargo_test_support::process(release_bin))
.run();

cargo_test_support::process(llvm_profdata)
cargo_test_support::process("llvm-profdata")
.arg("merge")
.arg("-o")
.arg(&profdata_path)
Expand Down

0 comments on commit 4e1de0e

Please sign in to comment.