1515#include " CompilationManager.h"
1616#include " ../Detection/UnitDetector.h"
1717#include " ../Utils/FileManager.h"
18+ #include " ../Utils/UnitMetadata.h"
1819#include " CommandAnalyzer.h"
1920#include " DataExtractor.h"
2021#include " llvm/ADT/DenseSet.h"
2324#include " llvm/Support/Path.h"
2425#include < chrono>
2526#include < cstdlib>
27+ #include < ctime>
2628#include < unordered_set>
2729
2830namespace llvm {
@@ -59,6 +61,23 @@ CompilationManager::CompilationManager(const AdvisorConfig &config)
5961 if (config_.getVerbose ()) {
6062 llvm::outs () << " Using temporary directory: " << tempDir_ << " \n " ;
6163 }
64+
65+ // Initialize unit metadata tracking
66+ llvm::SmallString<256 > outputDirPath;
67+ if (llvm::sys::path::is_absolute (config_.getOutputDir ())) {
68+ outputDirPath = config_.getOutputDir ();
69+ } else {
70+ outputDirPath = initialWorkingDir_;
71+ llvm::sys::path::append (outputDirPath, config_.getOutputDir ());
72+ }
73+
74+ unitMetadata_ = std::make_unique<utils::UnitMetadata>(outputDirPath.str ());
75+ if (auto Err = unitMetadata_->loadMetadata ()) {
76+ if (config_.getVerbose ()) {
77+ llvm::errs () << " Failed to load metadata: "
78+ << llvm::toString (std::move (Err)) << " \n " ;
79+ }
80+ }
6281}
6382
6483CompilationManager::~CompilationManager () {
@@ -95,6 +114,9 @@ llvm::Expected<int> CompilationManager::executeWithDataCollection(
95114 llvm::SmallVector<std::unique_ptr<CompilationUnit>, 4 > units;
96115 for (auto &unitInfo : *detectedUnits) {
97116 units.push_back (std::make_unique<CompilationUnit>(unitInfo, tempDir_));
117+
118+ // Register unit in metadata tracker
119+ unitMetadata_->registerUnit (unitInfo.name );
98120 }
99121
100122 // Scan existing files before compilation
@@ -119,6 +141,17 @@ llvm::Expected<int> CompilationManager::executeWithDataCollection(
119141 llvm::errs () << " Data extraction failed: "
120142 << llvm::toString (std::move (Err)) << " \n " ;
121143 }
144+ // Mark unit as failed if data extraction fails
145+ unitMetadata_->updateUnitStatus (unit->getName (), " failed" );
146+ } else {
147+ // Update unit metadata with file counts and artifact types
148+ const auto &generatedFiles = unit->getAllGeneratedFiles ();
149+ size_t totalFiles = 0 ;
150+ for (const auto &category : generatedFiles) {
151+ totalFiles += category.second .size ();
152+ unitMetadata_->addArtifactType (unit->getName (), category.first );
153+ }
154+ unitMetadata_->updateUnitFileCount (unit->getName (), totalFiles);
122155 }
123156 }
124157
@@ -128,6 +161,23 @@ llvm::Expected<int> CompilationManager::executeWithDataCollection(
128161 llvm::errs () << " Output organization failed: "
129162 << llvm::toString (std::move (Err)) << " \n " ;
130163 }
164+ // Mark units as failed if output organization fails
165+ for (auto &unit : units) {
166+ unitMetadata_->updateUnitStatus (unit->getName (), " failed" );
167+ }
168+ } else {
169+ // Mark units as completed on successful organization
170+ for (auto &unit : units) {
171+ unitMetadata_->updateUnitStatus (unit->getName (), " completed" );
172+ }
173+ }
174+
175+ // Save metadata to disk
176+ if (auto Err = unitMetadata_->saveMetadata ()) {
177+ if (config_.getVerbose ()) {
178+ llvm::errs () << " Failed to save metadata: "
179+ << llvm::toString (std::move (Err)) << " \n " ;
180+ }
131181 }
132182
133183 // Clean up leaked files from source directory
@@ -208,22 +258,34 @@ llvm::Error CompilationManager::organizeOutput(
208258 llvm::outs () << " Output directory: " << outputDir << " \n " ;
209259 }
210260
261+ // Generate timestamp for this compilation run
262+ auto now = std::chrono::system_clock::now ();
263+ auto time_t = std::chrono::system_clock::to_time_t (now);
264+ auto tm = *std::localtime (&time_t );
265+
266+ char timestampStr[20 ];
267+ std::strftime (timestampStr, sizeof (timestampStr), " %Y%m%d_%H%M%S" , &tm);
268+
211269 // Move collected files to organized structure
212270 for (const auto &unit : units) {
213- std::string unitDir = outputDir + " / " + unit-> getName ();
214-
215- // Remove existing unit directory if it exists
216- if ( llvm::sys::fs::exists (unitDir)) {
217- if ( auto EC = llvm::sys::fs::remove_directories (unitDir)) {
218- if (config_. getVerbose ()) {
219- llvm::errs () << " Warning: Could not remove existing unit directory: "
220- << unitDir << " \n " ;
221- }
222- }
271+ // Create base unit directory if it doesn't exist
272+ std::string baseUnitDir = outputDir + " / " + unit-> getName ();
273+ llvm::sys::fs::create_directories (baseUnitDir);
274+
275+ // Create timestamped run directory
276+ std::string runDirName = unit-> getName () + " _ " + std::string (timestampStr);
277+ std::string unitDir = baseUnitDir + " / " + runDirName;
278+
279+ if (config_. getVerbose ()) {
280+ llvm::outs () << " Creating run directory: " << unitDir << " \n " ;
223281 }
224282
225- // Create fresh unit directory
283+ // Create timestamped run directory
226284 if (auto EC = llvm::sys::fs::create_directories (unitDir)) {
285+ if (config_.getVerbose ()) {
286+ llvm::errs () << " Warning: Could not create run directory: " << unitDir
287+ << " \n " ;
288+ }
227289 continue ; // Skip if we can't create the directory
228290 }
229291
0 commit comments