changeset 35:4154894d638c plugin-path-config

Trim KnownPlugins class down to static info and environment variable lookup; introduce KnownPluginCandidates to provide its former API
author Chris Cannam
date Wed, 06 Jun 2018 15:54:26 +0100
parents 6905d8b146f6
children ae74d39e9e4e
files README checker.pri checker/knownplugincandidates.h checker/knownplugins.h src/checker.cpp src/knownplugincandidates.cpp src/knownplugins.cpp version.h
diffstat 8 files changed, 292 insertions(+), 141 deletions(-) [+]
line wrap: on
line diff
--- a/README	Wed Jun 06 13:49:23 2018 +0100
+++ b/README	Wed Jun 06 15:54:26 2018 +0100
@@ -104,7 +104,7 @@
 Written by Chris Cannam at the Centre for Digital Music, Queen Mary
 University of London.
 
-    Copyright (c) 2016-2017 Queen Mary, University of London.
+    Copyright (c) 2016-2018 Queen Mary, University of London.
 
     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation
--- a/checker.pri	Wed Jun 06 13:49:23 2018 +0100
+++ b/checker.pri	Wed Jun 06 15:54:26 2018 +0100
@@ -13,10 +13,12 @@
 
 HEADERS += \
 	checker/plugincandidates.h \
+	checker/knownplugincandidates.h \
 	checker/knownplugins.h
 
 SOURCES += \
 	src/plugincandidates.cpp \
+	src/knownplugincandidates.cpp \
 	src/knownplugins.cpp
 
         
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/checker/knownplugincandidates.h	Wed Jun 06 15:54:26 2018 +0100
@@ -0,0 +1,88 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+/*
+    Copyright (c) 2016-2018 Queen Mary, University of London
+
+    Permission is hereby granted, free of charge, to any person
+    obtaining a copy of this software and associated documentation
+    files (the "Software"), to deal in the Software without
+    restriction, including without limitation the rights to use, copy,
+    modify, merge, publish, distribute, sublicense, and/or sell copies
+    of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+    Except as contained in this notice, the names of the Centre for
+    Digital Music and Queen Mary, University of London shall not be
+    used in advertising or otherwise to promote the sale, use or other
+    dealings in this Software without prior written authorization.
+*/
+
+#ifndef KNOWN_PLUGIN_CANDIDATES_H
+#define KNOWN_PLUGIN_CANDIDATES_H
+
+#include "plugincandidates.h"
+#include "knownplugins.h"
+
+#include <string>
+#include <map>
+#include <vector>
+
+/**
+ * Class to identify and list candidate shared-library files possibly
+ * containing plugins in a hardcoded set of known formats. Uses a
+ * separate process (the "helper", whose executable name must be
+ * provided at construction) to test-load each library in order to
+ * winnow out any that fail to load or crash on load.
+ *
+ * In v1 of the checker library this was the role of the KnownPlugins
+ * class, but that has been changed so that it only provides static
+ * known information about plugin formats and paths, and this class
+ * has been introduced instead (tying that static information together
+ * with a PluginCandidates object that handles the run-time query).
+ *
+ * Requires C++11 and the Qt5 QtCore library.
+ */
+class KnownPluginCandidates
+{
+    typedef std::vector<std::string> stringlist;
+    
+public:
+    KnownPluginCandidates(std::string helperExecutableName,
+                          PluginCandidates::LogCallback *cb = 0);
+    
+    std::vector<KnownPlugins::PluginType> getKnownPluginTypes() const {
+        return m_known.getKnownPluginTypes();
+    }
+
+    std::string getTagFor(KnownPlugins::PluginType type) const {
+        return m_known.getTagFor(type);
+    }
+    
+    stringlist getCandidateLibrariesFor(KnownPlugins::PluginType type) const {
+        return m_candidates.getCandidateLibrariesFor(m_known.getTagFor(type));
+    }
+
+    std::string getHelperExecutableName() const {
+        return m_helperExecutableName;
+    }
+
+    std::string getFailureReport() const;
+    
+private:
+    KnownPlugins m_known;
+    PluginCandidates m_candidates;
+    std::string m_helperExecutableName;
+};
+
+#endif
+
--- a/checker/knownplugins.h	Wed Jun 06 13:49:23 2018 +0100
+++ b/checker/knownplugins.h	Wed Jun 06 15:54:26 2018 +0100
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 /*
-    Copyright (c) 2016 Queen Mary, University of London
+    Copyright (c) 2016-2018 Queen Mary, University of London
 
     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation
@@ -30,18 +30,13 @@
 #ifndef KNOWN_PLUGINS_H
 #define KNOWN_PLUGINS_H
 
-#include "plugincandidates.h"
-
 #include <string>
 #include <map>
 #include <vector>
 
 /**
- * Class to identify and list candidate shared-library files possibly
- * containing plugins in a hardcoded set of known formats. Uses a
- * separate process (the "helper", whose executable name must be
- * provided at construction) to test-load each library in order to
- * winnow out any that fail to load or crash on load.
+ * Class to provide information about a hardcoded set of known plugin
+ * formats.
  *
  * Requires C++11 and the Qt5 QtCore library.
  */
@@ -56,8 +51,12 @@
         DSSIPlugin
     };
 
-    KnownPlugins(std::string helperExecutableName,
-                 PluginCandidates::LogCallback *cb = 0);
+    enum BinaryFormat {
+        FormatNative,
+        FormatNonNative32Bit // i.e. a 32-bit plugin but on a 64-bit host
+    };
+    
+    KnownPlugins(BinaryFormat format);
 
     std::vector<PluginType> getKnownPluginTypes() const;
     
@@ -65,48 +64,40 @@
         return m_known.at(type).tag;
     }
 
-    stringlist getCandidateLibrariesFor(PluginType type) const {
-        return m_candidates.getCandidateLibrariesFor(getTagFor(type));
-    }
-
     std::string getPathEnvironmentVariableFor(PluginType type) const {
         return m_known.at(type).variable;
     }
     
+    stringlist getDefaultPathFor(PluginType type) const {
+        return m_known.at(type).defaultPath;
+    }
+
     stringlist getPathFor(PluginType type) const {
         return m_known.at(type).path;
     }
 
-    std::string getHelperExecutableName() const {
-        return m_helperExecutableName;
+    std::string getDescriptorFor(PluginType type) const {
+        return m_known.at(type).descriptor;
     }
-
-    std::string getFailureReport() const;
     
 private:
     struct TypeRec {
         std::string tag;
         std::string variable;
+        stringlist defaultPath;
         stringlist path;
         std::string descriptor;
     };
     typedef std::map<PluginType, TypeRec> Known;
     Known m_known;
 
-    stringlist expandConventionalPath(PluginType type, std::string var);
-    std::string getDefaultPath(PluginType type);
+    std::string getUnexpandedDefaultPathString(PluginType type);
+    std::string getDefaultPathString(PluginType type);
+    
+    stringlist expandPathString(std::string pathString);
+    stringlist expandConventionalPath(PluginType type, std::string variable);
 
-    PluginCandidates m_candidates;
-    std::string m_helperExecutableName;
-
-    /** This returns true if the helper has a name ending in "-32". By
-     *  our convention, this means that it is a 32-bit helper found on
-     *  a 64-bit system, so (depending on the OS) we may need to look
-     *  in 32-bit-specific paths. Note that is32bit() is *not* usually
-     *  true on 32-bit systems; it's used specifically to indicate a
-     *  "non-native" 32-bit helper.
-     */
-    bool is32bit() const;
+    BinaryFormat m_format;
 };
 
 #endif
--- a/src/checker.cpp	Wed Jun 06 13:49:23 2018 +0100
+++ b/src/checker.cpp	Wed Jun 06 15:54:26 2018 +0100
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 /*
-    Copyright (c) 2016 Queen Mary, University of London
+    Copyright (c) 2016-2018 Queen Mary, University of London
 
     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation
@@ -27,7 +27,7 @@
     dealings in this Software without prior written authorization.
 */
 
-#include "knownplugins.h"
+#include "knownplugincandidates.h"
 
 #include <iostream>
 
@@ -42,7 +42,7 @@
 int main(int, char **)
 {
     LogCallback cb;
-    KnownPlugins kp("./vamp-plugin-load-checker", &cb);
+    KnownPluginCandidates kp("./vamp-plugin-load-checker", &cb);
     
     for (auto t: kp.getKnownPluginTypes()) {
         cout << "successful libraries for plugin type \""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/knownplugincandidates.cpp	Wed Jun 06 15:54:26 2018 +0100
@@ -0,0 +1,106 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+/*
+    Copyright (c) 2016-2018 Queen Mary, University of London
+
+    Permission is hereby granted, free of charge, to any person
+    obtaining a copy of this software and associated documentation
+    files (the "Software"), to deal in the Software without
+    restriction, including without limitation the rights to use, copy,
+    modify, merge, publish, distribute, sublicense, and/or sell copies
+    of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+    Except as contained in this notice, the names of the Centre for
+    Digital Music and Queen Mary, University of London shall not be
+    used in advertising or otherwise to promote the sale, use or other
+    dealings in this Software without prior written authorization.
+*/
+
+#include "knownplugincandidates.h"
+
+#include <sstream>
+
+using namespace std;
+
+/** This returns true if the helper has a name ending in "-32". By our
+ *  convention, this means that it is a 32-bit helper found on a
+ *  64-bit system, so (depending on the OS) we may need to look in
+ *  32-bit-specific paths. Note that is32bit() is *not* usually true
+ *  on 32-bit systems; it's used specifically to indicate a
+ *  "non-native" 32-bit helper.
+ */
+static
+bool
+is32bit(string helperExecutableName)
+{
+    return helperExecutableName.find("-32") != string::npos;
+}
+
+KnownPluginCandidates::KnownPluginCandidates(string helperExecutableName,
+                                             PluginCandidates::LogCallback *cb) :
+    m_known(is32bit(helperExecutableName) ?
+            KnownPlugins::FormatNonNative32Bit :
+            KnownPlugins::FormatNative),
+    m_candidates(helperExecutableName),
+    m_helperExecutableName(helperExecutableName)
+{
+    m_candidates.setLogCallback(cb);
+
+    auto knownTypes = m_known.getKnownPluginTypes();
+        
+    for (auto type: knownTypes) {
+        m_candidates.scan(m_known.getTagFor(type),
+                          m_known.getPathFor(type),
+                          m_known.getDescriptorFor(type));
+    }
+}
+
+string
+KnownPluginCandidates::getFailureReport() const
+{
+    vector<PluginCandidates::FailureRec> failures;
+
+    for (auto t: getKnownPluginTypes()) {
+        auto ff = m_candidates.getFailedLibrariesFor(m_known.getTagFor(t));
+        failures.insert(failures.end(), ff.begin(), ff.end());
+    }
+
+    if (failures.empty()) return "";
+
+    int n = int(failures.size());
+    int i = 0;
+
+    ostringstream os;
+    
+    os << "<ul>";
+    for (auto f: failures) {
+        os << "<li>" + f.library;
+        if (f.message != "") {
+            os << "<br><i>" + f.message + "</i>";
+        } else {
+            os << "<br><i>unknown error</i>";
+        }
+        os << "</li>";
+
+        if (n > 10) {
+            if (++i == 5) {
+                os << "<li>(... and " << (n - i) << " further failures)</li>";
+                break;
+            }
+        }
+    }
+    os << "</ul>";
+
+    return os.str();
+}
--- a/src/knownplugins.cpp	Wed Jun 06 13:49:23 2018 +0100
+++ b/src/knownplugins.cpp	Wed Jun 06 15:54:26 2018 +0100
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 /*
-  Copyright (c) 2016 Queen Mary, University of London
+  Copyright (c) 2016-2018 Queen Mary, University of London
 
   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
@@ -29,8 +29,6 @@
 
 #include "knownplugins.h"
 
-#include <sstream>
-
 using namespace std;
 
 #if defined(_WIN32)
@@ -39,48 +37,45 @@
 #define PATH_SEPARATOR ':'
 #endif
 
-KnownPlugins::KnownPlugins(string helperExecutableName,
-                           PluginCandidates::LogCallback *cb) :
-    m_candidates(helperExecutableName),
-    m_helperExecutableName(helperExecutableName)
+KnownPlugins::KnownPlugins(BinaryFormat format) :
+    m_format(format)
 {
-    m_candidates.setLogCallback(cb);
-
-    std::string variableSuffix = "";
-    if (is32bit()) {
+    string variableSuffix = "";
+    if (m_format == FormatNonNative32Bit) {
         variableSuffix = "_32";
     }
     
     m_known[VampPlugin] = {
         "vamp",
         "VAMP_PATH" + variableSuffix,
-        expandConventionalPath(VampPlugin, "VAMP_PATH" + variableSuffix),
+        {}, {},
         "vampGetPluginDescriptor"
     };
     
     m_known[LADSPAPlugin] = {
         "ladspa",
         "LADSPA_PATH" + variableSuffix,
-        expandConventionalPath(LADSPAPlugin, "LADSPA_PATH" + variableSuffix),
+        {}, {},
         "ladspa_descriptor"
     };
 
     m_known[DSSIPlugin] = {
         "dssi",
         "DSSI_PATH" + variableSuffix,
-        expandConventionalPath(DSSIPlugin, "DSSI_PATH" + variableSuffix),
+        {}, {},
         "dssi_descriptor"
     };
 
-    for (const auto &k: m_known) {
-        m_candidates.scan(k.second.tag, k.second.path, k.second.descriptor);
+    for (auto &k: m_known) {
+        k.second.defaultPath = expandPathString(getDefaultPathString(k.first));
+        k.second.path = expandConventionalPath(k.first, k.second.variable);
     }
 }
 
-std::vector<KnownPlugins::PluginType>
+vector<KnownPlugins::PluginType>
 KnownPlugins::getKnownPluginTypes() const
 {
-    std::vector<PluginType> kt;
+    vector<PluginType> kt;
 
     for (const auto &k: m_known) {
         kt.push_back(k.first);
@@ -89,14 +84,8 @@
     return kt;
 }
 
-bool
-KnownPlugins::is32bit() const
-{
-    return m_helperExecutableName.find("-32") != std::string::npos;
-}
-
 string
-KnownPlugins::getDefaultPath(PluginType type)
+KnownPlugins::getUnexpandedDefaultPathString(PluginType type)
 {
     switch (type) {
 
@@ -132,56 +121,56 @@
     throw logic_error("unknown or unhandled plugin type");
 }
 
+string
+KnownPlugins::getDefaultPathString(PluginType type)
+{
+    string path = getUnexpandedDefaultPathString(type);
+
+    if (path == "") {
+        return path;
+    }
+
+    char *home = getenv("HOME");
+    if (home) {
+        string::size_type f;
+        while ((f = path.find("$HOME")) != string::npos &&
+               f < path.length()) {
+            path.replace(f, 5, home);
+        }
+    }
+
+#ifdef _WIN32
+    const char *pfiles = 0;
+    const char *pfiles32 = 0;
+
+    pfiles = getenv("ProgramFiles");
+    if (!pfiles) {
+        pfiles = "C:\\Program Files";
+    }
+    
+    pfiles32 = getenv("ProgramFiles(x86)");
+    if (!pfiles32) {
+        pfiles32 = "C:\\Program Files (x86)";
+    }
+    
+    string::size_type f;
+    while ((f = path.find("%ProgramFiles%")) != string::npos &&
+           f < path.length()) {
+        if (m_format == FormatNonNative32Bit) {
+            path.replace(f, 14, pfiles32);
+        } else {
+            path.replace(f, 14, pfiles);
+        }
+    }
+#endif
+
+    return path;
+}
+
 vector<string>
-KnownPlugins::expandConventionalPath(PluginType type, string var)
+KnownPlugins::expandPathString(string path)
 {
     vector<string> pathList;
-    string path;
-
-    char *cpath = getenv(var.c_str());
-    if (cpath) path = cpath;
-
-    if (path == "") {
-
-        path = getDefaultPath(type);
-
-        if (path != "") {
-
-            char *home = getenv("HOME");
-            if (home) {
-                string::size_type f;
-                while ((f = path.find("$HOME")) != string::npos &&
-                       f < path.length()) {
-                    path.replace(f, 5, home);
-                }
-            }
-
-#ifdef _WIN32
-            const char *pfiles = 0;
-            const char *pfiles32 = 0;
-
-            pfiles = getenv("ProgramFiles");
-            if (!pfiles) {
-                pfiles = "C:\\Program Files";
-            }
-
-            pfiles32 = getenv("ProgramFiles(x86)");
-            if (!pfiles32) {
-                pfiles32 = "C:\\Program Files (x86)";
-            }
-            
-            string::size_type f;
-            while ((f = path.find("%ProgramFiles%")) != string::npos &&
-                   f < path.length()) {
-                if (is32bit()) {
-                    path.replace(f, 14, pfiles32);
-                } else {
-                    path.replace(f, 14, pfiles);
-                }
-            }
-#endif
-        }
-    }
 
     string::size_type index = 0, newindex = 0;
 
@@ -195,41 +184,16 @@
     return pathList;
 }
 
-string
-KnownPlugins::getFailureReport() const
+vector<string>
+KnownPlugins::expandConventionalPath(PluginType type, string var)
 {
-    vector<PluginCandidates::FailureRec> failures;
+    string path;
 
-    for (auto t: getKnownPluginTypes()) {
-        auto ff = m_candidates.getFailedLibrariesFor(getTagFor(t));
-        failures.insert(failures.end(), ff.begin(), ff.end());
+    char *cpath = getenv(var.c_str());
+    if (cpath) path = cpath;
+    if (path == "") {
+        path = getDefaultPathString(type);
     }
 
-    if (failures.empty()) return "";
-
-    int n = int(failures.size());
-    int i = 0;
-
-    ostringstream os;
-    
-    os << "<ul>";
-    for (auto f: failures) {
-        os << "<li>" + f.library;
-        if (f.message != "") {
-            os << "<br><i>" + f.message + "</i>";
-        } else {
-            os << "<br><i>unknown error</i>";
-        }
-        os << "</li>";
-
-        if (n > 10) {
-            if (++i == 5) {
-                os << "<li>(... and " << (n - i) << " further failures)</li>";
-                break;
-            }
-        }
-    }
-    os << "</ul>";
-
-    return os.str();
+    return expandPathString(path);
 }
--- a/version.h	Wed Jun 06 13:49:23 2018 +0100
+++ b/version.h	Wed Jun 06 15:54:26 2018 +0100
@@ -1,1 +1,1 @@
-#define CHECKER_VERSION "1.0"
+#define CHECKER_VERSION "2.0"