1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#[derive(Clone)]
pub struct CompilationCommandBuilder {
compiler: String,
target: Option<String>,
cxx_toolchain_dir: Option<String>,
arch_flags: Vec<String>,
optimization: String,
include_paths: Vec<String>,
project_root: Option<String>,
output: String,
input: String,
linker: Option<String>,
extra_flags: Vec<String>,
}
impl CompilationCommandBuilder {
pub fn new() -> Self {
Self {
compiler: String::new(),
target: None,
cxx_toolchain_dir: None,
arch_flags: Vec::new(),
optimization: "2".to_string(),
include_paths: Vec::new(),
project_root: None,
output: String::new(),
input: String::new(),
linker: None,
extra_flags: Vec::new(),
}
}
pub fn set_compiler(mut self, compiler: &str) -> Self {
self.compiler = compiler.to_string();
self
}
pub fn set_target(mut self, target: &str) -> Self {
self.target = Some(target.to_string());
self
}
pub fn set_cxx_toolchain_dir(mut self, path: Option<&str>) -> Self {
self.cxx_toolchain_dir = path.map(|p| p.to_string());
self
}
pub fn add_arch_flags(mut self, flags: Vec<&str>) -> Self {
let mut new_arch_flags = flags.into_iter().map(|v| v.to_string()).collect();
self.arch_flags.append(&mut new_arch_flags);
self
}
pub fn set_opt_level(mut self, optimization: &str) -> Self {
self.optimization = optimization.to_string();
self
}
/// Sets a list of include paths for compilation.
/// The paths that are passed must be relative to the
/// "cxx_toolchain_dir" directory path.
pub fn set_include_paths(mut self, paths: Vec<&str>) -> Self {
self.include_paths = paths.into_iter().map(|path| path.to_string()).collect();
self
}
/// Sets the root path of all the generated test files.
pub fn set_project_root(mut self, path: &str) -> Self {
self.project_root = Some(path.to_string());
self
}
/// The name of the output executable, without any suffixes
pub fn set_output_name(mut self, path: &str) -> Self {
self.output = path.to_string();
self
}
/// The name of the input C file, without any suffixes
pub fn set_input_name(mut self, path: &str) -> Self {
self.input = path.to_string();
self
}
pub fn set_linker(mut self, linker: String) -> Self {
self.linker = Some(linker);
self
}
pub fn add_extra_flags(mut self, flags: Vec<&str>) -> Self {
let mut flags: Vec<String> = flags.into_iter().map(|f| f.to_string()).collect();
self.extra_flags.append(&mut flags);
self
}
pub fn add_extra_flag(self, flag: &str) -> Self {
self.add_extra_flags(vec![flag])
}
}
impl CompilationCommandBuilder {
pub fn make_string(self) -> String {
let arch_flags = self.arch_flags.join("+");
let flags = std::env::var("CPPFLAGS").unwrap_or("".into());
let project_root = self.project_root.unwrap_or_default();
let project_root_str = project_root.as_str();
let mut output = self.output.clone();
if self.linker.is_some() {
output += ".o"
};
let mut command = format!(
"{} {flags} -march={arch_flags} \
-O{} \
-o {project_root}/{} \
{project_root}/{}.cpp",
self.compiler, self.optimization, output, self.input,
);
command = command + " " + self.extra_flags.join(" ").as_str();
if let Some(target) = &self.target {
command = command + " --target=" + target;
}
if let (Some(linker), Some(cxx_toolchain_dir)) = (&self.linker, &self.cxx_toolchain_dir) {
let include_args = self
.include_paths
.iter()
.map(|path| "--include-directory=".to_string() + cxx_toolchain_dir + path)
.collect::<Vec<_>>()
.join(" ");
command = command
+ " -c "
+ include_args.as_str()
+ " && "
+ linker
+ " "
+ project_root_str
+ "/"
+ &output
+ " -o "
+ project_root_str
+ "/"
+ &self.output
+ " && rm "
+ project_root_str
+ "/"
+ &output;
}
command
}
}
|