From 233b05f53fa6ab77c5b086bad5a722523fd428b9 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Sun, 20 Oct 2024 01:02:40 +0200 Subject: [PATCH] Hogmaker: Only rebuild the hog file if any input file changed --- tools/HogMaker/HogMaker.cpp | 57 ++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/tools/HogMaker/HogMaker.cpp b/tools/HogMaker/HogMaker.cpp index 0fde353e0..7ea6881a6 100644 --- a/tools/HogMaker/HogMaker.cpp +++ b/tools/HogMaker/HogMaker.cpp @@ -25,19 +25,13 @@ #include "d3_version.h" #include "HogFormat.h" -// TODO: To our descendants from the future: remove it when code will support C++20 -template std::time_t to_time_t(TP tp) { - auto sctp = std::chrono::duration_cast(tp - TP::clock::now()) + - std::chrono::system_clock::now(); - return std::chrono::system_clock::to_time_t(sctp); -} - // Find requested file in search paths std::filesystem::path resolve_path(const std::vector &search_path, const std::filesystem::path &file) { for (const auto &i : search_path) { - if (is_regular_file((i / file))) { - return (i / file); + std::filesystem::path testfile = (i / file); + if (is_regular_file(testfile)) { + return testfile; } } // Return empty path @@ -45,8 +39,8 @@ std::filesystem::path resolve_path(const std::vector &sea } int main(int argc, char *argv[]) { - std::vector input_files; + // Print usage information on demand if (argc < 3) { std::cout << "HogMaker v" << D3_MAJORVER << "." << D3_MINORVER << "." << D3_BUILD << "-g" << D3_GIT_HASH << "\n" << "Usage:\n" @@ -55,8 +49,26 @@ int main(int argc, char *argv[]) { return 1; } + // Filenames + std::filesystem::path hogmaker_filename{argv[0]}; std::filesystem::path hog_filename{argv[1]}; std::filesystem::path input_filename{argv[2]}; + + // Check if the input file is usable + if (!is_regular_file(input_filename)) { + std::cout << input_filename << " is not a regular file!" << std::endl; + return 1; + } + + // Timestamps + auto hog_timestamp = std::filesystem::file_time_type::min(); + if (std::filesystem::is_regular_file(hog_filename)) { + hog_timestamp = std::filesystem::last_write_time(hog_filename); + } + auto newer_than_hog = [&hog_timestamp](const std::filesystem::path &file) { + return (std::filesystem::last_write_time(file) >= hog_timestamp); + }; + // Default search path std::vector search_paths{input_filename.parent_path()}; // Additional search paths @@ -66,12 +78,9 @@ int main(int argc, char *argv[]) { } } - if (!is_regular_file(input_filename)) { - std::cout << input_filename << " is not a regular file!" << std::endl; - return 1; - } - D3::HogFormat hog_table; + // Check if the hogmaker or the input file is newer than the hog file + bool file_changed = (newer_than_hog(hogmaker_filename) || newer_than_hog(input_filename)); try { create_directories(absolute(hog_filename).parent_path()); @@ -80,28 +89,38 @@ int main(int argc, char *argv[]) { auto resolved_file = resolve_path(search_paths, line); if (resolved_file.empty()) { std::cout << "Warning! File " << line << " from " << input_filename << " not found! Skipping..." << std::endl; + file_changed = true; } else if (line.size() > 36) { std::cout << "Warning! Length of name of file " << line << " is more than 36 symbols! Skipping..." << std::endl; } else { hog_table.AddEntry(D3::HogFileEntry(resolved_file)); + // Check if this file is newer than the hog file + if (newer_than_hog(resolved_file)) { + file_changed = true; + } } } - } catch (std::exception &e) { std::cout << "Exception: " << e.what() << std::endl; return 1; } + // Don't do anything if there are no changed files + if (!file_changed) { + std::cout << "Already up to date" << std::endl; + return 0; + } + hog_table.SortEntries(); std::cout << "Creating " << absolute(hog_filename) << "..." << std::endl; - std::fstream ofs(hog_filename, std::ios_base::out | std::ios_base::binary); + std::ofstream ofs(hog_filename, std::ios_base::out | std::ios_base::binary); ofs << hog_table; - for (const auto& i : hog_table.GetEntries()) { + for (const auto &i : hog_table.GetEntries()) { std::cout << "Adding " << i.GetName() << "... "; try { - std::fstream ifs(i.GetRealPath(), std::ios_base::in | std::ios_base::binary); + std::ifstream ifs(i.GetRealPath(), std::ios_base::in | std::ios_base::binary); std::copy((std::istreambuf_iterator(ifs)), std::istreambuf_iterator(), std::ostreambuf_iterator(ofs)); } catch (std::exception &e) {