CQtDeployer/Deploy/deployutils.cpp

408 lines
14 KiB
C++
Raw Normal View History

2019-01-26 07:54:56 +03:00
/*
* Copyright (C) 2018-2019 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2018-12-15 20:51:25 +03:00
#include "deploy.h"
2018-12-09 17:35:07 +03:00
#include "deployutils.h"
2018-12-15 20:51:25 +03:00
#include "quasarapp.h"
2018-12-09 17:35:07 +03:00
2018-12-15 20:51:25 +03:00
#include <QDebug>
#include <QDir>
2018-12-09 17:35:07 +03:00
#include <QFileInfo>
2019-08-12 18:05:28 +03:00
#include <QLibraryInfo>
2018-12-09 17:35:07 +03:00
QString DeployUtils::qtDir = "";
QStringList DeployUtils::extraPaths = QStringList();
2019-03-25 20:13:30 +03:00
2019-01-27 15:37:19 +03:00
QtModuleEntry DeployUtils::qtModuleEntries[] = {
{ QtBluetoothModule, "bluetooth", "Qt5Bluetooth", nullptr },
{ QtConcurrentModule, "concurrent", "Qt5Concurrent", "qtbase" },
{ QtCoreModule, "core", "Qt5Core", "qtbase" },
{ QtDeclarativeModule, "declarative", "Qt5Declarative", "qtquick1" },
{ QtDesignerModule, "designer", "Qt5Designer", nullptr },
{ QtDesignerComponents, "designercomponents", "Qt5DesignerComponents", nullptr },
{ QtEnginioModule, "enginio", "Enginio", nullptr },
{ QtGamePadModule, "gamepad", "Qt5Gamepad", nullptr },
{ QtGuiModule, "gui", "Qt5Gui", "qtbase" },
{ QtHelpModule, "qthelp", "Qt5Help", "qt_help" },
{ QtMultimediaModule, "multimedia", "Qt5Multimedia", "qtmultimedia" },
{ QtMultimediaWidgetsModule, "multimediawidgets", "Qt5MultimediaWidgets", "qtmultimedia" },
{ QtMultimediaQuickModule, "multimediaquick", "Qt5MultimediaQuick_p", "qtmultimedia" },
{ QtNetworkModule, "network", "Qt5Network", "qtbase" },
{ QtNfcModule, "nfc", "Qt5Nfc", nullptr },
{ QtOpenGLModule, "opengl", "Qt5OpenGL", nullptr },
{ QtPositioningModule, "positioning", "Qt5Positioning", nullptr },
{ QtPrintSupportModule, "printsupport", "Qt5PrintSupport", nullptr },
{ QtQmlModule, "qml", "Qt5Qml", "qtdeclarative" },
{ QtQmlToolingModule, "qmltooling", "qmltooling", nullptr },
{ QtQuickModule, "quick", "Qt5Quick", "qtdeclarative" },
{ QtQuickParticlesModule, "quickparticles", "Qt5QuickParticles", nullptr },
{ QtQuickWidgetsModule, "quickwidgets", "Qt5QuickWidgets", nullptr },
{ QtScriptModule, "script", "Qt5Script", "qtscript" },
{ QtScriptToolsModule, "scripttools", "Qt5ScriptTools", "qtscript" },
{ QtSensorsModule, "sensors", "Qt5Sensors", nullptr },
{ QtSerialPortModule, "serialport", "Qt5SerialPort", "qtserialport" },
{ QtSqlModule, "sql", "Qt5Sql", "qtbase" },
{ QtSvgModule, "svg", "Qt5Svg", nullptr },
{ QtTestModule, "test", "Qt5Test", "qtbase" },
{ QtWebKitModule, "webkit", "Qt5WebKit", nullptr },
{ QtWebKitWidgetsModule, "webkitwidgets", "Qt5WebKitWidgets", nullptr },
{ QtWebSocketsModule, "websockets", "Qt5WebSockets", nullptr },
{ QtWidgetsModule, "widgets", "Qt5Widgets", "qtbase" },
{ QtWinExtrasModule, "winextras", "Qt5WinExtras", nullptr },
{ QtXmlModule, "xml", "Qt5Xml", "qtbase" },
{ QtXmlPatternsModule, "xmlpatterns", "Qt5XmlPatterns", "qtxmlpatterns" },
{ QtWebEngineCoreModule, "webenginecore", "Qt5WebEngineCore", nullptr },
{ QtWebEngineModule, "webengine", "Qt5WebEngine", "qtwebengine" },
{ QtWebEngineWidgetsModule, "webenginewidgets", "Qt5WebEngineWidgets", nullptr },
{ Qt3DCoreModule, "3dcore", "Qt53DCore", nullptr },
{ Qt3DRendererModule, "3drenderer", "Qt53DRender", nullptr },
{ Qt3DQuickModule, "3dquick", "Qt53DQuick", nullptr },
{ Qt3DQuickRendererModule, "3dquickrenderer", "Qt53DQuickRender", nullptr },
{ Qt3DInputModule, "3dinput", "Qt53DInput", nullptr },
{ Qt3DAnimationModule, "3danimation", "Qt53DAnimation", nullptr },
{ Qt3DExtrasModule, "3dextras", "Qt53DExtras", nullptr },
{ QtLocationModule, "geoservices", "Qt5Location", nullptr },
{ QtWebChannelModule, "webchannel", "Qt5WebChannel", nullptr },
{ QtTextToSpeechModule, "texttospeech", "Qt5TextToSpeech", nullptr },
{ QtSerialBusModule, "serialbus", "Qt5SerialBus", nullptr },
{ QtWebViewModule, "webview", "Qt5WebView", nullptr }
};
2019-03-25 21:27:26 +03:00
libPriority DeployUtils::getLibPriority(const QString &lib) {
2018-12-09 17:35:07 +03:00
if (!QFileInfo(lib).isFile()) {
2019-03-25 21:27:26 +03:00
return NotFile;
2018-12-09 17:35:07 +03:00
}
if (isQtLib(lib)) {
2019-03-25 21:27:26 +03:00
return QtLib;
2018-12-09 17:35:07 +03:00
}
if (isExtraLib(lib)) {
2019-03-25 21:27:26 +03:00
return ExtraLib;
2018-12-09 17:35:07 +03:00
}
2019-04-04 18:11:38 +03:00
return SystemLib;
2018-12-09 17:35:07 +03:00
}
2018-12-15 20:51:25 +03:00
void DeployUtils::verboseLog(const QString &str) {
if (QuasarAppUtils::Params::isEndable("verbose")) {
qDebug() << str;
}
}
void DeployUtils::help() {
2019-08-23 21:29:02 +03:00
QStringList help = {
{ "CQtDeployer version: " + getAppVersion()},
{ "Usage: cqtdeployer <-bin [params]> [options]"},
{ "" },
{ "Options:" },
{ " help / h : Shows help." },
{ " always-overwrite : Copies files and replaces the existing ones." },
{ " -bin [list, params] : Deployable file or folder." },
{ " | For example -bin /my/project/bin/,/my/project/bin.exe" },
{ " -binDir [params] : A folder which includes deployable files (recursive search)." },
{ " | WARNING: this flag supports 'so', 'dll' and 'exe' files only." },
{ " | Use '-bin' flag if you want to deploy linux binary files" },
{ " -qmlDir [params] : Qml data dir. For example -qmlDir ~/my/project/qml" },
{ " deploySystem : Deploys all libs" },
{ " noLibc : Skip Deploys libc and ld-linux libs" },
{ " -qmake [params] : Qmake path." },
{ " | For example -qmake ~/Qt/5.11.1/gcc_64/bin/qmake" },
{ " -ignore [list,params] : The list of libs to ignore." },
{ " | For example -ignore libicudata.so.56,libicudata2.so.56" },
{ " -ignoreEnv [list,params] : The list of the environment to ignore" },
{ " | For example -ignoreEnv /bad/dir,/my/bad/Dir" },
{ " clear : Deletes deployable files of the previous session." },
{ " allQmlDependes : Extracts all the qml libraries." },
{ " | (not recommended, as it takes great amount of computer memory)" },
{ " -libDir [list,params] : Sets additional paths for extra libs of an app." },
{ " | For example -libDir /myLib,/newLibs " },
{ " -extraPlugin[list,params]: Sets an additional path to extraPlugin of an app" },
{ " -recursiveDepth [params] : Sets the Depth of recursive search of libs (default 0)" },
{ " -targetDir [params] : Sets target directory(by default it is the path to the first deployable file)" },
{ " noStrip : Skips strip step" },
{ " noTranslations : Skips the translations files." },
{ " qmlExtern : Use the qml external scanner (qmlimportscaner)" },
{ " | It doesn't work without qmake and inside a snap package" },
{ " v / version : show compiled version" },
{ "" },
{ "Extra options:" }};
help.append(QuasarAppUtils::Params::getparamsHelp());
help.append({
{ "" },
{ "Example: cqtdeployer -bin myApp -qmlDir ~/Qt/5.14.0/gcc_64/qml -qmake ~/Qt/5.14.0/gcc_64/bin/qmake clear" },
{ "Example (only C libs): cqtdeployer -bin myApp clear" }});
QuasarAppUtils::Params::showHelp(help);
2018-12-15 20:51:25 +03:00
}
bool DeployUtils::parseQt(Deploy *deploy) {
2018-12-19 23:05:00 +03:00
if (!(QuasarAppUtils::Params::isEndable("bin") ||
QuasarAppUtils::Params::isEndable("binDir"))) {
qWarning() << "you need to use -bin or -binDir flags";
return false;
}
2018-12-15 20:51:25 +03:00
auto bin = QuasarAppUtils::Params::getStrArg("bin").split(',');
bin.removeAll("");
if (!deploy->setTargets(bin)) {
auto binDir = QuasarAppUtils::Params::getStrArg("binDir");
if (!deploy->setTargetsRecursive(binDir)) {
qCritical() << "error init targeet dir";
return false;
}
}
2019-06-19 23:38:31 +03:00
deploy->initIgnoreEnvList();
deploy->initEnvirement();
deploy->initIgnoreList();
2018-12-15 20:51:25 +03:00
if (QuasarAppUtils::Params::isEndable("clear")) {
qInfo() << "clear old data";
deploy->clear();
}
int limit = 0;
if (QuasarAppUtils::Params::isEndable("recursiveDepth")) {
bool ok;
limit = QuasarAppUtils::Params::getArg("recursiveDepth").toInt(&ok);
if (!ok) {
limit = 0;
qWarning() << "recursiveDepth is invalid! use default value 0";
}
}
deploy->setDepchLimit(limit);
auto listLibDir = QuasarAppUtils::Params::getStrArg("libDir").split(",");
auto listExtraPlugin =
QuasarAppUtils::Params::getStrArg("extraPlugin").split(",");
deploy->setExtraPath(listLibDir);
deploy->setExtraPlugins(listExtraPlugin);
auto qmake = QuasarAppUtils::Params::getStrArg("qmake");
QString basePath = "";
QFileInfo info(qmake);
if (!info.isFile() || (info.baseName() != "qmake")) {
qInfo() << "deploy only C libs because qmake is not found";
return true;
}
basePath = info.absolutePath();
deploy->setQmake(qmake);
2019-04-06 14:42:22 +03:00
auto qmlDir = QuasarAppUtils::Params::getStrArg("qmlDir");
2018-12-15 20:51:25 +03:00
QDir dir(basePath);
2019-04-06 14:42:22 +03:00
if (QuasarAppUtils::Params::isEndable("qmlExtern")) {
2018-12-15 20:51:25 +03:00
2019-04-06 14:42:22 +03:00
#ifdef Q_OS_WIN
auto scaner = basePath + QDir::separator() + "qmlimportscanner.exe";
#else
auto scaner = basePath + QDir::separator() + "qmlimportscanner";
#endif
if ( !QFileInfo::exists(scaner)) {
QuasarAppUtils::Params::verboseLog("qml scaner not defined, using own scaner!",
QuasarAppUtils::VerboseLvl::Warning);
QuasarAppUtils::Params::setEnable("qmlExtern", false);
} else {
deploy->setQmlScaner(scaner);
}
}
if (QFileInfo::exists(qmlDir) ||
QuasarAppUtils::Params::isEndable("allQmlDependes")) {
2018-12-15 20:51:25 +03:00
deploy->setDeployQml(true);
2019-04-06 14:42:22 +03:00
2018-12-15 20:51:25 +03:00
} else {
QuasarAppUtils::Params::verboseLog("qml dir not exits!",
QuasarAppUtils::VerboseLvl::Warning);
2018-12-15 20:51:25 +03:00
}
if (!dir.cdUp()) {
return false;
}
deploy->setQtDir(dir.absolutePath());
return true;
}
2019-01-27 15:37:19 +03:00
QStringList DeployUtils::extractTranslation(const QStringList &libs) {
QSet<QString> res;
const size_t qtModulesCount = sizeof(qtModuleEntries) / sizeof(QtModuleEntry);
for (auto &&lib: libs) {
for (size_t i = 0; i < qtModulesCount; ++i) {
if (lib.contains(qtModuleEntries[i].libraryName) &&
qtModuleEntries[i].translation) {
res.insert(qtModuleEntries[i].translation);
}
}
}
return res.toList();
}
2019-08-12 18:05:28 +03:00
QString DeployUtils::getAppVersion() {
return APP_VERSION;
}
QString DeployUtils::getQtVersion() {
#ifdef QT_VERSION_STR
return QT_VERSION_STR;
#else
return "without qt";
#endif
}
void DeployUtils::printVersion() {
qInfo() << "CQtDeployer: " + getAppVersion();
qInfo() << "Qt: " + getQtVersion();
}
2019-03-28 10:32:46 +03:00
MSVCVersion DeployUtils::getMSVC(const QString &_qmake) {
QFileInfo qmake(_qmake);
int res = MSVCVersion::MSVC_Unknown;
if (!qmake.isFile()) {
QuasarAppUtils::Params::verboseLog("qmake is wrong!");
return static_cast<MSVCVersion>(res);
}
QDir dir = qmake.absoluteDir();
if (!dir.cdUp()) {
QuasarAppUtils::Params::verboseLog("is not standart qt repo");
return static_cast<MSVCVersion>(res);
}
auto msvcPath = dir.absolutePath();
if (!(dir.cdUp() && dir.cdUp())) {
QuasarAppUtils::Params::verboseLog("is not standart qt repo");
return static_cast<MSVCVersion>(res);
}
if (!msvcPath.contains("msvc")) {
QuasarAppUtils::Params::verboseLog("vcredis not defined");
return static_cast<MSVCVersion>(res);
}
auto base = msvcPath.mid(msvcPath.indexOf("msvc"), 11);
auto version = base.mid(4 , 4);
auto type = base.right(2);
if (version == "2013") {
res |= MSVC_13;
}
else if (version == "2015") {
res |= MSVC_15;
}
else if (version == "2017") {
res |= MSVC_17;
}
else if (version == "2019") {
res |= MSVC_19;
}
if (type == "32") {
res |= MSVC_x32;
}
else if (type == "64") {
res |= MSVC_x64;
}
return static_cast<MSVCVersion>(res);
}
QString DeployUtils::getVCredist(const QString &_qmake) {
auto msvc = getMSVC(_qmake);
2019-03-28 12:36:21 +03:00
QFileInfo qmake(_qmake);
QDir dir = qmake.absoluteDir();
if (!(dir.cdUp() && dir.cdUp() && dir.cdUp() && dir.cd("vcredist"))) {
QuasarAppUtils::Params::verboseLog("redist not findet!");
return "";
}
auto infoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
auto name = getMSVCName(msvc);
auto version = getMSVCVersion(msvc);
for (auto &&info: infoList) {
auto file = QFileInfo(info).fileName();
if (file.contains(name) && file.contains(version)) {
return info.absoluteFilePath();
}
}
return "";
}
QString DeployUtils::getMSVCName(MSVCVersion msvc) {
if (msvc | MSVCVersion::MSVC_13) {
return "msvc2013";
} else if (msvc | MSVCVersion::MSVC_15) {
return "msvc2015";
} else if (msvc | MSVCVersion::MSVC_17) {
return "msvc2017";
} else if (msvc | MSVCVersion::MSVC_19) {
return "msvc2019";
}
return "";
}
QString DeployUtils::getMSVCVersion(MSVCVersion msvc) {
if (msvc | MSVCVersion::MSVC_x32) {
return "x86";
} else if (msvc | MSVCVersion::MSVC_x64) {
return "x64";
}
2019-03-28 10:32:46 +03:00
return "";
}
2018-12-09 17:35:07 +03:00
bool DeployUtils::isQtLib(const QString &lib) {
QFileInfo info(lib);
2019-01-26 15:34:39 +03:00
return !qtDir.isEmpty() && info.absoluteFilePath().contains(qtDir);
2018-12-09 17:35:07 +03:00
}
bool DeployUtils::isExtraLib(const QString &lib) {
QFileInfo info(lib);
for (auto i : extraPaths) {
2019-01-26 15:34:39 +03:00
if (info.absoluteFilePath().contains(i)) {
2018-12-09 17:35:07 +03:00
return true;
}
}
return false;
}