This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
lib.rs
245 lines (219 loc) · 6.52 KB
/
lib.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
#[macro_use]
extern crate pest_derive;
extern crate metrohash;
#[macro_use]
extern crate log;
extern crate askama;
extern crate pbr;
extern crate rayon;
pub mod abs;
pub mod ast;
pub mod emitter;
pub mod emitter_docs;
pub mod emitter_js;
pub mod emitter_py;
pub mod emitter_rs;
pub mod emitter_go;
pub mod emitter_common;
pub mod expand;
pub mod export_make;
pub mod export_cmake;
pub mod export_esp;
pub mod flatten;
pub mod loader;
pub mod make;
pub mod makro;
pub mod name;
pub mod parser;
pub mod pipeline;
pub mod project;
pub mod repos;
pub mod smt;
pub mod symbolic;
pub mod mergecc;
use name::Name;
use std::collections::HashMap;
use std::collections::HashSet;
pub struct Error {
message: String,
details: Vec<(ast::Location, String)>,
}
impl Error {
pub fn new(message: String, details: Vec<(ast::Location, String)>) -> Self {
Self { message, details }
}
}
#[derive(PartialEq, Debug)]
pub enum BuildSet {
Tests,
Run,
Check(Option<std::path::PathBuf>),
All,
Export,
Named(String),
}
pub fn build(buildset: BuildSet, variant: &str, stage: make::Stage, _slow: bool) {
let (root, mut project) = project::load_cwd();
std::env::set_current_dir(&root).unwrap();
//
//
let mut searchpaths : Vec<std::path::PathBuf> = repos::index(&project).into_iter().collect();
let td = project::target_dir();
std::fs::create_dir_all(td.join(stage.to_string()).join("c"))
.expect("create target dir");
std::fs::create_dir_all(td.join(stage.to_string()).join("zz"))
.expect("create target dir");
std::fs::create_dir_all(td.join("gen"))
.expect("create target dir");
std::fs::create_dir_all(td.join("c"))
.expect("create target dir");
std::fs::create_dir_all(td.join("include").join("zz"))
.expect("create target dir");
let project_name = Name(vec![String::new(), project.project.name.clone()]);
let project_tests_name = Name(vec![
String::new(),
project.project.name.clone(),
"tests".to_string(),
]);
let mut modules = HashMap::new();
if root.join("src").exists() {
loader::load(
&mut modules,
&project.project,
&project_name,
&root.join("src"),
&stage,
);
}
if root.join("tests").exists() {
loader::load(
&mut modules,
&project.project,
&project_tests_name,
&root.join("tests").canonicalize().unwrap(),
&stage,
);
}
searchpaths.push(
std::env::current_exe()
.expect("self path")
.canonicalize()
.expect("self path")
.parent()
.expect("self path")
.parent()
.expect("self path")
.parent()
.expect("self path")
.join("modules"),
);
searchpaths.push(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("modules"));
if let Ok(zz_path) = std::env::var("ZZ_MODULE_PATHS") {
let module_paths = if cfg!(windows) {
zz_path.split(";")
} else {
zz_path.split(":")
};
for path in module_paths {
searchpaths.push(std::path::Path::new(&path).to_path_buf());
}
}
let mut visited = HashSet::new();
if let Some(deps) = &project.dependencies {
for (name, dep) in deps {
match dep {
toml::Value::String(_) => {
getdep(
&mut visited,
name,
&mut modules,
&mut project.project,
&mut searchpaths,
&stage,
);
}
_ => (),
}
std::env::set_current_dir(&root).unwrap();
}
}
let pipeline = pipeline::Pipeline::new(project, stage, variant.to_string(), modules);
pipeline.build(buildset);
}
fn getdep(
visited: &mut HashSet<String>,
name: &str,
modules: &mut HashMap<Name, loader::Module>,
rootproj: &mut project::Project,
searchpaths: &mut Vec<std::path::PathBuf>,
stage: &make::Stage,
) {
if !visited.insert(name.to_string()) {
return;
}
searchpaths.push(std::env::current_dir().unwrap().join("modules"));
let mut found = None;
for searchpath in searchpaths.iter() {
let modpath = searchpath.join(name).join("zz.toml");
if modpath.exists() {
found = Some(searchpath.join(name));
}
}
let found = match found {
Some(v) => v,
None => {
eprintln!(
"dependency \"{}\" not found in any of {:#?}",
name, searchpaths
);
std::process::exit(9);
}
};
let pp = std::env::current_dir().unwrap();
//std::env::set_current_dir(&found).unwrap();
let (root, project) = project::load(&found);
let project_name = Name(vec![String::new(), project.project.name.clone()]);
if found.join("src").exists() {
loader::load(
modules,
&project.project,
&project_name,
&found.join("src"),
&stage,
);
}
std::env::set_current_dir(&root).unwrap();
let depsearchpaths = repos::index(&project);
std::env::set_current_dir(pp).unwrap();
searchpaths.push(root.join("modules"));
searchpaths.extend(depsearchpaths);
for i in project.project.cincludes {
let ii = root.join(&i);
let i = std::fs::canonicalize(&ii)
.expect(&format!("{}: cannot resolve cinclude {:?}", name, ii));
rootproj.cincludes.push(i.to_string_lossy().into());
}
for i in project.project.cobjects {
let ii = root.join(&i);
let i = std::fs::canonicalize(&ii)
.expect(&format!("{}: cannot resolve cobject {:?}", name, ii));
rootproj.cobjects.push(i.to_string_lossy().into());
}
for i in project.project.pkgconfig {
let ii = root.join(&i);
let i = std::fs::canonicalize(&ii)
.expect(&format!("{}: cannot resolve pkgconfig {:?}", name, ii));
rootproj.pkgconfig.push(i.to_string_lossy().into());
}
rootproj.cflags.extend(project.project.cflags);
if let Some(deps) = &project.dependencies {
for (name, dep) in deps {
match dep {
toml::Value::String(_) => {
getdep(visited, name, modules, rootproj, searchpaths, stage);
}
_ => (),
}
}
}
}