changeset 37:3ccc384c0161

Merge from branch plugin-path-config
author Chris Cannam
date Mon, 11 Jun 2018 17:46:50 +0100
parents cf18645ff411 (current diff) ae74d39e9e4e (diff)
children a43d7a2867d2
files
diffstat 8 files changed, 383 insertions(+), 147 deletions(-) [+]
line wrap: on
line diff
--- a/README	Sun Mar 05 17:15:22 2017 +0000
+++ b/README	Mon Jun 11 17:46:50 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	Sun Mar 05 17:15:22 2017 +0000
+++ b/checker.pri	Mon Jun 11 17:46:50 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	Mon Jun 11 17:46:50 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	Sun Mar 05 17:15:22 2017 +0000
+++ b/checker/knownplugins.h	Mon Jun 11 17:46:50 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,42 +51,53 @@
         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 {
-        return { VampPlugin, LADSPAPlugin, DSSIPlugin };
-    };
+    std::vector<PluginType> getKnownPluginTypes() const;
     
     std::string getTagFor(PluginType type) const {
         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;
     }
 
-    std::string getHelperExecutableName() const {
-        return m_helperExecutableName;
+    stringlist getPathFor(PluginType type) const {
+        return m_known.at(type).path;
     }
 
-    std::string getFailureReport() const;
+    std::string getDescriptorFor(PluginType type) const {
+        return m_known.at(type).descriptor;
+    }
     
 private:
     struct TypeRec {
         std::string tag;
+        std::string variable;
+        stringlist defaultPath;
         stringlist path;
         std::string descriptor;
     };
-    std::map<PluginType, TypeRec> m_known;
+    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;
-
-    bool is32bit() const; // true if helper looks to be 32-bit on 64-bit system
+    BinaryFormat m_format;
 };
 
 #endif
--- a/src/checker.cpp	Sun Mar 05 17:15:22 2017 +0000
+++ b/src/checker.cpp	Mon Jun 11 17:46:50 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	Mon Jun 11 17:46:50 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	Sun Mar 05 17:15:22 2017 +0000
+++ b/src/knownplugins.cpp	Mon Jun 11 17:46:50 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,61 +29,128 @@
 
 #include "knownplugins.h"
 
-#include <sstream>
+#include <iostream>
 
 using namespace std;
 
 #if defined(_WIN32)
+#include <windows.h>
 #define PATH_SEPARATOR ';'
 #else
 #define PATH_SEPARATOR ':'
 #endif
 
-KnownPlugins::KnownPlugins(string helperExecutableName,
-                           PluginCandidates::LogCallback *cb) :
-    m_candidates(helperExecutableName),
-    m_helperExecutableName(helperExecutableName)
+static bool
+getEnvUtf8(std::string variable, std::string &value)
 {
-    m_candidates.setLogCallback(cb);
+    value = "";
     
-    m_known = {
-        {
-            VampPlugin,
-            {
-                "vamp",
-                expandConventionalPath(VampPlugin, "VAMP_PATH"),
-                "vampGetPluginDescriptor"
-            },
-        }, {
-            LADSPAPlugin,
-            {
-                "ladspa",
-                expandConventionalPath(LADSPAPlugin, "LADSPA_PATH"),
-                "ladspa_descriptor"
-            },
-        }, {
-            DSSIPlugin,
-            {
-                "dssi",
-                expandConventionalPath(DSSIPlugin, "DSSI_PATH"),
-                "dssi_descriptor"
-            }
-        }
+#ifdef _WIN32
+    int wvarlen = MultiByteToWideChar(CP_UTF8, 0,
+                                      variable.c_str(), int(variable.length()),
+                                      0, 0);
+    if (wvarlen < 0) {
+        cerr << "WARNING: Unable to convert environment variable name "
+             << variable << " to wide characters" << endl;
+        return false;
+    }
+    
+    wchar_t *wvarbuf = new wchar_t[wvarlen + 1];
+    (void)MultiByteToWideChar(CP_UTF8, 0,
+                              variable.c_str(), int(variable.length()),
+                              wvarbuf, wvarlen);
+    wvarbuf[wvarlen] = L'\0';
+    
+    wchar_t *wvalue = _wgetenv(wvarbuf);
+
+    delete[] wvarbuf;
+
+    if (!wvalue) {
+        return false;
+    }
+
+    int wvallen = int(wcslen(wvalue));
+    int vallen = WideCharToMultiByte(CP_UTF8, 0,
+                                     wvalue, wvallen,
+                                     0, 0, 0, 0);
+    if (vallen < 0) {
+        cerr << "WARNING: Unable to convert environment value to UTF-8" << endl;
+        return false;
+    }
+
+    char *val = new char[vallen + 1];
+    (void)WideCharToMultiByte(CP_UTF8, 0,
+                              wvalue, wvallen,
+                              val, vallen, 0, 0);
+    val[vallen] = '\0';
+
+    value = val;
+
+    delete[] val;
+    return true;
+
+#else
+
+    char *val = getenv(variable.c_str());
+    if (!val) {
+        return false;
+    }
+
+    value = val;
+    return true;
+    
+#endif
+}
+
+KnownPlugins::KnownPlugins(BinaryFormat format) :
+    m_format(format)
+{
+    string variableSuffix = "";
+    if (m_format == FormatNonNative32Bit) {
+        variableSuffix = "_32";
+    }
+    
+    m_known[VampPlugin] = {
+        "vamp",
+        "VAMP_PATH" + variableSuffix,
+        {}, {},
+        "vampGetPluginDescriptor"
+    };
+    
+    m_known[LADSPAPlugin] = {
+        "ladspa",
+        "LADSPA_PATH" + variableSuffix,
+        {}, {},
+        "ladspa_descriptor"
     };
 
-    for (const auto &k: m_known) {
-        m_candidates.scan(k.second.tag, k.second.path, k.second.descriptor);
+    m_known[DSSIPlugin] = {
+        "dssi",
+        "DSSI_PATH" + variableSuffix,
+        {}, {},
+        "dssi_descriptor"
+    };
+
+    for (auto &k: m_known) {
+        k.second.defaultPath = expandPathString(getDefaultPathString(k.first));
+        k.second.path = expandConventionalPath(k.first, k.second.variable);
     }
 }
 
-bool
-KnownPlugins::is32bit() const
+vector<KnownPlugins::PluginType>
+KnownPlugins::getKnownPluginTypes() const
 {
-    return m_helperExecutableName.find("-32") != std::string::npos;
+    vector<PluginType> kt;
+
+    for (const auto &k: m_known) {
+        kt.push_back(k.first);
+    }
+
+    return kt;
 }
 
 string
-KnownPlugins::getDefaultPath(PluginType type)
+KnownPlugins::getUnexpandedDefaultPathString(PluginType type)
 {
     switch (type) {
 
@@ -119,57 +186,51 @@
     throw logic_error("unknown or unhandled plugin type");
 }
 
+string
+KnownPlugins::getDefaultPathString(PluginType type)
+{
+    string path = getUnexpandedDefaultPathString(type);
+
+    if (path == "") {
+        return path;
+    }
+
+    string home;
+    if (getEnvUtf8("HOME", home)) {
+        string::size_type f;
+        while ((f = path.find("$HOME")) != string::npos &&
+               f < path.length()) {
+            path.replace(f, 5, home);
+        }
+    }
+
+#ifdef _WIN32
+    string pfiles, pfiles32;
+    if (!getEnvUtf8("ProgramFiles", pfiles)) {
+        pfiles = "C:\\Program Files";
+    }
+    if (!getEnvUtf8("ProgramFiles(x86)", 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;
-
-#ifdef _WIN32
-    bool is32 = is32bit();
-#endif
-
-    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;
-            if (is32) {
-                pfiles = getenv("ProgramFiles(x86)");
-            }
-            if (!pfiles) {
-                pfiles = getenv("ProgramFiles");
-            }
-            if (!pfiles) {
-                if (is32) {
-                    pfiles = "C:\\Program Files (x86)";
-                } else {
-                    pfiles = "C:\\Program Files";
-                }
-            }
-            string::size_type f;
-            while ((f = path.find("%ProgramFiles%")) != string::npos &&
-                   f < path.length()) {
-                path.replace(f, 14, pfiles);
-            }
-#endif
-        }
-    }
 
     string::size_type index = 0, newindex = 0;
 
@@ -183,41 +244,14 @@
     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());
+    if (!getEnvUtf8(var, 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	Sun Mar 05 17:15:22 2017 +0000
+++ b/version.h	Mon Jun 11 17:46:50 2018 +0100
@@ -1,1 +1,1 @@
-#define CHECKER_VERSION "1.0"
+#define CHECKER_VERSION "2.0"