diff options
-rw-r--r-- | libcppbuild.cc | 8 | ||||
-rw-r--r-- | task.cc | 95 | ||||
-rw-r--r-- | task.h | 7 |
3 files changed, 61 insertions, 49 deletions
diff --git a/libcppbuild.cc b/libcppbuild.cc index 3cba49f..82a70d1 100644 --- a/libcppbuild.cc +++ b/libcppbuild.cc @@ -38,7 +38,7 @@ int main(int argc, const char* argv[]) for(const auto& file : files) { tasks.emplace_back(config, settings, file); - objects.push_back(tasks.back().targetFile); + objects.push_back(tasks.back().target()); } if(argc == 2 && std::string(argv[1]) == "clean") @@ -69,6 +69,12 @@ int main(int argc, const char* argv[]) while(processes.size() < settings.parallel_processes && task != tasks.end()) { + if(!task->dirty()) + { + ++task; + continue; + } + processes.emplace_back( std::async(std::launch::async, [task]() @@ -94,84 +94,80 @@ Task::Task(const BuildConfiguration& config, const Settings& settings, depsFile += ".d"; } -int Task::run() +bool Task::dirty() { if(!std::filesystem::exists(sourceFile)) { - std::cout << "Missing source file: " << std::string(sourceFile) << "\n"; - return 1; + //std::cout << "Missing source file: " << std::string(sourceFile) << "\n"; + return true; } - bool recompile{false}; - - if(!recompile && - !std::filesystem::exists(targetFile)) + if(!std::filesystem::exists(targetFile)) { - recompile = true; //std::cout << "Missing targetFile\n"; + return true; } - if(!recompile && - !std::filesystem::exists(depsFile)) + if(!std::filesystem::exists(depsFile)) { - recompile = true; //std::cout << "Missing depsFile\n"; + return true; } - if(!recompile && - std::filesystem::last_write_time(sourceFile) > + if(std::filesystem::last_write_time(sourceFile) > std::filesystem::last_write_time(depsFile)) { - recompile = true; //std::cout << "The sourceFile newer than depsFile\n"; + return true; } - if(!recompile) + auto depList = readDeps(depsFile); + for(const auto& dep : depList) { - auto depList = readDeps(depsFile); - for(const auto& dep : depList) + if(!std::filesystem::exists(dep) || + std::filesystem::last_write_time(targetFile) < + std::filesystem::last_write_time(dep)) { - if(!std::filesystem::exists(dep) || - std::filesystem::last_write_time(targetFile) < - std::filesystem::last_write_time(dep)) - { - recompile = true; - //std::cout << "The targetFile older than dep\n"; - break; - } + //std::cout << "The targetFile older than " << std::string(dep) << "\n"; + return true; } } - if(!recompile && - std::filesystem::last_write_time(sourceFile) > + if(std::filesystem::last_write_time(sourceFile) > std::filesystem::last_write_time(targetFile)) { - recompile = true; //std::cout << "The targetFile older than sourceFile\n"; + return true; } - if(recompile) + return false; +} + +int Task::run() +{ + if(!std::filesystem::exists(sourceFile)) { - std::string comp = "g++"; - std::string flags = config.cxxflags; - if(std::string(sourceFile.extension()) == ".c") - { - comp = "gcc"; - flags = config.cflags; - } - std::string cmd = comp + - " -MMD -c " + std::string(sourceFile) + " " + - flags + " " + - "-o " + std::string(targetFile); - std::cout << cmd << "\n"; + std::cout << "Missing source file: " << std::string(sourceFile) << "\n"; + return 1; + } - if(system(cmd.data())) - { - return 1; - } - return 0; + std::string comp = "g++"; + std::string flags = config.cxxflags; + if(std::string(sourceFile.extension()) == ".c") + { + comp = "gcc"; + flags = config.cflags; } + std::string cmd = comp + + " -MMD -c " + std::string(sourceFile) + " " + + flags + " " + + "-o " + std::string(targetFile); + std::cout << cmd << "\n"; + if(system(cmd.data())) + { + return 1; + } return 0; } @@ -192,7 +188,12 @@ int Task::clean() return 0; } -std::vector<std::string> Task::depends() +std::vector<std::string> Task::depends() const { return {}; } + +std::string Task::target() const +{ + return targetFile; +} @@ -16,11 +16,16 @@ public: const Settings& settings, const std::string& source); + bool dirty(); + int run(); int clean(); - std::vector<std::string> depends(); + std::vector<std::string> depends() const; + + std::string target() const; +private: std::filesystem::path sourceFile; std::filesystem::path targetFile; std::filesystem::path depsFile; |