Skip to content
Snippets Groups Projects
Commit f2ec5eef authored by Christian Dietrich's avatar Christian Dietrich
Browse files

clang-hash-plugin: make backup of object file

parent df544943
Branches
No related tags found
No related merge requests found
...@@ -8,12 +8,23 @@ ...@@ -8,12 +8,23 @@
#include <fstream> #include <fstream>
#include <unistd.h> #include <unistd.h>
#include <utime.h> #include <utime.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace clang; using namespace clang;
using namespace llvm; using namespace llvm;
static std::chrono::high_resolution_clock::time_point StartCompilation; 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 { class HashTranslationUnitConsumer : public ASTConsumer {
public: public:
HashTranslationUnitConsumer(raw_ostream *HS, raw_ostream *OS, HashTranslationUnitConsumer(raw_ostream *HS, raw_ostream *OS,
...@@ -48,6 +59,13 @@ public: ...@@ -48,6 +59,13 @@ public:
delete TopLevelHashStream; 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 // Sometimes we do terminal output
if (Terminal) { if (Terminal) {
*Terminal << "hash-start-time-ns " *Terminal << "hash-start-time-ns "
...@@ -86,10 +104,17 @@ public: ...@@ -86,10 +104,17 @@ public:
*Terminal << "]\n"; *Terminal << "]\n";
*Terminal << "skipped: " << (StopCompiling ? "true" : "false") << "\n"; *Terminal << "skipped: " << (StopCompiling ? "true" : "false") << "\n";
} }
if (StopCompiling) { 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 utime(OutputFile.c_str(), nullptr); // touch object file
exit(0); exit(0);
} }
} }
private: private:
...@@ -213,7 +238,9 @@ private: ...@@ -213,7 +238,9 @@ private:
} }
return HashString; return HashString;
} }
}; };
static FrontendPluginRegistry::Add<HashTranslationUnitAction> static FrontendPluginRegistry::Add<HashTranslationUnitAction>
X("clang-hash", "hash translation unit"); X("clang-hash", "hash translation unit");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment