corge/command/
compilation_database.rs1pub mod compilation_database_path;
2
3use crate::cli::{CompilationDatabaseArgs};
4use crate::command::build::dependency_path::DependencyPath;
5use crate::tool::files_fetcher::fetch_files;
6use anyhow::{bail, Context, Result};
7use serde::Serialize;
8use std::fs;
9use crate::command::compilation_database::compilation_database_path::CompilationDatabasePath;
10
11#[derive(Serialize)]
12struct CompileCommand {
13 directory: String,
14 file: String,
15 command: String,
16}
17
18pub fn compilation_database(compilation_database_args: CompilationDatabaseArgs) -> Result<()> {
19 let project_path = compilation_database_args.path.clone();
20
21 log::info!("Generating compilation database in directory {:?}", &project_path);
22
23 let dependency_path = DependencyPath::create(&project_path)?;
24 let compilation_database_path = CompilationDatabasePath::create(&project_path)?;
25
26 let source_files_paths = fetch_files(&project_path.join("src"), "c").context("Failed to fetch source files for project")?;
28
29 let project_path = fs::canonicalize(project_path)?;
30 let include_path = fs::canonicalize(dependency_path.include)?;
31
32 let compile_commands: Result<Vec<CompileCommand>> = source_files_paths
33 .iter()
34 .map(|source_file_path| {
35 let source_file_path = fs::canonicalize(source_file_path)?;
36
37 Ok(CompileCommand {
38 directory: project_path.display().to_string(),
39 file: source_file_path.display().to_string(),
40 command: format!("gcc -c {} -I {}", source_file_path.display(), include_path.display())
41 })
42 }).collect();
43
44 match compile_commands {
45 Ok(compile_commands) => {
46 let serialized = serde_json::to_string_pretty(&compile_commands)?;
47 fs::write(compilation_database_path.json, serialized)?;
48 }
49 Err(err) => bail!("Failed to generate compile commands: {}", err)
50 }
51
52 log::info!("COMPILATION DATABASE GENERATED SUCCESSFULLY");
53 Ok(())
54}