|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | +use std::process::{Command, Output}; |
| 3 | +use std::io::{BufReader, Read, Write}; |
| 4 | +use std::fs::File; |
| 5 | + |
| 6 | +use crate::{env_var, handle_failed_output}; |
| 7 | + |
| 8 | +/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available |
| 9 | +/// at `$LLVM_BIN_DIR/llvm-readobj`. |
| 10 | +pub fn llvm_readobj() -> LlvmReadobj { |
| 11 | + LlvmReadobj::new() |
| 12 | +} |
| 13 | + |
| 14 | +/// Construct a new `llvm-profdata` invocation. This assumes that `llvm-profdata` is available |
| 15 | +/// at `$LLVM_BIN_DIR/llvm-profdata`. |
| 16 | +pub fn llvm_profdata() -> LlvmProfdata { |
| 17 | + LlvmProfdata::new() |
| 18 | +} |
| 19 | + |
| 20 | +/// Construct a new `llvm-filecheck` invocation. This assumes that `llvm-filecheck` is available |
| 21 | +/// at `$LLVM_FILECHECK`. |
| 22 | +pub fn llvm_filecheck() -> LlvmFilecheck { |
| 23 | + LlvmFilecheck::new() |
| 24 | +} |
| 25 | + |
| 26 | +/// A `llvm-readobj` invocation builder. |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct LlvmReadobj { |
| 29 | + cmd: Command, |
| 30 | +} |
| 31 | + |
| 32 | +/// A `llvm-profdata` invocation builder. |
| 33 | +#[derive(Debug)] |
| 34 | +pub struct LlvmProfdata { |
| 35 | + cmd: Command, |
| 36 | +} |
| 37 | + |
| 38 | +/// A `llvm-filecheck` invocation builder. |
| 39 | +#[derive(Debug)] |
| 40 | +pub struct LlvmFilecheck { |
| 41 | + cmd: Command, |
| 42 | +} |
| 43 | + |
| 44 | +crate::impl_common_helpers!(LlvmReadobj); |
| 45 | + |
| 46 | +/// Generate the path to the bin directory of LLVM. |
| 47 | +pub fn llvm_bin_dir() -> PathBuf { |
| 48 | + let llvm_bin_dir = env_var("LLVM_BIN_DIR") |
| 49 | + .expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`"); |
| 50 | + PathBuf::from(llvm_bin_dir) |
| 51 | +} |
| 52 | + |
| 53 | +impl LlvmReadobj { |
| 54 | + /// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available |
| 55 | + /// at `$LLVM_BIN_DIR/llvm-readobj`. |
| 56 | + pub fn new() -> Self { |
| 57 | + let llvm_readobj = llvm_bin_dir().join("llvm-readobj"); |
| 58 | + let cmd = Command::new(llvm_readobj); |
| 59 | + Self { cmd } |
| 60 | + } |
| 61 | + |
| 62 | + /// Provide an input file. |
| 63 | + pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 64 | + self.cmd.arg(path.as_ref()); |
| 65 | + self |
| 66 | + } |
| 67 | + |
| 68 | + /// Pass `--file-header` to display file headers. |
| 69 | + pub fn file_header(&mut self) -> &mut Self { |
| 70 | + self.cmd.arg("--file-header"); |
| 71 | + self |
| 72 | + } |
| 73 | + |
| 74 | + /// Get the [`Output`][::std::process::Output] of the finished process. |
| 75 | + #[track_caller] |
| 76 | + pub fn command_output(&mut self) -> Output { |
| 77 | + self.cmd.output().expect("failed to get output of finished process") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl LlvmProfdata { |
| 82 | + /// Construct a new `llvm-profdata` invocation. This assumes that `llvm-profdata` is available |
| 83 | + /// at `$LLVM_BIN_DIR/llvm-profdata`. |
| 84 | + pub fn new() -> Self { |
| 85 | + let llvm_profdata = llvm_bin_dir().join("llvm-profdata"); |
| 86 | + let cmd = Command::new(llvm_profdata); |
| 87 | + Self { cmd } |
| 88 | + } |
| 89 | + |
| 90 | + /// Provide an input file. |
| 91 | + pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 92 | + self.cmd.arg("-o"); |
| 93 | + self.cmd.arg(path.as_ref()); |
| 94 | + self |
| 95 | + } |
| 96 | + |
| 97 | + /// Specify the output file path. |
| 98 | + pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 99 | + self.cmd.arg(path.as_ref()); |
| 100 | + self |
| 101 | + } |
| 102 | + |
| 103 | + /// Take several profile data files generated by PGO instrumentation and merge them |
| 104 | + /// together into a single indexed profile data file. |
| 105 | + pub fn merge(&mut self) -> &mut Self { |
| 106 | + self.cmd.arg("merge"); |
| 107 | + self |
| 108 | + } |
| 109 | + |
| 110 | + /// Get the [`Output`][::std::process::Output] of the finished process. |
| 111 | + #[track_caller] |
| 112 | + pub fn command_output(&mut self) -> Output { |
| 113 | + self.cmd.output().expect("failed to get output of finished process") |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl LlvmFilecheck { |
| 118 | + /// Construct a new `llvm-filecheck` invocation. This assumes that `llvm-filecheck` is available |
| 119 | + /// at `$LLVM_FILECHECK`. |
| 120 | + pub fn new() -> Self { |
| 121 | + let llvm_filecheck = env_var("LLVM_FILECHECK").expect("LLVM_FILECHECK env var not specified"); |
| 122 | + let cmd = Command::new(llvm_filecheck); |
| 123 | + Self { cmd } |
| 124 | + } |
| 125 | + |
| 126 | + /// Pipe a file into standard input containing patterns that will be matched against the .patterns(path) call. |
| 127 | + pub fn stdin<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 128 | + let file = File::open(path).unwrap(); |
| 129 | + let reader = BufReader::new(file); |
| 130 | + let byte_vec = read_bytes(reader).expect("failed to read bytes of standard input"); |
| 131 | + let byte_slice = byte_vec.as_slice(); |
| 132 | + self.cmd.stdin(std::process::Stdio::piped()); |
| 133 | + let mut child = self.cmd.spawn().unwrap(); |
| 134 | + let mut stdin = child.stdin.take().unwrap(); |
| 135 | + stdin.write_all(byte_slice).unwrap(); |
| 136 | + stdin.flush().unwrap(); |
| 137 | + child.wait_with_output().unwrap(); |
| 138 | + self |
| 139 | + } |
| 140 | + |
| 141 | + /// Provide the patterns that need to be matched. |
| 142 | + pub fn patterns<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { |
| 143 | + self.cmd.arg(path.as_ref()); |
| 144 | + self |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +fn read_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>, std::io::Error> { |
| 149 | + let mut buffer = Vec::new(); |
| 150 | + reader.read_to_end(&mut buffer)?; |
| 151 | + Ok(buffer) |
| 152 | +} |
0 commit comments