3 #include "chi/DefaultModuleCache.hpp" 12 #include <boost/filesystem/operations.hpp> 14 #if LLVM_VERSION_LESS_EQUAL(3, 9) 15 #include <llvm/Bitcode/ReaderWriter.h> 17 #include <llvm/Bitcode/BitcodeWriter.h> 20 #include <llvm/IR/Module.h> 21 #include <llvm/Support/FileSystem.h> 22 #include <llvm/Support/raw_ostream.h> 29 llvm::Module& compiledModule, std::time_t timeAtFileRead) {
30 assert(!moduleName.empty() &&
31 "Cannot pass a empty module name to DefaultModuleCache::cacheModule");
38 boost::filesystem::create_directories(cachePath.parent_path());
43 std::error_code errCode;
44 std::string errString;
45 llvm::raw_fd_ostream fileStream {
46 cachePath.string().c_str(),
47 #if LLVM_VERSION_LESS_EQUAL(3, 5) 55 if (errCode || !errString.empty()) {
56 res.addEntry(
"EUKN",
"Failed to open file", {{
"Path", cachePath.string()}});
61 llvm::WriteBitcodeToFile(&compiledModule, fileStream);
65 boost::filesystem::last_write_time(cachePath, timeAtFileRead);
71 const boost::filesystem::path& moduleName)
const {
76 assert(!moduleName.empty() &&
"Cannot pass empty path to DefaultModuleCache::invalidateCache");
80 boost::filesystem::remove(cachePath);
88 const boost::filesystem::path& moduleName, std::time_t atLeastThisNew) {
89 assert(!moduleName.empty() &&
90 "Cannot pass empty path to DefaultModuleCache::retrieveFromCache");
95 if (!boost::filesystem::is_regular_file(cachePath)) {
return nullptr; }
99 if (cacheEditTime < atLeastThisNew) {
return nullptr; }
102 std::unique_ptr<llvm::Module> fetchedMod;
105 if (!res) {
return nullptr; }
Result parseBitcodeFile(const boost::filesystem::path &file, llvm::LLVMContext &ctx, std::unique_ptr< llvm::Module > *toFill)
Parse a bitcode file.
void invalidateCache(const boost::filesystem::path &moduleName) override
Defines the Result class and related functions.
boost::filesystem::path workspacePath() const
Get the workspace path of the Context.
std::unique_ptr< llvm::Module > retrieveFromCache(const boost::filesystem::path &moduleName, std::time_t atLeastThisNew) override
Retrieve a module from the cache.
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
boost::filesystem::path cachePathForModule(const boost::filesystem::path &moduleName) const
Get the cache name for a module.
std::time_t cacheUpdateTime(const boost::filesystem::path &moduleName) const override
Get the time that a cache was updated.
This class provides an interface for creating module caches.
Result cacheModule(const boost::filesystem::path &moduleName, llvm::Module &compiledModule, std::time_t timeAtFileRead) override
Cache a module.
Context & context() const
Get the context this cache is bound to.
The namespace where chigraph lives.
Defines the Context class and related functions.
The result object, used for identifiying errors with good diagnostics.
DefaultModuleCache(Context &ctx)
Default constrcutor.