CQtDeployer/Deploy/extracter.cpp

483 lines
15 KiB
C++
Raw Normal View History

2019-09-08 13:37:33 +03:00
/*
* Copyright (C) 2018-2021 QuasarApp.
2019-09-08 13:37:33 +03:00
* 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>
2020-01-12 16:43:03 +03:00
#include <cstdio>
2019-09-08 13:37:33 +03:00
2020-01-12 16:43:03 +03:00
#include <cassert>
2019-09-08 13:37:33 +03:00
#include <fstream>
bool Extracter::deployMSVC() {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("try deploy msvc",
QuasarAppUtils::Info);
2019-12-20 17:25:20 +03:00
auto msvcInstaller = DeployCore::getVCredist(DeployCore::_config->qtDir.getBins());
2019-09-08 13:37:33 +03:00
if (msvcInstaller.isEmpty()) {
return false;
}
2019-12-14 17:38:43 +03:00
return _fileManager->copyFile(msvcInstaller, DeployCore::_config->getTargetDir());
2019-09-08 13:37:33 +03:00
}
2020-01-27 20:02:25 +03:00
bool Extracter::isWebEngine(const QString &package) const {
auto qtModules = _packageDependencyes.value(package).qtModules();
2020-11-28 23:22:59 +03:00
return static_cast<quint64>(qtModules) & static_cast<quint64>(DeployCore::QtModule::QtWebEngineCoreModule);
2019-12-15 18:30:24 +03:00
}
2019-09-15 20:31:31 +03:00
2019-12-15 18:30:24 +03:00
bool Extracter::extractWebEngine() {
2019-11-01 15:12:03 +03:00
2019-12-15 18:30:24 +03:00
auto cnf = DeployCore::_config;
2020-01-27 20:02:25 +03:00
for (auto i = cnf->packages().cbegin(); i != cnf->packages().cend(); ++i) {
const auto &package = i.key();
2020-11-28 20:02:51 +03:00
2020-01-27 20:02:25 +03:00
if (isWebEngine(package)) {
2020-04-04 16:21:44 +03:00
auto webEngeneBin = cnf->qtDir.getLibexecs();
2020-04-04 16:21:44 +03:00
if (cnf->qtDir.getQtPlatform() & Platform::Unix) {
2019-12-15 18:30:24 +03:00
webEngeneBin += "/QtWebEngineProcess";
2020-10-13 14:07:51 +03:00
} else if (cnf->qtDir.getQtPlatform() & Platform::Win) {
2019-12-15 18:30:24 +03:00
webEngeneBin += "/QtWebEngineProcess.exe";
}
2020-12-03 20:00:14 +03:00
auto destWebEngine = cnf->getTargetDir() + "/" + package + cnf->getDistroFromPackage(package).getBinOutDir();
auto resOut = cnf->getTargetDir() + "/" + package + cnf->getDistroFromPackage(package).getResOutDir();
auto libOut = cnf->getTargetDir() + "/" + package + cnf->getDistroFromPackage(package).getLibOutDir();
2020-04-04 16:21:44 +03:00
auto res = cnf->qtDir.getResources();
if (!_fileManager->copyFiles(angleGLLibs(), libOut)) {
return false;
}
2019-12-15 18:30:24 +03:00
if (!_fileManager->copyFile(webEngeneBin, destWebEngine)) {
return false;
}
if (!_fileManager->copyFolder(res, resOut)) {
return false;
}
2019-09-15 20:31:31 +03:00
}
}
return true;
}
QList<QString> Extracter::angleGLLibs() {
auto cnf = DeployCore::_config;
if (cnf->qtDir.getQtPlatform() & Platform::Win) {
return {
2020-07-22 16:04:03 +03:00
cnf->qtDir.getBins() + "/d3dcompiler_47.dll",
cnf->qtDir.getBins() + "/libEGL.dll",
cnf->qtDir.getBins() + "/libGLESv2.dll",
};
}
return {};
}
2019-09-14 15:51:23 +03:00
void Extracter::extractAllTargets() {
2019-12-15 18:30:24 +03:00
auto cfg = DeployCore::_config;
2020-01-27 20:02:25 +03:00
for (auto i = cfg->packages().cbegin(); i != cfg->packages().cend(); ++i) {
auto &dep = _packageDependencyes[i.key()];
2019-09-08 13:37:33 +03:00
2020-01-12 16:43:03 +03:00
for (const auto &target : i.value().targets()) {
extract(target, &dep);
}
}
}
void Extracter::extractExtraDataTargets() {
auto cfg = DeployCore::_config;
for (auto i = cfg->packages().cbegin(); i != cfg->packages().cend(); ++i) {
auto &dep = _packageDependencyes[i.key()];
for (const auto &target : i.value().extraData()) {
dep.addExtraData(target);
2019-12-15 18:30:24 +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::clear() {
2019-09-08 13:37:33 +03:00
if (QuasarAppUtils::Params::isEndable("clear") ||
QuasarAppUtils::Params::isEndable("force-clear")) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("clear old data",
QuasarAppUtils::Info);
2019-12-15 18:30:24 +03:00
_fileManager->clear(DeployCore::_config->getTargetDir(),
2019-09-08 13:37:33 +03:00
QuasarAppUtils::Params::isEndable("force-clear"));
}
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
void Extracter::copyExtraPlugins(const QString& package) {
2020-07-04 23:08:41 +03:00
QFileInfo info;
2020-07-02 16:10:11 +03:00
auto cnf = DeployCore::_config;
auto targetPath = cnf->getTargetDir() + "/" + package;
2020-07-02 16:10:11 +03:00
auto distro = cnf->getDistroFromPackage(package);
for (auto extraPlugin : distro.extraPlugins()) {
info.setFile(extraPlugin);
2020-07-07 11:05:27 +03:00
if (info.isFile()) {
if (!_fileManager->copyFile(info.absoluteFilePath(),
targetPath + distro.getPluginsOutDir())) {
2020-07-07 11:05:27 +03:00
QuasarAppUtils::Params::log("fail to copy extra plugin from:" + info.absoluteFilePath() +
" to: " + targetPath + distro.getPluginsOutDir(),
QuasarAppUtils::Warning);
}
2020-07-02 16:10:11 +03:00
extractPluginLib(info.absoluteFilePath(), package);
2020-07-07 11:05:27 +03:00
continue;
}
if (info.isDir()) {
QStringList plugins;
if (!_fileManager->copyFolder(info.absoluteFilePath(),
2020-08-16 08:04:01 -07:00
targetPath + distro.getPluginsOutDir() + info.fileName(),
DeployCore::debugExtensions(),
2020-08-16 08:04:01 -07:00
&plugins)) {
2020-07-07 11:05:27 +03:00
QuasarAppUtils::Params::log("fail to copy extra plugin from:" + info.absoluteFilePath() +
" to: " + targetPath + distro.getPluginsOutDir(),
QuasarAppUtils::Warning);
}
for (const auto& plugin : plugins) {
extractPluginLib(plugin, package);
}
}
2020-07-02 16:10:11 +03:00
}
}
2019-12-15 18:30:24 +03:00
void Extracter::extractPlugins() {
auto cnf = DeployCore::_config;
2019-09-08 13:37:33 +03:00
2020-07-05 13:53:15 +03:00
_pluginsParser->initDeployPluginsList();
2020-01-27 20:02:25 +03:00
for (auto i = cnf->packages().cbegin(); i != cnf->packages().cend(); ++i) {
auto targetPath = cnf->getTargetDir() + "/" + i.key();
2020-01-27 20:02:25 +03:00
auto distro = cnf->getDistroFromPackage(i.key());
2019-12-15 18:30:24 +03:00
QStringList plugins;
QStringList listItems;
2020-07-02 16:00:34 +03:00
_pluginsParser->scan(cnf->qtDir.getPlugins(), plugins, _packageDependencyes[i.key()].qtModules(), i.key());
_fileManager->copyFiles(plugins, targetPath + distro.getPluginsOutDir(), 1,
DeployCore::debugExtensions(), &listItems);
for (const auto &item : listItems) {
extractPluginLib(item, i.key());
2020-07-02 16:00:34 +03:00
}
copyExtraPlugins(i.key());
2019-12-15 18:30:24 +03:00
}
2019-09-14 15:51:23 +03:00
}
2019-09-08 13:37:33 +03:00
2020-01-27 20:02:25 +03:00
void Extracter::copyLibs(const QSet<QString> &files, const QString& package) {
2019-12-15 18:30:24 +03:00
auto cnf = DeployCore::_config;
2020-02-27 10:50:11 +03:00
auto targetPath = cnf->getTargetDir() + "/" + package;
2020-01-27 20:02:25 +03:00
auto distro = cnf->getDistroFromPackage(package);
2019-12-15 18:30:24 +03:00
2020-01-12 16:43:03 +03:00
for (const auto &file : files) {
2019-12-15 18:30:24 +03:00
if (!_fileManager->smartCopyFile(file, targetPath + distro.getLibOutDir())) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log(file + " not copied");
2019-12-15 18:30:24 +03:00
}
2019-09-08 13:37:33 +03:00
}
2019-12-15 18:30:24 +03:00
}
2019-09-08 13:37:33 +03:00
void Extracter::copyExtraData(const QSet<QString> &files, const QString &package) {
auto cnf = DeployCore::_config;
auto targetPath = cnf->getTargetDir() + "/" + package;
auto distro = cnf->getDistroFromPackage(package);
for (const auto &file : files) {
if (!_fileManager->cp(file, targetPath + distro.getExtraDataOutDir())) {
QuasarAppUtils::Params::log(file + " not copied");
}
}
}
2019-12-15 18:30:24 +03:00
void Extracter::copyFiles() {
auto cnf = DeployCore::_config;
2020-01-27 20:02:25 +03:00
for (auto i = cnf->packages().cbegin(); i != cnf->packages().cend(); ++i) {
2019-12-15 18:30:24 +03:00
2020-01-27 20:02:25 +03:00
copyLibs(_packageDependencyes[i.key()].neadedLibs(), i.key());
2019-12-15 18:30:24 +03:00
if (QuasarAppUtils::Params::isEndable("deploySystem")) {
2020-01-27 20:02:25 +03:00
copyLibs(_packageDependencyes[i.key()].systemLibs(), i.key());
2019-12-15 18:30:24 +03:00
}
2019-12-15 18:30:24 +03:00
if (!QuasarAppUtils::Params::isEndable("noStrip") && !_fileManager->strip(cnf->getTargetDir())) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("strip failed!");
2019-12-15 18:30:24 +03:00
}
copyExtraData(_packageDependencyes[i.key()].extraData(), i.key());
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-12-15 18:30:24 +03:00
void Extracter::copyTr() {
2019-09-08 13:37:33 +03:00
if (!QuasarAppUtils::Params::isEndable("noTranslations")) {
2019-12-15 18:30:24 +03:00
auto cnf = DeployCore::_config;
2020-01-27 20:02:25 +03:00
for (auto i = cnf->packages().cbegin(); i != cnf->packages().cend(); ++i) {
if (!copyTranslations(DeployCore::extractTranslation(_packageDependencyes[i.key()].neadedLibs()),
2019-12-15 18:30:24 +03:00
i.key())) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("Failed to copy standard Qt translations",
QuasarAppUtils::Warning);
2019-12-15 18:30:24 +03:00
}
2019-09-08 13:37:33 +03:00
}
2019-12-15 18:30:24 +03:00
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() {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("target deploy started!!",
QuasarAppUtils::Info);
2019-09-14 15:51:23 +03:00
clear();
_cqt->smartMoveTargets();
2020-02-26 14:29:04 +03:00
_scaner->setEnvironment(DeployCore::_config->envirement.environmentList());
2019-09-14 15:51:23 +03:00
extractAllTargets();
extractExtraDataTargets();
2019-09-14 15:51:23 +03:00
if (DeployCore::_config->deployQml && !extractQml()) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("qml not extacted!",
QuasarAppUtils::Error);
2019-09-14 15:51:23 +03:00
}
extractPlugins();
copyFiles();
copyTr();
2019-09-15 20:31:31 +03:00
if (!extractWebEngine()) {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("deploy webEngine failed", QuasarAppUtils::Error);
2019-09-15 20:31:31 +03:00
}
2019-09-14 15:51:23 +03:00
if (!deployMSVC()) {
2020-07-09 00:27:30 +03:00
QuasarAppUtils::Params::log("deploy msvc failed", QuasarAppUtils::Warning);
2019-09-14 15:51:23 +03:00
}
2019-09-15 17:38:47 +03:00
_metaFileManager->createRunMetaFiles();
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("deploy done!",
QuasarAppUtils::Info);
2019-09-08 13:37:33 +03:00
}
2020-01-27 20:02:25 +03:00
bool Extracter::copyTranslations(const QStringList &list, const QString& package) {
2019-09-08 13:37:33 +03:00
2019-12-15 18:30:24 +03:00
auto cnf = DeployCore::_config;
2019-12-23 10:12:23 +03:00
QDir dir(cnf->qtDir.getTranslations());
2019-09-08 13:37:33 +03:00
if (!dir.exists() || list.isEmpty()) {
return false;
}
QStringList filters;
2020-01-12 16:43:03 +03:00
for (const auto &i: list) {
2019-09-08 13:37:33 +03:00
filters.push_back("*" + i + "*");
}
auto listItems = dir.entryInfoList(filters, QDir::Files | QDir::NoDotAndDotDot);
2020-02-27 10:50:11 +03:00
auto targetPath = cnf->getTargetDir() + "/" + package;
2020-01-27 20:02:25 +03:00
auto distro = cnf->getDistroFromPackage(package);
2019-12-15 18:30:24 +03:00
2020-01-12 16:43:03 +03:00
for (const auto &i: listItems) {
2019-12-15 18:30:24 +03:00
_fileManager->copyFile(i.absoluteFilePath(), targetPath + distro.getTrOutDir());
2019-09-08 13:37:33 +03:00
}
2020-01-27 20:02:25 +03:00
if (isWebEngine(package)) {
2019-12-15 18:30:24 +03:00
auto trOut = targetPath + distro.getTrOutDir();
2020-04-04 16:21:44 +03:00
auto tr = cnf->qtDir.getTranslations() + "/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) {
2019-09-08 13:37:33 +03:00
QFileInfoList files;
QDir dir(dirpath);
auto list = dir.entryInfoList( QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
2020-01-12 16:43:03 +03:00
for (const auto & item :list) {
2019-09-08 13:37:33 +03:00
if (item.isFile()) {
if (item.fileName().contains(name)) {
files += item;
}
} else {
files += findFilesInsideDir(name, item.absoluteFilePath());
}
}
return files;
}
2019-12-08 13:57:20 +03:00
void Extracter::extractLib(const QString &file,
DependencyMap* depMap,
const QString& mask) {
assert(depMap);
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("extract lib :" + file,
QuasarAppUtils::Debug);
2019-09-08 13:37:33 +03:00
2019-11-13 18:18:44 +03:00
auto data = _scaner->scan(file);
2019-09-08 13:37:33 +03:00
2020-01-12 16:43:03 +03:00
for (const auto &line : data) {
2019-12-20 17:25:20 +03:00
if (mask.size() && !line.getName().contains(mask, ONLY_WIN_CASE_INSENSIATIVE)) {
continue;
}
2019-09-23 10:06:22 +03:00
if (DeployCore::_config->ignoreList.isIgnore(line)) {
2019-09-08 13:37:33 +03:00
continue;
}
2020-02-25 15:18:14 +03:00
if (line.getPriority() < LibPriority::SystemLib && !depMap->containsNeadedLib(line.fullPath())) {
2019-12-08 13:57:20 +03:00
depMap->addNeadedLib(line.fullPath());
2019-09-08 13:37:33 +03:00
} else if (QuasarAppUtils::Params::isEndable("deploySystem") &&
line.getPriority() >= LibPriority::SystemLib &&
!depMap->containsSysLib(line.fullPath())) {
2019-12-08 13:57:20 +03:00
depMap->addSystemLib(line.fullPath());
2019-09-08 13:37:33 +03:00
}
}
}
2020-02-29 16:42:18 +03:00
void Extracter::extractPluginLib(const QString& item, const QString& package) {
2020-10-16 23:07:29 +03:00
extract(item, &_packageDependencyes[package]);
2020-02-29 16:42:18 +03:00
}
bool Extracter::extractQml() {
2019-09-08 13:37:33 +03:00
if (QuasarAppUtils::Params::isEndable("qmlDir")) {
auto cnf = DeployCore::_config;
2019-09-08 13:37:33 +03:00
for (auto i = cnf->packages().cbegin(); i != cnf->packages().cend(); ++i) {
auto targetPath = cnf->getTargetDir() + "/" + i.key();
auto distro = cnf->getDistroFromPackage(i.key());
2019-09-08 13:37:33 +03:00
QStringList plugins;
QStringList listItems;
for (const auto &qmlInput: distro.qmlInput()) {
QFileInfo info(qmlInput);
if (!info.isDir()) {
QuasarAppUtils::Params::log("extract qml fail! qml source dir not exits or is not dir " + qmlInput,
QuasarAppUtils::Error);
continue;
}
QuasarAppUtils::Params::log("extractQmlFromSource " + info.absoluteFilePath());
if (!QFileInfo::exists(cnf->qtDir.getQmls())) {
QuasarAppUtils::Params::log("qml dir wrong!",
QuasarAppUtils::Warning);
continue;
}
QML ownQmlScaner(cnf->qtDir.getQmls());
if (!ownQmlScaner.scan(plugins, info.absoluteFilePath())) {
QuasarAppUtils::Params::log("qml scaner run failed!",
QuasarAppUtils::Error);
continue;
}
2020-01-02 19:27:40 +03:00
}
2019-09-08 13:37:33 +03:00
if (!_fileManager->copyFolder(cnf->qtDir.getQmls(),
targetPath + distro.getQmlOutDir(),
DeployCore::debugExtensions() ,
&listItems, &plugins)) {
return false;
2020-01-02 19:27:40 +03:00
}
2019-09-08 13:37:33 +03:00
for (const auto &item : listItems) {
extractPluginLib(item, i.key());
2020-01-02 19:27:40 +03:00
}
2019-09-08 13:37:33 +03:00
2019-11-05 18:24:55 +03:00
}
2019-12-15 18:30:24 +03:00
return true;
2019-09-08 13:37:33 +03:00
}
2020-01-12 16:43:03 +03:00
return false;
2019-09-08 13:37:33 +03:00
}
2019-12-08 13:57:20 +03:00
void Extracter::extract(const QString &file,
DependencyMap *depMap,
const QString &mask) {
assert(depMap);
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)) {
2019-12-08 13:57:20 +03:00
extractLib(file, depMap, mask);
2019-09-08 13:37:33 +03:00
} else {
2020-04-04 15:43:46 +03:00
QuasarAppUtils::Params::log("file with sufix " + sufix + " not supported!");
2019-09-08 13:37:33 +03:00
}
}
Extracter::Extracter(FileManager *fileManager, PluginsParser *pluginsParser, ConfigParser *cqt,
2019-11-13 18:18:44 +03:00
DependenciesScanner *scaner):
_scaner(scaner),
_fileManager(fileManager),
_pluginsParser(pluginsParser),
2019-11-13 18:18:44 +03:00
_cqt(cqt)
{
2019-09-08 13:37:33 +03:00
assert(_cqt);
2019-09-08 13:37:33 +03:00
assert(_fileManager);
assert(_pluginsParser);
2019-09-08 13:37:33 +03:00
assert(DeployCore::_config);
2019-09-15 17:38:47 +03:00
_metaFileManager = new MetaFileManager(_fileManager);
2019-09-08 13:37:33 +03:00
}