summaryrefslogtreecommitdiff
path: root/libcppbuild.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-06-11 21:22:21 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-06-11 21:22:21 +0200
commit6e3070181fadbe47511b52c12af5e1409a9a70b0 (patch)
tree12b7c080c28ecae3fcc344dccbeaa0c5f20dccbb /libcppbuild.cc
parent799921f0b1f5dcaf8dd2aa2f40adaf8c8490affc (diff)
Refactor at 4'o clock
Diffstat (limited to 'libcppbuild.cc')
-rw-r--r--libcppbuild.cc176
1 files changed, 27 insertions, 149 deletions
diff --git a/libcppbuild.cc b/libcppbuild.cc
index ddc0b66..46f1bfc 100644
--- a/libcppbuild.cc
+++ b/libcppbuild.cc
@@ -2,182 +2,60 @@
#include <string>
#include <filesystem>
#include <iostream>
-#include <fstream>
-#include <regex>
#include <utility>
-//#include <thread>
-#include <future>
#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<char> bytes(fileSize);
- ifs.read(bytes.data(), fileSize);
+ auto project = configs();
+ std::string output = builddir / project.target;
+ const auto& files = project.sources;
+ std::vector<std::string> objects;
- return std::string(bytes.data(), fileSize);
-}
+ std::vector<Task> tasks;
-std::vector<std::string> 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<std::string> 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<std::string> objects;
-
- std::vector<std::future<int>> 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;
}