CQtDeployer/Deploy/extracter.cpp

407 lines
11 KiB
C++
Raw Normal View History

2019-09-08 13:37:33 +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.
*/
#include "extracter.h"
#include "deploycore.h"
#include "pluginsparser.h"
2019-09-14 13:59:11 +03:00
#include "configparser.h"
2019-09-15 17:38:47 +03:00
#include "metafilemanager.h"
#include "pathutils.h"
2019-09-08 13:37:33 +03:00
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QProcess>
#include <QRegularExpression>
#include <quasarapp.h>
#include <stdio.h>
#include <fstream>
bool Extracter::deployMSVC() {
qInfo () << "try deploy msvc";
2019-11-01 15:12:03 +03:00
auto msvcInstaller = DeployCore::getVCredist(DeployCore::_config->qtDir.bins);
2019-09-08 13:37:33 +03:00
if (msvcInstaller.isEmpty()) {
return false;
}
return _fileManager->copyFile(msvcInstaller, DeployCore::_config->targetDir);
}
2019-09-15 20:31:31 +03:00
bool Extracter::extractWebEngine() {
auto test = static_cast<quint64>(_qtModules) & static_cast<quint64>(DeployCore::QtModule::QtWebEngineModule);
if (test) {
2019-11-01 15:12:03 +03:00
auto webEngeneBin = DeployCore::_config->qtDir.libexecs;
if (DeployCore::_config->qtDir.qtPlatform & Platform::Unix) {
webEngeneBin += "/QtWebEngineProcess";
} else {
webEngeneBin += "/QtWebEngineProcess.exe";
}
2019-09-15 20:31:31 +03:00
auto destWebEngine = DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getBinOutDir();
auto resOut = DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getResOutDir();
2019-11-01 15:12:03 +03:00
auto res = DeployCore::_config->qtDir.resources;
2019-09-15 20:31:31 +03:00
if (!_fileManager->copyFile(webEngeneBin, destWebEngine)) {
return false;
}
2019-11-01 15:12:03 +03:00
2019-09-15 20:31:31 +03:00
if (!_fileManager->copyFolder(res, resOut)) {
return false;
}
}
return true;
}
2019-09-08 13:37:33 +03:00
bool Extracter::copyPlugin(const QString &plugin) {
QStringList listItems;
2019-09-15 17:38:47 +03:00
auto pluginPath = DeployCore::_config->targetDir +
DeployCore::_config->distroStruct.getPluginsOutDir() +
QFileInfo(plugin).fileName();
if (!_fileManager->copyFolder(plugin, pluginPath,
QStringList() << ".so.debug" << "d.dll", &listItems)) {
2019-09-08 13:37:33 +03:00
return false;
}
for (auto item : listItems) {
2019-11-05 18:24:55 +03:00
if (QuasarAppUtils::Params::isEndable("extractPlugins")) {
extract(item);
} else {
extract(item, "Qt");
}
2019-09-08 13:37:33 +03:00
}
return true;
}
2019-09-14 15:51:23 +03:00
void Extracter::copyExtraPlugins() {
2019-09-08 13:37:33 +03:00
QFileInfo info;
for (auto extraPlugin : DeployCore::_config->extraPlugins) {
if (!PathUtils::isPath(extraPlugin)) {
2019-11-01 15:12:03 +03:00
extraPlugin = DeployCore::_config->qtDir.plugins + "/" + extraPlugin;
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
info.setFile(extraPlugin);
2019-11-01 15:12:03 +03:00
if (info.isDir() && DeployCore::_config->qtDir.isQt(info.absoluteFilePath())) {
2019-09-15 18:01:07 +03:00
copyPlugin(info.absoluteFilePath());
2019-09-15 17:38:47 +03:00
2019-09-14 15:51:23 +03:00
} else if (info.exists()) {
2019-09-08 13:37:33 +03:00
_fileManager->copyFile(info.absoluteFilePath(),
2019-09-15 17:38:47 +03:00
DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getPluginsOutDir());
2019-11-05 18:24:55 +03:00
if (QuasarAppUtils::Params::isEndable("extractPlugins")) {
extract(info.absoluteFilePath());
} else {
extract(info.absoluteFilePath(), "Qt");
}
2019-09-08 13:37:33 +03:00
}
}
}
2019-09-14 15:51:23 +03:00
void Extracter::copyPlugins(const QStringList &list) {
2019-10-15 13:35:50 +03:00
for (auto plugin : list) {
2019-09-14 15:51:23 +03:00
if (!copyPlugin(plugin)) {
qWarning() << plugin << " not copied!";
}
}
copyExtraPlugins();
}
void Extracter::extractAllTargets() {
for (auto i = DeployCore::_config->targets.cbegin(); i != DeployCore::_config->targets.cend(); ++i) {
2019-11-13 18:18:44 +03:00
extract(i.key());
2019-09-14 15:51:23 +03:00
}
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::initQtModules() {
for (auto i: neadedLibs) {
DeployCore::addQtModule(_qtModules, i);
}
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::clear() {
2019-09-08 13:37:33 +03:00
if (QuasarAppUtils::Params::isEndable("clear") ||
QuasarAppUtils::Params::isEndable("force-clear")) {
qInfo() << "clear old data";
_fileManager->clear(DeployCore::_config->targetDir,
QuasarAppUtils::Params::isEndable("force-clear"));
}
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::extractPlugins()
{
PluginsParser pluginsParser;
2019-09-08 13:37:33 +03:00
QStringList plugins;
2019-11-01 15:12:03 +03:00
pluginsParser.scan(DeployCore::_config->qtDir.plugins, plugins, _qtModules);
2019-09-08 13:37:33 +03:00
copyPlugins(plugins);
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::copyFiles()
{
2019-09-25 12:32:05 +03:00
_fileManager->copyLibs(neadedLibs);
2019-09-08 13:37:33 +03:00
if (QuasarAppUtils::Params::isEndable("deploySystem")) {
2019-09-25 12:32:05 +03:00
_fileManager->copyLibs(systemLibs);
2019-09-08 13:37:33 +03:00
}
if (!QuasarAppUtils::Params::isEndable("noStrip") && !_fileManager->strip(DeployCore::_config->targetDir)) {
QuasarAppUtils::Params::verboseLog("strip failed!");
}
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::copyTr()
{
2019-09-08 13:37:33 +03:00
if (!QuasarAppUtils::Params::isEndable("noTranslations")) {
if (!copyTranslations(DeployCore::extractTranslation(neadedLibs))) {
QuasarAppUtils::Params::verboseLog("Failed to copy standard Qt translations",
QuasarAppUtils::Warning);
2019-09-08 13:37:33 +03:00
}
}
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
void Extracter::deploy() {
qInfo() << "target deploy started!!";
clear();
_cqt->smartMoveTargets();
2019-11-13 18:18:44 +03:00
_scaner->setEnvironment(DeployCore::_config->envirement.deployEnvironment());
2019-09-14 15:51:23 +03:00
extractAllTargets();
if (DeployCore::_config->deployQml && !extractQml()) {
qCritical() << "qml not extacted!";
}
initQtModules();
extractPlugins();
copyFiles();
copyTr();
2019-09-15 20:31:31 +03:00
if (!extractWebEngine()) {
QuasarAppUtils::Params::verboseLog("deploy webEngine failed", QuasarAppUtils::Error);
}
2019-09-14 15:51:23 +03:00
if (!deployMSVC()) {
QuasarAppUtils::Params::verboseLog("deploy msvc failed");
}
2019-09-15 17:38:47 +03:00
_metaFileManager->createRunMetaFiles();
2019-09-08 13:37:33 +03:00
qInfo() << "deploy done!";
}
bool Extracter::copyTranslations(QStringList list) {
2019-11-01 20:30:29 +03:00
QDir dir(DeployCore::_config->qtDir.translations);
2019-09-08 13:37:33 +03:00
if (!dir.exists() || list.isEmpty()) {
return false;
}
QStringList filters;
for (auto &&i: list) {
filters.push_back("*" + i + "*");
}
auto listItems = dir.entryInfoList(filters, QDir::Files | QDir::NoDotAndDotDot);
for (auto &&i: listItems) {
2019-09-15 17:38:47 +03:00
_fileManager->copyFile(i.absoluteFilePath(), DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getTrOutDir());
2019-09-08 13:37:33 +03:00
}
2019-09-17 14:35:40 +03:00
auto webEngine = static_cast<quint64>(_qtModules) & static_cast<quint64>(DeployCore::QtModule::QtWebEngineModule);
if (webEngine) {
auto trOut = DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getTrOutDir();
2019-11-01 15:12:03 +03:00
auto tr = DeployCore::_config->qtDir.translations + "/qtwebengine_locales";
2019-09-17 14:35:40 +03:00
_fileManager->copyFolder(tr, trOut + "/qtwebengine_locales");
}
2019-09-08 13:37:33 +03:00
return true;
}
QFileInfoList Extracter::findFilesInsideDir(const QString &name,
const QString &dirpath) {
QFileInfoList files;
QDir dir(dirpath);
auto list = dir.entryInfoList( QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
for (auto && item :list) {
if (item.isFile()) {
if (item.fileName().contains(name)) {
files += item;
}
} else {
files += findFilesInsideDir(name, item.absoluteFilePath());
}
}
return files;
}
void Extracter::extractLib(const QString &file, const QString& mask) {
2019-09-08 13:37:33 +03:00
qInfo() << "extract lib :" << file;
2019-11-13 18:18:44 +03:00
auto data = _scaner->scan(file);
2019-09-08 13:37:33 +03:00
for (auto &line : data) {
if (mask.size() && !line.getName().contains(mask)) {
continue;
}
2019-09-23 10:06:22 +03:00
if (DeployCore::_config->ignoreList.isIgnore(line)) {
2019-09-08 13:37:33 +03:00
continue;
}
if (line.getPriority() != LibPriority::SystemLib && !neadedLibs.contains(line.fullPath())) {
neadedLibs << line.fullPath();
} else if (QuasarAppUtils::Params::isEndable("deploySystem") &&
line.getPriority() == LibPriority::SystemLib &&
!systemLibs.contains(line.fullPath())) {
systemLibs << line.fullPath();
}
}
}
bool Extracter::extractQmlAll() {
2019-11-01 15:12:03 +03:00
if (!QFileInfo::exists(DeployCore::_config->qtDir.qmls)) {
2019-09-08 13:37:33 +03:00
qWarning() << "qml dir wrong!";
return false;
}
QStringList listItems;
2019-11-01 15:12:03 +03:00
if (!_fileManager->copyFolder(DeployCore::_config->qtDir.qmls,
2019-09-15 17:38:47 +03:00
DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getQmlOutDir(),
2019-10-15 13:35:50 +03:00
QStringList() << ".so.debug" << "d.dll" << ".pdb",
2019-09-08 13:37:33 +03:00
&listItems)) {
return false;
}
for (auto item : listItems) {
2019-11-05 18:24:55 +03:00
if (QuasarAppUtils::Params::isEndable("extractPlugins")) {
extract(item);
} else {
extract(item, "Qt");
}
2019-09-08 13:37:33 +03:00
}
return true;
}
bool Extracter::extractQmlFromSource(const QString& sourceDir) {
QFileInfo info(sourceDir);
if (!info.isDir()) {
qCritical() << "extract qml fail! qml source dir not exits or is not dir " << sourceDir;
return false;
}
QuasarAppUtils::Params::verboseLog("extractQmlFromSource " + info.absoluteFilePath());
2019-11-01 15:12:03 +03:00
if (!QFileInfo::exists(DeployCore::_config->qtDir.qmls)) {
2019-09-08 13:37:33 +03:00
qWarning() << "qml dir wrong!";
return false;
}
QStringList plugins;
QStringList listItems;
QStringList filter;
filter << ".so.debug" << "d.dll" << ".pdb";
2019-11-01 15:12:03 +03:00
QML ownQmlScaner(DeployCore::_config->qtDir.qmls);
2019-09-08 13:37:33 +03:00
if (!ownQmlScaner.scan(plugins, info.absoluteFilePath())) {
QuasarAppUtils::Params::verboseLog("qml scaner run failed!");
return false;
}
2019-11-01 15:12:03 +03:00
if (!_fileManager->copyFolder(DeployCore::_config->qtDir.qmls,
2019-09-15 17:38:47 +03:00
DeployCore::_config->targetDir + DeployCore::_config->distroStruct.getQmlOutDir(),
2019-09-08 13:37:33 +03:00
filter , &listItems, &plugins)) {
return false;
}
for (auto item : listItems) {
2019-11-05 18:24:55 +03:00
if (QuasarAppUtils::Params::isEndable("extractPlugins")) {
extract(item);
} else {
extract(item, "Qt");
}
2019-09-08 13:37:33 +03:00
}
return true;
}
bool Extracter::extractQml() {
if (QuasarAppUtils::Params::isEndable("qmlDir")) {
return extractQmlFromSource(
QuasarAppUtils::Params::getStrArg("qmlDir"));
} else if (QuasarAppUtils::Params::isEndable("allQmlDependes")) {
return extractQmlAll();
} else {
return false;
}
}
void Extracter::extract(const QString &file, const QString &mask) {
2019-09-08 13:37:33 +03:00
QFileInfo info(file);
auto sufix = info.completeSuffix();
2019-09-14 13:59:11 +03:00
if (sufix.compare("dll", Qt::CaseSensitive) == 0 ||
sufix.compare("exe", Qt::CaseSensitive) == 0 ||
2019-09-08 13:37:33 +03:00
sufix.isEmpty() || sufix.contains("so", Qt::CaseSensitive)) {
extractLib(file, mask);
2019-09-08 13:37:33 +03:00
} else {
QuasarAppUtils::Params::verboseLog("file with sufix " + sufix + " not supported!");
}
}
2019-11-13 18:18:44 +03:00
Extracter::Extracter(FileManager *fileManager, ConfigParser *cqt,
DependenciesScanner *scaner):
_scaner(scaner),
_fileManager(fileManager),
2019-11-13 18:18:44 +03:00
_cqt(cqt)
{
2019-09-08 13:37:33 +03:00
2019-09-14 15:51:23 +03:00
_qtModules = DeployCore::QtModule::NONE;
assert(_cqt);
2019-09-08 13:37:33 +03:00
assert(_fileManager);
assert(DeployCore::_config);
2019-09-15 17:38:47 +03:00
_metaFileManager = new MetaFileManager(_fileManager);
2019-09-08 13:37:33 +03:00
}