-
Notifications
You must be signed in to change notification settings - Fork 48
/
project.rs
330 lines (291 loc) · 10.1 KB
/
project.rs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::{
env,
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
use glob::glob;
use crate::{
build_config::{LinkageInfo, ProjectConfig},
object::Object,
};
use plc::output::FormatOption;
use source_code::{SourceContainer, SourceType};
#[derive(Debug)]
pub enum Linkage {
Static,
Shared(Package),
}
/// How a library is intended to be packaged for the project
#[derive(Debug)]
pub enum Package {
/// The library is available locally, it needs to be shipped with the project
Local,
/// The library is available on the target system, no need to ship it
System,
}
/// Representation of a PLC Library
#[derive(Debug)]
pub enum Library<T: SourceContainer> {
Compiled(CompiledLibrary<T>),
Source(Project<T>),
}
/// A Compiled library to be included in the project
#[derive(Debug, Clone)]
pub struct CompiledLibrary<T: SourceContainer> {
// TODO: name: String,
//TODO: Version
/// Location of the header files to be included in the project
headers: Vec<T>,
/// Objects files for the compiled library
objects: Vec<Object>,
// architectures: Vec<Target>,
}
/// The information required by a project to successfully include a library
#[derive(Debug)]
pub struct LibraryInformation<T: SourceContainer> {
/// Location of the library if available
location: Option<PathBuf>,
/// Library name, this will be used when including the library
name: String,
/// How should the library be linked
linkage: Linkage,
/// The actual library in question
library: Library<T>,
}
/// A PLC project to build
#[derive(Debug)]
pub struct Project<T: SourceContainer> {
/// Name of the project
name: String,
/// The full path for the project, i.e where the build description exists
location: Option<PathBuf>,
//TODO: Version
/// Source code for the project
sources: Vec<T>,
/// Files that will be referenced in the project but are not to be compiled (headers)
includes: Vec<T>,
/// Object files that do not need to be compiled
objects: Vec<Object>,
/// Libraries included in the project configuration
libraries: Vec<LibraryInformation<T>>,
/// Additional library paths to consider
library_paths: Vec<PathBuf>,
/// Output format
format: FormatOption,
/// Output Name
output: Option<String>,
}
impl<T: SourceContainer> LibraryInformation<T> {
pub fn get_includes(&self) -> &[T] {
match &self.library {
Library::Compiled(lib) => &lib.headers,
Library::Source(lib) => lib.get_sources(),
}
}
/// Returns the name used to link the Library
pub fn get_link_name(&self) -> &str {
&self.name
}
pub fn get_path(&self) -> Option<&Path> {
self.location.as_deref()
}
pub fn should_copy(&self) -> bool {
matches!(self.linkage, Linkage::Shared(Package::Local))
}
}
impl<T: SourceContainer + Clone> LibraryInformation<T> {
pub fn get_compiled_lib(&self) -> CompiledLibrary<T> {
match &self.library {
Library::Compiled(lib) => lib.clone(),
_ => todo!("Convert source lib to compiled lib"),
}
}
}
impl<T: SourceContainer> CompiledLibrary<T> {
pub fn get_objects(&self) -> &[Object] {
&self.objects
}
}
//configuration
impl Project<PathBuf> {
/// Retrieve a project for compilation from a json description
pub fn from_config(config: &Path) -> Result<Self> {
let project_config = ProjectConfig::from_file(config)?;
let libraries = project_config
.libraries
.into_iter()
.map(|conf| {
let lib_path = config.parent().map(|it| it.join(&conf.path)).unwrap_or_else(|| conf.path);
let linkage: Linkage = conf.package.into();
// Use the linkage type to find the library from the given name
// TODO: We should allow for a fix name in the configuration if the library does not follow the unix convention
// TODO: We should also allow a way to define objects based on the architecture
let object_name = match linkage {
Linkage::Static => format! {"lib{}.a", &conf.name},
Linkage::Shared(_) => format! {"lib{}.so", &conf.name},
};
let lib_file = lib_path.join(object_name);
let mut objects = vec![];
if lib_file.exists() {
objects.push(lib_file.into());
}
let compiled_library = CompiledLibrary {
objects,
headers: resolve_file_paths(Some(&lib_path), conf.include_path)?,
};
Ok(LibraryInformation {
name: conf.name,
location: Some(lib_path),
linkage: conf.package.into(),
library: Library::Compiled(compiled_library),
})
})
.collect::<Result<Vec<_>>>()?;
let current_dir = env::current_dir()?;
let location = config.parent().map(Path::to_path_buf).or(Some(current_dir));
let sources = resolve_file_paths(location.as_deref(), project_config.files)?;
Ok(Project {
name: project_config.name,
location,
sources,
libraries,
format: project_config.compile_type,
output: project_config.output,
includes: vec![],
objects: vec![],
library_paths: vec![],
})
}
pub fn with_file_paths(self, files: Vec<PathBuf>) -> Self {
let mut proj = self;
let files = resolve_file_paths(proj.get_location(), files).unwrap();
for file in files {
if matches!(file.get_type(), SourceType::Unknown) {
let obj = file.into();
proj.objects.push(obj);
} else {
proj.sources.push(file);
}
}
proj
}
pub fn with_include_paths(self, files: Vec<PathBuf>) -> Self {
let mut proj = self;
proj.includes = resolve_file_paths(proj.get_location(), files).unwrap();
proj
}
pub fn with_library_paths(self, paths: Vec<PathBuf>) -> Self {
let mut proj = self;
proj.library_paths.extend(resolve_file_paths(proj.get_location(), paths).unwrap());
proj
}
pub fn with_format(self, format: FormatOption) -> Self {
let mut proj = self;
proj.format = format;
proj
}
pub fn with_output_name(self, output: Option<String>) -> Self {
let mut proj = self;
proj.output = output.or(proj.output);
proj
}
pub fn get_library_paths(&self) -> &[PathBuf] {
&self.library_paths
}
}
impl<S: SourceContainer> Project<S> {
pub fn new(name: String) -> Self {
Project {
name,
location: None,
sources: vec![],
includes: vec![],
objects: vec![],
libraries: vec![],
library_paths: vec![],
format: FormatOption::default(),
output: None,
}
}
pub fn with_sources<T: IntoIterator<Item = S>>(mut self, sources: T) -> Self {
self.sources.extend(sources);
self
}
pub fn with_source_includes<T: IntoIterator<Item = S>>(mut self, includes: T) -> Self {
self.includes.extend(includes);
self
}
pub fn with_libraries(self, libraries: Vec<String>) -> Self {
let mut proj = self;
for library in libraries {
proj.libraries.push(LibraryInformation {
name: library.to_string(),
location: None,
linkage: Linkage::Shared(Package::System),
library: Library::Compiled(CompiledLibrary { headers: vec![], objects: vec![] }),
});
}
proj
}
pub fn get_location(&self) -> Option<&Path> {
self.location.as_deref()
}
pub fn get_sources(&self) -> &[S] {
&self.sources
}
pub fn get_includes(&self) -> &[S] {
&self.includes
}
pub fn get_libraries(&self) -> &[LibraryInformation<S>] {
&self.libraries
}
pub fn get_objects(&self) -> &[Object] {
&self.objects
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn get_output_name(&self) -> String {
self.output.as_ref().map(|it| it.to_string()).unwrap_or_else(|| {
let input = self.get_name();
match self.format {
FormatOption::Object | FormatOption::Relocatable => format!("{input}.o"),
FormatOption::Static => format!("{input}.out"),
FormatOption::Shared | FormatOption::PIC | FormatOption::NoPIC => format!("{input}.so"),
FormatOption::Bitcode => format!("{input}.bc"),
FormatOption::IR => format!("{input}.ll"),
}
})
}
pub fn get_output_format(&self) -> FormatOption {
self.format
}
/// Returns the validation schema used for this project
pub fn get_validation_schema(&self) -> impl AsRef<str> {
include_str!("../schema/plc-json.schema")
}
}
fn resolve_file_paths(location: Option<&Path>, inputs: Vec<PathBuf>) -> Result<Vec<PathBuf>> {
let mut sources = Vec::new();
//Ensure we are working with a directory
let location = location.and_then(|it| if it.is_file() { it.parent() } else { Some(it) });
for input in &inputs {
let input = location.map(|it| it.join(input)).unwrap_or(input.to_path_buf());
let path = &input.to_string_lossy();
let paths = glob(path).context(format!("Failed to read glob pattern {path}"))?;
for p in paths {
let path = p.context("Illegal Path")?;
sources.push(path);
}
}
Ok(sources)
}
impl From<LinkageInfo> for Linkage {
fn from(value: LinkageInfo) -> Self {
match value {
LinkageInfo::Copy | LinkageInfo::Local => Self::Shared(Package::Local),
LinkageInfo::System => Self::Shared(Package::System),
LinkageInfo::Static => Self::Static,
}
}
}