summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2025-03-30 14:23:26 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2025-03-30 14:38:01 +0200
commit0022a217643dd7119df5dc0fcca41161d3201acc (patch)
tree8917a1f72958c4c595df4e72ca26389f9780b6c6
parent0778d3c45d1aef18d9b2ae6769e372b2a3d83b8e (diff)
Refactor code for registering build_configuration registrations.
-rw-r--r--src/configure.cc5
-rw-r--r--src/ctor.h4
-rw-r--r--src/libctor.cc10
-rw-r--r--src/rebuild.cc139
-rw-r--r--src/rebuild.h18
-rw-r--r--src/tasks.cc2
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<build_configuration>;
-int reg(ctor::build_configurations (*cb)(const ctor::settings&),
+int reg(std::function<ctor::build_configurations (const ctor::settings&)> 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<ctor::external_configuration>;
-int reg(ctor::external_configurations (*cb)(const ctor::settings&),
+int reg(std::function<ctor::external_configurations (const ctor::settings&)> 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<std::string> 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 <source_location>
#include <cstring>
#include <span>
+#include <vector>
#include "configure.h"
#include "ctor.h"
@@ -18,34 +19,38 @@
#include "tools.h"
#include "util.h"
-std::array<BuildConfigurationEntry, 1024> configFiles;
-std::size_t numConfigFiles{0};
+std::vector<BuildConfigurationEntry>& getConfigFileList()
+{
+ static std::vector<BuildConfigurationEntry> configFiles;
+ return configFiles;
+}
+
+std::vector<ExternalConfigurationEntry>& getExternalConfigFileList()
+{
+ static std::vector<ExternalConfigurationEntry> externalConfigFiles;
+ return externalConfigFiles;
+}
namespace ctor {
-int reg(ctor::build_configurations (*cb)(const ctor::settings&),
+int reg(std::function<ctor::build_configurations (const ctor::settings&)> 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<ctor::build_configuration>{}; };
- ++numConfigFiles;
+ entry.file = location;
+ entry.cb =
+ [](const ctor::settings&)
+ {
+ return std::vector<ctor::build_configuration>{};
+ };
+ 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<int>(erasedConfigs) + static_cast<int>(erasedExternals);
}
-std::array<ExternalConfigurationEntry, 1024> externalConfigFiles;
-std::size_t numExternalConfigFiles{0};
-
namespace ctor {
-int reg(ctor::external_configurations (*cb)(const ctor::settings&),
+int reg(std::function<ctor::external_configurations (const ctor::settings&)> 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 <vector>
#include <array>
+#include <string>
+#include <functional>
#include "ctor.h"
struct BuildConfigurationEntry
{
- const char* file;
- ctor::build_configurations (*cb)(const ctor::settings&);
+ std::string file;
+ std::function<ctor::build_configurations (const ctor::settings&)> cb;
};
+std::vector<BuildConfigurationEntry>& getConfigFileList();
+
struct ExternalConfigurationEntry
{
- const char* file;
- ctor::external_configurations (*cb)(const ctor::settings&);
+ std::string file;
+ std::function<ctor::external_configurations (const ctor::settings&)> cb;
};
-extern std::array<BuildConfigurationEntry, 1024> configFiles;
-extern std::size_t numConfigFiles;
-
-extern std::array<ExternalConfigurationEntry, 1024> externalConfigFiles;
-extern std::size_t numExternalConfigFiles;
+std::vector<ExternalConfigurationEntry>& 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<Target>& 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<Target> targets;