annotate base/HelperExecPath.cpp @ 1858:14747f24ad04

Use cancellable serialiser; add some more profiling points
author Chris Cannam
date Thu, 14 May 2020 16:36:48 +0100
parents cf3eb6252f42
children
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@1781 61 // Helpers are expected to exist in one of the following, in order
Chris@1781 62 // from most strongly preferred to least:
Chris@1781 63 //
Chris@1781 64 // 1. (on Mac only) in <mydir>/../Resources
Chris@1781 65 //
Chris@1781 66 // 2. (on non-Windows non-Mac platforms only) in
Chris@1781 67 // <mydir>/../lib/application-name/
Chris@1781 68 //
Chris@1781 69 // 3. (on non-Mac platforms only) in <mydir>/helpers
Chris@1781 70 //
Chris@1781 71 // 4. in <mydir>
Chris@1694 72
Chris@1243 73 QStringList dirs;
Chris@1781 74 QString appName = QCoreApplication::applicationName();
Chris@1243 75 QString myDir = QCoreApplication::applicationDirPath();
Chris@1783 76 QString binaryName = QFileInfo(QCoreApplication::arguments().at(0))
Chris@1783 77 .fileName();
Chris@1783 78
Chris@1694 79 #ifdef Q_OS_MAC
Chris@1694 80 dirs.push_back(myDir + "/../Resources");
Chris@1694 81 #else
Chris@1781 82 #ifndef Q_OS_WIN32
Chris@1783 83 if (binaryName != "") {
Chris@1783 84 dirs.push_back(myDir + "/../lib/" + binaryName);
Chris@1783 85 }
Chris@1781 86 dirs.push_back(myDir + "/../lib/" + appName);
Chris@1781 87 #endif
Chris@1243 88 dirs.push_back(myDir + "/helpers");
Chris@1694 89 #endif
Chris@1243 90 dirs.push_back(myDir);
Chris@1243 91 return dirs;
Chris@1243 92 }
Chris@1243 93
Chris@1243 94 QStringList
Chris@1243 95 HelperExecPath::getHelperCandidatePaths(QString basename)
Chris@1243 96 {
Chris@1243 97 QStringList candidates;
Chris@1243 98 (void)search(basename, candidates);
Chris@1243 99 return candidates;
Chris@1243 100 }
Chris@1243 101
Chris@1246 102 QList<HelperExecPath::HelperExec>
Chris@1243 103 HelperExecPath::search(QString basename, QStringList &candidates)
Chris@1243 104 {
Chris@1243 105 QString extension = "";
Chris@1243 106 #ifdef _WIN32
Chris@1243 107 extension = ".exe";
Chris@1243 108 #endif
Chris@1243 109
Chris@1246 110 QList<HelperExec> executables;
Chris@1243 111 QStringList dirs = getHelperDirPaths();
Chris@1243 112
Chris@1246 113 for (QString t: getTags()) {
Chris@1243 114 for (QString d: dirs) {
Chris@1246 115 QString path = d + QDir::separator() + basename;
Chris@1246 116 if (t != QString()) path += "-" + t;
Chris@1246 117 path += extension;
Chris@1243 118 candidates.push_back(path);
Chris@1243 119 if (isGood(path)) {
Chris@1246 120 executables.push_back({ path, t });
Chris@1243 121 break;
Chris@1243 122 }
Chris@1243 123 }
Chris@1243 124 }
Chris@1243 125
Chris@1243 126 return executables;
Chris@1243 127 }
Chris@1243 128