From a2be8eba3aaf11a9a73092793d27d6d46b4270cd Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Jun 2021 09:18:26 +0200 Subject: Add .so target. Fix ready state checks. --- task_so.cc | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 task_so.cc (limited to 'task_so.cc') diff --git a/task_so.cc b/task_so.cc new file mode 100644 index 0000000..afe5c32 --- /dev/null +++ b/task_so.cc @@ -0,0 +1,198 @@ +#include "task_so.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); +} + +std::vector addPrefix(const std::vector& lst, + const Settings& settings) +{ + std::vector out; + for(const auto& item : lst) + { + std::filesystem::path file = settings.builddir; + file /= item; + out.push_back(file.string()); + } + return out; +} +} // namespace :: + +TaskSO::TaskSO(const BuildConfiguration& config, + const Settings& settings, + const std::string& target, + const std::vector& objects) + : Task(addPrefix(config.depends, settings)) + , config(config) + , settings(settings) +{ + targetFile = settings.builddir; + targetFile /= target; + for(const auto& object : objects) + { + std::filesystem::path objectFile = object; + objectFiles.push_back(objectFile); + dependsStr.push_back(objectFile); + } + + for(const auto& dep : config.depends) + { + std::filesystem::path depFile = settings.builddir; + depFile /= dep; + depFiles.push_back(depFile); + } + + flagsFile = settings.builddir / targetFile.stem(); + flagsFile += ".flags"; +} + +bool TaskSO::dirtyInner() +{ + 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 TaskSO::runInner() +{ + std::string objectlist; + for(const auto& objectFile : objectFiles) + { + if(!objectlist.empty()) + { + objectlist += " "; + } + objectlist += std::string(objectFile); + } + + std::vector args; + + args.push_back("-fPIC"); + args.push_back("-shared"); + + args.push_back("-o"); + args.push_back(std::string(targetFile)); + + for(const auto& objectFile : objectFiles) + { + args.push_back(std::string(objectFile)); + } + + for(const auto& depFile : depFiles) + { + args.push_back(depFile.string()); + } + + for(const auto& flag : config.ldflags) + { + args.push_back(flag); + } + + { // Write flags to file. + std::ofstream flagsStream(flagsFile); + flagsStream << flagsString(); + } + + if(settings.verbose == 0) + { + std::cout << "LD => " << targetFile.string() << "\n"; + } + + return execute("/usr/bin/g++", args, settings.verbose > 0); +} + +int TaskSO::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 TaskSO::depends() const +{ + std::vector deps; + for(const auto& objectFile : objectFiles) + { + deps.push_back(objectFile.string()); + } + + for(const auto& depFile : depFiles) + { + deps.push_back(depFile.string()); + } + + return deps; +} + +std::string TaskSO::target() const +{ + return std::string(targetFile); +} + +std::string TaskSO::flagsString() const +{ + std::string flagsStr; + for(const auto& flag : config.ldflags) + { + if(!flagsStr.empty()) + { + flagsStr += " "; + } + flagsStr += flag; + } + return flagsStr; +} -- cgit v1.2.3