From d917c5fd92e7ae50b35e7024a0a2365438bead72 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 26 Dec 2022 13:28:21 +0200 Subject: [PATCH 1/1] Use priorities to determine the default toolchain --- source/androidtools.cpp | 9 +++++++++ source/androidtools.h | 2 ++ source/architecture.cpp | 14 +++----------- source/builder.cpp | 14 ++++++++------ source/clangtools.cpp | 13 ++++++++++++- source/clangtools.h | 2 ++ source/gnutools.cpp | 13 ++++++++++++- source/gnutools.h | 2 ++ source/microsofttools.cpp | 13 ++++++++++++- source/microsofttools.h | 2 ++ source/toolchain.cpp | 5 +++-- source/toolchain.h | 8 ++++++++ 12 files changed, 75 insertions(+), 22 deletions(-) diff --git a/source/androidtools.cpp b/source/androidtools.cpp index 0073dff..e0f2bb1 100644 --- a/source/androidtools.cpp +++ b/source/androidtools.cpp @@ -224,6 +224,7 @@ void AndroidNdk::init_api_level(unsigned api) AndroidTools::AndroidTools(Builder &builder, const Architecture &arch): + Toolchain("android-gnu", get_priority(arch)), sdk(builder), ndk(builder, arch, sdk) { @@ -248,3 +249,11 @@ AndroidTools::AndroidTools(Builder &builder, const Architecture &arch): add_tool(new ApkBuilder(builder)); add_tool(new JarSigner(builder)); } + +int AndroidTools::get_priority(const Architecture &arch) +{ + if(arch.get_system()=="android") + return 30; + else + return -10; +} diff --git a/source/androidtools.h b/source/androidtools.h index b08691c..6410cdd 100644 --- a/source/androidtools.h +++ b/source/androidtools.h @@ -78,6 +78,8 @@ private: public: AndroidTools(Builder &, const Architecture &); + + static int get_priority(const Architecture &); }; #endif diff --git a/source/architecture.cpp b/source/architecture.cpp index 758d8b9..dbe4001 100644 --- a/source/architecture.cpp +++ b/source/architecture.cpp @@ -128,16 +128,6 @@ Architecture::Architecture(Builder &b, const string &spec): native = true; } - if(toolchain.empty()) - { - if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang")) - toolchain = "clang"; - else if(system=="windows" && native) - toolchain = "msvc"; - else - toolchain = "gnu"; - } - update(); } @@ -154,7 +144,9 @@ void Architecture::update() name += format("-%s", cpu); if(!fpu.empty()) name += format("-%s", fpu); - name += format("-%d-%s-%s", bits, system, toolchain); + name += format("-%d-%s", bits, system); + if(!toolchain.empty()) + name += format("-%s", toolchain); filename_patterns.clear(); if(system=="windows") diff --git a/source/builder.cpp b/source/builder.cpp index 032bb87..3fbb1b6 100644 --- a/source/builder.cpp +++ b/source/builder.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -92,17 +93,18 @@ void Builder::set_temp_directory(const FS::Path &p) void Builder::add_default_tools() { - const string &arch_tc = current_arch->get_toolchain(); + toolchain.add_toolchain(new GnuTools(*this, *current_arch)); + toolchain.add_toolchain(new ClangTools(*this, *current_arch)); if(current_arch->get_system()=="android") toolchain.add_toolchain(new AndroidTools(*this, *current_arch)); - else if(arch_tc=="msvc") + if(current_arch->get_system()=="windows") toolchain.add_toolchain(new MicrosoftTools(*this, *current_arch)); - else if(arch_tc=="clang") - toolchain.add_toolchain(new ClangTools(*this, *current_arch)); - else if(arch_tc=="gnu") - toolchain.add_toolchain(new GnuTools(*this, *current_arch)); toolchain.add_toolchain(new BuiltinTools(*this)); toolchain.add_tool(new DataTool(*this)); + + auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); }); + if(i!=toolchain.get_toolchains().end()) + current_arch->refine((*i)->get_name()); } void Builder::set_logger(const Logger *l) diff --git a/source/clangtools.cpp b/source/clangtools.cpp index d0910ff..80968a7 100644 --- a/source/clangtools.cpp +++ b/source/clangtools.cpp @@ -6,7 +6,8 @@ using namespace std; -ClangTools::ClangTools(Builder &builder, const Architecture &arch) +ClangTools::ClangTools(Builder &builder, const Architecture &arch): + Toolchain("clang", get_priority(arch)) { add_tool(new ClangCompiler(builder, arch, "CC")); add_tool(new ClangCompiler(builder, arch, "CXX")); @@ -15,3 +16,13 @@ ClangTools::ClangTools(Builder &builder, const Architecture &arch) add_tool(new GnuLinker(builder, arch)); add_tool(new GnuArchiver(builder, arch)); } + +int ClangTools::get_priority(const Architecture &arch) +{ + if(arch.get_toolchain()=="clang") + return 20; + else if(arch.get_system()=="darwin" || arch.get_system()=="freebsd") + return 10; + else + return 0; +} diff --git a/source/clangtools.h b/source/clangtools.h index ba86cc3..9faaf1a 100644 --- a/source/clangtools.h +++ b/source/clangtools.h @@ -10,6 +10,8 @@ class ClangTools: public Toolchain { public: ClangTools(Builder &, const Architecture &); + + static int get_priority(const Architecture &); }; #endif diff --git a/source/gnutools.cpp b/source/gnutools.cpp index fdd6e79..e64bcdb 100644 --- a/source/gnutools.cpp +++ b/source/gnutools.cpp @@ -5,7 +5,8 @@ #include "gnutools.h" #include "mingwdlltool.h" -GnuTools::GnuTools(Builder &builder, const Architecture &arch) +GnuTools::GnuTools(Builder &builder, const Architecture &arch): + Toolchain("gnu", get_priority(arch)) { add_tool(new GnuCompiler(builder, arch, "CC")); add_tool(new GnuCompiler(builder, arch, "CXX")); @@ -17,3 +18,13 @@ GnuTools::GnuTools(Builder &builder, const Architecture &arch) if(arch.get_system()=="windows") add_tool(new MingwDllTool(builder, arch)); } + +int GnuTools::get_priority(const Architecture &arch) +{ + if(arch.get_toolchain()=="gnu") + return 20; + else if(arch.get_system()=="linux") + return 10; + else + return 0; +} diff --git a/source/gnutools.h b/source/gnutools.h index 98e2856..5aeb966 100644 --- a/source/gnutools.h +++ b/source/gnutools.h @@ -10,6 +10,8 @@ class GnuTools: public Toolchain { public: GnuTools(Builder &, const Architecture &); + + static int get_priority(const Architecture &); }; #endif diff --git a/source/microsofttools.cpp b/source/microsofttools.cpp index d6ed9e4..c320ecb 100644 --- a/source/microsofttools.cpp +++ b/source/microsofttools.cpp @@ -13,7 +13,8 @@ using namespace std; using namespace Msp; -MicrosoftTools::MicrosoftTools(Builder &builder, const Architecture &arch) +MicrosoftTools::MicrosoftTools(Builder &builder, const Architecture &arch): + Toolchain("msvc", get_priority(arch)) { find_vc_bin_dir(builder, arch); find_windows_sdk_dir(builder); @@ -86,3 +87,13 @@ void MicrosoftTools::find_windows_sdk_dir(Builder &builder) builder.get_logger().log("tools", "Windows SDK version %s found in %s", win_sdk_version, win_sdk_dir); } + +int MicrosoftTools::get_priority(const Architecture &arch) +{ + if(arch.get_toolchain()=="msvc") + return 20; + else if(arch.get_system()=="windows") + return 10; + else + return 0; +} diff --git a/source/microsofttools.h b/source/microsofttools.h index 027499a..f2e3c37 100644 --- a/source/microsofttools.h +++ b/source/microsofttools.h @@ -27,6 +27,8 @@ public: const Msp::FS::Path &get_vc_bin_dir() const { return vc_bin_dir; } const Msp::FS::Path &get_windows_sdk_dir() const { return win_sdk_dir; } const std::string &get_windows_sdk_version() const { return win_sdk_version; } + + static int get_priority(const Architecture &); }; #endif diff --git a/source/toolchain.cpp b/source/toolchain.cpp index 8790c83..c5b8caa 100644 --- a/source/toolchain.cpp +++ b/source/toolchain.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include "tool.h" #include "toolchain.h" @@ -21,7 +21,8 @@ void Toolchain::add_tool(Tool *tool) void Toolchain::add_toolchain(Toolchain *chain) { - chains.push_back(chain); + auto i = upper_bound(chains, chain->get_priority(), [](int p, Toolchain *tc){ return p>tc->get_priority(); }); + chains.insert(i, chain); } bool Toolchain::has_tool(const string &tag) const diff --git a/source/toolchain.h b/source/toolchain.h index ea45240..34fc0a2 100644 --- a/source/toolchain.h +++ b/source/toolchain.h @@ -13,17 +13,25 @@ A container for tools. Performs lookup based on tag or filename extension. class Toolchain { private: + std::string name; + int priority = 0; std::map tools; std::vector chains; +protected: + Toolchain(const std::string &n, unsigned p): name(n), priority(p) { } public: + Toolchain() = default; ~Toolchain(); + const std::string &get_name() const { return name; } + int get_priority() const { return priority; } void add_tool(Tool *); void add_toolchain(Toolchain *); bool has_tool(const std::string &) const; Tool &get_tool(const std::string &) const; Tool *get_tool_for_suffix(const std::string &, bool = false) const; + const std::vector &get_toolchains() { return chains; } }; #endif -- 2.43.0