diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-15 18:44:14 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-15 18:44:14 +0200 |
commit | f4eb3c80e63b5323dbb19b1103ba1db2dd1c4c41 (patch) | |
tree | dc1a8febaf00b34992c03f99740d39940a12fe3d | |
parent | 23eee0d41ba0dcc88f568a7124538368688db1ce (diff) |
Recompile affected files if compiler flags change in build configuration.
-rw-r--r-- | task_cc.cc | 75 | ||||
-rw-r--r-- | task_cc.h | 5 |
2 files changed, 72 insertions, 8 deletions
@@ -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<std::string> 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<std::string> 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++"; +} @@ -29,9 +29,14 @@ public: std::string target() const override; private: + std::vector<std::string> 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; |