From bf0883b6dd3946612922aa1b7c04a87d06442df7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 31 Aug 2021 21:13:23 +0300 Subject: [PATCH] More flexible way to manage filename patterns --- source/architecture.cpp | 32 ++++++++++++++++++++----------- source/architecture.h | 37 ++++++++++++++++++++++++++++-------- source/executable.cpp | 8 +------- source/executable.h | 3 --- source/gnulinker.cpp | 3 +-- source/objectfile.cpp | 2 +- source/sharedlibrary.cpp | 14 ++++---------- source/staticlibrary.cpp | 2 +- source/virtualfilesystem.cpp | 4 ++-- 9 files changed, 60 insertions(+), 45 deletions(-) diff --git a/source/architecture.cpp b/source/architecture.cpp index 2c5d383..a3f3b13 100644 --- a/source/architecture.cpp +++ b/source/architecture.cpp @@ -3,6 +3,10 @@ #include #include "architecture.h" #include "builder.h" +#include "executable.h" +#include "objectfile.h" +#include "sharedlibrary.h" +#include "staticlibrary.h" #include "sysutils.h" using namespace std; @@ -137,26 +141,26 @@ Architecture::Architecture(Builder &b, const string &spec): name += format("-%s", fpu); name += format("-%d-%s-%s", bits, system, toolchain); - object_patterns.push_back(Pattern("%.o")); + add_pattern("%.o"); if(system=="windows") { - sharedlib_patterns.push_back(Pattern("%.dll")); - sharedlib_patterns.push_back(Pattern("lib%.dll")); + add_pattern("%.dll"); + add_pattern("lib%.dll"); /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries, even though technically they are linked statically. */ - sharedlib_patterns.push_back(Pattern("lib%.dll.a")); - staticlib_patterns.push_back(Pattern("lib%.a")); - staticlib_patterns.push_back(Pattern("%.lib")); - executable_patterns.push_back(Pattern("%.exe")); + add_pattern("lib%.dll.a"); + add_pattern("lib%.a"); + add_pattern("%.lib"); + add_pattern("%.exe"); } else { if(system=="darwin") - sharedlib_patterns.push_back(Pattern("lib%.dylib")); + add_pattern("lib%.dylib"); else - sharedlib_patterns.push_back(Pattern("lib%.so")); - staticlib_patterns.push_back(Pattern("lib%.a")); - executable_patterns.push_back(Pattern("%")); + add_pattern("lib%.so"); + add_pattern("lib%.a"); + add_pattern("%"); } } @@ -199,6 +203,12 @@ string Architecture::best_match(const vector &names) const return best; } +template +void Architecture::add_pattern(const string &pat) +{ + filename_patterns[typeid(T).name()].push_back(Pattern(pat)); +} + void Architecture::resolve_aliases(vector &parts) { for(unsigned i=0; i #include #include "buildinfo.h" #include "pattern.h" @@ -33,10 +34,7 @@ private: std::string name; bool native; std::string cross_prefix; - PatternList object_patterns; - PatternList sharedlib_patterns; - PatternList staticlib_patterns; - PatternList executable_patterns; + std::map filename_patterns; public: Architecture(Builder &b, const std::string &spec); @@ -55,14 +53,37 @@ public: const std::string &get_cross_prefix() const { return cross_prefix; } - const PatternList &get_object_patterns() const { return object_patterns; } - const PatternList &get_shared_library_patterns() const { return sharedlib_patterns; } - const PatternList &get_static_library_patterns() const { return staticlib_patterns; } - const PatternList &get_executable_patterns() const { return executable_patterns; } + template + const PatternList &get_patterns() const; + + template + std::string create_filename(const std::string &) const; + +private: + template + void add_pattern(const std::string &); private: static void resolve_aliases(std::vector &); void parse_specification(const std::string &); }; +template +inline const Architecture::PatternList &Architecture::get_patterns() const +{ + std::map::const_iterator i = filename_patterns.find(typeid(T).name()); + if(i!=filename_patterns.end()) + return i->second; + + static PatternList empty; + return empty; +} + +template +inline std::string Architecture::create_filename(const std::string &base) const +{ + const PatternList &patterns = get_patterns(); + return patterns.empty() ? base : patterns.front().apply(base); +} + #endif diff --git a/source/executable.cpp b/source/executable.cpp index 0cdeec4..9cd493d 100644 --- a/source/executable.cpp +++ b/source/executable.cpp @@ -11,13 +11,7 @@ Executable::Executable(Builder &b, const FS::Path &p): { } Executable::Executable(Builder &b, const Component &c, const list &objs): - Binary(b, c, generate_filename(c), objs) + Binary(b, c, b.get_current_arch().create_filename(c.get_name()), objs) { install_location = "bin"; } - -string Executable::generate_filename(const Component &comp) -{ - const Architecture &arch = comp.get_package().get_builder().get_current_arch(); - return arch.get_executable_patterns().front().apply(comp.get_name()); -} diff --git a/source/executable.h b/source/executable.h index 967aaf9..a8950f1 100644 --- a/source/executable.h +++ b/source/executable.h @@ -8,10 +8,7 @@ class Executable: public Binary public: Executable(Builder &, const Msp::FS::Path &); Executable(Builder &, const Component &, const std::list &); -private: - static std::string generate_filename(const Component &); -public: virtual const char *get_type() const { return "Executable"; } }; diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index 9f6230e..2d37a4d 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -85,8 +85,7 @@ Target *GnuLinker::create_install(Target &target) const builder.get_build_graph().add_installed_target(*shlib->get_import_library()); else { - const Pattern &pattern = architecture->get_shared_library_patterns().front(); - string link_name = pattern.apply(shlib->get_libname()); + string link_name = architecture->create_filename(shlib->get_libname()); if(link_name!=FS::basename(inst_tgt->get_path())) inst_tgt->set_symlink(link_name); } diff --git a/source/objectfile.cpp b/source/objectfile.cpp index ab10c61..1e6cb23 100644 --- a/source/objectfile.cpp +++ b/source/objectfile.cpp @@ -36,7 +36,7 @@ FS::Path ObjectFile::generate_target_path(const Component &comp, const FS::Path fn += *i; } const Architecture &arch = comp.get_package().get_builder().get_current_arch(); - return temp_dir/arch.get_object_patterns().front().apply(FS::basepart(fn)); + return temp_dir/arch.create_filename(FS::basepart(fn)); } void ObjectFile::set_used_in_shared_library(bool u) diff --git a/source/sharedlibrary.cpp b/source/sharedlibrary.cpp index c25c6f2..5e479f3 100644 --- a/source/sharedlibrary.cpp +++ b/source/sharedlibrary.cpp @@ -37,18 +37,12 @@ SharedLibrary::SharedLibrary(Builder &b, const Component &c, const listget_name(), version)); + soname = arch.create_filename(format("%s-%s", libname, version)); else if(arch.get_system()=="darwin") - { - string filename = pattern.apply(component->get_name()); - string base = FS::basepart(filename); - string ext = FS::extpart(filename); - soname = format("%s.%s%s", base, version, ext); - } + soname = arch.create_filename(format("%s.%s", libname, version)); else - soname = format("%s.%s", pattern.apply(component->get_name()), version); + soname = format("%s.%s", arch.create_filename(libname), version); install_filename = soname; } @@ -66,7 +60,7 @@ string SharedLibrary::generate_filename(const Component &comp) else { const Architecture &arch = comp.get_package().get_builder().get_current_arch(); - return arch.get_shared_library_patterns().front().apply(comp.get_name()); + return arch.create_filename(comp.get_name()); } } diff --git a/source/staticlibrary.cpp b/source/staticlibrary.cpp index be88b41..92a3af0 100644 --- a/source/staticlibrary.cpp +++ b/source/staticlibrary.cpp @@ -26,7 +26,7 @@ StaticLibrary::StaticLibrary(Builder &b, const Component &c, const list(comp.get_name()); } void StaticLibrary::add_required_library(const string &lib) diff --git a/source/virtualfilesystem.cpp b/source/virtualfilesystem.cpp index 820fb5b..b514226 100644 --- a/source/virtualfilesystem.cpp +++ b/source/virtualfilesystem.cpp @@ -86,11 +86,11 @@ FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath list shared_names; if(mode!=BuildInfo::FORCE_STATIC) - shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib); + shared_names = Pattern::apply_list(arch.get_patterns(), lib); list static_names; if(mode!=BuildInfo::FORCE_DYNAMIC) - static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib); + static_names = Pattern::apply_list(arch.get_patterns(), lib); for(list::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i) { -- 2.45.2