llama_cpp_wrapper_sources/
compile_commands_file.rs1use std::fs::File;
2use std::path::Path;
3use std::path::PathBuf;
4
5use crate::compile_command::CompileCommand;
6use crate::cpp_standard::CPP_STANDARD;
7use crate::wrapper_include_dirs::WRAPPER_INCLUDE_DIRS;
8use crate::wrapper_source_paths::wrapper_source_paths;
9use crate::wrapper_sources_error::WrapperSourcesError;
10
11const CPP_COMPILER: &str = "c++";
12
13fn absolute_path(directory: &Path, relative: &str) -> String {
14 directory.join(relative).display().to_string()
15}
16
17pub struct CompileCommandsFile {
18 pub output_path: PathBuf,
19 pub sys_dir: PathBuf,
20}
21
22impl CompileCommandsFile {
23 #[must_use]
24 pub fn commands(&self) -> Vec<CompileCommand> {
25 wrapper_source_paths(&self.sys_dir)
26 .into_iter()
27 .map(|file| {
28 let mut arguments = vec![CPP_COMPILER.to_owned(), format!("-std={CPP_STANDARD}")];
29
30 for include_dir in WRAPPER_INCLUDE_DIRS {
31 arguments.push(format!("-I{}", absolute_path(&self.sys_dir, include_dir)));
32 }
33
34 arguments.push("-c".to_owned());
35 arguments.push(file.display().to_string());
36
37 CompileCommand {
38 arguments,
39 directory: self.sys_dir.clone(),
40 file,
41 }
42 })
43 .collect()
44 }
45
46 pub fn write(&self) -> Result<(), WrapperSourcesError> {
51 let destination =
52 File::create(&self.output_path).map_err(|source| WrapperSourcesError::Create {
53 path: self.output_path.clone(),
54 source,
55 })?;
56
57 serde_json::to_writer_pretty(destination, &self.commands()).map_err(|source| {
58 WrapperSourcesError::Serialize {
59 path: self.output_path.clone(),
60 source,
61 }
62 })
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use std::path::Path;
69 use std::path::PathBuf;
70
71 use super::CompileCommandsFile;
72 use crate::wrapper_include_dirs::WRAPPER_INCLUDE_DIRS;
73 use crate::wrapper_source_paths::wrapper_source_paths;
74 use crate::wrapper_sources_error::WrapperSourcesError;
75
76 fn database_for(output_path: &str) -> CompileCommandsFile {
77 CompileCommandsFile {
78 output_path: PathBuf::from(output_path),
79 sys_dir: PathBuf::from("/repo/llama-cpp-bindings-sys"),
80 }
81 }
82
83 #[test]
84 fn every_wrapper_source_becomes_one_entry() {
85 let commands = database_for("/unused").commands();
86
87 assert_eq!(
88 commands
89 .iter()
90 .map(|command| command.file.clone())
91 .collect::<Vec<PathBuf>>(),
92 wrapper_source_paths(Path::new("/repo/llama-cpp-bindings-sys"))
93 );
94 }
95
96 #[test]
97 fn every_include_dir_is_resolved_against_the_sys_dir() {
98 let commands = database_for("/unused").commands();
99
100 assert_eq!(
101 commands
102 .first()
103 .map(|command| command
104 .arguments
105 .iter()
106 .filter(|argument| argument.starts_with("-I"))
107 .cloned()
108 .collect::<Vec<String>>())
109 .unwrap_or_default(),
110 WRAPPER_INCLUDE_DIRS
111 .iter()
112 .map(|include_dir| {
113 format!(
114 "-I{}",
115 Path::new("/repo/llama-cpp-bindings-sys")
116 .join(include_dir)
117 .display()
118 )
119 })
120 .collect::<Vec<String>>()
121 );
122 }
123
124 #[test]
125 fn the_compiler_and_standard_lead_the_command_line() {
126 let commands = database_for("/unused").commands();
127
128 assert_eq!(
129 commands
130 .first()
131 .map(|command| command
132 .arguments
133 .iter()
134 .take(2)
135 .cloned()
136 .collect::<Vec<String>>())
137 .unwrap_or_default(),
138 vec!["c++".to_owned(), "-std=c++17".to_owned()]
139 );
140 }
141
142 #[test]
143 fn creating_the_database_under_a_missing_directory_reports_the_path() {
144 let output_path = "/nonexistent-directory/compile_commands.json";
145
146 assert!(matches!(
147 database_for(output_path).write(),
148 Err(WrapperSourcesError::Create { ref path, .. }) if path == Path::new(output_path)
149 ));
150 }
151
152 #[cfg(target_os = "linux")]
153 #[test]
154 fn a_destination_that_cannot_absorb_the_bytes_reports_the_path() {
155 assert!(matches!(
156 database_for("/dev/full").write(),
157 Err(WrapperSourcesError::Serialize { ref path, .. }) if path == Path::new("/dev/full")
158 ));
159 }
160}