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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
use std::fs;
use std::sync::{Arc, Mutex};
use crate::utils::{BuildConfig, TargetConfig, Package, log, LogLevel, self};
use std::path::{Path, PathBuf};
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use itertools::Itertools;
use std::collections::HashMap;
use crate::hasher;
use rayon::prelude::*;
use indicatif::{ProgressBar, ProgressStyle};
use colored::Colorize;
static BUILD_DIR : &str = ".bld_cpp/bin";
#[cfg(target_os = "windows")]
static OBJ_DIR: &str = ".bld_cpp/obj_win32";
#[cfg(target_os = "linux")]
static OBJ_DIR: &str = ".bld_cpp/obj_linux";
struct Target<'a> {
srcs: Vec<Src>,
build_config: &'a BuildConfig,
target_config: &'a TargetConfig,
dependant_includes: HashMap<String, Vec<String>>,
bin_path: String,
hash_file_path: String,
path_hash: HashMap<String, String>,
dependant_libs: Vec<Target<'a>>,
packages: &'a Vec<Package>,
}
struct Src {
path: String,
name: String,
obj_name: String,
bin_path: String,
dependant_includes: Vec<String>,
}
impl<'a> Target<'a> {
pub fn new(build_config: &'a BuildConfig, target_config: &'a TargetConfig, targets: &'a Vec<TargetConfig>, packages: &'a Vec<Package>) -> Self {
let srcs = Vec::new();
let dependant_includes: HashMap<String, Vec<String>> = HashMap::new();
let mut bin_path = String::new();
bin_path.push_str(BUILD_DIR);
bin_path.push_str("/");
bin_path.push_str(&target_config.name);
#[cfg(target_os = "windows")]
if target_config.typ == "exe" {
bin_path.push_str(".exe");
} else if target_config.typ == "dll" {
bin_path.push_str(".dll");
}
#[cfg(target_os = "linux")]
if target_config.typ == "exe" {
bin_path.push_str("");
} else if target_config.typ == "dll" {
bin_path.push_str(".so");
}
#[cfg(target_os = "windows")]
let hash_file_path = format!(".bld_cpp/{}.win32.hash", &target_config.name);
#[cfg(target_os = "linux")]
let hash_file_path = format!(".bld_cpp/{}.linux.hash", &target_config.name);
let path_hash = hasher::load_hashes_from_file(&hash_file_path);
let mut dependant_libs = Vec::new();
for dependant_lib in &target_config.deps {
for target in targets {
if target.name == *dependant_lib {
dependant_libs.push(Target::new(build_config, target, targets, packages));
}
}
}
for dep_lib in &dependant_libs {
if dep_lib.target_config.typ != "dll" {
utils::log(LogLevel::Error, "Can add only dlls as dependant libs");
utils::log(LogLevel::Error, &format!("Target: {} is not a dll", dep_lib.target_config.name));
utils::log(LogLevel::Error, &format!("Target: {} is a {}", dep_lib.target_config.name, dep_lib.target_config.typ));
std::process::exit(1);
}
else {
utils::log(LogLevel::Info, &format!("Adding dependant lib: {}", dep_lib.target_config.name));
}
if !dep_lib.target_config.name.starts_with("lib") {
utils::log(LogLevel::Error, "Dependant lib name must start with lib");
utils::log(LogLevel::Error, &format!("Target: {} does not start with lib", dep_lib.target_config.name));
std::process::exit(1);
}
}
if target_config.deps.len() > dependant_libs.len() + packages.len() {
utils::log(LogLevel::Error, "Dependant libs not found");
utils::log(LogLevel::Error, &format!("Dependant libs: {:?}", target_config.deps));
utils::log(LogLevel::Error, &format!("Found libs: {:?}", targets.iter().map(|x| {
if x.typ == "dll" {
x.name.clone()
} else {
"".to_string()
}
}).collect::<Vec<String>>().into_iter().filter(|x| x != "").collect::<Vec<String>>()));
std::process::exit(1);
}
let mut target = Target::<'a> {
srcs,
build_config,
target_config,
dependant_includes,
bin_path,
path_hash,
hash_file_path,
dependant_libs,
packages,
};
target.get_srcs(&target_config.src, target_config);
target
}
pub fn build(&mut self, gen_cc: bool ) {
if !Path::new(".bld_cpp").exists() {
std::fs::create_dir(".bld_cpp").unwrap_or_else(|why| {
utils::log(LogLevel::Error, &format!("Couldn't create .bld_cpp directory: {}", why));
std::process::exit(1);
});
}
for pkg in self.packages {
for target in &pkg.target_configs {
let empty: Vec<Package> = Vec::new();
if target.typ == "dll" {
let mut pkg_tgt = Target::new(&pkg.build_config, &target, &pkg.target_configs, &empty);
pkg_tgt.build(gen_cc);
}
}
}
let mut to_link : bool = false;
let mut link_causer : Vec<&str> = Vec::new();
let mut srcs_needed = 0;
let total_srcs = self.srcs.len();
let mut src_ccs = Vec::new();
for src in &self.srcs {
let (to_build, _) = src.to_build(&self.path_hash);
log(LogLevel::Debug, &format!("{}: {}", src.path, to_build));
if to_build {
to_link = true;
link_causer.push(&src.path);
srcs_needed += 1;
}
if gen_cc {
src_ccs.push(self.gen_cc(&src));
}
}
if gen_cc {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true)
.open("./compile_commands.json")
.unwrap();
for src_cc in src_ccs {
if let Err(e) = writeln!(file, "{},", src_cc) {
eprintln!("Couldn't write to file: {}", e);
}
}
}
if to_link {
log(LogLevel::Log, &format!("Compiling Target: {}", &self.target_config.name));
log(LogLevel::Log, &format!("\t {} of {} source files have to be compiled", srcs_needed, total_srcs));
if !Path::new(OBJ_DIR).exists() {
fs::create_dir(OBJ_DIR).unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Couldn't create obj dir: {}", why));
});
}
} else {
log(LogLevel::Log, &format!("Target: {} is up to date", &self.target_config.name));
return;
}
let progress_bar = Arc::new(Mutex::new(ProgressBar::new(srcs_needed as u64)
));
let num_complete = Arc::new(Mutex::new(0));
let src_hash_to_update = Arc::new(Mutex::new(Vec::new()));
self.srcs.par_iter().for_each(|src| {
let (to_build, _message) = src.to_build(&self.path_hash);
log(LogLevel::Debug, &format!("{}: {}", src.path, to_build));
if to_build {
src.build(self.build_config, self.target_config, &self.dependant_libs);
src_hash_to_update.lock().unwrap().push(src);
log(LogLevel::Info, &format!("Compiled: {}", src.path));
let log_level = std::env::var("BUILDER_CPP_LOG_LEVEL").unwrap_or("".to_string());
if !(log_level == "Info" || log_level == "Debug"){
let mut num_complete = num_complete.lock().unwrap();
*num_complete += 1;
let progress_bar = progress_bar.lock().unwrap();
let template = format!(" {}{}", "Compiling :".cyan(), "[{bar:40.}] {pos}/{len} ({percent}%) {msg}[{elapsed_precise}] ");
progress_bar.set_style(ProgressStyle::with_template(&template)
.unwrap()
.progress_chars("=>-"));
progress_bar.inc(1);
}
}
});
for src in src_hash_to_update.lock().unwrap().iter() {
hasher::save_hash(&src.path, &mut self.path_hash);
}
if to_link {
log(LogLevel::Log, "Linking: Since source files were compiled");
for src in link_causer {
log(LogLevel::Info, &format!("\tFile: {}", &src));
}
for src in &self.srcs {
for include in &src.dependant_includes {
hasher::save_hash(include, &mut self.path_hash);
}
}
hasher::save_hashes_to_file(&self.hash_file_path, &self.path_hash);
self.link(&self.dependant_libs);
}
}
pub fn link(&self, dep_targets: &Vec<Target>) {
let mut objs = Vec::new();
if !Path::new(BUILD_DIR).exists() {
let cmd = format!("mkdir -p {}", BUILD_DIR);
let output = Command::new("sh")
.arg("-c")
.arg(cmd)
.output()
.expect("failed to execute process");
if !output.status.success() {
log(LogLevel::Error, &format!("Couldn't create build dir: {}", String::from_utf8_lossy(&output.stderr)));
}
}
for src in &self.srcs {
objs.push(&src.obj_name);
}
let mut cmd = String::new();
cmd.push_str(&self.build_config.compiler);
cmd.push_str(" -o ");
cmd.push_str(&self.bin_path);
if self.target_config.typ == "dll" {
cmd.push_str(" -shared ");
}
for obj in objs {
cmd.push_str(" ");
cmd.push_str(obj);
}
cmd.push_str(" ");
cmd.push_str(&self.target_config.cflags);
cmd.push_str(" ");
for dep_target in dep_targets {
cmd.push_str(" -I");
cmd.push_str(&dep_target.target_config.include_dir);
cmd.push_str(" ");
let lib_name = dep_target.target_config.name.clone();
let lib_name = lib_name.replace("lib", "-l");
cmd.push_str(&lib_name);
cmd.push_str(" ");
}
for package in self.packages {
for target in &package.target_configs {
cmd.push_str(" -I");
cmd.push_str(&target.include_dir);
cmd.push_str(" ");
let lib_name = target.name.clone();
let lib_name = lib_name.replace("lib", "-l");
cmd.push_str(&lib_name);
cmd.push_str(" ");
}
cmd.push_str(" -I");
cmd.push_str(" ");
cmd.push_str(" -l");
cmd.push_str(&package.name);
cmd.push_str(" ");
}
if self.packages.len() + self.dependant_libs.len() > 0 {
cmd.push_str(" -L");
cmd.push_str(BUILD_DIR);
cmd.push_str(" ");
cmd.push_str(" -Wl,-rpath,");
cmd.push_str(BUILD_DIR);
cmd.push_str(" ");
}
cmd.push_str(&self.target_config.libs);
log(LogLevel::Info, &format!("Linking target: {}", &self.target_config.name));
log(LogLevel::Info, &format!(" Command: {}", &cmd));
let output = Command::new("sh")
.arg("-c")
.arg(&cmd)
.output()
.expect("failed to execute process");
if output.status.success() {
log(LogLevel::Info, " Linking successful");
hasher::save_hashes_to_file(&self.hash_file_path, &self.path_hash);
} else {
log(LogLevel::Error, " Linking failed");
log(LogLevel::Error, &format!(" Command: {}", &cmd));
log(LogLevel::Error, &format!(" Error: {}", String::from_utf8_lossy(&output.stderr)));
std::process::exit(1);
}
}
fn gen_cc(&self, src: &Src) -> String {
let mut cc = String::new();
cc.push_str("{\n");
if self.build_config.compiler == "clang++" || self.build_config.compiler == "g++" {
cc.push_str("\t\"command\": \"c++");
} else if self.build_config.compiler == "clang" || self.build_config.compiler == "gcc"{
cc.push_str("\t\"command\": \"cc");
} else {
log(LogLevel::Error, &format!("Compiler: {} is not supported", &self.build_config.compiler));
log(LogLevel::Error, "Supported compilers: clang++, g++, clang, gcc");
std::process::exit(1);
}
cc.push_str(" -c -o ");
cc.push_str(&src.obj_name);
cc.push_str(" -I");
cc.push_str(&self.target_config.include_dir);
for pack in self.packages {
for tgtg in &pack.target_configs {
cc.push_str(" -I");
cc.push_str(&tgtg.include_dir);
}
}
cc.push_str(" ");
let cflags = &self.target_config.cflags;
let include_mentions = cflags
.split_whitespace()
.filter(|s| s.starts_with("-I"));
for include_mention in include_mentions {
cc.push_str(include_mention);
cc.push_str(" ");
}
let pkg_mentions = cflags.split('`').filter(|s| s.contains("pkg-config")).collect::<Vec<&str>>();
for pkg in pkg_mentions {
let pkg_output = Command::new("sh")
.arg("-c")
.arg(pkg)
.output()
.expect("failed to execute process");
cc.push_str(&String::from_utf8_lossy(&pkg_output.stdout));
cc.pop();
}
let flags_left = cflags.split("`").
filter(|s| !s.contains("pkg-config"))
.collect::<Vec<&str>>();
let flags_left = flags_left.join(" ").split_whitespace()
.filter(|s| !s.starts_with("-I")).collect::<Vec<&str>>().join(" ");
cc.push_str(&flags_left);
cc.push_str(" ");
#[cfg(target_os = "linux")]
if self.target_config.typ == "dll" {
cc.push_str("-fPIC ");
}
cc.push_str(&src.path);
cc.push_str("\",\n");
let mut dirent = String::new();
dirent.push_str("\t\"directory\": \"");
dirent.push_str(&std::env::current_dir().unwrap().to_str().unwrap().replace("\\", "/"));
dirent.push_str("\",\n");
let dirent = dirent.replace("/", "\\\\").replace("\\\\.\\\\", "\\\\");
cc.push_str(&dirent);
let mut fileent = String::new();
fileent.push_str("\t\"file\": \"");
fileent.push_str(&std::env::current_dir().unwrap().to_str().unwrap().replace("\\", "/"));
fileent.push_str("/");
fileent.push_str(&src.path);
fileent.push_str("\"");
let fileent = fileent.replace("/", "\\\\").replace("\\\\.\\\\", "\\\\");
cc.push_str(&fileent);
cc.push_str("\n}");
#[cfg(target_os = "linux")]
return cc.replace("\\\\", "/");
#[cfg(target_os = "windows")]
return cc;
}
fn get_srcs(&mut self, root_path: &str, target_config: &'a TargetConfig) -> Vec<Src> {
let root_dir = PathBuf::from(root_path);
let mut srcs : Vec<Src> = Vec::new();
let root_entries = std::fs::read_dir(root_dir).unwrap_or_else(|_| {
log(LogLevel::Error, &format!("Could not read directory: {}", root_path));
std::process::exit(1);
});
for entry in root_entries {
let entry = entry.unwrap();
if entry.path().is_dir() {
let path = entry.path().to_str().unwrap().to_string();
srcs.append(&mut self.get_srcs(&path, target_config));
} else {
if !entry.path().to_str().unwrap().ends_with(".cpp") && !entry.path().to_str().unwrap().ends_with(".c") {
continue;
}
let path = entry.path().to_str().unwrap().to_string().replace("\\", "/");
self.add_src(path);
}
}
srcs
}
fn add_src(&mut self, path: String) {
let name = Target::get_src_name(&path);
let obj_name = self.get_src_obj_name(&name);
let dependant_includes = self.get_dependant_includes(&path);
let bin_path = self.bin_path.clone();
self.srcs.push(Src::new(path, name, obj_name, bin_path, dependant_includes));
}
fn get_src_name(path: &str) -> String {
let path_buf = PathBuf::from(path);
let file_name = path_buf.file_name().unwrap().to_str().unwrap();
let name = file_name.split('.').next().unwrap();
name.to_string()
}
fn get_src_obj_name(&self, src_name: &str) -> String {
let mut obj_name = String::new();
obj_name.push_str(OBJ_DIR);
obj_name.push_str("/");
obj_name.push_str(&self.target_config.name);
obj_name.push_str(&src_name);
obj_name.push_str(".o");
obj_name
}
fn get_dependant_includes(&mut self, path: &str) -> Vec<String> {
let mut result = Vec::new();
let include_substrings = self.get_include_substrings(path).unwrap_or_else(|| {
log(LogLevel::Error, &format!("Failed to get include substrings for file: {}", path));
std::process::exit(1);
});
if include_substrings.len() == 0 {
return result;
}
for include_substring in include_substrings {
let dep_path = format!("{}/{}", &self.target_config.include_dir, &include_substring);
if self.dependant_includes.contains_key(&dep_path) {
continue;
}
result.append(&mut self.get_dependant_includes(&dep_path));
result.push(dep_path);
self.dependant_includes.insert(include_substring, result.clone());
}
let result = result.into_iter().unique().collect();
result
}
fn get_include_substrings(&self, path: &str) -> Option<Vec<String>> {
let file = std::fs::File::open(path);
if file.is_err() {
return None;
}
let mut file = file.unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
let mut lines = buf.lines();
let mut include_substrings = Vec::new();
while let Some(line) = lines.next() {
if line.starts_with("#include \"") {
let include_path = line.split("\"").nth(1).unwrap().to_owned();
include_substrings.push(include_path);
}
}
Some(include_substrings)
}
}
impl Src {
fn new(path: String, name: String, obj_name: String, bin_path: String, dependant_includes: Vec<String>) -> Self {
Self {
path,
name,
obj_name,
bin_path,
dependant_includes,
}
}
fn to_build(&self, path_hash: &HashMap<String, String>) -> (bool, String) {
if !Path::new(&self.bin_path).exists() {
let result = (true, format!("\tBinary does not exist: {}", &self.bin_path));
return result;
}
if hasher::is_file_changed(&self.path, &path_hash) {
let result = (true, format!("\tSource file has changed: {}", &self.path));
return result;
}
for dependant_include in &self.dependant_includes {
if hasher::is_file_changed(&dependant_include.clone(), path_hash) {
let result = (true, format!("\tSource file: {} depends on changed include file: {}", &self.path, &dependant_include));
return result;
}
}
let result = (false, format!("Source file: {} does not need to be built", &self.path));
result
}
fn build(&self, build_config: &BuildConfig, target_config: &TargetConfig, dependant_libs: &Vec<Target>) {
let mut cmd = String::new();
cmd.push_str(&build_config.compiler);
cmd.push_str(" -c ");
cmd.push_str(&self.path);
cmd.push_str(" -o ");
cmd.push_str(&self.obj_name);
cmd.push_str(" -I");
cmd.push_str(&target_config.include_dir);
cmd.push_str(" ");
for dependant_lib in dependant_libs {
cmd.push_str("-I");
cmd.push_str(dependant_lib.target_config.include_dir.as_str());
cmd.push_str(" ");
}
if build_config.packages.len() > 0 {
for package in &build_config.packages {
cmd.push_str("-I");
cmd.push_str(&format!(".bld_cpp/includes/{} ", &package.split_whitespace().into_iter().next().unwrap().split('/').last().unwrap().replace(",", "")));
cmd.push_str(" ");
}
}
cmd.push_str(&target_config.cflags);
if target_config.typ == "dll" {
cmd.push_str(" -fPIC");
}
log(LogLevel::Info, &format!("Building: {}", &self.name));
log(LogLevel::Info, &format!(" Command: {}", &cmd));
let output = Command::new("sh")
.arg("-c")
.arg(&cmd)
.output()
.expect("failed to execute process");
if output.status.success() {
log(LogLevel::Info, &format!(" Success: {}", &self.name));
let stdout = String::from_utf8_lossy(&output.stdout);
if stdout.len() > 0 {
log(LogLevel::Info, &format!(" Stdout: {}", stdout));
}
let stderr = String::from_utf8_lossy(&output.stderr);
if stderr.len() > 0 {
log(LogLevel::Info, &format!(" Stderr: {}", stderr));
}
} else {
log(LogLevel::Error, &format!(" Error: {}", &self.name));
log(LogLevel::Error, &format!(" Command: {}", &cmd));
log(LogLevel::Error, &format!(" Stdout: {}", String::from_utf8_lossy(&output.stdout)));
log(LogLevel::Error, &format!(" Stderr: {}", String::from_utf8_lossy(&output.stderr)));
std::process::exit(1);
}
}
}
pub fn clean(targets: &Vec<TargetConfig>) {
if Path::new(".bld_cpp").exists() {
fs::create_dir_all(".bld_cpp").unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not remove binary directory: {}", why));
});
}
if Path::new(OBJ_DIR).exists() {
fs::remove_dir_all(OBJ_DIR).unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not remove object directory: {}", why));
});
log(LogLevel::Info, &format!("Cleaning: {}", OBJ_DIR));
}
for target in targets {
#[cfg(target_os = "windows")]
let hash_path = format!(".bld_cpp/{}.win32.hash", &target.name);
#[cfg(target_os = "linux")]
let hash_path = format!(".bld_cpp/{}.linux.hash", &target.name);
if Path::new(&hash_path).exists() {
fs::remove_file(&hash_path).unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not remove hash file: {}", why));
});
log(LogLevel::Info, &format!("Cleaning: {}", &hash_path));
}
if Path::new(BUILD_DIR).exists() {
let mut bin_name = String::new();
bin_name.push_str(BUILD_DIR);
bin_name.push_str("/");
bin_name.push_str(&target.name);
#[cfg(target_os = "windows")]
if target.typ == "exe" {
bin_name.push_str(".exe");
} else if target.typ == "dll" {
bin_name.push_str(".dll");
}
#[cfg(target_os = "linux")]
if target.typ == "exe" {
bin_name.push_str("");
} else if target.typ == "dll" {
bin_name.push_str(".so");
}
if Path::new(&bin_name).exists() {
fs::remove_file(&bin_name).unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not remove binary file: {}", why));
});
log(LogLevel::Log, &format!("Cleaning: {}", &bin_name));
} else {
log(LogLevel::Log, &format!("Binary file does not exist: {}", &bin_name));
}
}
}
}
pub fn clean_packages(packages: &Vec<Package>) {
for pack in packages {
for target in &pack.target_configs {
#[cfg(target_os = "windows")]
let pack_bin_path = format!("{}/{}.dll",BUILD_DIR, &target.name);
#[cfg(target_os = "linux")]
let pack_bin_path = format!("{}/{}.so",BUILD_DIR, &target.name);
if !Path::new(&pack_bin_path).exists() {
log(LogLevel::Log, &format!("Package binary does not exist: {}", &pack_bin_path));
continue;
}
let cmd_str = format!("rm {}", &pack_bin_path);
log(LogLevel::Debug, cmd_str.as_str());
let output = Command::new("sh")
.arg("-c")
.arg(&cmd_str)
.output()
.expect("failed to execute process");
if output.status.success() {
log(LogLevel::Log, &format!("Cleaned package: {} of {}", &pack.name, &pack.repo));
} else {
log(LogLevel::Error, &format!("Could not clean package: {} of {}", &pack.name, &pack.repo));
}
}
}
}
pub fn build(build_config: &BuildConfig, targets: &Vec<TargetConfig>, gen_cc: bool, packages: &Vec<Package>) {
if !Path::new("./.bld_cpp").exists() {
fs::create_dir(".bld_cpp").unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not create bld_cpp directory: {}", why));
std::process::exit(1);
});
}
if gen_cc {
let mut cc_file = fs::OpenOptions::new()
.write(true)
.append(true)
.open("compile_commands.json")
.unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not open cc file: {}", why));
std::process::exit(1);
});
cc_file.write_all(b"[").unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not write to cc file: {}", why));
std::process::exit(1);
});
}
for target in targets {
let mut tgt = Target::new(build_config, &target, &targets, &packages);
tgt.build(gen_cc);
}
if gen_cc {
let mut cc_file = fs::OpenOptions::new()
.write(true)
.read(true)
.append(true)
.open("compile_commands.json")
.unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not open cc file: {}", why));
std::process::exit(1);
});
cc_file.write_all(b"]").unwrap_or_else(|why| {
log(LogLevel::Error, &format!("Could not write to cc file: {}", why));
std::process::exit(1);
});
}
log(LogLevel::Info, "Build complete");
}
pub fn run (build_config: &BuildConfig, exe_target: &TargetConfig, targets: &Vec<TargetConfig>, packages: &Vec<Package>) {
let trgt = Target::new(build_config, exe_target, &targets, &packages);
if !Path::new(&trgt.bin_path).exists() {
log(LogLevel::Error, &format!("Could not find binary: {}", &trgt.bin_path));
std::process::exit(1);
}
log(LogLevel::Log, &format!("Running: {}", &trgt.bin_path));
let mut cmd = std::process::Command::new(&trgt.bin_path);
let output = cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()).output();
if !output.is_err() {
log(LogLevel::Info, &format!(" Success: {}", &trgt.bin_path));
} else {
log(LogLevel::Error, &format!(" Error: {}", &trgt.bin_path));
std::process::exit(1);
}
}