From 6e3070181fadbe47511b52c12af5e1409a9a70b0 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 11 Jun 2021 21:22:21 +0200 Subject: Refactor at 4'o clock --- libcppbuild.cc | 176 +++++++++------------------------------------------------ 1 file changed, 27 insertions(+), 149 deletions(-) (limited to 'libcppbuild.cc') diff --git a/libcppbuild.cc b/libcppbuild.cc index ddc0b66..46f1bfc 100644 --- a/libcppbuild.cc +++ b/libcppbuild.cc @@ -2,182 +2,60 @@ #include #include #include -#include -#include #include -//#include -#include #include "libcppbuild.h" +#include "task.h" +#include "settings.h" -namespace -{ -std::string readFile(const std::string &fileName) +int main(int argc, const char* argv[]) { - 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); + Settings settings; + settings.builddir = "build/foo"; + std::filesystem::path builddir(settings.builddir); + std::filesystem::create_directories(builddir); - std::vector bytes(fileSize); - ifs.read(bytes.data(), fileSize); + auto project = configs(); + std::string output = builddir / project.target; + const auto& files = project.sources; + std::vector objects; - return std::string(bytes.data(), fileSize); -} + std::vector tasks; -std::vector readDeps(const std::string& depFile) -{ - if(!std::filesystem::exists(depFile)) + for(const auto& file : files) { - return {}; + tasks.emplace_back(project, settings, file); + objects.push_back(tasks.back().targetFile); } - auto str = readFile(depFile); - - std::vector output; - std::string tmp; - bool start{false}; - bool in_whitespace{false}; - for(const auto& c : str) + if(argc == 2 && std::string(argv[1]) == "clean") { - if(c == '\\' || c == '\n') - { - continue; - } - - if(c == ':') - { - start = true; - continue; - } - - if(!start) + std::cout << "Cleaning\n"; + //std::filesystem::remove_all(builddir); + for(auto& task : tasks) { - continue; - } - - if(c == ' ' || c == '\t') - { - if(in_whitespace) - { - continue; - } - - if(!tmp.empty()) + if(task.clean() != 0) { - output.push_back(tmp); + return 1; } - tmp.clear(); - in_whitespace = true; - } - else - { - in_whitespace = false; - tmp += c; } - } - - if(!tmp.empty()) - { - output.push_back(tmp); - } - - return output; -} -} // namespace :: + std::filesystem::remove(output); - -int main(int argc, const char* argv[]) -{ - if(argc == 2 && std::string(argv[1]) == "clean") - { - system("rm -Rf build"); return 0; } - std::filesystem::path builddir("build/"); - std::filesystem::create_directory(builddir); - - auto project = configs(); - std::string output = "build/" + project.target; - const auto& files = project.sources; - std::vector objects; - - std::vector> tasks; - std::cout << "Building\n"; - for(const auto& file : files) + // Start all tasks + for(auto& task : tasks) { - auto path = std::filesystem::path(file); - std::string object = builddir / path.stem(); - object += ".o"; - objects.push_back(object); - - std::string deps = builddir / path.stem(); - deps += ".d"; - - if(!std::filesystem::exists(file)) - { - std::cout << "Missing source file: " << file << "\n"; - return 1; - } - - bool recompile{false}; - - if(!std::filesystem::exists(object) || - !std::filesystem::exists(deps)) - { - recompile = true; - } - - if(!recompile && - std::filesystem::last_write_time(file) > - std::filesystem::last_write_time(deps)) - { - recompile = true; - } - - if(!recompile) - { - auto depList = readDeps(deps); - for(const auto& dep : depList) - { - if(!std::filesystem::exists(dep) || - std::filesystem::last_write_time(object) < - std::filesystem::last_write_time(dep)) - { - recompile = true; - break; - } - } - } - - if(recompile || - !std::filesystem::exists(object) || - std::filesystem::last_write_time(file) > - std::filesystem::last_write_time(object)) - { - - tasks.emplace_back( - std::async(std::launch::async, - [file, object]() - { - std::string compiler = "g++ -MMD -c " + file + " -o " + object; - std::cout << compiler << "\n"; - if(system(compiler.data())) - { - return 1; - } - return 0; - })); - } + task.start(); } + // Wait for all tasks for(auto& task : tasks) { - task.wait(); - if(task.get() != 0) + if(task.wait() != 0) { return 1; } -- cgit v1.2.3