annotate base/HelperExecPath.cpp @ 1476:5afbac960a30 plugin-path-config

Environment var tests (beginnings of)
author Chris Cannam
date Thu, 07 Jun 2018 16:18:42 +0100
parents 75aefcc9f07d
children a9d0b5a2c242
rev   line source
Chris@1243 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1243 2
Chris@1243 3 /*
Chris@1243 4 Sonic Visualiser
Chris@1243 5 An audio file viewer and annotation editor.
Chris@1243 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1243 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1243 8
Chris@1243 9 This program is free software; you can redistribute it and/or
Chris@1243 10 modify it under the terms of the GNU General Public License as
Chris@1243 11 published by the Free Software Foundation; either version 2 of the
Chris@1243 12 License, or (at your option) any later version. See the file
Chris@1243 13 COPYING included with this distribution for more information.
Chris@1243 14 */
Chris@1243 15
Chris@1243 16 #include "HelperExecPath.h"
Chris@1243 17
Chris@1243 18 #include <QCoreApplication>
Chris@1243 19 #include <QFile>
Chris@1243 20 #include <QDir>
Chris@1243 21 #include <QFileInfo>
Chris@1243 22
Chris@1246 23 QStringList
Chris@1246 24 HelperExecPath::getTags()
Chris@1243 25 {
Chris@1243 26 if (sizeof(void *) == 8) {
Chris@1246 27 if (m_type == NativeArchitectureOnly) {
Chris@1246 28 return { "64", "" };
Chris@1246 29 } else {
Chris@1246 30 return { "64", "", "32" };
Chris@1246 31 }
Chris@1243 32 } else {
Chris@1246 33 return { "", "32" };
Chris@1243 34 }
Chris@1243 35 }
Chris@1243 36
Chris@1243 37 static bool
Chris@1243 38 isGood(QString path)
Chris@1243 39 {
Chris@1243 40 return QFile(path).exists() && QFileInfo(path).isExecutable();
Chris@1243 41 }
Chris@1243 42
Chris@1246 43 QList<HelperExecPath::HelperExec>
Chris@1243 44 HelperExecPath::getHelperExecutables(QString basename)
Chris@1243 45 {
Chris@1243 46 QStringList dummy;
Chris@1243 47 return search(basename, dummy);
Chris@1243 48 }
Chris@1243 49
Chris@1243 50 QString
Chris@1243 51 HelperExecPath::getHelperExecutable(QString basename)
Chris@1243 52 {
Chris@1246 53 auto execs = getHelperExecutables(basename);
Chris@1243 54 if (execs.empty()) return "";
Chris@1246 55 else return execs[0].executable;
Chris@1243 56 }
Chris@1243 57
Chris@1243 58 QStringList
Chris@1243 59 HelperExecPath::getHelperDirPaths()
Chris@1243 60 {
Chris@1243 61 QStringList dirs;
Chris@1243 62 QString myDir = QCoreApplication::applicationDirPath();
Chris@1243 63 dirs.push_back(myDir + "/helpers");
Chris@1243 64 dirs.push_back(myDir);
Chris@1243 65 return dirs;
Chris@1243 66 }
Chris@1243 67
Chris@1243 68 QStringList
Chris@1243 69 HelperExecPath::getHelperCandidatePaths(QString basename)
Chris@1243 70 {
Chris@1243 71 QStringList candidates;
Chris@1243 72 (void)search(basename, candidates);
Chris@1243 73 return candidates;
Chris@1243 74 }
Chris@1243 75
Chris@1246 76 QList<HelperExecPath::HelperExec>
Chris@1243 77 HelperExecPath::search(QString basename, QStringList &candidates)
Chris@1243 78 {
Chris@1243 79 // Helpers are expected to exist either in the same directory as
Chris@1243 80 // this executable was found, or in a subdirectory called helpers.
Chris@1243 81
Chris@1243 82 QString extension = "";
Chris@1243 83 #ifdef _WIN32
Chris@1243 84 extension = ".exe";
Chris@1243 85 #endif
Chris@1243 86
Chris@1246 87 QList<HelperExec> executables;
Chris@1243 88 QStringList dirs = getHelperDirPaths();
Chris@1243 89
Chris@1246 90 for (QString t: getTags()) {
Chris@1243 91 for (QString d: dirs) {
Chris@1246 92 QString path = d + QDir::separator() + basename;
Chris@1246 93 if (t != QString()) path += "-" + t;
Chris@1246 94 path += extension;
Chris@1243 95 candidates.push_back(path);
Chris@1243 96 if (isGood(path)) {
Chris@1246 97 executables.push_back({ path, t });
Chris@1243 98 break;
Chris@1243 99 }
Chris@1243 100 }
Chris@1243 101 }
Chris@1243 102
Chris@1243 103 return executables;
Chris@1243 104 }
Chris@1243 105