diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-04-03 07:56:04 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-04-03 08:17:16 +0200 |
commit | cd6c3ea1c3ca853fc38da4fa5fd62545c31aa92d (patch) | |
tree | 57d7956ab19a756cfcf8fdd65aa8fb6e511133c5 | |
parent | 494401c0e0d29018f0ac53e91a3481d12e427ea6 (diff) |
-rw-r--r-- | src/configure.cc | 90 | ||||
-rw-r--r-- | src/libctor.cc | 14 | ||||
-rw-r--r-- | src/util.cc | 24 | ||||
-rw-r--r-- | src/util.h | 10 |
4 files changed, 90 insertions, 48 deletions
diff --git a/src/configure.cc b/src/configure.cc index 11caa92..910b878 100644 --- a/src/configure.cc +++ b/src/configure.cc @@ -32,11 +32,7 @@ const ctor::configuration& __attribute__((weak)) ctor::get_configuration() if(!initialised) { std::string cxx_prog{"c++"}; - auto cxx_env = std::getenv("CXX"); - if(cxx_env) - { - cxx_prog = cxx_env; - } + get_env("CXX", cxx_prog); cfg.build_toolchain = getToolChain(cfg.get(ctor::cfg::build_cxx, cxx_prog)); @@ -100,11 +96,32 @@ std::string ctor::configuration::get(const std::string& key, return ctor::conf_values[key]; } - if(has(key)) + if(tools.find(key) != tools.end()) { return tools.at(key); } + std::string value; + if(key == ctor::cfg::build_cxx && get_env("CXX", value)) + { + return value; + } + + if(key == ctor::cfg::build_cc && get_env("CC", value)) + { + return value; + } + + if(key == ctor::cfg::build_ld && get_env("LD", value)) + { + return value; + } + + if(key == ctor::cfg::build_ar && get_env("AR", value)) + { + return value; + } + return default_value; } @@ -116,10 +133,9 @@ std::string ctor::configuration::getenv(const std::string& key) const return envit->second; } - auto sysenv = std::getenv(key.data()); - if(sysenv) + if(std::string value; get_env(key.data(), value)) { - return sysenv; + return value; } return {}; @@ -132,20 +148,16 @@ public: Args(const std::vector<std::string>& args) { resize(args.size() + 1); - (*this)[0] = strdup("./ctor"); + owning_container.push_back("./ctor"); + (*this)[0] = owning_container.back().data(); for(std::size_t i = 0; i < size() - 1; ++i) { - (*this)[i + 1] = strdup(args[i].data()); + owning_container.push_back(args[i]); + (*this)[i + 1] = owning_container.back().data(); } } - ~Args() - { - for(std::size_t i = 0; i < size(); ++i) - { - free((*this)[i]); - } - } + std::deque<std::string> owning_container; }; namespace { @@ -730,6 +742,7 @@ int regenerateCache(ctor::settings& settings, { ctor::conf_values[ctor::cfg::builddir] = builddir; } + ctor::conf_values[ctor::cfg::host_cxx] = host_cxx; ctor::conf_values[ctor::cfg::build_cxx] = build_cxx; @@ -912,52 +925,45 @@ int configure(const ctor::settings& global_settings, int argc, char* argv[]) } std::map<std::string, std::string> env; - auto cc_env = getenv("CC"); - if(cc_env) + std::string value; + if(get_env("CC", value)) { - env["CC"] = cc_env; + env["CC"] = value; } - auto cflags_env = getenv("CFLAGS"); - if(cflags_env) + if(get_env("CFLAGS", value)) { - env["CFLAGS"] = cflags_env; + env["CFLAGS"] = value; } - auto cxx_env = getenv("CXX"); - if(cxx_env) + if(get_env("CXX", value)) { - env["CXX"] = cxx_env; + env["CXX"] = value; } - auto cxxflags_env = getenv("CXXFLAGS"); - if(cxxflags_env) + if(get_env("CXXFLAGS", value)) { - env["CXXFLAGS"] = cxxflags_env; + env["CXXFLAGS"] = value; } - auto ar_env = getenv("AR"); - if(ar_env) + if(get_env("AR", value)) { - env["AR"] = ar_env; + env["AR"] = value; } - auto ld_env = getenv("LD"); - if(ld_env) + if(get_env("LD", value)) { - env["LD"] = ld_env; + env["LD"] = value; } - auto ldflags_env = getenv("LDFLAGS"); - if(ldflags_env) + if(get_env("LDFLAGS", value)) { - env["LDFLAGS"] = ldflags_env; + env["LDFLAGS"] = value; } - auto path_env = getenv("PATH"); - if(path_env) + if(get_env("PATH", value)) { - env["PATH"] = path_env; + env["PATH"] = value; } auto ret = regenerateCache(settings, args_span[0], args, env); diff --git a/src/libctor.cc b/src/libctor.cc index d72d82b..2685ec0 100644 --- a/src/libctor.cc +++ b/src/libctor.cc @@ -17,7 +17,6 @@ #include <cstdlib> #include <span> - #include "ctor.h" #include "configure.h" #include "rebuild.h" @@ -25,6 +24,7 @@ #include "build.h" #include "unittest.h" #include "argparser.h" +#include "util.h" int main(int argc, char* argv[]) { @@ -221,10 +221,16 @@ Options: return res; } - auto verbose_env = std::getenv("V"); - if(verbose_env) + if(std::string value; get_env("V", value)) { - settings.verbose = std::atoi(verbose_env); + try + { + settings.verbose = std::stoi(value); + } + catch(...) + { + // not an integer + } } if(list_files) diff --git a/src/util.cc b/src/util.cc index 3517e0b..a4abd23 100644 --- a/src/util.cc +++ b/src/util.cc @@ -8,6 +8,7 @@ #include <algorithm> #include <sstream> #include <cctype> +#include <cstdlib> std::string to_lower(std::string str) { @@ -117,8 +118,18 @@ std::string esc(const std::string& in) return out; } -std::vector<std::string> get_paths(const std::string& path_env) +std::vector<std::string> get_paths(const std::string& path_env_) { + std::string path_env; + if(!path_env_.empty()) + { + path_env = path_env_; + } + else + { + get_env("PATH", path_env); + } + std::vector<std::string> paths; #ifdef _WIN32 @@ -285,3 +296,14 @@ std::vector<std::string> argsplit(const std::string& str) } return tokens; } + +bool get_env(std::string_view name, std::string& value) +{ + auto var = getenv(name.data()); + if(var) + { + value = var; + return true; + } + return false; +} @@ -23,7 +23,11 @@ void append(T& a, const T& b) std::string esc(const std::string& in); -std::vector<std::string> get_paths(const std::string& path_env = std::getenv("PATH")); +//! Get system paths (ie. env var PATH). +//! If path_env is provided, this search string will be used, other the PATH +//! env variable is used. +//! \returns a vector of the individual toknized paths. +std::vector<std::string> get_paths(const std::string& path_env = {}); std::string locate(const std::string& app, const std::vector<std::string>& paths, @@ -31,3 +35,7 @@ std::string locate(const std::string& app, //! Splits string into tokens adhering to quotations " and ' std::vector<std::string> argsplit(const std::string& str); + +//! Calls the system getenv and sets the string if the env name it exists. +//! \returns true if the env name existed, false otherwise. +bool get_env(std::string_view name, std::string& value); |