diff --git a/index-import.cpp b/index-import.cpp index 1546175..827ee2a 100644 --- a/index-import.cpp +++ b/index-import.cpp @@ -103,9 +103,27 @@ static const FileEntry *getFileEntry(FileManager &fileMgr, StringRef path) { } } -static IndexUnitWriter remapUnit(const std::unique_ptr &reader, - const Remapper &remapper, FileManager &fileMgr, - ModuleNameScope &moduleNames) { +// Returns true if the Unit file of given output file already exists and is +// older than the input file. +static bool isUnitUpToDate(StringRef outputFile, StringRef inputFile, + IndexUnitWriter &writer) { + std::string error; + auto isUpToDateOpt = + writer.isUnitUpToDateForOutputFile(outputFile, inputFile, error); + if (!isUpToDateOpt.hasValue()) { + errs() << "error: failed file status check:\n" << error << "\n"; + return false; + } + + return *isUpToDateOpt; +} + +// Returns None if the Unit file is already up to date +static Optional +remapUnit(StringRef inputUnitPath, + const std::unique_ptr &reader, + const Remapper &remapper, FileManager &fileMgr, + ModuleNameScope &moduleNames) { // The set of remapped paths. auto workingDir = remapper.remap(reader->getWorkingDirectory()); auto outputFile = remapper.remap(reader->getOutputFile()); @@ -122,6 +140,18 @@ static IndexUnitWriter remapUnit(const std::unique_ptr &reader, reader->isModuleUnit(), reader->isDebugCompilation(), reader->getTarget(), sysrootPath, moduleNames.getModuleInfo); + // Check if the unit file is already up to date + SmallString<256> remappedOutputFilePath; + if (outputFile[0] != '/') { + // Convert outputFile to absolute path + path::append(remappedOutputFilePath, workingDir, outputFile); + } else { + remappedOutputFilePath = outputFile; + } + if (isUnitUpToDate(remappedOutputFilePath, inputUnitPath, writer)) { + return None; + } + reader->foreachDependency([&](const IndexUnitReader::DependencyInfo &info) { const auto name = info.UnitOrRecordName; const auto moduleNameRef = moduleNames.getReference(info.ModuleName); @@ -285,13 +315,15 @@ static bool remapIndex(const Remapper &remapper, } ModuleNameScope moduleNames; - auto writer = remapUnit(reader, remapper, fileMgr, moduleNames); + auto writer = remapUnit(unitPath, reader, remapper, fileMgr, moduleNames); - std::string unitWriteError; - if (writer.write(unitWriteError)) { - errs() << "error: failed to write index store; " << unitWriteError - << "\n"; - success = false; + if (writer.hasValue()) { + std::string unitWriteError; + if (writer->write(unitWriteError)) { + errs() << "error: failed to write index store; " << unitWriteError + << "\n"; + success = false; + } } }