From 3c29644d3bc8c4daad68ab92003a9e754f39de2a Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 18 Nov 2021 22:02:57 +0100 Subject: Refactor configure and the way it generates its cache. --- src/configure.cc | 190 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 147 insertions(+), 43 deletions(-) (limited to 'src/configure.cc') diff --git a/src/configure.cc b/src/configure.cc index 9ebed8a..13409ef 100644 --- a/src/configure.cc +++ b/src/configure.cc @@ -19,8 +19,8 @@ std::filesystem::path configurationFile("configuration.cc"); std::filesystem::path configHeaderFile("config.h"); -const std::map default_configuration{}; -const std::map& __attribute__((weak)) configuration() +const Configuration default_configuration{}; +const Configuration& __attribute__((weak)) configuration() { return default_configuration; } @@ -44,7 +44,7 @@ bool hasConfiguration(const std::string& key) } const auto& c = configuration(); - return c.find(key) != c.end(); + return c.tools.find(key) != c.tools.end(); } const std::string& getConfiguration(const std::string& key, @@ -63,7 +63,7 @@ const std::string& getConfiguration(const std::string& key, const auto& c = configuration(); if(hasConfiguration(key)) { - return c.at(key); + return c.tools.at(key); } return defaultValue; @@ -120,19 +120,36 @@ std::string locate(const std::string& arch, const std::string& app) return {}; } -int configure(const Settings& global_settings, int argc, char* argv[]) +class Args + : public std::vector { - Settings settings{global_settings}; - std::string cmd_str; - for(int i = 0; i < argc; ++i) +public: + Args(const std::vector& args) { - if(i > 0) + resize(args.size() + 1); + (*this)[0] = strdup("./ctor"); + for(std::size_t i = 0; i < size() - 1; ++i) { - cmd_str += " "; + (*this)[i + 1] = strdup(args[i].data()); } - cmd_str += argv[i]; } + ~Args() + { + for(std::size_t i = 0; i < size(); ++i) + { + free((*this)[i]); + } + } +}; + +int regenerateCache(const Settings& default_settings, + const std::vector& args, + const std::map& env) +{ + Settings settings{default_settings}; + Args vargs(args); + dg::Options opt; int key{128}; @@ -241,7 +258,7 @@ int configure(const Settings& global_settings, int argc, char* argv[]) return 0; }); - opt.process(argc, argv); + opt.process(vargs.size(), vargs.data()); if(host_arch.empty()) { @@ -274,32 +291,29 @@ int configure(const Settings& global_settings, int argc, char* argv[]) } } */ - auto cc_env = getenv("CC"); - if(cc_env) + + auto cc_env = env.find("CC"); + if(cc_env != env.end()) { - cmd_str = std::string("CC=") + cc_env + " " + cmd_str; - cc_prog = cc_env; + cc_prog = cc_env->second; } - auto cxx_env = getenv("CXX"); - if(cxx_env) + auto cxx_env = env.find("CXX"); + if(cxx_env != env.end()) { - cmd_str = std::string("CXX=") + cxx_env + " " + cmd_str; - cxx_prog = cxx_env; + cxx_prog = cxx_env->second; } - auto ar_env = getenv("AR"); - if(ar_env) + auto ar_env = env.find("AR"); + if(ar_env != env.end()) { - cmd_str = std::string("AR=") + ar_env + " " + cmd_str; - ar_prog = ar_env; + ar_prog = ar_env->second; } - auto ld_env = getenv("LD"); - if(ld_env) + auto ld_env = env.find("LD"); + if(ld_env != env.end()) { - cmd_str = std::string("LD=") + ld_env + " " + cmd_str; - ld_prog = ld_env; + ld_prog = ld_env->second; } std::string host_cc = locate(host_arch, cc_prog); @@ -315,32 +329,47 @@ int configure(const Settings& global_settings, int argc, char* argv[]) { std::ofstream istr(configurationFile); istr << "#include \n\n"; - istr << "const std::map& configuration()\n"; + istr << "const Configuration& configuration()\n"; istr << "{\n"; - istr << " static std::map c =\n"; + istr << " static Configuration cfg =\n"; istr << " {\n"; - istr << " { \"cmd\", \"" << cmd_str << "\" },\n"; - istr << " { \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; - 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 << " .args = {"; + for(const auto& arg : args) + { + istr << "\"" << arg << "\","; + } + istr << "},\n"; + istr << " .env = {"; + for(const auto& e : env) + { + istr << "{\"" << e.first << "\", \"" << e.second << "\"}, "; + } + istr << "},\n"; + + istr << " .tools = {\n"; + istr << " { \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; + 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"; if(!ctor_includedir.empty()) { - istr << " { \"" << cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n"; + istr << " { \"" << cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n"; ctor::includedir = ctor_includedir; } if(!ctor_libdir.empty()) { - istr << " { \"" << cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n"; + istr << " { \"" << cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n"; ctor::libdir = ctor_libdir; } + + istr << " },\n"; istr << " };\n"; - istr << " return c;\n"; + istr << " return cfg;\n"; istr << "}\n"; } @@ -351,7 +380,82 @@ int configure(const Settings& global_settings, int argc, char* argv[]) istr << "//#define HAS_BAR 1\n"; } + return 0; +} + +int configure(const Settings& global_settings, int argc, char* argv[]) +{ + Settings settings{global_settings}; + + std::vector args; + for(int i = 2; i < argc; ++i) // skip command and the first 'configure' arg + { + args.push_back(argv[i]); + } + + std::map env; + auto cc_env = getenv("CC"); + if(cc_env) + { + env["CC"] = cc_env; + } + + auto cxx_env = getenv("CXX"); + if(cxx_env) + { + env["CXX"] = cxx_env; + } + + auto ar_env = getenv("AR"); + if(ar_env) + { + env["AR"] = ar_env; + } + + auto ld_env = getenv("LD"); + if(ld_env) + { + env["LD"] = ld_env; + } + + auto ret = regenerateCache(settings, args, env); + if(ret != 0) + { + return ret; + } + recompileCheck(settings, 1, argv, false); return 0; } + +int reconfigure(const Settings& settings, int argc, char* argv[]) +{ + const auto& cfg = configuration(); + + std::cout << "Re-running configure:\n"; + for(const auto& e : cfg.env) + { + std::cout << e.first << "=\"" << e.second << "\" "; + } + std::cout << argv[0] << " configure "; + for(const auto& arg : cfg.args) + { + std::cout << arg << " "; + } + std::cout << "\n"; + + auto ret = regenerateCache(settings, cfg.args, cfg.env); + if(ret != 0) + { + return ret; + } + + std::vector args; + for(int i = 2; i < argc; ++i) // skip command and the first 'reconfigure' arg + { + args.push_back(argv[i]); + } + + return execute(argv[0], args); +} -- cgit v1.2.3