#include #include #include #include std::ostream& operator<<(std::ostream& stream, const std::vector& vs); std::ostream& operator<<(std::ostream& stream, const ctor::toolchain& tool_chain); #include #include #include using namespace std::string_literals; std::ostream& operator<<(std::ostream& stream, const ctor::toolchain& tool_chain) { switch(tool_chain) { case ctor::toolchain::none: stream << "ctor::toolchain::none"; break; case ctor::toolchain::any: stream << "ctor::toolchain::any"; break; case ctor::toolchain::gcc: stream << "ctor::toolchain::gcc"; break; case ctor::toolchain::clang: stream << "ctor::toolchain::clang"; break; } return stream; } std::ostream& operator<<(std::ostream& stream, const std::vector& vs) { bool first{true}; stream << "{ "; for(const auto& v : vs) { if(!first) { stream << ", "; } stream << "'" << v << "'"; first = false; } stream << " }"; return stream; } std::ostream& operator<<(std::ostream& stream, opt o) { // Adding to this enum should also imply adding to the unit-tests below switch(o) { case opt::output: stream << "opt::output"; break; case opt::debug: stream << "opt::debug"; break; case opt::strip: stream << "opt::strip"; break; case opt::warn_all: stream << "opt::warn_all"; break; case opt::warnings_as_errors: stream << "opt::warnings_as_errors"; break; case opt::generate_dep_tree: stream << "opt::generate_dep_tree"; break; case opt::no_link: stream << "opt::no_link"; break; case opt::include_path: stream << "opt::include_path"; break; case opt::library_path: stream << "opt::library_path"; break; case opt::link: stream << "opt::link"; break; case opt::cpp_std: stream << "opt::cpp_std"; break; case opt::build_shared: stream << "opt::build_shared"; break; case opt::threads: stream << "opt::threads"; break; case opt::optimization: stream << "opt::optimization"; break; case opt::position_independent_code: stream << "opt::position_independent_code"; break; case opt::position_independent_executable: stream << "opt::position_independent_executable"; break; case opt::custom: stream << "opt::custom"; break; } return stream; } std::ostream& operator<<(std::ostream& stream, const std::pair& vs) { stream << "{ " << vs.first << ", " << vs.second << " }"; return stream; } namespace { std::string conf_host_cxx{}; std::string conf_build_cxx{}; } const ctor::configuration& ctor::get_configuration() { 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 == ctor::cfg::build_cxx) { return conf_build_cxx; } assert(false); // bad key static std::string res{}; return res; } class ToolsTest : public uUnit { public: ToolsTest() { uTEST(ToolsTest::getToolChain_test); uTEST(ToolsTest::getOption_toolchain_test); uTEST(ToolsTest::getOption_str_test); } void getToolChain_test() { uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/g++")); uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/g++-10")); uASSERT_EQUAL(ctor::toolchain::gcc, getToolChain("/usr/bin/x86_64-pc-linux-gnu-g++-9.3.0")); uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang++")); uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/bin/clang++-10")); uASSERT_EQUAL(ctor::toolchain::clang, getToolChain("/usr/lib/llvm/12/bin/i686-pc-linux-gnu-clang++-12")); } void getOption_toolchain_test() { std::vector exp; std::vector act; exp = { "-o", "foo" }; act = getOption(ctor::toolchain::clang, opt::output, "foo"); uASSERT_EQUAL(exp, act); exp = { "-g" }; act = getOption(ctor::toolchain::clang, opt::debug); uASSERT_EQUAL(exp, act); exp = { "-s" }; act = getOption(ctor::toolchain::clang, opt::strip); uASSERT_EQUAL(exp, act); exp = { "-Wall" }; act = getOption(ctor::toolchain::clang, opt::warn_all); uASSERT_EQUAL(exp, act); exp = { "-Werror" }; act = getOption(ctor::toolchain::clang, opt::warnings_as_errors); uASSERT_EQUAL(exp, act); exp = { "-MMD" }; act = getOption(ctor::toolchain::clang, opt::generate_dep_tree); uASSERT_EQUAL(exp, act); exp = { "-c" }; act = getOption(ctor::toolchain::clang, opt::no_link); uASSERT_EQUAL(exp, act); exp = { "-Ifoo" }; act = getOption(ctor::toolchain::clang, opt::include_path, "foo"); uASSERT_EQUAL(exp, act); exp = { "-Lfoo" }; act = getOption(ctor::toolchain::clang, opt::library_path, "foo"); uASSERT_EQUAL(exp, act); exp = { "-lfoo" }; act = getOption(ctor::toolchain::clang, opt::link, "foo"); uASSERT_EQUAL(exp, act); exp = { "-std=foo" }; act = getOption(ctor::toolchain::clang, opt::cpp_std, "foo"); uASSERT_EQUAL(exp, act); exp = { "-shared" }; act = getOption(ctor::toolchain::clang, opt::build_shared); uASSERT_EQUAL(exp, act); exp = { "-pthread" }; act = getOption(ctor::toolchain::clang, opt::threads); uASSERT_EQUAL(exp, act); exp = { "-Ofoo" }; act = getOption(ctor::toolchain::clang, opt::optimization, "foo"); uASSERT_EQUAL(exp, act); exp = { "-fPIC" }; act = getOption(ctor::toolchain::clang, opt::position_independent_code); uASSERT_EQUAL(exp, act); exp = { "-fPIE" }; act = getOption(ctor::toolchain::clang, opt::position_independent_executable); uASSERT_EQUAL(exp, act); exp = { "-foo" }; act = getOption(ctor::toolchain::clang, opt::custom, "-foo"); uASSERT_EQUAL(exp, act); exp = { "-o", "foo" }; act = getOption(ctor::toolchain::gcc, opt::output, "foo"); uASSERT_EQUAL(exp, act); exp = { "-g" }; act = getOption(ctor::toolchain::gcc, opt::debug); uASSERT_EQUAL(exp, act); exp = { "-s" }; act = getOption(ctor::toolchain::gcc, opt::strip); uASSERT_EQUAL(exp, act); exp = { "-Wall" }; act = getOption(ctor::toolchain::gcc, opt::warn_all); uASSERT_EQUAL(exp, act); exp = { "-Werror" }; act = getOption(ctor::toolchain::gcc, opt::warnings_as_errors); uASSERT_EQUAL(exp, act); exp = { "-MMD" }; act = getOption(ctor::toolchain::gcc, opt::generate_dep_tree); uASSERT_EQUAL(exp, act); exp = { "-c" }; act = getOption(ctor::toolchain::gcc, opt::no_link); uASSERT_EQUAL(exp, act); exp = { "-Ifoo" }; act = getOption(ctor::toolchain::gcc, opt::include_path, "foo"); uASSERT_EQUAL(exp, act); exp = { "-Lfoo" }; act = getOption(ctor::toolchain::gcc, opt::library_path, "foo"); uASSERT_EQUAL(exp, act); exp = { "-lfoo" }; act = getOption(ctor::toolchain::gcc, opt::link, "foo"); uASSERT_EQUAL(exp, act); exp = { "-std=foo" }; act = getOption(ctor::toolchain::gcc, opt::cpp_std, "foo"); uASSERT_EQUAL(exp, act); exp = { "-shared" }; act = getOption(ctor::toolchain::gcc, opt::build_shared); uASSERT_EQUAL(exp, act); exp = { "-pthread" }; act = getOption(ctor::toolchain::gcc, opt::threads); uASSERT_EQUAL(exp, act); exp = { "-Ofoo" }; act = getOption(ctor::toolchain::gcc, opt::optimization, "foo"); uASSERT_EQUAL(exp, act); exp = { "-fPIC" }; act = getOption(ctor::toolchain::gcc, opt::position_independent_code); uASSERT_EQUAL(exp, act); exp = { "-fPIE" }; act = getOption(ctor::toolchain::gcc, opt::position_independent_executable); uASSERT_EQUAL(exp, act); exp = { "-foo" }; act = getOption(ctor::toolchain::gcc, opt::custom, "-foo"); uASSERT_EQUAL(exp, act); } void getOption_str_test() { std::pair exp; std::pair act; exp = { opt::include_path, "foo" }; act = getOption("-Ifoo", ctor::toolchain::gcc); uASSERT_EQUAL(exp, act); exp = { opt::library_path, "foo" }; act = getOption("-Lfoo", ctor::toolchain::gcc); uASSERT_EQUAL(exp, act); exp = { opt::include_path, "foo" }; act = getOption("-Ifoo", ctor::toolchain::clang); uASSERT_EQUAL(exp, act); exp = { opt::library_path, "foo" }; act = getOption("-Lfoo", ctor::toolchain::clang); uASSERT_EQUAL(exp, act); } }; // Registers the fixture into the 'registry' static ToolsTest test;