From 752e7c7bbd98f79c123784e5fb40b279d10f82a9 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Mon, 30 Sep 2024 11:11:42 +0200 Subject: [PATCH] - First commit --- .gitignore | 2 + lagueirtofile | 5 + main.cpp | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 lagueirtofile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66a4031 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +lagueirto +.vscode/* diff --git a/lagueirtofile b/lagueirtofile new file mode 100644 index 0000000..712f892 --- /dev/null +++ b/lagueirtofile @@ -0,0 +1,5 @@ +libs = -lSDL2 -lSDL2_mixer +cppflags = -D DEBUG -g +executable = thepool_debug +sourcepath = source +buildpath = build diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9a2bfb8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,252 @@ +#include +#include +#include +#include +#include + +#include + +std::string libs = ""; +std::string cppflags = ""; +std::string executable = "out"; +std::string source_path = ""; +std::string build_path = ""; + +std::vector keys = {"libs", "cppflags", "executable", "sourcepath", "buildpath"}; +enum tokens {LIBS, CPPFLAGS, EXECUTABLE, SOURCEPATH, BUILDPATH}; + +char *getBufferFromFile(const char* filename) +{ + FILE *f = fopen(filename, "rb"); + fseek(f, 0, SEEK_END); + long size = ftell(f); + fseek(f, 0, SEEK_SET); + char *buffer = (char*)malloc(size+1); + fread(buffer, size, 1, f); + buffer[size] = 0; + return buffer; +} + +#define IGNOREWHITESPACE while (*p!=0 && *p <= 32) { p++; } +void loadLagueirtoFile() +{ + char *buffer = getBufferFromFile("lagueirtofile"); + char *p = buffer; + char token[255]; + while (*p!=0) + { + IGNOREWHITESPACE; // Ignore whitespace + int i=0; + while (*p>32 && *p!='=') { token[i]=*p; i++; p++; } + IGNOREWHITESPACE + if (*p=='=') + { + token[i]=0; + int tokennum = -1; + for (int j=0;j getIncludes(std::string filename) +{ + char *buffer = getBufferFromFile(filename.c_str()); + + std::vector includes; + + char *p = buffer; + while (*p != 0) + { + if (*p=='#') + { + if (textFound(buffer, "#include")) + { + p+=8; + while(strchr(" \t", *p)) p++; // Ignore whitespace (spaces and tabs) + if (*p == '"') + { + p++; + char *p2 = p; + while (*p2!='"' && *p2!=0 ) p2++; + if (*p2!=0) { + *p2=0; + includes.push_back(std::string(p)); + p = p2+1; + } + } + } + } + p++; + } + + free(buffer); + return includes; +} + +int main() +{ + loadLagueirtoFile(); + bool must_link = false; + if (!std::filesystem::is_directory(source_path)) + { + std::cout << "ERROR: Directory '" << source_path <<"' does not exists." << std::endl; + exit(1); + } + + std::chrono::steady_clock::time_point begin_all = std::chrono::steady_clock::now(); + + if (!std::filesystem::is_directory(build_path)) + { + std::filesystem::create_directory(build_path); + } + + std::string path = "./" + source_path; + for (const auto & entry : std::filesystem::directory_iterator(path)) + { + std::string source_file = entry.path(); + if (getFileExtension(source_file)=="cpp") + { + bool must_recompile = false; + std::string object_file = build_path + "/" + getFileNameWithoutExtension(source_file)+".o"; + + // si el objecte no existeix, fa falta recompilar + if (!std::filesystem::exists(object_file)) + { + must_recompile = true; + //std::cout << "'" << object_file << "' No existeix. "; + + } + else + { + // Si sí que existeix, agafem la data de modificació + auto object_file_write_time = std::filesystem::last_write_time(object_file); + + // Si la data de modificació del cpp es major que la del objecte, fa falta recompilar + auto source_file_write_time = std::filesystem::last_write_time(source_file); + if (source_file_write_time > object_file_write_time) + { + //std::cout << "'" << source_file << "' es més nou. "; + must_recompile = true; + } + else + { + // Sino, mirem els includes + auto include_files = getIncludes(source_file); + for (auto include : include_files) + { + std::string include_file = source_path + "/" + include; + if (!std::filesystem::exists(include_file)) + { + std::cout << "WARNING: Include file '" << include_file << "' not found." << std::endl; + } + else + { + auto include_file_write_time = std::filesystem::last_write_time(include_file); + if (include_file_write_time > object_file_write_time) + { + //std::cout << "El include '" << include_file << "' es més nou. "; + must_recompile = true; + break; + } + } + } + + } + } + if (must_recompile) + { + must_link = true; + //std::cout << object_file << " s'ha de reompilar" << std::endl; + std::string command = "g++ " + source_file + " " + cppflags + " -c -o " + object_file; + std::cout << command << std::endl; + + std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); + system(command.c_str()); + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + float t = float(std::chrono::duration_cast(end - begin).count())/1000000; + std::cout << "(" << t << " seconds)" << std::endl; + } + else + { + //std::cout << object_file << " està actualitzat" << std::endl; + } + } + } + if (must_link) + { + std::string command = "g++ " + build_path + "/*.o " + libs + " -o " + executable; + std::cout << command << std::endl; + system(command.c_str()); + std::chrono::steady_clock::time_point end_all = std::chrono::steady_clock::now(); + float t = float(std::chrono::duration_cast(end_all - begin_all).count())/1000000; + std::cout << "(" << t << " seconds)" << std::endl; + } + else + { + std::cout << "Everything is up to date. Nothing to do." << std::endl; + } +} \ No newline at end of file