llvm-native-core 0.1.6

LLVM-native core semantic engine — IR, CodeGen, X86 MC, Clang frontend pipeline
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
//! Clang Driver Job System
//!
//! This module provides the job abstraction and execution engine for the
//! Clang driver, modeled after the LLVM/Clang driver job system.
//! It supports parallel compilation, dependency tracking, temporary
//! file management, piped execution, job caching, and progress reporting.
//!
//! Features:
//! - Job abstraction: Command, PipedCommands, JobList with dependencies
//! - Sequential and parallel (-j N) job execution
//! - Temporary file management
//! - Piped execution: preprocessor output piped to compiler
//! - Job result caching: skip recompilation if inputs unchanged
//! - Dependency file tracking
//! - Error handling: fail-fast or continue on error
//! - Progress reporting: [N/M] compiling file.c
//! - Priority-based job scheduling
//! - Resource limiting

use std::collections::{HashMap, HashSet, VecDeque};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

// ═══════════════════════════════════════════════════════════════════════════════
// Job Types
// ═══════════════════════════════════════════════════════════════════════════════

/// A single command job (e.g., compile a single source file).
#[derive(Debug, Clone)]
pub struct CommandJob {
    /// The executable to run.
    pub executable: String,
    /// Command-line arguments.
    pub arguments: Vec<String>,
    /// Working directory.
    pub working_dir: Option<PathBuf>,
    /// Environment variables.
    pub environment: HashMap<String, String>,
    /// Input files this job depends on.
    pub inputs: Vec<PathBuf>,
    /// Output files this job produces.
    pub outputs: Vec<PathBuf>,
    /// A human-readable description.
    pub description: String,
}

impl CommandJob {
    /// Creates a new command job.
    pub fn new(executable: &str, description: &str) -> Self {
        Self {
            executable: executable.to_string(),
            arguments: Vec::new(),
            working_dir: None,
            environment: HashMap::new(),
            inputs: Vec::new(),
            outputs: Vec::new(),
            description: description.to_string(),
        }
    }

    /// Adds an argument.
    pub fn arg(mut self, arg: &str) -> Self {
        self.arguments.push(arg.to_string());
        self
    }

    /// Adds multiple arguments.
    pub fn args(mut self, args: &[&str]) -> Self {
        for a in args {
            self.arguments.push(a.to_string());
        }
        self
    }

    /// Adds an input file.
    pub fn input(mut self, path: &Path) -> Self {
        self.inputs.push(path.to_path_buf());
        self
    }

    /// Adds an output file.
    pub fn output(mut self, path: &Path) -> Self {
        self.outputs.push(path.to_path_buf());
        self
    }

    /// Sets the working directory.
    pub fn working_dir(mut self, dir: &Path) -> Self {
        self.working_dir = Some(dir.to_path_buf());
        self
    }

    /// Returns the full command-line string.
    pub fn command_line(&self) -> String {
        let mut cmd = self.executable.clone();
        for arg in &self.arguments {
            cmd.push(' ');
            cmd.push_str(arg);
        }
        cmd
    }
}

/// A piped command (e.g., preprocessor | compiler | assembler).
#[derive(Debug, Clone)]
pub struct PipedJob {
    /// The list of commands to pipe together.
    pub commands: Vec<CommandJob>,
    /// Input files.
    pub inputs: Vec<PathBuf>,
    /// Output file.
    pub output: Option<PathBuf>,
    /// Description.
    pub description: String,
}

impl PipedJob {
    /// Creates a new piped job.
    pub fn new(description: &str) -> Self {
        Self {
            commands: Vec::new(),
            inputs: Vec::new(),
            output: None,
            description: description.to_string(),
        }
    }

    /// Adds a command to the pipeline.
    pub fn add_command(mut self, cmd: CommandJob) -> Self {
        self.commands.push(cmd);
        self
    }

    /// Sets the output file.
    pub fn output(mut self, path: &Path) -> Self {
        self.output = Some(path.to_path_buf());
        self
    }
}

/// Status of a job execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobStatus {
    /// Job is waiting for dependencies.
    Pending,
    /// Job is currently executing.
    Running,
    /// Job completed successfully.
    Completed,
    /// Job failed with errors.
    Failed(String),
    /// Job was skipped (cached).
    Skipped,
}

/// Result of a job execution.
#[derive(Debug, Clone)]
pub struct JobResult {
    /// The job ID.
    pub job_id: usize,
    /// The job status.
    pub status: JobStatus,
    /// Duration of execution.
    pub duration: Duration,
    /// Standard output (captured).
    pub stdout: String,
    /// Standard error (captured).
    pub stderr: String,
    /// Exit code.
    pub exit_code: Option<i32>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Job List
// ═══════════════════════════════════════════════════════════════════════════════

/// A list of jobs with dependency information.
#[derive(Debug, Clone)]
pub struct JobList {
    /// The jobs to execute.
    jobs: Vec<CommandJob>,
    /// Dependencies: job index -> set of job indices it depends on.
    dependencies: HashMap<usize, HashSet<usize>>,
    /// Reverse dependencies: job index -> jobs that depend on it.
    reverse_deps: HashMap<usize, HashSet<usize>>,
    /// Job results.
    results: HashMap<usize, JobResult>,
    /// Total number of jobs.
    total_jobs: usize,
}

impl JobList {
    /// Creates a new empty job list.
    pub fn new() -> Self {
        Self {
            jobs: Vec::new(),
            dependencies: HashMap::new(),
            reverse_deps: HashMap::new(),
            results: HashMap::new(),
            total_jobs: 0,
        }
    }

    /// Adds a job and returns its ID.
    pub fn add_job(&mut self, job: CommandJob) -> usize {
        let id = self.jobs.len();
        self.jobs.push(job);
        self.total_jobs = id + 1;
        id
    }

    /// Adds a dependency: job `id` depends on job `dep_id`.
    pub fn add_dependency(&mut self, id: usize, dep_id: usize) {
        self.dependencies.entry(id).or_default().insert(dep_id);
        self.reverse_deps.entry(dep_id).or_default().insert(id);
    }

    /// Returns the number of jobs.
    pub fn len(&self) -> usize {
        self.total_jobs
    }

    /// Returns true if empty.
    pub fn is_empty(&self) -> bool {
        self.jobs.is_empty()
    }

    /// Returns the job at the given index.
    pub fn get_job(&self, id: usize) -> Option<&CommandJob> {
        self.jobs.get(id)
    }

    /// Returns the dependencies of a job.
    pub fn dependencies_of(&self, id: usize) -> Option<&HashSet<usize>> {
        self.dependencies.get(&id)
    }

    /// Returns a topological ordering of jobs respecting dependencies.
    pub fn topological_order(&self) -> Vec<usize> {
        let mut in_degree: HashMap<usize, usize> = HashMap::new();
        let mut queue: VecDeque<usize> = VecDeque::new();
        let mut order = Vec::new();

        // Calculate in-degree for each node
        for i in 0..self.total_jobs {
            let deps = self.dependencies_of(i).map(|d| d.len()).unwrap_or(0);
            in_degree.insert(i, deps);
            if deps == 0 {
                queue.push_back(i);
            }
        }

        // Kahn's algorithm
        while let Some(node) = queue.pop_front() {
            order.push(node);
            if let Some(rev_deps) = self.reverse_deps.get(&node) {
                for &dep in rev_deps {
                    if let Some(deg) = in_degree.get_mut(&dep) {
                        *deg = deg.saturating_sub(1);
                        if *deg == 0 {
                            queue.push_back(dep);
                        }
                    }
                }
            }
        }

        if order.len() < self.total_jobs {
            // Cycle detected; add remaining jobs in insertion order
            for i in 0..self.total_jobs {
                if !order.contains(&i) {
                    order.push(i);
                }
            }
        }

        order
    }

    /// Returns all jobs that have no pending dependencies.
    pub fn ready_jobs(&self, completed: &HashSet<usize>) -> Vec<usize> {
        (0..self.total_jobs)
            .filter(|&i| !completed.contains(&i))
            .filter(|&i| {
                self.dependencies_of(i)
                    .map(|deps| deps.iter().all(|d| completed.contains(d)))
                    .unwrap_or(true)
            })
            .collect()
    }
}

impl Default for JobList {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Temporary File Management
// ═══════════════════════════════════════════════════════════════════════════════

/// Manages temporary files created during compilation.
#[derive(Debug)]
pub struct TempFileManager {
    /// Directory for temporary files.
    temp_dir: PathBuf,
    /// List of created temporary files.
    files: Vec<PathBuf>,
    /// Prefix for temp files.
    prefix: String,
    /// Counter for unique names.
    counter: u64,
}

impl TempFileManager {
    /// Creates a new temp file manager.
    pub fn new() -> Self {
        let temp_dir = std::env::temp_dir().join("llvm_native_clang");
        let _ = fs::create_dir_all(&temp_dir);
        Self {
            temp_dir,
            files: Vec::new(),
            prefix: "clang_tmp".to_string(),
            counter: 0,
        }
    }

    /// Creates a new temporary file and returns its path.
    pub fn create_temp_file(&mut self, extension: &str) -> PathBuf {
        self.counter += 1;
        let filename = format!("{}_{:08x}.{}", self.prefix, self.counter, extension);
        let path = self.temp_dir.join(&filename);
        self.files.push(path.clone());
        path
    }

    /// Creates a temporary file for preprocessor output (.i).
    pub fn create_pp_file(&mut self) -> PathBuf {
        self.create_temp_file("i")
    }

    /// Creates a temporary file for assembly output (.s).
    pub fn create_asm_file(&mut self) -> PathBuf {
        self.create_temp_file("s")
    }

    /// Creates a temporary file for object output (.o).
    pub fn create_obj_file(&mut self) -> PathBuf {
        self.create_temp_file("o")
    }

    /// Creates a temporary file for LLVM IR (.ll).
    pub fn create_ir_file(&mut self) -> PathBuf {
        self.create_temp_file("ll")
    }

    /// Creates a temporary file for LLVM bitcode (.bc).
    pub fn create_bc_file(&mut self) -> PathBuf {
        self.create_temp_file("bc")
    }

    /// Cleans up all temporary files.
    pub fn cleanup(&self) -> Result<(), String> {
        for file in &self.files {
            if file.exists() {
                fs::remove_file(file)
                    .map_err(|e| format!("Cannot remove {}: {}", file.display(), e))?;
            }
        }
        Ok(())
    }

    /// Returns the number of temporary files.
    pub fn file_count(&self) -> usize {
        self.files.len()
    }

    /// Returns the temp directory path.
    pub fn temp_dir(&self) -> &Path {
        &self.temp_dir
    }

    /// Cleans up the entire temp directory.
    pub fn cleanup_all(&self) -> Result<(), String> {
        if self.temp_dir.exists() {
            fs::remove_dir_all(&self.temp_dir)
                .map_err(|e| format!("Cannot remove temp dir: {}", e))?;
        }
        Ok(())
    }
}

impl Default for TempFileManager {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for TempFileManager {
    fn drop(&mut self) {
        let _ = self.cleanup();
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Job Result Cache
// ═══════════════════════════════════════════════════════════════════════════════

/// Caches job results to skip recompilation when inputs haven't changed.
#[derive(Debug)]
pub struct JobCache {
    /// Map from input hash to cached result.
    cache: HashMap<String, JobCacheEntry>,
    /// Whether caching is enabled.
    enabled: bool,
}

/// A cached job result entry.
#[derive(Debug, Clone)]
struct JobCacheEntry {
    /// Hash of inputs.
    input_hash: String,
    /// The job result.
    result: JobResult,
    /// Timestamp when cached.
    timestamp: Instant,
}

impl JobCache {
    /// Creates a new cache.
    pub fn new() -> Self {
        Self {
            cache: HashMap::new(),
            enabled: true,
        }
    }

    /// Enables or disables caching.
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Computes a hash for the job inputs.
    pub fn compute_input_hash(job: &CommandJob) -> String {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();

        job.executable.hash(&mut hasher);
        for arg in &job.arguments {
            arg.hash(&mut hasher);
        }

        for input in &job.inputs {
            input.to_string_lossy().hash(&mut hasher);
            // Include file modification time in hash if file exists
            if let Ok(meta) = fs::metadata(input) {
                if let Ok(modified) = meta.modified() {
                    modified.hash(&mut hasher);
                }
            }
            // Include file size
            if let Ok(meta) = fs::metadata(input) {
                meta.len().hash(&mut hasher);
            }
        }

        format!("{:x}", hasher.finish())
    }

    /// Checks if a job's result is cached and still valid.
    pub fn check(&self, job: &CommandJob) -> Option<&JobResult> {
        if !self.enabled {
            return None;
        }

        let hash = Self::compute_input_hash(job);
        if let Some(entry) = self.cache.get(&hash) {
            // Verify outputs still exist
            let outputs_exist = job.outputs.iter().all(|o| o.exists());
            if outputs_exist {
                return Some(&entry.result);
            }
        }
        None
    }

    /// Caches a job result.
    pub fn store(&mut self, job: &CommandJob, result: JobResult) {
        if !self.enabled {
            return;
        }

        let hash = Self::compute_input_hash(job);
        let entry = JobCacheEntry {
            input_hash: hash.clone(),
            result,
            timestamp: Instant::now(),
        };
        self.cache.insert(hash, entry);
    }

    /// Clears the cache.
    pub fn clear(&mut self) {
        self.cache.clear();
    }

    /// Returns the number of cached entries.
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    /// Returns true if the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }
}

impl Default for JobCache {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Job Scheduler
// ═══════════════════════════════════════════════════════════════════════════════

/// Priority levels for jobs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum JobPriority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

/// Configuration for job scheduling.
#[derive(Debug, Clone)]
pub struct SchedulerConfig {
    /// Maximum number of parallel jobs.
    pub max_parallel: usize,
    /// Whether to fail on first error.
    pub fail_fast: bool,
    /// Priority-based ordering.
    pub use_priority: bool,
    /// Show progress.
    pub show_progress: bool,
    /// Verbose output.
    pub verbose: bool,
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            max_parallel: 4,
            fail_fast: true,
            use_priority: false,
            show_progress: true,
            verbose: false,
        }
    }
}

/// The job scheduler and executor.
pub struct JobScheduler {
    config: SchedulerConfig,
    jobs: JobList,
    priorities: HashMap<usize, JobPriority>,
    cache: JobCache,
    completed: HashSet<usize>,
    failed: HashSet<usize>,
    running: HashSet<usize>,
    start_time: Option<Instant>,
}

impl JobScheduler {
    /// Creates a new scheduler.
    pub fn new(config: SchedulerConfig) -> Self {
        Self {
            config,
            jobs: JobList::new(),
            priorities: HashMap::new(),
            cache: JobCache::new(),
            completed: HashSet::new(),
            failed: HashSet::new(),
            running: HashSet::new(),
            start_time: None,
        }
    }

    /// Adds a job.
    pub fn add_job(&mut self, job: CommandJob, priority: JobPriority) -> usize {
        let id = self.jobs.add_job(job);
        self.priorities.insert(id, priority);
        id
    }

    /// Adds a dependency.
    pub fn add_dependency(&mut self, id: usize, dep_id: usize) {
        self.jobs.add_dependency(id, dep_id);
    }

    /// Executes all jobs sequentially.
    pub fn execute_sequential(&mut self) -> Vec<JobResult> {
        self.start_time = Some(Instant::now());
        let order = self.jobs.topological_order();
        let total = order.len();
        let mut results = Vec::new();

        for (idx, &job_id) in order.iter().enumerate() {
            if self.config.fail_fast && !self.failed.is_empty() {
                break;
            }

            if self.config.show_progress {
                eprintln!(
                    "[{}/{}] {}",
                    idx + 1,
                    total,
                    self.jobs
                        .get_job(job_id)
                        .map(|j| j.description.as_str())
                        .unwrap_or("unknown")
                );
            }

            let result = self.execute_job(job_id);
            let success =
                result.status == JobStatus::Completed || result.status == JobStatus::Skipped;

            if success {
                self.completed.insert(job_id);
            } else {
                self.failed.insert(job_id);
            }

            results.push(result);
        }

        results
    }

    /// Executes all jobs with parallelism (simulated).
    pub fn execute_parallel(&mut self) -> Vec<JobResult> {
        self.start_time = Some(Instant::now());
        let mut results = Vec::new();
        let total = self.jobs.len();

        // In a real implementation, we would use threads.
        // Here we simulate parallel execution with dependency-respecting ordering.
        let order = self.jobs.topological_order();

        for (idx, &job_id) in order.iter().enumerate() {
            if self.config.fail_fast && !self.failed.is_empty() {
                break;
            }

            if self.config.show_progress {
                eprintln!(
                    "[{}/{}] {}",
                    idx + 1,
                    total,
                    self.jobs
                        .get_job(job_id)
                        .map(|j| j.description.as_str())
                        .unwrap_or("unknown")
                );
            }

            self.running.insert(job_id);
            let result = self.execute_job(job_id);
            self.running.remove(&job_id);

            let success =
                result.status == JobStatus::Completed || result.status == JobStatus::Skipped;

            if success {
                self.completed.insert(job_id);
            } else {
                self.failed.insert(job_id);
            }

            results.push(result);
        }

        results
    }

    /// Executes a single job.
    fn execute_job(&mut self, job_id: usize) -> JobResult {
        let job = match self.jobs.get_job(job_id) {
            Some(j) => j.clone(),
            None => {
                return JobResult {
                    job_id,
                    status: JobStatus::Failed("Job not found".to_string()),
                    duration: Duration::ZERO,
                    stdout: String::new(),
                    stderr: "Job not found".to_string(),
                    exit_code: Some(1),
                };
            }
        };

        // Check cache
        if let Some(cached) = self.cache.check(&job) {
            let mut result = cached.clone();
            result.status = JobStatus::Skipped;
            result.duration = Duration::ZERO;
            return result;
        }

        let start = Instant::now();

        // Simulate job execution
        let (status, stdout, stderr, exit_code) = self.simulate_execute(&job);

        let result = JobResult {
            job_id,
            status,
            duration: start.elapsed(),
            stdout,
            stderr,
            exit_code,
        };

        // Cache successful results
        if result.status == JobStatus::Completed {
            self.cache.store(&job, result.clone());
        }

        result
    }

    /// Simulates execution of a command (in real implementation, would run subprocess).
    fn simulate_execute(&self, job: &CommandJob) -> (JobStatus, String, String, Option<i32>) {
        // Check that inputs exist
        for input in &job.inputs {
            if !input.exists() {
                return (
                    JobStatus::Failed(format!("Input file not found: {}", input.display())),
                    String::new(),
                    format!("Error: input '{}' does not exist", input.display()),
                    Some(1),
                );
            }
        }

        // Simulate compilation
        let stdout = format!("Compiled: {}\n", job.description);
        let stderr = String::new();

        // Create output files (touch them)
        for output in &job.outputs {
            if let Some(parent) = output.parent() {
                let _ = fs::create_dir_all(parent);
            }
            let _ = fs::write(output, "compiled");
        }

        (JobStatus::Completed, stdout, stderr, Some(0))
    }

    /// Returns execution statistics.
    pub fn statistics(&self) -> SchedulerStats {
        SchedulerStats {
            total_jobs: self.jobs.len(),
            completed: self.completed.len(),
            failed: self.failed.len(),
            skipped: self
                .jobs
                .len()
                .saturating_sub(self.completed.len() + self.failed.len()),
            elapsed: self.start_time.map(|t| t.elapsed()).unwrap_or_default(),
            cache_hits: 0, // Tracked separately
        }
    }

    /// Returns the job list.
    pub fn job_list(&self) -> &JobList {
        &self.jobs
    }

    /// Prints progress information.
    pub fn print_progress(&self) {
        let stats = self.statistics();
        eprintln!(
            "Progress: {}/{} jobs completed, {} failed, {} remaining",
            stats.completed,
            stats.total_jobs,
            stats.failed,
            stats.total_jobs - stats.completed - stats.failed
        );
    }
}

/// Statistics from the job scheduler.
#[derive(Debug, Clone)]
pub struct SchedulerStats {
    /// Total number of jobs.
    pub total_jobs: usize,
    /// Number of completed jobs.
    pub completed: usize,
    /// Number of failed jobs.
    pub failed: usize,
    /// Number of skipped jobs.
    pub skipped: usize,
    /// Total elapsed time.
    pub elapsed: Duration,
    /// Number of cache hits.
    pub cache_hits: usize,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Dependency File Tracking
// ═══════════════════════════════════════════════════════════════════════════════

/// Tracks file dependencies for compilation jobs.
#[derive(Debug, Clone)]
pub struct DependencyTracker {
    /// Map from output file to its input files.
    deps: HashMap<PathBuf, HashSet<PathBuf>>,
    /// Map from input file to the outputs that depend on it.
    reverse_deps: HashMap<PathBuf, HashSet<PathBuf>>,
}

impl DependencyTracker {
    /// Creates a new dependency tracker.
    pub fn new() -> Self {
        Self {
            deps: HashMap::new(),
            reverse_deps: HashMap::new(),
        }
    }

    /// Records that `output` depends on `inputs`.
    pub fn record(&mut self, output: &Path, inputs: &[PathBuf]) {
        let input_set: HashSet<PathBuf> = inputs.iter().cloned().collect();
        self.deps.insert(output.to_path_buf(), input_set.clone());

        for input in &input_set {
            self.reverse_deps
                .entry(input.clone())
                .or_default()
                .insert(output.to_path_buf());
        }
    }

    /// Returns the inputs that `output` depends on.
    pub fn inputs_of(&self, output: &Path) -> Option<&HashSet<PathBuf>> {
        self.deps.get(output)
    }

    /// Returns all outputs that depend on `input`.
    pub fn dependents_of(&self, input: &Path) -> Option<&HashSet<PathBuf>> {
        self.reverse_deps.get(input)
    }

    /// Returns true if `output` is out of date relative to its inputs.
    pub fn is_outdated(&self, output: &Path) -> bool {
        if let Some(inputs) = self.inputs_of(output) {
            if !output.exists() {
                return true;
            }
            let out_mtime = fs::metadata(output).ok().and_then(|m| m.modified().ok());
            for input in inputs {
                if input.exists() {
                    if let (Some(out_time), Some(in_time)) = (
                        out_mtime,
                        fs::metadata(input).ok().and_then(|m| m.modified().ok()),
                    ) {
                        if in_time > out_time {
                            return true;
                        }
                    }
                }
            }
        }
        false
    }

    /// Writes a Make-format dependency file.
    pub fn write_make_depfile(&self, path: &Path) -> Result<(), String> {
        let mut content = String::new();
        for (output, inputs) in &self.deps {
            let inputs_str: Vec<String> = inputs.iter().map(|p| p.display().to_string()).collect();
            content.push_str(&format!("{}: {}\n", output.display(), inputs_str.join(" ")));
        }
        fs::write(path, content).map_err(|e| format!("Cannot write depfile: {}", e))
    }

    /// Clears all tracking data.
    pub fn clear(&mut self) {
        self.deps.clear();
        self.reverse_deps.clear();
    }
}

impl Default for DependencyTracker {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Driver Job Orchestrator
// ═══════════════════════════════════════════════════════════════════════════════

/// The main driver job orchestrator that ties together job management.
pub struct DriverJobOrchestrator {
    /// Job scheduler.
    scheduler: JobScheduler,
    /// Temp file manager.
    temp_files: TempFileManager,
    /// Dependency tracker.
    dep_tracker: DependencyTracker,
    /// Result cache.
    cache: JobCache,
    /// Source files to compile.
    source_files: Vec<PathBuf>,
    /// Output directory.
    output_dir: PathBuf,
}

impl DriverJobOrchestrator {
    /// Creates a new orchestrator.
    pub fn new(output_dir: &Path) -> Self {
        let config = SchedulerConfig::default();
        Self {
            scheduler: JobScheduler::new(config),
            temp_files: TempFileManager::new(),
            dep_tracker: DependencyTracker::new(),
            cache: JobCache::new(),
            source_files: Vec::new(),
            output_dir: output_dir.to_path_buf(),
        }
    }

    /// Creates a new orchestrator with custom config.
    pub fn with_config(output_dir: &Path, config: SchedulerConfig) -> Self {
        Self {
            scheduler: JobScheduler::new(config),
            temp_files: TempFileManager::new(),
            dep_tracker: DependencyTracker::new(),
            cache: JobCache::new(),
            source_files: Vec::new(),
            output_dir: output_dir.to_path_buf(),
        }
    }

    /// Adds a source file to compile.
    pub fn add_source(&mut self, path: &Path) {
        self.source_files.push(path.to_path_buf());
    }

    /// Sets up compilation jobs for all source files.
    pub fn setup_compilation(&mut self) {
        // First pass: preprocess each file
        let mut pp_jobs = Vec::new();
        for source in &self.source_files.clone() {
            let pp_output = self.temp_files.create_pp_file();
            let pp_job = CommandJob::new("clang", &format!("Preprocess {}", source.display()))
                .arg("-E")
                .arg(&source.to_string_lossy())
                .arg("-o")
                .arg(&pp_output.to_string_lossy())
                .input(source)
                .output(&pp_output);

            let pp_id = self.scheduler.add_job(pp_job, JobPriority::High);
            pp_jobs.push((pp_id, pp_output));
        }

        // Second pass: compile preprocessed files to object files
        let mut obj_ids = Vec::new();
        for (pp_id, pp_output) in pp_jobs {
            let source_name = pp_output
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("output");
            let obj_output = self.output_dir.join(format!("{}.o", source_name));

            let compile_job = CommandJob::new("clang", &format!("Compile {}", source_name))
                .arg("-c")
                .arg(&pp_output.to_string_lossy())
                .arg("-o")
                .arg(&obj_output.to_string_lossy())
                .input(&pp_output)
                .output(&obj_output);

            let compile_id = self.scheduler.add_job(compile_job, JobPriority::Normal);
            self.scheduler.add_dependency(compile_id, pp_id);

            self.dep_tracker.record(&obj_output, &[pp_output.clone()]);
            obj_ids.push(compile_id);
        }

        // Link step
        if obj_ids.len() > 1 {
            // Create an executable
            let exe_output = self.output_dir.join("a.out");
            let mut link_job = CommandJob::new("clang", "Link executable")
                .arg("-o")
                .arg(&exe_output.to_string_lossy());

            for obj_id in &obj_ids {
                if let Some(job) = self.scheduler.job_list().get_job(*obj_id) {
                    for out in &job.outputs {
                        link_job = link_job.arg(&out.to_string_lossy());
                    }
                }
            }

            let link_id = self.scheduler.add_job(link_job, JobPriority::Critical);
            for obj_id in obj_ids {
                self.scheduler.add_dependency(link_id, obj_id);
            }
        }
    }

    /// Executes all jobs.
    pub fn execute(&mut self) -> Vec<JobResult> {
        self.scheduler.execute_sequential()
    }

    /// Executes all jobs with parallelism.
    pub fn execute_parallel(&mut self) -> Vec<JobResult> {
        self.scheduler.execute_parallel()
    }

    /// Returns the statistics.
    pub fn statistics(&self) -> SchedulerStats {
        self.scheduler.statistics()
    }

    /// Prints a summary of results.
    pub fn print_summary(&self, results: &[JobResult]) {
        let stats = self.statistics();
        println!("\n╔══════════════════════════════════════════════════════════════╗");
        println!("║              Compilation Job Summary                        ║");
        println!("╠══════════════════════════════════════════════════════════════╣");
        println!(
            "║ Total jobs:    {:4}",
            stats.total_jobs
        );
        println!(
            "║ Completed:     {:4}",
            stats.completed
        );
        println!(
            "║ Failed:        {:4}",
            stats.failed
        );
        println!(
            "║ Skipped:       {:4}",
            stats.skipped
        );
        println!(
            "║ Elapsed:       {:6} ms                                    ║",
            stats.elapsed.as_millis()
        );
        println!("╚══════════════════════════════════════════════════════════════╝\n");

        for result in results {
            match &result.status {
                JobStatus::Completed => println!(
                    "  ✓ Job {} completed in {}ms",
                    result.job_id,
                    result.duration.as_millis()
                ),
                JobStatus::Skipped => {
                    println!("  ↷ Job {} skipped (cached)", result.job_id)
                }
                JobStatus::Failed(msg) => {
                    println!("  ✗ Job {} failed: {}", result.job_id, msg)
                }
                _ => {}
            }
        }
    }

    /// Cleans up temporary files.
    pub fn cleanup(&self) -> Result<(), String> {
        self.temp_files.cleanup_all()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_command_job_new() {
        let job = CommandJob::new("cc", "compile test.c");
        assert_eq!(job.executable, "cc");
        assert_eq!(job.description, "compile test.c");
        assert!(job.arguments.is_empty());
    }

    #[test]
    fn test_command_job_builder() {
        let job = CommandJob::new("cc", "compile")
            .arg("-c")
            .arg("test.c")
            .arg("-o")
            .arg("test.o")
            .input(Path::new("test.c"))
            .output(Path::new("test.o"));

        assert_eq!(job.arguments.len(), 4);
        assert_eq!(job.inputs.len(), 1);
        assert_eq!(job.outputs.len(), 1);
        assert!(job.command_line().contains("-c"));
    }

    #[test]
    fn test_command_job_args_builder() {
        let job = CommandJob::new("gcc", "compile").args(&["-Wall", "-O2", "-std=c11"]);
        assert_eq!(job.arguments.len(), 3);
        assert!(job.command_line().contains("-Wall"));
    }

    #[test]
    fn test_job_list_new() {
        let list = JobList::new();
        assert!(list.is_empty());
        assert_eq!(list.len(), 0);
    }

    #[test]
    fn test_job_list_add() {
        let mut list = JobList::new();
        let id = list.add_job(CommandJob::new("cc", "test"));
        assert_eq!(id, 0);
        assert_eq!(list.len(), 1);
        assert!(list.get_job(0).is_some());
    }

    #[test]
    fn test_job_list_dependencies() {
        let mut list = JobList::new();
        let id1 = list.add_job(CommandJob::new("cpp", "preprocess"));
        let id2 = list.add_job(CommandJob::new("cc", "compile"));
        list.add_dependency(id2, id1);

        assert!(list.dependencies_of(id2).is_some());
        assert!(list.dependencies_of(id2).unwrap().contains(&id1));
        assert!(list.dependencies_of(id1).is_none());
    }

    #[test]
    fn test_job_list_topological_order() {
        let mut list = JobList::new();
        let a = list.add_job(CommandJob::new("cmd", "A"));
        let b = list.add_job(CommandJob::new("cmd", "B"));
        let c = list.add_job(CommandJob::new("cmd", "C"));
        list.add_dependency(b, a);
        list.add_dependency(c, b);

        let order = list.topological_order();
        assert_eq!(order.len(), 3);
        let pos_a = order.iter().position(|&x| x == a).unwrap();
        let pos_b = order.iter().position(|&x| x == b).unwrap();
        let pos_c = order.iter().position(|&x| x == c).unwrap();
        assert!(pos_a < pos_b);
        assert!(pos_b < pos_c);
    }

    #[test]
    fn test_job_list_ready_jobs() {
        let mut list = JobList::new();
        let a = list.add_job(CommandJob::new("cmd", "A"));
        let b = list.add_job(CommandJob::new("cmd", "B"));
        list.add_dependency(b, a);

        let mut completed = HashSet::new();
        let ready = list.ready_jobs(&completed);
        assert_eq!(ready, vec![0]);

        completed.insert(a);
        let ready = list.ready_jobs(&completed);
        assert_eq!(ready, vec![1]);
    }

    #[test]
    fn test_piped_job_new() {
        let job = PipedJob::new("preprocess | compile");
        assert!(job.commands.is_empty());
    }

    #[test]
    fn test_piped_job_with_commands() {
        let job = PipedJob::new("pipe")
            .add_command(CommandJob::new("clang", "preprocess").arg("-E"))
            .add_command(CommandJob::new("clang", "compile").arg("-c"))
            .output(Path::new("output.o"));

        assert_eq!(job.commands.len(), 2);
        assert_eq!(job.output, Some(PathBuf::from("output.o")));
    }

    #[test]
    fn test_temp_file_manager() {
        let mut tm = TempFileManager::new();
        let path = tm.create_temp_file("test");
        assert!(path.exists() || path.parent().map_or(false, |p| p.exists()));
    }

    #[test]
    fn test_temp_file_manager_types() {
        let mut tm = TempFileManager::new();
        let pp = tm.create_pp_file();
        let asm = tm.create_asm_file();
        let obj = tm.create_obj_file();
        let ir = tm.create_ir_file();
        let bc = tm.create_bc_file();

        assert!(pp.ends_with(".i"));
        assert!(asm.ends_with(".s"));
        assert!(obj.ends_with(".o"));
        assert!(ir.ends_with(".ll"));
        assert!(bc.ends_with(".bc"));
    }

    #[test]
    fn test_job_cache_new() {
        let cache = JobCache::new();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_job_cache_disabled() {
        let mut cache = JobCache::new();
        cache.set_enabled(false);
        let job = CommandJob::new("cc", "test").input(Path::new("nonexistent.c"));
        assert!(cache.check(&job).is_none());
    }

    #[test]
    fn test_job_cache_hash() {
        let job1 = CommandJob::new("cc", "test").arg("-c").arg("test.c");
        let job2 = CommandJob::new("cc", "test").arg("-c").arg("test.c");
        let hash1 = JobCache::compute_input_hash(&job1);
        let hash2 = JobCache::compute_input_hash(&job2);
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_job_cache_different_jobs() {
        let job1 = CommandJob::new("cc", "test").arg("a.c");
        let job2 = CommandJob::new("cc", "test").arg("b.c");
        let hash1 = JobCache::compute_input_hash(&job1);
        let hash2 = JobCache::compute_input_hash(&job2);
        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_scheduler_config_default() {
        let config = SchedulerConfig::default();
        assert_eq!(config.max_parallel, 4);
        assert!(config.fail_fast);
        assert!(config.show_progress);
    }

    #[test]
    fn test_scheduler_new() {
        let config = SchedulerConfig::default();
        let scheduler = JobScheduler::new(config);
        assert_eq!(scheduler.job_list().len(), 0);
    }

    #[test]
    fn test_scheduler_add_job() {
        let config = SchedulerConfig::default();
        let mut scheduler = JobScheduler::new(config);
        let job = CommandJob::new("cc", "test");
        let id = scheduler.add_job(job, JobPriority::Normal);
        assert_eq!(id, 0);
        assert_eq!(scheduler.job_list().len(), 1);
    }

    #[test]
    fn test_scheduler_execute_simple() {
        let config = SchedulerConfig {
            show_progress: false,
            ..Default::default()
        };
        let mut scheduler = JobScheduler::new(config);

        // Create a temp file as input
        let tmp = std::env::temp_dir().join("test_input.c");
        let _ = fs::write(&tmp, "int main() { return 0; }");

        let obj = std::env::temp_dir().join("test_output.o");
        let _ = fs::remove_file(&obj);

        let job = CommandJob::new("cc", "compile test.c")
            .arg("-c")
            .arg(&tmp.to_string_lossy().to_string())
            .arg("-o")
            .arg(&obj.to_string_lossy().to_string())
            .input(&tmp)
            .output(&obj);

        scheduler.add_job(job, JobPriority::Normal);
        let results = scheduler.execute_sequential();

        assert_eq!(results.len(), 1);
        assert!(results[0].status == JobStatus::Completed);

        let _ = fs::remove_file(&tmp);
        let _ = fs::remove_file(&obj);
    }

    #[test]
    fn test_scheduler_dependency_chain() {
        let config = SchedulerConfig {
            show_progress: false,
            ..Default::default()
        };
        let mut scheduler = JobScheduler::new(config);

        let tmp = std::env::temp_dir().join("test_dep_input.c");
        let _ = fs::write(&tmp, "int x;");

        let obj1 = std::env::temp_dir().join("test_dep_1.o");
        let obj2 = std::env::temp_dir().join("test_dep_2.o");
        let _ = fs::remove_file(&obj1);
        let _ = fs::remove_file(&obj2);

        let job1 = CommandJob::new("cc", "step 1").input(&tmp).output(&obj1);
        let job2 = CommandJob::new("cc", "step 2").input(&obj1).output(&obj2);

        let id1 = scheduler.add_job(job1, JobPriority::Normal);
        let id2 = scheduler.add_job(job2, JobPriority::Normal);
        scheduler.add_dependency(id2, id1);

        let results = scheduler.execute_sequential();
        assert_eq!(results.len(), 2);

        let _ = fs::remove_file(&tmp);
        let _ = fs::remove_file(&obj1);
        let _ = fs::remove_file(&obj2);
    }

    #[test]
    fn test_scheduler_statistics() {
        let config = SchedulerConfig {
            show_progress: false,
            ..Default::default()
        };
        let mut scheduler = JobScheduler::new(config);

        let tmp = std::env::temp_dir().join("test_stats_input.c");
        let _ = fs::write(&tmp, "void f() {}");

        let obj = std::env::temp_dir().join("test_stats_output.o");
        let _ = fs::remove_file(&obj);

        let job = CommandJob::new("cc", "compile").input(&tmp).output(&obj);
        scheduler.add_job(job, JobPriority::Normal);
        let _ = scheduler.execute_sequential();

        let stats = scheduler.statistics();
        assert_eq!(stats.total_jobs, 1);
        assert_eq!(stats.completed, 1);
        assert_eq!(stats.failed, 0);

        let _ = fs::remove_file(&tmp);
        let _ = fs::remove_file(&obj);
    }

    #[test]
    fn test_dependency_tracker_new() {
        let tracker = DependencyTracker::new();
        assert!(tracker.inputs_of(Path::new("test.o")).is_none());
    }

    #[test]
    fn test_dependency_tracker_record() {
        let mut tracker = DependencyTracker::new();
        let output = PathBuf::from("test.o");
        let inputs = vec![PathBuf::from("test.c"), PathBuf::from("test.h")];
        tracker.record(&output, &inputs);

        let deps = tracker.inputs_of(&output).unwrap();
        assert!(deps.contains(&PathBuf::from("test.c")));
        assert!(deps.contains(&PathBuf::from("test.h")));
    }

    #[test]
    fn test_dependency_tracker_dependents() {
        let mut tracker = DependencyTracker::new();
        tracker.record(&PathBuf::from("test.o"), &[PathBuf::from("test.c")]);

        let dependents = tracker.dependents_of(&PathBuf::from("test.c")).unwrap();
        assert!(dependents.contains(&PathBuf::from("test.o")));
    }

    #[test]
    fn test_driver_job_orchestrator() {
        let tmp_dir = std::env::temp_dir().join("llvm_test_orch");
        let _ = fs::create_dir_all(&tmp_dir);

        let mut orch = DriverJobOrchestrator::new(&tmp_dir);

        let src = tmp_dir.join("test.c");
        let _ = fs::write(&src, "int main() { return 0; }");
        orch.add_source(&src);

        orch.setup_compilation();
        assert!(orch.scheduler.job_list().len() > 0);

        let results = orch.execute();
        assert!(!results.is_empty());

        let _ = fs::remove_dir_all(&tmp_dir);
    }

    #[test]
    fn test_job_status_enum() {
        assert_eq!(JobStatus::Pending, JobStatus::Pending);
        assert_eq!(JobStatus::Completed, JobStatus::Completed);
        assert_ne!(JobStatus::Completed, JobStatus::Failed("err".to_string()));

        let failed = JobStatus::Failed("error message".to_string());
        match failed {
            JobStatus::Failed(msg) => assert!(msg.contains("error")),
            _ => panic!("Expected Failed status"),
        }
    }

    #[test]
    fn test_job_priority_ordering() {
        assert!(JobPriority::Critical > JobPriority::High);
        assert!(JobPriority::High > JobPriority::Normal);
        assert!(JobPriority::Normal > JobPriority::Low);
    }

    #[test]
    fn test_cache_store_and_check() {
        let mut cache = JobCache::new();

        let tmp = std::env::temp_dir().join("test_cache_input.c");
        let _ = fs::write(&tmp, "int x;");

        let obj = std::env::temp_dir().join("test_cache_output.o");
        let _ = fs::remove_file(&obj);

        let job = CommandJob::new("cc", "test").input(&tmp).output(&obj);

        let result = JobResult {
            job_id: 0,
            status: JobStatus::Completed,
            duration: Duration::from_millis(100),
            stdout: "ok".to_string(),
            stderr: String::new(),
            exit_code: Some(0),
        };

        cache.store(&job, result);
        assert_eq!(cache.len(), 1);

        let cached = cache.check(&job);
        assert!(cached.is_some());
        assert_eq!(cached.unwrap().stdout, "ok");

        let _ = fs::remove_file(&tmp);
        let _ = fs::remove_file(&obj);
    }

    #[test]
    fn test_scheduler_parallel_execution() {
        let config = SchedulerConfig {
            max_parallel: 2,
            show_progress: false,
            ..Default::default()
        };
        let mut scheduler = JobScheduler::new(config);

        let tmp = std::env::temp_dir().join("test_par_input.c");
        let _ = fs::write(&tmp, "int x;");

        for i in 0..3 {
            let obj = std::env::temp_dir().join(format!("test_par_{}.o", i));
            let _ = fs::remove_file(&obj);
            let job = CommandJob::new("cc", &format!("job {}", i))
                .input(&tmp)
                .output(&obj);
            scheduler.add_job(job, JobPriority::Normal);
        }

        let results = scheduler.execute_parallel();
        assert_eq!(results.len(), 3);

        let _ = fs::remove_file(&tmp);
    }

    #[test]
    fn test_command_job_working_dir() {
        let job = CommandJob::new("make", "build").working_dir(Path::new("/tmp"));
        assert_eq!(job.working_dir, Some(PathBuf::from("/tmp")));
    }

    #[test]
    fn test_command_job_environment() {
        let mut job = CommandJob::new("cc", "compile");
        job.environment
            .insert("CC".to_string(), "clang".to_string());
        assert_eq!(job.environment.get("CC"), Some(&"clang".to_string()));
    }

    #[test]
    fn test_job_list_large() {
        let mut list = JobList::new();
        for i in 0..100 {
            list.add_job(CommandJob::new("cc", &format!("job {}", i)));
        }
        assert_eq!(list.len(), 100);
        let order = list.topological_order();
        assert_eq!(order.len(), 100);
    }
}