From eebc67e0888989b46026c0b0dcce74acd210d6ea Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 28 May 2025 15:53:32 +0200 Subject: [PATCH] =?UTF-8?q?-=20[NEW]=20Ara=20es=20poden=20especificar=20ta?= =?UTF-8?q?mb=C3=A9=20arxius=20cpp=20solts=20a=20compilar=20(a=20part=20de?= =?UTF-8?q?=20directoris=20sencers,=20com=20abans)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 159 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/main.cpp b/main.cpp index cad36c2..68a2d20 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,8 @@ std::string build_path = ""; std::vector keys = {"libs", "cppflags", "executable", "sourcepath", "buildpath"}; enum tokens {LIBS, CPPFLAGS, EXECUTABLE, SOURCEPATH, BUILDPATH}; +bool must_link = false; + char *getBufferFromFile(const char* filename) { FILE *f = fopen(filename, "rb"); @@ -169,12 +171,83 @@ std::vector split(std::string str) return strings; } +void MaybeRecompile(std::string source_file) +{ + 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::filesystem::path fullpath(source_file); + auto path_without_filename = fullpath.remove_filename(); + std::string src_path = path_without_filename.string(); + + std::string include_file = src_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; + } +} + int main(int argc, char *argv[]) { if (argc>1) std::filesystem::current_path(argv[1]); loadLagueirtoFile(); - bool must_link = false; std::chrono::steady_clock::time_point begin_all = std::chrono::steady_clock::now(); @@ -189,79 +262,21 @@ int main(int argc, char *argv[]) { if (!std::filesystem::is_directory(src_path)) { - std::cout << "ERROR: Directory '" << src_path <<"' does not exists." << std::endl; - exit(1); - } - - std::string path = "./" + src_path; - for (const auto & entry : std::filesystem::directory_iterator(path)) - { - std::string source_file = entry.path(); - if (getFileExtension(source_file)=="cpp") + if (std::filesystem::is_regular_file(src_path) && getFileExtension(src_path)=="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)) + MaybeRecompile(src_path); + } else { + std::cout << "ERROR: '" << src_path <<"' does not exists." << std::endl; + exit(1); + } + } else { + std::string path = "./" + src_path; + for (const auto & entry : std::filesystem::directory_iterator(path)) + { + std::string source_file = entry.path(); + if (getFileExtension(source_file)=="cpp") { - 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 = src_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; + MaybeRecompile(source_file); } } }