CQtDeployer/Deploy/deploycore.cpp

413 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.
*/
2019-09-08 13:37:33 +03:00
#include "extracter.h"
2019-09-03 18:15:05 +03:00
#include "deploycore.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>
2019-09-14 13:59:11 +03:00
#include <configparser.h>
2018-12-09 17:35:07 +03:00
2019-09-07 12:01:20 +03:00
//QString DeployCore::qtDir = "";
//QStringList DeployCore::extraPaths = QStringList();
2018-12-09 17:35:07 +03:00
2019-03-25 20:13:30 +03:00
2019-09-08 13:37:33 +03:00
const DeployConfig* DeployCore::_config = nullptr;
2019-09-03 18:15:05 +03:00
QtModuleEntry DeployCore::qtModuleEntries[] = {
2019-01-27 15:37:19 +03:00
{ 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-09-03 18:15:05 +03:00
DeployCore::QtModule DeployCore::getQtModule(const QString& path) {
auto priority = DeployCore::getLibPriority(path);
2019-08-31 22:22:26 +03:00
if (priority != QtLib) {
2019-09-03 18:15:05 +03:00
return DeployCore::QtModule::NONE;
2019-08-31 22:22:26 +03:00
}
int modulesCount = sizeof (qtModuleEntries) / sizeof (QtModuleEntry);
auto lIbName = QFileInfo(path).fileName();
for (int i = 0; i < modulesCount; ++i) {
2019-09-04 16:06:28 +03:00
if (lIbName.contains(qtModuleEntries[i].libraryName)) {
2019-09-03 18:15:05 +03:00
return static_cast<DeployCore::QtModule>(qtModuleEntries[i].module);
2019-08-31 22:22:26 +03:00
}
}
2019-09-03 18:15:05 +03:00
return DeployCore::QtModule::NONE;
2019-08-31 22:22:26 +03:00
}
2019-09-03 18:15:05 +03:00
void DeployCore::addQtModule(DeployCore::QtModule &module, const QString &path) {
2019-09-04 16:06:28 +03:00
2019-09-04 16:37:31 +03:00
QuasarAppUtils::Params::verboseLog("current module " + QString::number(module),
QuasarAppUtils::Info);
2019-09-04 16:06:28 +03:00
auto mod = getQtModule(path);
QuasarAppUtils::Params::verboseLog("add new module from path " + path +
" module value " + QString::number(mod),
QuasarAppUtils::Info);
2019-09-03 18:15:05 +03:00
module = static_cast<DeployCore::QtModule>(
2019-09-04 16:06:28 +03:00
static_cast<quint64>(module) | static_cast<quint64>(mod));
2019-08-31 22:22:26 +03:00
}
2019-09-03 18:15:05 +03:00
LibPriority DeployCore::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
}
2019-09-03 18:15:05 +03:00
void DeployCore::verboseLog(const QString &str) {
2018-12-15 20:51:25 +03:00
if (QuasarAppUtils::Params::isEndable("verbose")) {
qDebug() << str;
}
}
2019-08-24 17:56:42 +03:00
#define C(X) QuasarAppUtils::Params::isEndable(X)
2019-09-03 18:15:05 +03:00
RunMode DeployCore::getMode() {
2019-08-24 17:56:42 +03:00
if (C("help") || C("h") || C("v") || C("version")) {
return RunMode::Info;
}
2019-09-16 12:38:48 +03:00
if (QuasarAppUtils::Params::customParamasSize() == 0 || C("bin") || C("binDir")) {
2019-08-24 17:56:42 +03:00
return RunMode::Deploy;
}
if (C("clear") || C("force-clear")) {
return RunMode::Clear;
}
return RunMode::Info;
}
2019-09-15 12:02:56 +03:00
QString DeployCore::help() {
2019-08-23 21:29:02 +03:00
QStringList help = {
{ "CQtDeployer version: " + getAppVersion()},
{ "Usage: cqtdeployer <-bin [params]> [options]"},
{ "" },
{ "Options:" },
{ " help / h : Shows help." },
2019-09-15 12:02:56 +03:00
{ " noOverwrite : Prevents replacing existing files." },
2019-08-23 21:29:02 +03:00
{ " -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" },
2019-09-24 15:11:30 +03:00
{ " deploySystem-with-libc : Skip Deploys system core libs libs" },
2019-08-23 21:29:02 +03:00
{ " -qmake [params] : Qmake path." },
2019-09-24 15:11:30 +03:00
{ " | For example -qmake ~/Qt/5.14.0/gcc_64/bin/qmake" },
2019-08-23 21:29:02 +03:00
{ " -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." },
2019-08-24 01:42:13 +03:00
{ " force-clear : Deletes the destination directory before deployment." },
2019-08-23 21:29:02 +03:00
{ " 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." },
{ " | It doesn't work without qmake and inside a snap package" },
2019-08-26 17:05:18 +03:00
{ " -qmlOut [params] : Sets path to qml out directory" },
{ " -libOut [params] : Sets path to libraries out directory" },
{ " -trOut [params] : Sets path to translations out directory" },
{ " -pluginOut [params] : Sets path to plugins out directory" },
2019-08-26 21:16:47 +03:00
{ " -binOut [params] : Sets path to binary out directory" },
2019-08-23 23:10:31 +03:00
{ " v / version : Shows compiled version" },
{ " verbose [1-3] : Shows debug log" },
2019-08-23 21:29:02 +03:00
2019-08-23 23:10:31 +03:00
{ "" },
2019-08-23 21:29:02 +03:00
{ "" },
{ "Example: cqtdeployer -bin myApp -qmlDir ~/Qt/5.14.0/gcc_64/qml -qmake ~/Qt/5.14.0/gcc_64/bin/qmake clear" },
2019-08-23 23:10:31 +03:00
{ "Example (only C libs): cqtdeployer -bin myApp clear" }};
2019-08-23 21:29:02 +03:00
QuasarAppUtils::Params::showHelp(help);
2019-09-10 18:22:49 +03:00
return help.join(" ");
2018-12-15 20:51:25 +03:00
}
2019-09-10 15:40:12 +03:00
QStringList DeployCore::helpKeys() {
return {
"help",
2019-09-12 09:38:30 +03:00
"noOverwrite",
2019-09-10 15:40:12 +03:00
"bin",
"binDir",
"qmlDir",
"deploySystem",
2019-09-24 15:11:30 +03:00
"deploySystem-with-libc",
2019-09-10 15:40:12 +03:00
"qmake",
"ignore",
"ignoreEnv",
"clear",
"force-clear",
"allQmlDependes",
"libDir",
"extraPlugin",
"recursiveDepth",
"targetDir",
"noStrip",
"noTranslations",
"version",
"verbose"
};
2019-08-24 17:56:42 +03:00
}
2019-09-03 18:15:05 +03:00
QStringList DeployCore::extractTranslation(const QStringList &libs) {
2019-01-27 15:37:19 +03:00
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-09-03 18:15:05 +03:00
QString DeployCore::getAppVersion() {
2019-08-12 18:05:28 +03:00
return APP_VERSION;
}
2019-09-03 18:15:05 +03:00
QString DeployCore::getQtVersion() {
2019-08-12 18:05:28 +03:00
#ifdef QT_VERSION_STR
return QT_VERSION_STR;
#else
return "without qt";
#endif
}
2019-09-03 18:15:05 +03:00
void DeployCore::printVersion() {
2019-08-12 18:05:28 +03:00
qInfo() << "CQtDeployer: " + getAppVersion();
qInfo() << "Qt: " + getQtVersion();
}
2019-09-15 11:31:31 +03:00
bool DeployCore::isExecutable(const QFileInfo& file) {
2019-08-28 17:23:49 +03:00
auto sufix = file.completeSuffix();
return sufix.contains("exe", Qt::CaseInsensitive) || sufix.contains("run", Qt::CaseInsensitive) || sufix.isEmpty();
}
2019-09-08 13:37:33 +03:00
int DeployCore::find(const QString &str, const QStringList &list) {
for (int i = 0 ; i < list.size(); ++i) {
if (list[i].contains(str))
return i;
}
return -1;
}
bool DeployCore::isLib(const QFileInfo &file) {
return file.completeSuffix().contains("so", Qt::CaseInsensitive)
|| file.completeSuffix().contains("dll", Qt::CaseInsensitive);
}
2019-09-14 15:51:23 +03:00
bool DeployCore::isPath(const QString &path) {
return path.contains('/') || path.contains('\\');
}
2019-09-03 18:15:05 +03:00
MSVCVersion DeployCore::getMSVC(const QString &_qmake) {
2019-03-28 10:32:46 +03:00
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);
}
2019-09-03 18:15:05 +03:00
QString DeployCore::getVCredist(const QString &_qmake) {
2019-03-28 10:32:46 +03:00
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 "";
}
2019-09-03 18:15:05 +03:00
QString DeployCore::getMSVCName(MSVCVersion msvc) {
2019-03-28 12:36:21 +03:00
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 "";
}
2019-09-03 18:15:05 +03:00
QString DeployCore::getMSVCVersion(MSVCVersion msvc) {
2019-03-28 12:36:21 +03:00
if (msvc | MSVCVersion::MSVC_x32) {
return "x86";
} else if (msvc | MSVCVersion::MSVC_x64) {
return "x64";
}
2019-03-28 10:32:46 +03:00
return "";
}
2019-09-03 18:15:05 +03:00
bool DeployCore::isQtLib(const QString &lib) {
2018-12-09 17:35:07 +03:00
QFileInfo info(lib);
2019-09-08 13:37:33 +03:00
return !_config->qtDir.isEmpty() && info.absoluteFilePath().contains(_config->qtDir);
2018-12-09 17:35:07 +03:00
}
2019-09-03 18:15:05 +03:00
bool DeployCore::isExtraLib(const QString &lib) {
2018-12-09 17:35:07 +03:00
QFileInfo info(lib);
2019-09-08 13:37:33 +03:00
for (auto i : _config->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;
}