diff --git a/src/clang-hash.cc b/src/clang-hash.cc index 829643cdde69ab0c6819664f9a81fd43898751a3..1430523d2f2793413d98c3f3709ac552d2ea04e3 100644 --- a/src/clang-hash.cc +++ b/src/clang-hash.cc @@ -8,12 +8,23 @@ #include <fstream> #include <unistd.h> #include <utime.h> +#include <sys/stat.h> +#include <sys/types.h> + using namespace clang; using namespace llvm; static std::chrono::high_resolution_clock::time_point StartCompilation; +static char *objectfile = NULL; +static char *objectfile_copy = NULL; +static void link_object_file() { + if (objectfile && objectfile_copy) { + link(objectfile, objectfile_copy); + } +} + class HashTranslationUnitConsumer : public ASTConsumer { public: HashTranslationUnitConsumer(raw_ostream *HS, raw_ostream *OS, @@ -48,6 +59,13 @@ public: delete TopLevelHashStream; } + // After this binary finishes, we have to call link_object_file to + // copy away our object file. + std::string OutputFileCopy = OutputFile + ".hash_copy"; + objectfile_copy = strdup(OutputFileCopy.c_str()); + objectfile = strdup(OutputFile.c_str()); + atexit(link_object_file); + // Sometimes we do terminal output if (Terminal) { *Terminal << "hash-start-time-ns " @@ -86,10 +104,17 @@ public: *Terminal << "]\n"; *Terminal << "skipped: " << (StopCompiling ? "true" : "false") << "\n"; } + if (StopCompiling) { + struct stat dummy; + // Object file not existing. Copy it from the backup + if (stat(OutputFile.c_str(), &dummy) == -1) { + link(OutputFileCopy.c_str(), OutputFile.c_str()); + } utime(OutputFile.c_str(), nullptr); // touch object file exit(0); } + } private: @@ -213,7 +238,9 @@ private: } return HashString; } + }; + static FrontendPluginRegistry::Add<HashTranslationUnitAction> X("clang-hash", "hash translation unit");