-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to Record Compile Invocation
This commit adds API to record the compilation invocations in order to emit compile_commands.json a.k.a. JSON compilation database. Fixes #497
- Loading branch information
1 parent
f2e1b1c
commit 4e56d15
Showing
3 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::fs::OpenOptions; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
/// An entry for creating a [JSON Compilation Database](https://clang.llvm.org/docs/JSONCompilationDatabase.html). | ||
#[derive(serde::Serialize)] | ||
pub struct CompileCommand { | ||
directory: PathBuf, | ||
arguments: Vec<String>, | ||
file: PathBuf, | ||
output: PathBuf, | ||
} | ||
|
||
impl CompileCommand { | ||
pub(crate) fn new(cmd: &Command, src: PathBuf, output: PathBuf) -> Self { | ||
let mut arguments = Vec::with_capacity(cmd.get_args().len() + 1); | ||
|
||
let program = String::from(cmd.get_program().to_str().unwrap()); | ||
arguments.push( | ||
crate::which(&program) | ||
.map(|p| p.to_string_lossy().into_owned()) | ||
.map(|p| p.to_string()) | ||
.unwrap_or(program), | ||
); | ||
arguments.extend( | ||
cmd.get_args() | ||
.flat_map(std::ffi::OsStr::to_str) | ||
.map(String::from), | ||
); | ||
|
||
Self { | ||
// TODO: is the assumption correct? | ||
directory: std::env::current_dir().unwrap(), | ||
arguments, | ||
file: src, | ||
output, | ||
} | ||
} | ||
} | ||
|
||
/// Stores the provided list of [compile commands](crate::CompileCommand) as [JSON | ||
/// Compilation Database](https://clang.llvm.org/docs/JSONCompilationDatabase.html). | ||
pub fn store_json_compilation_database<'a, C, P>(commands: C, path: P) | ||
where | ||
C: IntoIterator<Item = &'a CompileCommand>, | ||
P: AsRef<Path>, | ||
{ | ||
let file = OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.open(path) | ||
.unwrap(); | ||
serde_json::to_writer_pretty(&file, &commands.into_iter().collect::<Vec<_>>()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters