summaryrefslogtreecommitdiff
path: root/src/tools.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cc')
-rw-r--r--src/tools.cc648
1 files changed, 648 insertions, 0 deletions
diff --git a/src/tools.cc b/src/tools.cc
new file mode 100644
index 0000000..28d3903
--- /dev/null
+++ b/src/tools.cc
@@ -0,0 +1,648 @@
+// -*- c++ -*-
+// Distributed under the BSD 2-Clause License.
+// See accompanying file LICENSE for details.
+#include "tools.h"
+
+#include <filesystem>
+#include <iostream>
+#include <sstream>
+
+#include <cassert>
+
+std::ostream& operator<<(std::ostream& stream, const ctor::c_opt& opt)
+{
+ // Adding to this enum should also imply adding to the unit-tests below
+ switch(opt)
+ {
+ case ctor::c_opt::output: stream << "ctor::c_opt::output"; break;
+ case ctor::c_opt::debug: stream << "ctor::c_opt::debug"; break;
+ case ctor::c_opt::warn_all: stream << "ctor::c_opt::warn_all"; break;
+ case ctor::c_opt::warnings_as_errors: stream << "ctor::c_opt::warnings_as_errors"; break;
+ case ctor::c_opt::generate_dep_tree: stream << "ctor::c_opt::generate_dep_tree"; break;
+ case ctor::c_opt::no_link: stream << "ctor::c_opt::no_link"; break;
+ case ctor::c_opt::include_path: stream << "ctor::c_opt::include_path"; break;
+ case ctor::c_opt::c_std: stream << "ctor::c_opt::c_std"; break;
+ case ctor::c_opt::optimization: stream << "ctor::c_opt::optimization"; break;
+ case ctor::c_opt::position_independent_code: stream << "ctor::c_opt::position_independent_code"; break;
+ case ctor::c_opt::position_independent_executable: stream << "ctor::c_opt::position_independent_executable"; break;
+ case ctor::c_opt::custom: stream << "ctor::c_opt::custom"; break;
+ }
+
+ return stream;
+}
+
+std::ostream& operator<<(std::ostream& stream, const ctor::cxx_opt& opt)
+{
+ // Adding to this enum should also imply adding to the unit-tests below
+ switch(opt)
+ {
+ case ctor::cxx_opt::output: stream << "ctor::cxx_opt::output"; break;
+ case ctor::cxx_opt::debug: stream << "ctor::cxx_opt::debug"; break;
+ case ctor::cxx_opt::warn_all: stream << "ctor::cxx_opt::warn_all"; break;
+ case ctor::cxx_opt::warnings_as_errors: stream << "ctor::cxx_opt::warnings_as_errors"; break;
+ case ctor::cxx_opt::generate_dep_tree: stream << "ctor::cxx_opt::generate_dep_tree"; break;
+ case ctor::cxx_opt::no_link: stream << "ctor::cxx_opt::no_link"; break;
+ case ctor::cxx_opt::include_path: stream << "ctor::cxx_opt::include_path"; break;
+ case ctor::cxx_opt::cpp_std: stream << "ctor::cxx_opt::cpp_std"; break;
+ case ctor::cxx_opt::optimization: stream << "ctor::cxx_opt::optimization"; break;
+ case ctor::cxx_opt::position_independent_code: stream << "ctor::cxx_opt::position_independent_code"; break;
+ case ctor::cxx_opt::position_independent_executable: stream << "ctor::cxx_opt::position_independent_executable"; break;
+ case ctor::cxx_opt::custom: stream << "ctor::cxx_opt::custom"; break;
+ }
+
+ return stream;
+}
+
+std::ostream& operator<<(std::ostream& stream, const ctor::ld_opt& opt)
+{
+ // Adding to this enum should also imply adding to the unit-tests below
+ switch(opt)
+ {
+ case ctor::ld_opt::output: stream << "ctor::ld_opt::output"; break;
+ case ctor::ld_opt::strip: stream << "ctor::ld_opt::strip"; break;
+ case ctor::ld_opt::warn_all: stream << "ctor::ld_opt::warn_all"; break;
+ case ctor::ld_opt::warnings_as_errors: stream << "ctor::ld_opt::warnings_as_errors"; break;
+ case ctor::ld_opt::library_path: stream << "ctor::ld_opt::library_path"; break;
+ case ctor::ld_opt::link: stream << "ctor::ld_opt::link"; break;
+ case ctor::ld_opt::cpp_std: stream << "ctor::ld_opt::cpp_std"; break;
+ case ctor::ld_opt::build_shared: stream << "ctor::ld_opt::build_shared"; break;
+ case ctor::ld_opt::threads: stream << "ctor::ld_opt::threads"; break;
+ case ctor::ld_opt::position_independent_code: stream << "ctor::ld_opt::position_independent_code"; break;
+ case ctor::ld_opt::position_independent_executable: stream << "ctor::ld_opt::position_independent_executable"; break;
+ case ctor::ld_opt::custom: stream << "ctor::ld_opt::custom"; break;
+ }
+
+ return stream;
+}
+
+std::ostream& operator<<(std::ostream& stream, const ctor::ar_opt& opt)
+{
+ // Adding to this enum should also imply adding to the unit-tests below
+ switch(opt)
+ {
+ case ctor::ar_opt::replace: stream << "ctor::ar_opt::replace"; break;
+ case ctor::ar_opt::add_index: stream << "ctor::ar_opt::add_index"; break;
+ case ctor::ar_opt::create: stream << "ctor::ar_opt::create"; break;
+ case ctor::ar_opt::output: stream << "ctor::ar_opt::output"; break;
+ case ctor::ar_opt::custom: stream << "ctor::ar_opt::custom"; break;
+ }
+
+ return stream;
+}
+
+std::ostream& operator<<(std::ostream& stream, const ctor::asm_opt& opt)
+{
+ // Adding to this enum should also imply adding to the unit-tests below
+ switch(opt)
+ {
+ case ctor::asm_opt::custom: stream << "ctor::asm_opt::custom"; break;
+ }
+
+ return stream;
+}
+
+ctor::toolchain getToolChain(const std::string& compiler)
+{
+ std::filesystem::path cc(compiler);
+ auto cc_cmd = cc.stem().string();
+
+ // Note: "g++" is a substring of "clang++" so "clang++" must be tested first.
+ if(cc_cmd.find("clang++") != std::string::npos)
+ {
+ return ctor::toolchain::clang;
+ }
+ else if(cc_cmd.find("g++") != std::string::npos)
+ {
+ return ctor::toolchain::gcc;
+ }
+
+ std::cerr << "Unsupported output system.\n";
+ return ctor::toolchain::gcc;
+}
+
+ctor::toolchain getToolChain(ctor::output_system system)
+{
+ const auto& cfg = ctor::get_configuration();
+ if(system == ctor::output_system::host)
+ {
+ if(cfg.host_toolchain != ctor::toolchain::none)
+ {
+ return cfg.host_toolchain;
+ }
+ return getToolChain(cfg.get(ctor::cfg::host_cxx, "g++"));
+ }
+ else
+ {
+ if(cfg.build_toolchain != ctor::toolchain::none)
+ {
+ return cfg.build_toolchain;
+ }
+ return getToolChain(cfg.get(ctor::cfg::build_cxx, "g++"));
+ }
+}
+
+namespace gcc {
+ctor::c_flag c_option(const std::string& flag)
+{
+ if(flag.substr(0, 2) == "-I")
+ {
+ std::string path = flag.substr(2);
+ path.erase(0, path.find_first_not_of(' '));
+ return { ctor::c_opt::include_path, path };
+ }
+
+ return { ctor::c_opt::custom, flag };
+}
+
+ctor::cxx_flag cxx_option(const std::string& flag)
+{
+ if(flag.substr(0, 2) == "-I")
+ {
+ std::string path = flag.substr(2);
+ path.erase(0, path.find_first_not_of(' '));
+ return { ctor::cxx_opt::include_path, path };
+ }
+
+ return { ctor::cxx_opt::custom, flag };
+}
+
+ctor::ld_flag ld_option(const std::string& flag)
+{
+ if(flag.substr(0, 2) == "-L")
+ {
+ std::string path = flag.substr(2);
+ path.erase(0, path.find_first_not_of(' '));
+ return { ctor::ld_opt::library_path, path };
+ }
+
+ return { ctor::ld_opt::custom, flag };
+}
+
+ctor::ar_flag ar_option(const std::string& flag)
+{
+ return { ctor::ar_opt::custom, flag };
+}
+
+std::vector<std::string> cxx_option(ctor::cxx_opt opt, const std::string& arg)
+{
+ switch(opt)
+ {
+ case ctor::cxx_opt::output:
+ return {"-o", arg};
+ case ctor::cxx_opt::debug:
+ return {"-g"};
+ case ctor::cxx_opt::warn_all:
+ return {"-Wall"};
+ case ctor::cxx_opt::warnings_as_errors:
+ return {"-Werror"};
+ case ctor::cxx_opt::generate_dep_tree:
+ return {"-MMD"};
+ case ctor::cxx_opt::no_link:
+ return {"-c"};
+ case ctor::cxx_opt::include_path:
+ return {"-I" + arg};
+ case ctor::cxx_opt::cpp_std:
+ return {"-std=" + arg};
+ case ctor::cxx_opt::optimization:
+ return {"-O" + arg};
+ case ctor::cxx_opt::position_independent_code:
+ return {"-fPIC"};
+ case ctor::cxx_opt::position_independent_executable:
+ return {"-fPIE"};
+ case ctor::cxx_opt::custom:
+ return {arg};
+ }
+
+ std::cerr << "Unsupported compiler option.\n";
+ return {};
+}
+
+std::vector<std::string> c_option(ctor::c_opt opt, const std::string& arg)
+{
+ switch(opt)
+ {
+ case ctor::c_opt::output:
+ return {"-o", arg};
+ case ctor::c_opt::debug:
+ return {"-g"};
+ case ctor::c_opt::warn_all:
+ return {"-Wall"};
+ case ctor::c_opt::warnings_as_errors:
+ return {"-Werror"};
+ case ctor::c_opt::generate_dep_tree:
+ return {"-MMD"};
+ case ctor::c_opt::no_link:
+ return {"-c"};
+ case ctor::c_opt::include_path:
+ return {"-I" + arg};
+ case ctor::c_opt::c_std:
+ return {"-std=" + arg};
+ case ctor::c_opt::optimization:
+ return {"-O" + arg};
+ case ctor::c_opt::position_independent_code:
+ return {"-fPIC"};
+ case ctor::c_opt::position_independent_executable:
+ return {"-fPIE"};
+ case ctor::c_opt::custom:
+ return {arg};
+ }
+
+ std::cerr << "Unsupported compiler option.\n";
+ return {};
+}
+
+std::vector<std::string> ld_option(ctor::ld_opt opt, const std::string& arg)
+{
+ switch(opt)
+ {
+ case ctor::ld_opt::output:
+ return {"-o", arg};
+ case ctor::ld_opt::strip:
+ return {"-s"};
+ case ctor::ld_opt::warn_all:
+ return {"-Wall"};
+ case ctor::ld_opt::warnings_as_errors:
+ return {"-Werror"};
+ case ctor::ld_opt::library_path:
+ return {"-L" + arg};
+ case ctor::ld_opt::link:
+ return {"-l" + arg};
+ case ctor::ld_opt::cpp_std:
+ return {"-std=" + arg};
+ case ctor::ld_opt::build_shared:
+ return {"-shared"};
+ case ctor::ld_opt::threads:
+ return {"-pthread"};
+ case ctor::ld_opt::position_independent_code:
+ return {"-fPIC"};
+ case ctor::ld_opt::position_independent_executable:
+ return {"-fPIE"};
+ case ctor::ld_opt::custom:
+ return {arg};
+ }
+
+ std::cerr << "Unsupported compiler option.\n";
+ return {};
+}
+
+std::vector<std::string> ar_option(ctor::ar_opt opt, const std::string& arg)
+{
+ switch(opt)
+ {
+ case ctor::ar_opt::replace:
+ return {"-r"};
+ case ctor::ar_opt::add_index:
+ return {"-s"};
+ case ctor::ar_opt::create:
+ return {"-c"};
+ case ctor::ar_opt::output:
+ return {arg};
+ case ctor::ar_opt::custom:
+ return {arg};
+ }
+
+ std::cerr << "Unsupported compiler option.\n";
+ return {};
+}
+
+std::vector<std::string> asm_option(ctor::asm_opt opt, const std::string& arg)
+{
+ switch(opt)
+ {
+ case ctor::asm_opt::custom:
+ return {arg};
+ }
+
+ std::cerr << "Unsupported compiler option.\n";
+ return {};
+}
+} // gcc::
+
+
+std::vector<std::string> c_option(ctor::toolchain toolchain,
+ ctor::c_opt opt,
+ const std::string& arg)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::c_option(opt, arg);
+ case ctor::toolchain::any:
+ {
+ std::ostringstream ss;
+ ss << "{" << opt;
+ if(!arg.empty())
+ {
+ ss << ", \"" << arg << "\"";
+ }
+ ss << "}";
+ return { ss.str() };
+ }
+ case ctor::toolchain::none:
+ break;
+ }
+
+ std::cerr << "Unsupported tool-chain.\n";
+ return {};
+}
+
+std::vector<std::string> cxx_option(ctor::toolchain toolchain,
+ ctor::cxx_opt opt,
+ const std::string& arg)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::cxx_option(opt, arg);
+ case ctor::toolchain::any:
+ {
+ std::ostringstream ss;
+ ss << "{" << opt;
+ if(!arg.empty())
+ {
+ ss << ", \"" << arg << "\"";
+ }
+ ss << "}";
+ return { ss.str() };
+ }
+ case ctor::toolchain::none:
+ break;
+ }
+
+ std::cerr << "Unsupported tool-chain.\n";
+ return {};
+}
+
+std::vector<std::string> ld_option(ctor::toolchain toolchain,
+ ctor::ld_opt opt,
+ const std::string& arg)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::ld_option(opt, arg);
+ case ctor::toolchain::any:
+ {
+ std::ostringstream ss;
+ ss << "{" << opt;
+ if(!arg.empty())
+ {
+ ss << ", \"" << arg << "\"";
+ }
+ ss << "}";
+ return { ss.str() };
+ }
+ case ctor::toolchain::none:
+ break;
+ }
+
+ std::cerr << "Unsupported tool-chain.\n";
+ return {};
+}
+
+std::vector<std::string> ar_option(ctor::toolchain toolchain,
+ ctor::ar_opt opt,
+ const std::string& arg)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::ar_option(opt, arg);
+ case ctor::toolchain::any:
+ {
+ std::ostringstream ss;
+ ss << "{" << opt;
+ if(!arg.empty())
+ {
+ ss << ", \"" << arg << "\"";
+ }
+ ss << "}";
+ return { ss.str() };
+ }
+ case ctor::toolchain::none:
+ break;
+ }
+
+ std::cerr << "Unsupported tool-chain.\n";
+ return {};
+}
+
+std::vector<std::string> asm_option(ctor::toolchain toolchain,
+ ctor::asm_opt opt,
+ const std::string& arg)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::asm_option(opt, arg);
+ case ctor::toolchain::any:
+ {
+ std::ostringstream ss;
+ ss << "{" << opt;
+ if(!arg.empty())
+ {
+ ss << ", \"" << arg << "\"";
+ }
+ ss << "}";
+ return { ss.str() };
+ }
+ case ctor::toolchain::none:
+ break;
+ }
+
+ std::cerr << "Unsupported tool-chain.\n";
+ return {};
+}
+
+
+ctor::c_flag c_option(const std::string& flag, ctor::toolchain toolchain)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::c_option(flag);
+ case ctor::toolchain::any:
+ case ctor::toolchain::none:
+ break;
+ }
+
+ return { ctor::c_opt::custom, flag };
+}
+
+ctor::cxx_flag cxx_option(const std::string& flag, ctor::toolchain toolchain)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::cxx_option(flag);
+ case ctor::toolchain::any:
+ case ctor::toolchain::none:
+ break;
+ }
+
+ return { ctor::cxx_opt::custom, flag };
+}
+
+ctor::ld_flag ld_option(const std::string& flag, ctor::toolchain toolchain)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::ld_option(flag);
+ case ctor::toolchain::any:
+ case ctor::toolchain::none:
+ break;
+ }
+
+ return { ctor::ld_opt::custom, flag };
+}
+
+ctor::ar_flag ar_option(const std::string& flag, ctor::toolchain toolchain)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ return gcc::ar_option(flag);
+ case ctor::toolchain::any:
+ case ctor::toolchain::none:
+ break;
+ }
+
+ return { ctor::ar_opt::custom, flag };
+}
+
+ctor::asm_flag asm_option(const std::string& flag, ctor::toolchain toolchain)
+{
+ switch(toolchain)
+ {
+ case ctor::toolchain::gcc:
+ case ctor::toolchain::clang:
+ case ctor::toolchain::any:
+ case ctor::toolchain::none:
+ break;
+ }
+
+ return { ctor::asm_opt::custom, flag };
+}
+
+// Flag to string coversions
+
+std::vector<std::string> to_strings(ctor::toolchain toolchain,
+ const ctor::c_flag& flag)
+{
+ if(flag.toolchain == ctor::toolchain::any ||
+ flag.toolchain == toolchain)
+ {
+ return c_option(toolchain, flag.opt, flag.arg);
+ }
+
+ return {};
+}
+
+std::vector<std::string> to_strings(ctor::toolchain toolchain,
+ const ctor::cxx_flag& flag)
+{
+ if(flag.toolchain == ctor::toolchain::any ||
+ flag.toolchain == toolchain)
+ {
+ return cxx_option(toolchain, flag.opt, flag.arg);
+ }
+
+ return {};
+}
+
+std::vector<std::string> to_strings(ctor::toolchain toolchain,
+ const ctor::ld_flag& flag)
+{
+ if(flag.toolchain == ctor::toolchain::any ||
+ flag.toolchain == toolchain)
+ {
+ return ld_option(toolchain, flag.opt, flag.arg);
+ }
+
+ return {};
+}
+
+std::vector<std::string> to_strings(ctor::toolchain toolchain,
+ const ctor::ar_flag& flag)
+{
+ if(flag.toolchain == ctor::toolchain::any ||
+ flag.toolchain == toolchain)
+ {
+ return ar_option(toolchain, flag.opt, flag.arg);
+ }
+
+ return {};
+}
+
+std::vector<std::string> to_strings(ctor::toolchain toolchain,
+ const ctor::asm_flag& flag)
+{
+ if(flag.toolchain == ctor::toolchain::any ||
+ flag.toolchain == toolchain)
+ {
+ return asm_option(toolchain, flag.opt, flag.arg);
+ }
+
+ return {};
+}
+
+namespace {
+ctor::toolchain guess_toolchain(const std::string& opt)
+{
+ if(opt.empty())
+ {
+ return ctor::toolchain::any;
+ }
+
+ if(opt[0] == '-')
+ {
+ return ctor::toolchain::gcc;
+ }
+
+ //if(opt[0] == '/')
+ //{
+ // return ctor::toolchain::msvc;
+ //}
+ return ctor::toolchain::any;
+}
+}
+
+template<>
+ctor::flag<ctor::c_opt>::flag(const char* str)
+{
+ *this = c_option(str, guess_toolchain(str));
+}
+
+template<>
+ctor::flag<ctor::cxx_opt>::flag(const char* str)
+{
+ *this = cxx_option(str, guess_toolchain(str));
+}
+
+template<>
+ctor::flag<ctor::ld_opt>::flag(const char* str)
+{
+ *this = ld_option(str, guess_toolchain(str));
+}
+
+template<>
+ctor::flag<ctor::ar_opt>::flag(const char* str)
+{
+ *this = ar_option(str, guess_toolchain(str));
+}
+
+template<>
+ctor::flag<ctor::asm_opt>::flag(const char* str)
+{
+ *this = asm_option(str, guess_toolchain(str));
+}