From 33addfbf9cc21cd69b3d6476eb0c062bb2c6fcfb Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 15 Jun 2021 19:14:45 +0200 Subject: Re-link if link flags change. --- task_ld.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- task_ld.h | 3 +++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/task_ld.cc b/task_ld.cc index 0cd4c95..de74af2 100644 --- a/task_ld.cc +++ b/task_ld.cc @@ -7,6 +7,22 @@ #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, @@ -21,6 +37,9 @@ TaskLD::TaskLD(const BuildConfiguration& config, std::filesystem::path objectFile = object; objectFiles.push_back(objectFile); } + + flagsFile = settings.builddir / targetFile.stem(); + flagsFile += ".flags"; } bool TaskLD::dirty() @@ -30,6 +49,11 @@ bool TaskLD::dirty() return true; } + if(!std::filesystem::exists(flagsFile)) + { + return true; + } + for(const auto& objectFile : objectFiles) { if(std::filesystem::last_write_time(targetFile) <= @@ -39,6 +63,15 @@ bool TaskLD::dirty() } } + { + auto lastFlags = readFile(flagsFile); + if(flagsString() != lastFlags) + { + //std::cout << "The compiler flags changed\n"; + return true; + } + } + return false; } @@ -66,12 +99,28 @@ int TaskLD::run() 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() { - std::filesystem::remove(targetFile); + 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; } @@ -82,6 +131,19 @@ std::vector TaskLD::depends() const std::string TaskLD::target() const { - std::cout << "Removing " << std::string(targetFile) << "\n"; 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; +} diff --git a/task_ld.h b/task_ld.h index 709f238..286fe39 100644 --- a/task_ld.h +++ b/task_ld.h @@ -30,8 +30,11 @@ public: std::string target() const override; private: + std::string flagsString() const; + std::vector objectFiles; std::filesystem::path targetFile; + std::filesystem::path flagsFile; const BuildConfiguration& config; const Settings& settings; -- cgit v1.2.3