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
use std::fs;
use std::sync::{Arc, Mutex};
use crate::utils::{BuildConfig, TargetConfig, log, LogLevel};
use std::path::{Path, PathBuf};
use std::io::{Read, Write};
use std::process::Command;
use itertools::Itertools;
use std::collections::HashMap;
use crate::hasher;
use rayon::prelude::*;
use indicatif::{ProgressBar, ProgressStyle};
use colored::Colorize;

//Represents a target
pub struct Target<'a> {
    pub srcs: Vec<Src>,
    pub build_config: &'a BuildConfig,
    pub target_config: &'a TargetConfig,
    dependant_includes: HashMap<String, Vec<String>>,
    pub bin_path: String,
    pub hash_file_path: String,
    path_hash: HashMap<String, String>,
}

//Represents a source file
//A single C or Cpp file
pub struct Src {
    pub path: String,
    pub name: String,
    pub obj_name: String,
    pub dependant_includes: Vec<String>,
}

impl<'a> Target<'a> {
    pub fn new(build_config: &'a BuildConfig, target_config: &'a TargetConfig) -> 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_config.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!("{}.win32.hash", &target_config.name);
        #[cfg(target_os = "linux")]
        let hash_file_path = format!("{}.linux.hash", &target_config.name);
        
        let path_hash = hasher::load_hashes_from_file(&hash_file_path);

        let mut target = Target {
            srcs,
            build_config,
            target_config,
            dependant_includes,
            bin_path,
            path_hash,
            hash_file_path,
        };
        target.get_srcs(&target_config.src, target_config);
        target
    }

    pub fn build(&mut self, gen_cc: bool ) {
        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);
            if to_build {
                hasher::save_hash(&src.path, &mut self.path_hash);
                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(&self.build_config.obj_dir).exists() {
                fs::create_dir(&self.build_config.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));
        self.srcs.par_iter().for_each(|src| {
            let (to_build, _message) = src.to_build(&self.path_hash);
            if to_build {
                src.build(self.build_config, self.target_config);
                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.white/white}] {pos}/{len} ({percent}%) {msg}[{elapsed_precise}] ");
                    progress_bar.set_style(ProgressStyle::with_template(&template)
                    .unwrap()
                    .progress_chars("=>-"));
                    progress_bar.inc(1);
                }
            }
        });
        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);
            log(LogLevel::Debug, &format!("Hashes: {:?}", &self.path_hash));
            self.link();
        }
    }

    pub fn link(&self) {
        let mut objs = Vec::new();
        if !Path::new(&self.build_config.build_dir).exists() {
            fs::create_dir(&self.build_config.build_dir).unwrap();
        }
        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(" ");
        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!("  Error: {}", String::from_utf8_lossy(&output.stderr)));
            std::process::exit(1);
        }
    }

    //-----------------
    //  "command": "c++ -c -o ./obj_lin/app.o -I./Engine/src/include -g -Wall -Wunused -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -pthread -std=c++17 -fPIC ./Engine/src/core/app.cpp",
    //  "directory": "/mnt/f/C++/Nomu_Engine",
    //  "file": "/mnt/f/C++/Nomu_Engine/Engine/src/core/app.cpp"
    fn gen_cc(&self, src: &Src) -> String {
        let mut cc = String::new();
        cc.push_str("{\n");
        cc.push_str("\t\"command\": \"c++ -c -o ");
        cc.push_str(&src.obj_name);
        cc.push_str(" -I");
        cc.push_str(&self.target_config.include_dir);
        cc.push_str(" ");
        let cflags = &self.target_config.cflags;
        //Extract the -I mentions
        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(" ");
        }
        //Expand the pkg-config mentiiions in cflags
        //Extract the pkg-config mentions
        //get strings which are inside ``
        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));
            //trim the end newline
            cc.pop();
        }
        //Add the rest of the cflags
        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;
    }
    //returns a vector of source files in the given root path
    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();
        for entry in std::fs::read_dir(root_dir).unwrap() {
            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
    }

    //adds a source file to the target
    fn add_src(&mut self, path: String) {
        let name = Target::get_src_name(&path);
        let obj_name = self.get_src_obj_name(&name, self.build_config);
        let dependant_includes = self.get_dependant_includes(&path);
        self.srcs.push(Src::new(path, name, obj_name, dependant_includes));
    }

    //returns the file name without the extension from the path
    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()
    }

    //retur the object file name for the given source file
    fn get_src_obj_name(&self, src_name: &str, build_config: &'a BuildConfig) -> String {
        let mut obj_name = String::new();
        obj_name.push_str(&build_config.obj_dir);
        obj_name.push_str("/");
        obj_name.push_str(&src_name);
        obj_name.push_str(".o");
        obj_name
    }

    //returns a vector of .h or .hpp files the given C/C++ depends on
    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 {
            log(LogLevel::Debug, &format!("  {} depends on: {:?}", path, result));
            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();
        log(LogLevel::Debug, &format!("  {} depends on: {:?}", path, result));
        result
    }

    //returns a vector of strings that are the include substrings
    //of the given C/C++ file as variaible path
    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 {
    //Creates a new source file
    fn new(path: String, name: String, obj_name: String, dependant_includes: Vec<String>) -> Self {
        Self {
            path,
            name,
            obj_name,
            dependant_includes,
        }
    }

    fn to_build(&self, path_hash: &HashMap<String, String>) -> (bool, String) {
        if !Path::new(&self.obj_name).exists() {
            let result = (true, format!("\tObject file does not exist: {}", &self.obj_name));
            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) {
        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(" ");
        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(build_config: &BuildConfig, targets: &Vec<TargetConfig>) {
    if Path::new(&build_config.obj_dir).exists() {
        fs::remove_dir_all(&build_config.obj_dir).unwrap_or_else(|why| {
            log(LogLevel::Error, &format!("Could not remove object directory: {}", why));
        });
        log(LogLevel::Info, &format!("Cleaning: {}", &build_config.obj_dir));
    }
    for target in targets {
        //remove hashes
        #[cfg(target_os = "windows")]
        let hash_path = format!("{}.win32.hash", &target.name);
        #[cfg(target_os = "linux")]
        let hash_path = format!("{}.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_config.build_dir).exists() {
            let mut bin_name = String::new();
            bin_name.push_str(&build_config.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::Info, &format!("Cleaning: {}", &bin_name));
            } else {
                log(LogLevel::Log, &format!("Binary file does not exist: {}", &bin_name));
            }
        }
    }
}

pub fn build(build_config: &BuildConfig, targets: &Vec<TargetConfig>, gen_cc: bool) {
    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 trgt = Target::new(build_config, target);
        trgt.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) {
    let trgt = Target::new(build_config, exe_target);
    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.output().expect("failed to execute process");
    if output.status.success() {
        log(LogLevel::Info, &format!("  Success: {}", &trgt.bin_path));
        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: {}", &trgt.bin_path));
        log(LogLevel::Warn, &format!("  Stdout: {}", String::from_utf8_lossy(&output.stdout)));
        log(LogLevel::Error, &format!("  Stderr: {}", String::from_utf8_lossy(&output.stderr)));
        std::process::exit(1);
    }
}