diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-01-11 19:57:00 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-01-11 20:06:56 +0100 |
commit | 6af7742c35ecdf2831908443ca0e04bf23317a96 (patch) | |
tree | 9ada42d1d4b6bcb2618c1325f155429d2035808a | |
parent | 648c89e879f739f82ed6dba7df7243e54b98601f (diff) |
Rename Configuation struct to configuration and make get/has functions member functions.
-rw-r--r-- | src/bootstrap.cc | 16 | ||||
-rw-r--r-- | src/configure.cc | 75 | ||||
-rw-r--r-- | src/ctor.h | 10 | ||||
-rw-r--r-- | src/libctor.cc | 7 | ||||
-rw-r--r-- | src/rebuild.cc | 9 | ||||
-rw-r--r-- | src/task.cc | 9 | ||||
-rw-r--r-- | src/task_ar.cc | 5 | ||||
-rw-r--r-- | src/tasks.cc | 2 | ||||
-rw-r--r-- | src/tools.cc | 5 | ||||
-rw-r--r-- | test/tools_test.cc | 15 |
10 files changed, 77 insertions, 76 deletions
diff --git a/src/bootstrap.cc b/src/bootstrap.cc index 09d6353..1fb3807 100644 --- a/src/bootstrap.cc +++ b/src/bootstrap.cc @@ -22,20 +22,18 @@ std::filesystem::path configurationFile("configuration.cc"); std::filesystem::path configHeaderFile("config.h"); -namespace ctor { -const Configuration default_configuration{}; -const Configuration& configuration() +const ctor::configuration default_configuration{}; +const ctor::configuration& ctor::get_configuration() { return default_configuration; } -bool hasConfiguration(const std::string& key) +bool ctor::configuration::has(const std::string& key) const { return false; } -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue) +const std::string& ctor::configuration::get(const std::string& key, const std::string& default_value) const { if(key == cfg::host_cxx && std::getenv("CXX")) { @@ -49,9 +47,8 @@ const std::string& getConfiguration(const std::string& key, return s; } - return defaultValue; + return default_value; } -} // namespace ctor:: int main(int argc, char* argv[]) { @@ -64,7 +61,8 @@ int main(int argc, char* argv[]) ctor::settings settings{}; - settings.builddir = ctor::getConfiguration(cfg::builddir, settings.builddir); + const auto& c = ctor::get_configuration(); + settings.builddir = c.get(cfg::builddir, settings.builddir); settings.parallel_processes = std::max(1u, std::thread::hardware_concurrency() * 2 - 1); settings.verbose = 0; diff --git a/src/configure.cc b/src/configure.cc index 129fa42..76103c6 100644 --- a/src/configure.cc +++ b/src/configure.cc @@ -24,31 +24,32 @@ std::filesystem::path configHeaderFile("config.h"); std::map<std::string, std::string> external_includedir; std::map<std::string, std::string> external_libdir; -namespace ctor { -const Configuration default_configuration{}; -const Configuration& __attribute__((weak)) configuration() +const ctor::configuration default_configuration{}; +const ctor::configuration& __attribute__((weak)) ctor::get_configuration() { return default_configuration; } +namespace ctor { std::optional<std::string> includedir; std::optional<std::string> libdir; std::optional<std::string> builddir; std::map<std::string, std::string> conf_values; +} // ctor:: -bool hasConfiguration(const std::string& key) +bool ctor::configuration::has(const std::string& key) const { - if(key == cfg::ctor_includedir && ctor::includedir) + if(key == ctor::cfg::ctor_includedir && ctor::includedir) { return true; } - if(key == cfg::ctor_libdir && ctor::libdir) + if(key == ctor::cfg::ctor_libdir && ctor::libdir) { return true; } - if(key == cfg::builddir && ctor::builddir) + if(key == ctor::cfg::builddir && ctor::builddir) { return true; } @@ -58,24 +59,22 @@ bool hasConfiguration(const std::string& key) return true; } - const auto& c = configuration(); - return c.tools.find(key) != c.tools.end(); + return tools.find(key) != tools.end(); } -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue) +const std::string& ctor::configuration::get(const std::string& key, const std::string& default_value) const { - if(key == cfg::ctor_includedir && ctor::includedir) + if(key == ctor::cfg::ctor_includedir && ctor::includedir) { return *ctor::includedir; } - if(key == cfg::ctor_libdir && ctor::libdir) + if(key == ctor::cfg::ctor_libdir && ctor::libdir) { return *ctor::libdir; } - if(key == cfg::builddir && ctor::builddir) + if(key == ctor::cfg::builddir && ctor::builddir) { return *ctor::builddir; } @@ -85,15 +84,13 @@ const std::string& getConfiguration(const std::string& key, return ctor::conf_values[key]; } - const auto& c = configuration(); - if(hasConfiguration(key)) + if(has(key)) { - return c.tools.at(key); + return tools.at(key); } - return defaultValue; + return default_value; } -} // namespace ctor:: std::string locate(const std::string& arch, const std::string& app) { @@ -403,27 +400,26 @@ int regenerateCache(ctor::settings& settings, // Store current values for execution in this execution context. if(!ctor_includedir.empty()) { - ctor::conf_values[cfg::ctor_includedir] = ctor_includedir; + ctor::conf_values[ctor::cfg::ctor_includedir] = ctor_includedir; } if(!ctor_libdir.empty()) { - ctor::conf_values[cfg::ctor_libdir] = ctor_libdir; + ctor::conf_values[ctor::cfg::ctor_libdir] = ctor_libdir; } if(!builddir.empty()) { - ctor::conf_values[cfg::builddir] = builddir; + ctor::conf_values[ctor::cfg::builddir] = builddir; } - ctor::conf_values[cfg::host_cxx] = host_cxx; - ctor::conf_values[cfg::build_cxx] = build_cxx; + ctor::conf_values[ctor::cfg::host_cxx] = host_cxx; + ctor::conf_values[ctor::cfg::build_cxx] = build_cxx; std::cout << "Writing results to: " << configurationFile.string() << "\n"; { std::ofstream istr(configurationFile); istr << "#include <ctor.h>\n\n"; - istr << "namespace ctor {\n"; - istr << "const Configuration& configuration()\n"; + istr << "const ctor::configuration& ctor::get_configuration()\n"; istr << "{\n"; - istr << " static Configuration cfg =\n"; + istr << " static ctor::configuration cfg =\n"; istr << " {\n"; istr << " .args = {"; for(const auto& arg : args) @@ -441,25 +437,25 @@ int regenerateCache(ctor::settings& settings, istr << " .tools = {\n"; if(!builddir.empty()) { - istr << " { \"" << cfg::builddir << "\", \"" << builddir << "\" },\n"; + istr << " { \"" << ctor::cfg::builddir << "\", \"" << builddir << "\" },\n"; ctor::builddir = builddir; } - istr << " { \"" << cfg::host_cc << "\", \"" << host_cc << "\" },\n"; - istr << " { \"" << cfg::host_cxx << "\", \"" << host_cxx << "\" },\n"; - istr << " { \"" << cfg::host_ar << "\", \"" << host_ar << "\" },\n"; - istr << " { \"" << cfg::host_ld << "\", \"" << host_ld << "\" },\n"; - istr << " { \"" << cfg::build_cc << "\", \"" << build_cc << "\" },\n"; - istr << " { \"" << cfg::build_cxx << "\", \"" << build_cxx << "\" },\n"; - istr << " { \"" << cfg::build_ar << "\", \"" << build_ar << "\" },\n"; - istr << " { \"" << cfg::build_ld << "\", \"" << build_ld << "\" },\n"; + istr << " { \"" << ctor::cfg::host_cc << "\", \"" << host_cc << "\" },\n"; + istr << " { \"" << ctor::cfg::host_cxx << "\", \"" << host_cxx << "\" },\n"; + istr << " { \"" << ctor::cfg::host_ar << "\", \"" << host_ar << "\" },\n"; + istr << " { \"" << ctor::cfg::host_ld << "\", \"" << host_ld << "\" },\n"; + istr << " { \"" << ctor::cfg::build_cc << "\", \"" << build_cc << "\" },\n"; + istr << " { \"" << ctor::cfg::build_cxx << "\", \"" << build_cxx << "\" },\n"; + istr << " { \"" << ctor::cfg::build_ar << "\", \"" << build_ar << "\" },\n"; + istr << " { \"" << ctor::cfg::build_ld << "\", \"" << build_ld << "\" },\n"; if(!ctor_includedir.empty()) { - istr << " { \"" << cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n"; + istr << " { \"" << ctor::cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n"; ctor::includedir = ctor_includedir; } if(!ctor_libdir.empty()) { - istr << " { \"" << cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n"; + istr << " { \"" << ctor::cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n"; ctor::libdir = ctor_libdir; } @@ -531,7 +527,6 @@ int regenerateCache(ctor::settings& settings, istr << " };\n"; istr << " return cfg;\n"; istr << "}\n"; - istr << "} // namespace ctor::\n\n"; } { @@ -607,7 +602,7 @@ int reconfigure(const ctor::settings& global_settings, int argc, char* argv[]) args.push_back(argv[i]); } - const auto& cfg = configuration(); + const auto& cfg = ctor::get_configuration(); std::cout << "Re-running configure:\n"; for(const auto& e : cfg.env) @@ -141,8 +141,11 @@ constexpr auto ctor_includedir = "ctor-includedir"; constexpr auto ctor_libdir = "ctor-libdir"; } -struct Configuration +struct configuration { + bool has(const std::string& key) const; + const std::string& get(const std::string& key, const std::string& default_value = {}) const; + std::vector<std::string> args; // vector of arguments used when last calling configure std::map<std::string, std::string> env; // env used when last calling configure @@ -150,9 +153,6 @@ struct Configuration std::map<std::string, ctor::flags> externals; }; -const Configuration& configuration(); -bool hasConfiguration(const std::string& key); -const std::string& getConfiguration(const std::string& key, - const std::string& defaultValue = {}); +const ctor::configuration& get_configuration(); } // namespace ctor:: diff --git a/src/libctor.cc b/src/libctor.cc index 67e734a..69d35cf 100644 --- a/src/libctor.cc +++ b/src/libctor.cc @@ -31,8 +31,9 @@ using namespace ctor; int main(int argc, char* argv[]) { ctor::settings settings{}; + const auto& c = ctor::get_configuration(); - settings.builddir = getConfiguration(cfg::builddir, settings.builddir); + settings.builddir = c.get(cfg::builddir, settings.builddir); settings.parallel_processes = std::max(1u, std::thread::hardware_concurrency()) * 2 - 1; @@ -265,13 +266,13 @@ Options: if(print_configure_cmd) { no_default_build = true; - std::cout << getConfiguration("cmd") << "\n"; + std::cout << c.get("cmd") << "\n"; } if(print_configure_db) { no_default_build = true; - const auto& c = configuration(); + const auto& c = ctor::get_configuration(); for(const auto& config : c.tools) { std::cout << config.first << ": " << config.second << "\n"; diff --git a/src/rebuild.cc b/src/rebuild.cc index 7e5259a..3033a46 100644 --- a/src/rebuild.cc +++ b/src/rebuild.cc @@ -170,17 +170,18 @@ bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[ getOption(tool_chain, opt::optimization, "3")); append(config.flags.cxxflags, getOption(tool_chain, opt::cpp_std, "c++20")); - if(hasConfiguration(cfg::ctor_includedir)) + const auto& c = ctor::get_configuration(); + if(c.has(cfg::ctor_includedir)) { append(config.flags.cxxflags, getOption(tool_chain, opt::include_path, - getConfiguration(cfg::ctor_includedir))); + c.get(cfg::ctor_includedir))); } - if(hasConfiguration(cfg::ctor_libdir)) + if(c.has(cfg::ctor_libdir)) { append(config.flags.ldflags, getOption(tool_chain, opt::library_path, - getConfiguration(cfg::ctor_libdir))); + c.get(cfg::ctor_libdir))); } append(config.flags.ldflags, getOption(tool_chain, opt::link, "ctor")); append(config.flags.ldflags, getOption(tool_chain, opt::threads)); diff --git a/src/task.cc b/src/task.cc index cff850e..27e6c44 100644 --- a/src/task.cc +++ b/src/task.cc @@ -131,23 +131,24 @@ ctor::output_system Task::outputSystem() const std::string Task::compiler() const { + const auto& c = ctor::get_configuration(); switch(sourceLanguage()) { case ctor::language::c: switch(outputSystem()) { case ctor::output_system::host: - return getConfiguration(cfg::host_cc, "/usr/bin/gcc"); + return c.get(cfg::host_cc, "/usr/bin/gcc"); case ctor::output_system::build: - return getConfiguration(cfg::build_cc, "/usr/bin/gcc"); + return c.get(cfg::build_cc, "/usr/bin/gcc"); } case ctor::language::cpp: switch(outputSystem()) { case ctor::output_system::host: - return getConfiguration(cfg::host_cxx, "/usr/bin/g++"); + return c.get(cfg::host_cxx, "/usr/bin/g++"); case ctor::output_system::build: - return getConfiguration(cfg::build_cxx, "/usr/bin/g++"); + return c.get(cfg::build_cxx, "/usr/bin/g++"); } default: std::cerr << "Unknown CC target type\n"; diff --git a/src/task_ar.cc b/src/task_ar.cc index 890209e..89bbc40 100644 --- a/src/task_ar.cc +++ b/src/task_ar.cc @@ -97,14 +97,15 @@ int TaskAR::runInner() std::cout << "AR => " << targetFile().string() << std::endl; } + const auto& c = ctor::get_configuration(); std::string tool; switch(outputSystem()) { case ctor::output_system::host: - tool = getConfiguration(cfg::host_ar, "/usr/bin/ar"); + tool = c.get(cfg::host_ar, "/usr/bin/ar"); break; case ctor::output_system::build: - tool = getConfiguration(cfg::build_ar, "/usr/bin/ar"); + tool = c.get(cfg::build_ar, "/usr/bin/ar"); break; } diff --git a/src/tasks.cc b/src/tasks.cc index ff3aaec..d10b83f 100644 --- a/src/tasks.cc +++ b/src/tasks.cc @@ -28,7 +28,7 @@ const std::deque<Target>& getTargets(const ctor::settings& settings, static std::deque<Target> targets; if(!initialised) { - const auto& externals = configuration().externals; + const auto& externals = ctor::get_configuration().externals; for(std::size_t i = 0; i < numConfigFiles; ++i) { std::string path = diff --git a/src/tools.cc b/src/tools.cc index e61a749..9e7102e 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -12,14 +12,15 @@ using namespace ctor; ToolChain getToolChain(ctor::output_system system) { + const auto& c = ctor::get_configuration(); std::string compiler; switch(system) { case ctor::output_system::host: - compiler = getConfiguration(cfg::host_cxx, "g++"); + compiler = c.get(cfg::host_cxx, "g++"); break; case ctor::output_system::build: - compiler = getConfiguration(cfg::build_cxx, "g++"); + compiler = c.get(cfg::build_cxx, "g++"); break; } diff --git a/test/tools_test.cc b/test/tools_test.cc index acd4219..8455ce6 100644 --- a/test/tools_test.cc +++ b/test/tools_test.cc @@ -87,16 +87,20 @@ std::string conf_host_cxx{}; std::string conf_build_cxx{}; } -namespace ctor { -const std::string& getConfiguration(const std::string& key, - const std::string& defval) +const ctor::configuration& ctor::get_configuration() { - if(key == cfg::host_cxx) + static ctor::configuration cfg; + return cfg; +} + +const std::string& ctor::configuration::get(const std::string& key, const std::string& defval) const +{ + if(key == ctor::cfg::host_cxx) { return conf_host_cxx; } - if(key == cfg::build_cxx) + if(key == ctor::cfg::build_cxx) { return conf_build_cxx; } @@ -106,7 +110,6 @@ const std::string& getConfiguration(const std::string& key, static std::string res{}; return res; } -} // namespace ctor:: class ToolsTest : public uUnit |