#include "task_ld.h" #include #include #include "libcppbuild.h" #include "settings.h" #include "execute.h" namespace { std::string readFile(const std::string &fileName) { std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); std::ifstream::pos_type fileSize = ifs.tellg(); ifs.seekg(0, std::ios::beg); std::vector bytes(fileSize); ifs.read(bytes.data(), fileSize); return std::string(bytes.data(), fileSize); } } // namespace :: TaskLD::TaskLD(const BuildConfiguration& config, const Settings& settings, const std::string& target, const std::vector& objects) : config(config) , settings(settings) { targetFile = settings.builddir; targetFile /= target; for(const auto& object : objects) { std::filesystem::path objectFile = object; objectFiles.push_back(objectFile); } flagsFile = settings.builddir / targetFile.stem(); flagsFile += ".flags"; } bool TaskLD::dirty() { if(!std::filesystem::exists(targetFile)) { return true; } if(!std::filesystem::exists(flagsFile)) { return true; } for(const auto& objectFile : objectFiles) { if(std::filesystem::last_write_time(targetFile) <= std::filesystem::last_write_time(objectFile)) { return true; } } { auto lastFlags = readFile(flagsFile); if(flagsString() != lastFlags) { //std::cout << "The compiler flags changed\n"; return true; } } return false; } int TaskLD::run() { std::string objectlist; for(const auto& objectFile : objectFiles) { if(!objectlist.empty()) { objectlist += " "; } objectlist += std::string(objectFile); } std::vector args; for(const auto& objectFile : objectFiles) { args.push_back(std::string(objectFile)); } for(const auto& flag : config.ldflags) { args.push_back(flag); } args.push_back("-o"); args.push_back(std::string(targetFile)); { // Write flags to file. std::ofstream flagsStream(flagsFile); flagsStream << flagsString(); } return execute("/usr/bin/g++", args); } int TaskLD::clean() { if(std::filesystem::exists(targetFile)) { std::cout << "Removing " << std::string(targetFile) << "\n"; std::filesystem::remove(targetFile); } if(std::filesystem::exists(flagsFile)) { std::cout << "Removing " << std::string(flagsFile) << "\n"; std::filesystem::remove(flagsFile); } return 0; } std::vector TaskLD::depends() const { return {}; } std::string TaskLD::target() const { return std::string(targetFile); } std::string TaskLD::flagsString() const { std::string flagsStr; for(const auto& flag : config.ldflags) { if(!flagsStr.empty()) { flagsStr += " "; } flagsStr += flag; } return flagsStr; }