|
| 1 | +#include "runcpp2/IncludeManager.hpp" |
| 2 | +#include "runcpp2/Data/ParseCommon.hpp" |
| 3 | +#include "ssLogger/ssLog.hpp" |
| 4 | + |
| 5 | +#include <fstream> |
| 6 | + |
| 7 | +namespace runcpp2 |
| 8 | +{ |
| 9 | + bool IncludeManager::Initialize(const ghc::filesystem::path& buildDir) |
| 10 | + { |
| 11 | + INTERNAL_RUNCPP2_SAFE_START(); |
| 12 | + ssLOG_FUNC_DEBUG(); |
| 13 | + |
| 14 | + IncludeRecordDir = buildDir / "IncludeMaps"; |
| 15 | + |
| 16 | + std::error_code e; |
| 17 | + if(!ghc::filesystem::exists(IncludeRecordDir, e)) |
| 18 | + { |
| 19 | + if(!ghc::filesystem::create_directories(IncludeRecordDir, e)) |
| 20 | + { |
| 21 | + ssLOG_ERROR("Failed to create IncludeMaps directory: " << IncludeRecordDir); |
| 22 | + return false; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return true; |
| 27 | + INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false); |
| 28 | + } |
| 29 | + |
| 30 | + bool IncludeManager::WriteIncludeRecord(const ghc::filesystem::path& sourceFile, |
| 31 | + const std::vector<ghc::filesystem::path>& includes) |
| 32 | + { |
| 33 | + INTERNAL_RUNCPP2_SAFE_START(); |
| 34 | + ssLOG_FUNC_DEBUG(); |
| 35 | + |
| 36 | + if(!sourceFile.is_absolute()) |
| 37 | + { |
| 38 | + ssLOG_ERROR("Source file is not absolute: " << sourceFile); |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + ghc::filesystem::path recordPath = GetRecordPath(sourceFile); |
| 43 | + |
| 44 | + std::ofstream recordFile(recordPath); |
| 45 | + if(!recordFile.is_open()) |
| 46 | + { |
| 47 | + ssLOG_ERROR("Failed to open include record file: " << recordPath); |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + for(const ghc::filesystem::path& include : includes) |
| 52 | + recordFile << include.string() << "\n"; |
| 53 | + |
| 54 | + return true; |
| 55 | + INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false); |
| 56 | + } |
| 57 | + |
| 58 | + bool IncludeManager::ReadIncludeRecord( const ghc::filesystem::path& sourceFile, |
| 59 | + std::vector<ghc::filesystem::path>& outIncludes, |
| 60 | + ghc::filesystem::file_time_type& outRecordTime) |
| 61 | + { |
| 62 | + INTERNAL_RUNCPP2_SAFE_START(); |
| 63 | + ssLOG_FUNC_DEBUG(); |
| 64 | + |
| 65 | + if(!sourceFile.is_absolute()) |
| 66 | + { |
| 67 | + ssLOG_ERROR("Source file is not absolute: " << sourceFile); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + outIncludes.clear(); |
| 72 | + ghc::filesystem::path recordPath = GetRecordPath(sourceFile); |
| 73 | + |
| 74 | + std::error_code e; |
| 75 | + if(!ghc::filesystem::exists(recordPath, e)) |
| 76 | + return false; |
| 77 | + |
| 78 | + outRecordTime = ghc::filesystem::last_write_time(recordPath, e); |
| 79 | + |
| 80 | + std::ifstream recordFile(recordPath); |
| 81 | + if(!recordFile.is_open()) |
| 82 | + { |
| 83 | + ssLOG_ERROR("Failed to open include record file: " << recordPath); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + std::string line; |
| 88 | + while(std::getline(recordFile, line)) |
| 89 | + { |
| 90 | + if(!line.empty()) |
| 91 | + outIncludes.push_back(ghc::filesystem::path(line)); |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | + INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false); |
| 96 | + } |
| 97 | + |
| 98 | + bool IncludeManager::NeedsUpdate( const ghc::filesystem::path& sourceFile, |
| 99 | + const std::vector<ghc::filesystem::path>& includes, |
| 100 | + const ghc::filesystem::file_time_type& recordTime) const |
| 101 | + { |
| 102 | + INTERNAL_RUNCPP2_SAFE_START(); |
| 103 | + ssLOG_FUNC_DEBUG(); |
| 104 | + |
| 105 | + std::error_code e; |
| 106 | + ghc::filesystem::file_time_type sourceTime = ghc::filesystem::last_write_time(sourceFile, e); |
| 107 | + |
| 108 | + if(sourceTime > recordTime) |
| 109 | + return true; |
| 110 | + |
| 111 | + for(const ghc::filesystem::path& include : includes) |
| 112 | + { |
| 113 | + if(ghc::filesystem::exists(include, e)) |
| 114 | + { |
| 115 | + ghc::filesystem::file_time_type includeTime = |
| 116 | + ghc::filesystem::last_write_time(include, e); |
| 117 | + if(includeTime > recordTime) |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(true); |
| 124 | + } |
| 125 | + |
| 126 | + ghc::filesystem::path IncludeManager::GetRecordPath(const ghc::filesystem::path& sourceFile) const |
| 127 | + { |
| 128 | + std::size_t pathHash = std::hash<std::string>{}(sourceFile.string()); |
| 129 | + return IncludeRecordDir / (std::to_string(pathHash) + ".Includes"); |
| 130 | + } |
| 131 | +} |
0 commit comments