From 0022a217643dd7119df5dc0fcca41161d3201acc Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 30 Mar 2025 14:23:26 +0200 Subject: Refactor code for registering build_configuration registrations. --- src/configure.cc | 5 +- src/ctor.h | 4 +- src/libctor.cc | 10 ++-- src/rebuild.cc | 139 +++++++++++++++++++++++-------------------------------- src/rebuild.h | 18 +++---- src/tasks.cc | 2 +- 6 files changed, 79 insertions(+), 99 deletions(-) diff --git a/src/configure.cc b/src/configure.cc index 3d73011..11caa92 100644 --- a/src/configure.cc +++ b/src/configure.cc @@ -350,9 +350,10 @@ int regenerateCache(ctor::settings& settings, // Resolv externals ctor::external_configurations externalConfigs; - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - auto newExternalConfigs = externalConfigFiles[i].cb(settings); + auto newExternalConfigs = externalConfigFile.cb(settings); externalConfigs.insert(externalConfigs.end(), newExternalConfigs.begin(), newExternalConfigs.end()); diff --git a/src/ctor.h b/src/ctor.h index 9c6fcf6..27b30af 100644 --- a/src/ctor.h +++ b/src/ctor.h @@ -233,7 +233,7 @@ struct build_configuration using build_configurations = std::vector; -int reg(ctor::build_configurations (*cb)(const ctor::settings&), +int reg(std::function cb, const std::source_location location = std::source_location::current()); // This type will use flags verbatim @@ -252,7 +252,7 @@ struct external_configuration using external_configurations = std::vector; -int reg(ctor::external_configurations (*cb)(const ctor::settings&), +int reg(std::function cb, const std::source_location location = std::source_location::current()); // Convenience macro - ugly but keeps things simple(r) diff --git a/src/libctor.cc b/src/libctor.cc index 1538370..d72d82b 100644 --- a/src/libctor.cc +++ b/src/libctor.cc @@ -231,14 +231,16 @@ Options: { no_default_build = true; std::vector files; - for(std::size_t i = 0; i < numConfigFiles; ++i) + const auto& configFiles = getConfigFileList(); + for(const auto& configFile : configFiles) { - files.emplace_back(configFiles[i].file); + files.emplace_back(configFile.file); } - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - files.emplace_back(externalConfigFiles[i].file); + files.emplace_back(externalConfigFile.file); } std::sort(files.begin(), files.end()); diff --git a/src/rebuild.cc b/src/rebuild.cc index a2b7ddd..d62e998 100644 --- a/src/rebuild.cc +++ b/src/rebuild.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include "configure.h" #include "ctor.h" @@ -18,34 +19,38 @@ #include "tools.h" #include "util.h" -std::array configFiles; -std::size_t numConfigFiles{0}; +std::vector& getConfigFileList() +{ + static std::vector configFiles; + return configFiles; +} + +std::vector& getExternalConfigFileList() +{ + static std::vector externalConfigFiles; + return externalConfigFiles; +} namespace ctor { -int reg(ctor::build_configurations (*cb)(const ctor::settings&), +int reg(std::function cb, const std::source_location location) { - // NOTE: std::cout cannot be used here - if(numConfigFiles >= configFiles.size()) - { - fprintf(stderr, "Max %d build configurations currently supported.\n", - (int)configFiles.size()); - exit(1); - } + BuildConfigurationEntry entry; auto loc = std::filesystem::path(location.file_name()); if(loc.is_absolute()) { auto pwd = std::filesystem::current_path(); auto rel = std::filesystem::relative(loc, pwd); - configFiles[numConfigFiles].file = strdup(rel.string().data()); // NOTE: This intentionally leaks memory + entry.file = rel.string(); } else { - configFiles[numConfigFiles].file = location.file_name(); + entry.file = location.file_name(); } - configFiles[numConfigFiles].cb = cb; - ++numConfigFiles; + entry.cb = cb; + auto& configFiles = getConfigFileList(); + configFiles.push_back(entry); return 0; } @@ -53,80 +58,50 @@ int reg(ctor::build_configurations (*cb)(const ctor::settings&), int reg(const char* location) { - // NOTE: std::cout cannot be used here - if(numConfigFiles >= configFiles.size()) - { - fprintf(stderr, "Max %d build configurations currently supported.\n", - (int)configFiles.size()); - exit(1); - } + BuildConfigurationEntry entry; - configFiles[numConfigFiles].file = location; - configFiles[numConfigFiles].cb = - [](const ctor::settings&){ return std::vector{}; }; - ++numConfigFiles; + entry.file = location; + entry.cb = + [](const ctor::settings&) + { + return std::vector{}; + }; + auto& configFiles = getConfigFileList(); + configFiles.push_back(entry); return 0; } int unreg(const char* location) { - int found{0}; - for(std::size_t i = 0; i < numConfigFiles;) - { - if(std::string(location) == configFiles[i].file) - { - ++found; - for(std::size_t j = i; j < numConfigFiles; ++j) - { - configFiles[j] = configFiles[j + 1]; - } - --numConfigFiles; - } - else - { - ++i; - } - } - - for(std::size_t i = 0; i < numExternalConfigFiles;) - { - if(std::string(location) == externalConfigFiles[i].file) - { - ++found; - for(std::size_t j = i; j < numExternalConfigFiles; ++j) - { - externalConfigFiles[j] = externalConfigFiles[j + 1]; - } - --numExternalConfigFiles; - } - else - { - ++i; - } - } - - return found; + auto& configFiles = getConfigFileList(); + auto erasedConfigs = + std::erase_if(configFiles, + [&](const BuildConfigurationEntry& entry) + { + return entry.file == location; + }); + + auto& externalConfigFiles = getExternalConfigFileList(); + auto erasedExternals = + std::erase_if(externalConfigFiles, + [&](const ExternalConfigurationEntry& entry) + { + return entry.file == location; + }); + + return static_cast(erasedConfigs) + static_cast(erasedExternals); } -std::array externalConfigFiles; -std::size_t numExternalConfigFiles{0}; - namespace ctor { -int reg(ctor::external_configurations (*cb)(const ctor::settings&), +int reg(std::function cb, const std::source_location location) { - // NOTE: std::cout cannot be used here - if(numExternalConfigFiles >= externalConfigFiles.size()) - { - fprintf(stderr, "Max %d external configurations currently supported.\n", - (int)externalConfigFiles.size()); - exit(1); - } - - externalConfigFiles[numExternalConfigFiles].file = location.file_name(); - externalConfigFiles[numExternalConfigFiles].cb = cb; - ++numExternalConfigFiles; + ExternalConfigurationEntry entry; + entry.file = location.file_name(); + entry.cb = cb; + auto& externalConfigFiles = getExternalConfigFileList(); + externalConfigFiles.push_back(entry); return 0; } @@ -155,9 +130,10 @@ bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[ using namespace std::string_literals; + const auto& configFiles = getConfigFileList(); if(global_settings.verbose > 1) { - std::cout << "Recompile check (" << numConfigFiles << "):\n"; + std::cout << "Recompile check (" << configFiles.size() << "):\n"; } ctor::build_configuration config; @@ -201,9 +177,9 @@ bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[ config.sources.emplace_back(configurationFile.string()); } - for(std::size_t i = 0; i < numConfigFiles; ++i) + for(const auto& configFile : configFiles) { - std::string location = configFiles[i].file; + std::string location = configFile.file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; @@ -216,9 +192,10 @@ bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[ } } - for(std::size_t i = 0; i < numExternalConfigFiles; ++i) + const auto& externalConfigFiles = getExternalConfigFileList(); + for(const auto& externalConfigFile : externalConfigFiles) { - std::string location = externalConfigFiles[i].file; + std::string location = externalConfigFile.file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; diff --git a/src/rebuild.h b/src/rebuild.h index efa6d42..8e0c78a 100644 --- a/src/rebuild.h +++ b/src/rebuild.h @@ -5,26 +5,26 @@ #include #include +#include +#include #include "ctor.h" struct BuildConfigurationEntry { - const char* file; - ctor::build_configurations (*cb)(const ctor::settings&); + std::string file; + std::function cb; }; +std::vector& getConfigFileList(); + struct ExternalConfigurationEntry { - const char* file; - ctor::external_configurations (*cb)(const ctor::settings&); + std::string file; + std::function cb; }; -extern std::array configFiles; -extern std::size_t numConfigFiles; - -extern std::array externalConfigFiles; -extern std::size_t numExternalConfigFiles; +std::vector& getExternalConfigFileList(); int reg(const char* location); int unreg(const char* location); diff --git a/src/tasks.cc b/src/tasks.cc index e853470..94fe269 100644 --- a/src/tasks.cc +++ b/src/tasks.cc @@ -25,7 +25,7 @@ const std::deque& getTargets(const ctor::settings& settings, bool resolve_externals) { - auto config_files = std::span(configFiles).subspan(0, numConfigFiles); + auto& config_files = getConfigFileList(); static bool initialised{false}; static std::deque targets; -- cgit v1.2.3