diff --git a/main.cpp b/main.cpp index fb65a42..f9ee4c1 100644 --- a/main.cpp +++ b/main.cpp @@ -134,7 +134,7 @@ std::vector getIncludes(std::string filename) if (*p=='#') { - if (textFound(buffer, "#include")) + if (textFound(p, "#include")) { p+=8; while(strchr(" \t", *p)) p++; // Ignore whitespace (spaces and tabs) @@ -180,17 +180,44 @@ std::vector split(std::string str) return strings; } -void MaybeRecompile(std::string source_file) +bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type object_file_write_time) +{ + auto include_files = getIncludes(file); + for (auto include : include_files) + { + std::filesystem::path fullpath(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) + { + return true; + } + else + { + if (HeadersNeedRecompile(include_file, object_file_write_time)) return true; + } + } + } + return false; +} + +bool MustRecompile(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. "; - + return true; } else { @@ -201,42 +228,23 @@ void MaybeRecompile(std::string source_file) 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; + return 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 (HeadersNeedRecompile(source_file, object_file_write_time)) + return true; } } - if (must_recompile) + return false; +} + +void MaybeRecompile(std::string source_file) +{ + if (MustRecompile(source_file)) { + std::string object_file = build_path + "/" + getFileNameWithoutExtension(source_file)+".o"; 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;