| 24 | ModuleCache::ModuleCache(chi::Context& ctx) : mContext{&ctx} {} |
| 25 | |
| 26 | Result ModuleCache::cacheModule(const boost::filesystem::path& moduleName, |
| 27 | llvm::Module& compiledModule, std::time_t timeAtFileRead) { |
| 28 | assert(!moduleName.empty() && "Cannot pass a empty module name to ModuleCache::cacheModule"); |
| 29 | |
| 30 | Result res; |
| 31 | |
| 32 | auto cachePath = cachePathForModule(moduleName); |
| 33 | |
| 34 | // make the directories |
| 35 | boost::filesystem::create_directories(cachePath.parent_path()); |
| 36 | |
| 37 | // open & write |
| 38 | { |
| 39 | // open the file |
| 40 | std::error_code errCode; |
| 41 | llvm::raw_fd_ostream fileStream(cachePath.string(), errCode, llvm::sys::fs::F_RW); |
| 42 | |
| 43 | if (errCode) { |
| 44 | res.addEntry("EUKN", "Failed to open file", {{"Path", cachePath.string()}}); |
| 45 | return res; |
| 46 | } |
| 47 | |
| 48 | // write it |
| 49 | llvm::WriteBitcodeToFile(&compiledModule, fileStream); |
| 50 | } |
| 51 | |
| 52 | // set age to be correct |
| 53 | boost::filesystem::last_write_time(cachePath, timeAtFileRead); |
| 54 | |
| 55 | return res; |
| 56 | } |
| 57 | |
| 58 | boost::filesystem::path ModuleCache::cachePathForModule( |
| 59 | const boost::filesystem::path& moduleName) const { |