From f4eb3c80e63b5323dbb19b1103ba1db2dd1c4c41 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 15 Jun 2021 18:44:14 +0200 Subject: Recompile affected files if compiler flags change in build configuration. --- task_cc.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- task_cc.h | 5 +++++ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/task_cc.cc b/task_cc.cc index 34129a3..bef108e 100644 --- a/task_cc.cc +++ b/task_cc.cc @@ -89,10 +89,15 @@ TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, , settings(settings) { sourceFile = source; + targetFile = settings.builddir / sourceFile.stem(); targetFile += ".o"; + depsFile = settings.builddir / sourceFile.stem(); depsFile += ".d"; + + flagsFile = settings.builddir / sourceFile.stem(); + flagsFile += ".flags"; } bool TaskCC::dirty() @@ -115,6 +120,12 @@ bool TaskCC::dirty() return true; } + if(!std::filesystem::exists(flagsFile)) + { + //std::cout << "Missing flagsFile\n"; + return true; + } + if(std::filesystem::last_write_time(sourceFile) > std::filesystem::last_write_time(depsFile)) { @@ -122,6 +133,15 @@ bool TaskCC::dirty() return true; } + { + auto lastFlags = readFile(flagsFile); + if(flagsString() != lastFlags) + { + //std::cout << "The compiler flags changed\n"; + return true; + } + } + auto depList = readDeps(depsFile); for(const auto& dep : depList) { @@ -152,13 +172,9 @@ int TaskCC::run() return 1; } - std::string comp = "/usr/bin/g++"; - auto flags = config.cxxflags; - if(std::string(sourceFile.extension()) == ".c") - { - comp = "/usr/bin/gcc"; - flags = config.cflags; - } + std::string comp = compiler(); + auto compiler_flags = flags(); + std::vector args; args.push_back("-MMD"); args.push_back("-c"); @@ -166,11 +182,16 @@ int TaskCC::run() args.push_back("-o"); args.push_back(std::string(targetFile)); - for(const auto& flag : flags) + for(const auto& flag : compiler_flags) { args.push_back(flag); } + { // Write flags to file. + std::ofstream flagsStream(flagsFile); + flagsStream << flagsString(); + } + return execute(comp, args); } @@ -188,6 +209,12 @@ int TaskCC::clean() std::filesystem::remove(depsFile); } + if(std::filesystem::exists(flagsFile)) + { + std::cout << "Removing " << std::string(flagsFile) << "\n"; + std::filesystem::remove(flagsFile); + } + return 0; } @@ -200,3 +227,35 @@ std::string TaskCC::target() const { return targetFile; } + +std::vector TaskCC::flags() const +{ + if(std::string(sourceFile.extension()) == ".c") + { + return config.cflags; + } + return config.cxxflags; +} + +std::string TaskCC::flagsString() const +{ + std::string flagsStr; + for(const auto& flag : flags()) + { + if(!flagsStr.empty()) + { + flagsStr += " "; + } + flagsStr += flag; + } + return flagsStr; +} + +std::string TaskCC::compiler() const +{ + if(std::string(sourceFile.extension()) == ".c") + { + return "/usr/bin/gcc"; + } + return "/usr/bin/g++"; +} diff --git a/task_cc.h b/task_cc.h index c73a1e5..5c19017 100644 --- a/task_cc.h +++ b/task_cc.h @@ -29,9 +29,14 @@ public: std::string target() const override; private: + std::vector flags() const; + std::string flagsString() const; + std::string compiler() const; + std::filesystem::path sourceFile; std::filesystem::path targetFile; std::filesystem::path depsFile; + std::filesystem::path flagsFile; const BuildConfiguration& config; const Settings& settings; -- cgit v1.2.3