]> git.tdb.fi Git - builder.git/commitdiff
More flexible way to manage filename patterns
authorMikko Rasa <tdb@tdb.fi>
Tue, 31 Aug 2021 18:13:23 +0000 (21:13 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 31 Aug 2021 18:13:23 +0000 (21:13 +0300)
source/architecture.cpp
source/architecture.h
source/executable.cpp
source/executable.h
source/gnulinker.cpp
source/objectfile.cpp
source/sharedlibrary.cpp
source/staticlibrary.cpp
source/virtualfilesystem.cpp

index 2c5d383ec3622cc25a9caa61b4e4448c41547ea1..a3f3b132447f3213ce7d5f0f797751f90beae79b 100644 (file)
@@ -3,6 +3,10 @@
 #include <msp/strings/utils.h>
 #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<ObjectFile>("%.o");
        if(system=="windows")
        {
-               sharedlib_patterns.push_back(Pattern("%.dll"));
-               sharedlib_patterns.push_back(Pattern("lib%.dll"));
+               add_pattern<SharedLibrary>("%.dll");
+               add_pattern<SharedLibrary>("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<SharedLibrary>("lib%.dll.a");
+               add_pattern<StaticLibrary>("lib%.a");
+               add_pattern<StaticLibrary>("%.lib");
+               add_pattern<Executable>("%.exe");
        }
        else
        {
                if(system=="darwin")
-                       sharedlib_patterns.push_back(Pattern("lib%.dylib"));
+                       add_pattern<SharedLibrary>("lib%.dylib");
                else
-                       sharedlib_patterns.push_back(Pattern("lib%.so"));
-               staticlib_patterns.push_back(Pattern("lib%.a"));
-               executable_patterns.push_back(Pattern("%"));
+                       add_pattern<SharedLibrary>("lib%.so");
+               add_pattern<StaticLibrary>("lib%.a");
+               add_pattern<Executable>("%");
        }
 }
 
@@ -199,6 +203,12 @@ string Architecture::best_match(const vector<string> &names) const
        return best;
 }
 
+template<typename T>
+void Architecture::add_pattern(const string &pat)
+{
+       filename_patterns[typeid(T).name()].push_back(Pattern(pat));
+}
+
 void Architecture::resolve_aliases(vector<string> &parts)
 {
        for(unsigned i=0; i<parts.size(); ++i)
index 752c84a2d19b7afd267ed8863fa630fa34e347cd..360f3dd9f6321919df742d4fa7992ffbce0f9e27 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef ARCHITECTURE_H_
 #define ARCHITECTURE_H_
 
+#include <typeinfo>
 #include <msp/datafile/loader.h>
 #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<std::string, PatternList> 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<typename T>
+       const PatternList &get_patterns() const;
+
+       template<typename T>
+       std::string create_filename(const std::string &) const;
+
+private:
+       template<typename T>
+       void add_pattern(const std::string &);
 
 private:
        static void resolve_aliases(std::vector<std::string> &);
        void parse_specification(const std::string &);
 };
 
+template<typename T>
+inline const Architecture::PatternList &Architecture::get_patterns() const
+{
+       std::map<std::string, PatternList>::const_iterator i = filename_patterns.find(typeid(T).name());
+       if(i!=filename_patterns.end())
+               return i->second;
+
+       static PatternList empty;
+       return empty;
+}
+
+template<typename T>
+inline std::string Architecture::create_filename(const std::string &base) const
+{
+       const PatternList &patterns = get_patterns<T>();
+       return patterns.empty() ? base : patterns.front().apply(base);
+}
+
 #endif
index 0cdeec41edda0a3c815d373fb326663fe863d2ac..9cd493d01c440935050cd51a6f70be65e9b8a5f0 100644 (file)
@@ -11,13 +11,7 @@ Executable::Executable(Builder &b, const FS::Path &p):
 { }
 
 Executable::Executable(Builder &b, const Component &c, const list<ObjectFile *> &objs):
-       Binary(b, c, generate_filename(c), objs)
+       Binary(b, c, b.get_current_arch().create_filename<Executable>(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());
-}
index 967aaf97c7ec0c8044eaf0c48965122029732eb5..a8950f11f1336d53ef7ed5a991809dcdb09b4d7e 100644 (file)
@@ -8,10 +8,7 @@ class Executable: public Binary
 public:
        Executable(Builder &, const Msp::FS::Path &);
        Executable(Builder &, const Component &, const std::list<ObjectFile *> &);
-private:
-       static std::string generate_filename(const Component &);
 
-public:
        virtual const char *get_type() const { return "Executable"; }
 };
 
index 9f6230e481f2f0260275d19d9a88e2ff4a41e46e..2d37a4d93533b4486d865a043f691f3725ff4aa5 100644 (file)
@@ -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<SharedLibrary>(shlib->get_libname());
                        if(link_name!=FS::basename(inst_tgt->get_path()))
                                inst_tgt->set_symlink(link_name);
                }
index ab10c6113eb4ab367bd3636ba15691e276005b2c..1e6cb232135ca489aeeacd2045d86168394b0098 100644 (file)
@@ -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<ObjectFile>(FS::basepart(fn));
 }
 
 void ObjectFile::set_used_in_shared_library(bool u)
index c25c6f213f3e7c791525aad240259384335c60ac..5e479f3c9f652f64c03a9ab3eb472ae645c807ed 100644 (file)
@@ -37,18 +37,12 @@ SharedLibrary::SharedLibrary(Builder &b, const Component &c, const list<ObjectFi
                if(!version.empty())
                {
                        const Architecture &arch = builder.get_current_arch();
-                       const Pattern &pattern = arch.get_shared_library_patterns().front();
                        if(arch.get_system()=="windows")
-                               soname = pattern.apply(format("%s-%s", component->get_name(), version));
+                               soname = arch.create_filename<SharedLibrary>(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<SharedLibrary>(format("%s.%s", libname, version));
                        else
-                               soname = format("%s.%s", pattern.apply(component->get_name()), version);
+                               soname = format("%s.%s", arch.create_filename<SharedLibrary>(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<SharedLibrary>(comp.get_name());
        }
 }
 
index be88b41bf31d61a34257ea75bfc726df4dca5adf..92a3af044cfc1b55de875b3414a25b9226125684 100644 (file)
@@ -26,7 +26,7 @@ StaticLibrary::StaticLibrary(Builder &b, const Component &c, const list<ObjectFi
 string StaticLibrary::generate_filename(const Component &comp)
 {
        const Architecture &arch = comp.get_package().get_builder().get_current_arch();
-       return arch.get_static_library_patterns().front().apply(comp.get_name());
+       return arch.create_filename<StaticLibrary>(comp.get_name());
 }
 
 void StaticLibrary::add_required_library(const string &lib)
index 820fb5b670bfc600a6df879bdc2c15f26ef39452..b5142266edf639203755f1733598df20abc9cf8b 100644 (file)
@@ -86,11 +86,11 @@ FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath
 
        list<string> 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<SharedLibrary>(), lib);
 
        list<string> 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<StaticLibrary>(), lib);
 
        for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
        {