cmake/Source/CPack/IFW/cmCPackIFWInstaller.cxx

428 lines
12 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmCPackIFWInstaller.h"
2016-10-30 18:24:19 +01:00
#include "CPack/cmCPackGenerator.h"
#include "CPack/cmCPackLog.h"
2015-04-27 22:25:09 +02:00
#include "cmCPackIFWGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmCPackIFWPackage.h"
#include "cmCPackIFWRepository.h"
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
#include "cmXMLParser.h"
#include "cmXMLWriter.h"
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
#include <cmConfigure.h>
#include <utility>
2015-04-27 22:25:09 +02:00
#ifdef cmCPackLogger
2016-07-09 11:21:54 +02:00
#undef cmCPackLogger
2015-04-27 22:25:09 +02:00
#endif
2016-07-09 11:21:54 +02:00
#define cmCPackLogger(logType, msg) \
do { \
std::ostringstream cmCPackLog_msg; \
cmCPackLog_msg << msg; \
if (Generator) { \
Generator->Logger->Log(logType, __FILE__, __LINE__, \
cmCPackLog_msg.str().c_str()); \
} \
} while (0)
cmCPackIFWInstaller::cmCPackIFWInstaller()
2016-10-30 18:24:19 +01:00
: Generator(CM_NULLPTR)
2015-04-27 22:25:09 +02:00
{
}
2016-07-09 11:21:54 +02:00
const char* cmCPackIFWInstaller::GetOption(const std::string& op) const
2015-04-27 22:25:09 +02:00
{
2016-10-30 18:24:19 +01:00
return Generator ? Generator->GetOption(op) : CM_NULLPTR;
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
bool cmCPackIFWInstaller::IsOn(const std::string& op) const
2015-04-27 22:25:09 +02:00
{
return Generator ? Generator->IsOn(op) : false;
}
2016-07-09 11:21:54 +02:00
bool cmCPackIFWInstaller::IsVersionLess(const char* version)
2015-08-17 11:37:30 +02:00
{
return Generator ? Generator->IsVersionLess(version) : false;
}
2016-07-09 11:21:54 +02:00
bool cmCPackIFWInstaller::IsVersionGreater(const char* version)
2015-08-17 11:37:30 +02:00
{
return Generator ? Generator->IsVersionGreater(version) : false;
}
2016-07-09 11:21:54 +02:00
bool cmCPackIFWInstaller::IsVersionEqual(const char* version)
2015-08-17 11:37:30 +02:00
{
return Generator ? Generator->IsVersionEqual(version) : false;
}
2015-04-27 22:25:09 +02:00
void cmCPackIFWInstaller::ConfigureFromOptions()
{
// Name;
if (const char* optIFW_PACKAGE_NAME =
2016-07-09 11:21:54 +02:00
this->GetOption("CPACK_IFW_PACKAGE_NAME")) {
2015-04-27 22:25:09 +02:00
Name = optIFW_PACKAGE_NAME;
2016-07-09 11:21:54 +02:00
} else if (const char* optPACKAGE_NAME =
this->GetOption("CPACK_PACKAGE_NAME")) {
2015-04-27 22:25:09 +02:00
Name = optPACKAGE_NAME;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
Name = "Your package";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Title;
if (const char* optIFW_PACKAGE_TITLE =
2016-07-09 11:21:54 +02:00
GetOption("CPACK_IFW_PACKAGE_TITLE")) {
2015-04-27 22:25:09 +02:00
Title = optIFW_PACKAGE_TITLE;
2016-07-09 11:21:54 +02:00
} else if (const char* optPACKAGE_DESCRIPTION_SUMMARY =
GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY")) {
2015-04-27 22:25:09 +02:00
Title = optPACKAGE_DESCRIPTION_SUMMARY;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
Title = "Your package description";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Version;
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_PACKAGE_VERSION")) {
2015-04-27 22:25:09 +02:00
Version = option;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
Version = "1.0.0";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Publisher
2016-07-09 11:21:54 +02:00
if (const char* optIFW_PACKAGE_PUBLISHER =
GetOption("CPACK_IFW_PACKAGE_PUBLISHER")) {
2015-04-27 22:25:09 +02:00
Publisher = optIFW_PACKAGE_PUBLISHER;
2016-07-09 11:21:54 +02:00
} else if (const char* optPACKAGE_VENDOR =
GetOption("CPACK_PACKAGE_VENDOR")) {
2015-04-27 22:25:09 +02:00
Publisher = optPACKAGE_VENDOR;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// ProductUrl
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_PRODUCT_URL")) {
2015-04-27 22:25:09 +02:00
ProductUrl = option;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// ApplicationIcon
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_PACKAGE_ICON")) {
if (cmSystemTools::FileExists(option)) {
2015-04-27 22:25:09 +02:00
InstallerApplicationIcon = option;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// TODO: implement warning
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// WindowIcon
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_PACKAGE_WINDOW_ICON")) {
if (cmSystemTools::FileExists(option)) {
2015-04-27 22:25:09 +02:00
InstallerWindowIcon = option;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// TODO: implement warning
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Logo
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_PACKAGE_LOGO")) {
if (cmSystemTools::FileExists(option)) {
2015-04-27 22:25:09 +02:00
Logo = option;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// TODO: implement warning
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2015-08-17 11:37:30 +02:00
// Start menu
if (const char* optIFW_START_MENU_DIR =
2016-07-09 11:21:54 +02:00
this->GetOption("CPACK_IFW_PACKAGE_START_MENU_DIRECTORY")) {
2015-08-17 11:37:30 +02:00
StartMenuDir = optIFW_START_MENU_DIR;
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
StartMenuDir = Name;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2015-04-27 22:25:09 +02:00
// Default target directory for installation
if (const char* optIFW_TARGET_DIRECTORY =
2016-07-09 11:21:54 +02:00
GetOption("CPACK_IFW_TARGET_DIRECTORY")) {
2015-04-27 22:25:09 +02:00
TargetDir = optIFW_TARGET_DIRECTORY;
2016-07-09 11:21:54 +02:00
} else if (const char* optPACKAGE_INSTALL_DIRECTORY =
GetOption("CPACK_PACKAGE_INSTALL_DIRECTORY")) {
2015-04-27 22:25:09 +02:00
TargetDir = "@ApplicationsDir@/";
TargetDir += optPACKAGE_INSTALL_DIRECTORY;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
TargetDir = "@RootDir@/usr/local";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Default target directory for installation with administrator rights
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_ADMIN_TARGET_DIRECTORY")) {
2015-04-27 22:25:09 +02:00
AdminTargetDir = option;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Maintenance tool
2016-07-09 11:21:54 +02:00
if (const char* optIFW_MAINTENANCE_TOOL =
this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_NAME")) {
2015-08-17 11:37:30 +02:00
MaintenanceToolName = optIFW_MAINTENANCE_TOOL;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Maintenance tool ini file
2016-07-09 11:21:54 +02:00
if (const char* optIFW_MAINTENANCE_TOOL_INI =
this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_INI_FILE")) {
2015-08-17 11:37:30 +02:00
MaintenanceToolIniFile = optIFW_MAINTENANCE_TOOL_INI;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Allow non-ASCII characters
2016-07-09 11:21:54 +02:00
if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
if (IsOn("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
2015-08-17 11:37:30 +02:00
AllowNonAsciiCharacters = "true";
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
AllowNonAsciiCharacters = "false";
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Space in path
2016-07-09 11:21:54 +02:00
if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
if (IsOn("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
2015-08-17 11:37:30 +02:00
AllowSpaceInPath = "true";
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
AllowSpaceInPath = "false";
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Control script
2016-07-09 11:21:54 +02:00
if (const char* optIFW_CONTROL_SCRIPT =
this->GetOption("CPACK_IFW_PACKAGE_CONTROL_SCRIPT")) {
2015-08-17 11:37:30 +02:00
ControlScript = optIFW_CONTROL_SCRIPT;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
// Resources
if (const char* optIFW_PACKAGE_RESOURCES =
this->GetOption("CPACK_IFW_PACKAGE_RESOURCES")) {
Resources.clear();
cmSystemTools::ExpandListArgument(optIFW_PACKAGE_RESOURCES, Resources);
}
2015-04-27 22:25:09 +02:00
}
2016-10-30 18:24:19 +01:00
/** \class cmCPackIFWResourcesParser
* \brief Helper class that parse resources form .qrc (Qt)
*/
class cmCPackIFWResourcesParser : public cmXMLParser
{
public:
cmCPackIFWResourcesParser(cmCPackIFWInstaller* i)
: installer(i)
, file(false)
{
path = i->Directory + "/resources";
}
bool ParseResource(size_t r)
{
hasFiles = false;
hasErrors = false;
basePath = cmSystemTools::GetFilenamePath(installer->Resources[r].data());
ParseFile(installer->Resources[r].data());
return hasFiles && !hasErrors;
}
cmCPackIFWInstaller* installer;
bool file, hasFiles, hasErrors;
std::string path, basePath;
protected:
void StartElement(const std::string& name, const char** /*atts*/) CM_OVERRIDE
{
file = name == "file" ? true : false;
if (file) {
hasFiles = true;
}
}
void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
{
if (file) {
std::string content(data, data + length);
content = cmSystemTools::TrimWhitespace(content);
std::string source = basePath + "/" + content;
std::string destination = path + "/" + content;
if (!cmSystemTools::CopyFileIfDifferent(source.data(),
destination.data())) {
hasErrors = true;
}
}
}
void EndElement(const std::string& /*name*/) CM_OVERRIDE {}
};
2015-04-27 22:25:09 +02:00
void cmCPackIFWInstaller::GenerateInstallerFile()
{
// Lazy directory initialization
2016-07-09 11:21:54 +02:00
if (Directory.empty() && Generator) {
2015-04-27 22:25:09 +02:00
Directory = Generator->toplevel;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Output stream
2016-07-09 11:21:54 +02:00
cmGeneratedFileStream fout((Directory + "/config/config.xml").data());
cmXMLWriter xout(fout);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
xout.StartDocument();
2015-08-17 11:37:30 +02:00
WriteGeneratedByToStrim(xout);
2016-07-09 11:21:54 +02:00
xout.StartElement("Installer");
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
xout.Element("Name", Name);
xout.Element("Version", Version);
xout.Element("Title", Title);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!Publisher.empty()) {
xout.Element("Publisher", Publisher);
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!ProductUrl.empty()) {
xout.Element("ProductUrl", ProductUrl);
}
2015-04-27 22:25:09 +02:00
// ApplicationIcon
2016-07-09 11:21:54 +02:00
if (!InstallerApplicationIcon.empty()) {
2015-04-27 22:25:09 +02:00
std::string name =
cmSystemTools::GetFilenameName(InstallerApplicationIcon);
std::string path = Directory + "/config/" + name;
name = cmSystemTools::GetFilenameWithoutExtension(name);
2016-07-09 11:21:54 +02:00
cmsys::SystemTools::CopyFileIfDifferent(InstallerApplicationIcon.data(),
path.data());
xout.Element("InstallerApplicationIcon", name);
}
2015-04-27 22:25:09 +02:00
// WindowIcon
2016-07-09 11:21:54 +02:00
if (!InstallerWindowIcon.empty()) {
2015-04-27 22:25:09 +02:00
std::string name = cmSystemTools::GetFilenameName(InstallerWindowIcon);
std::string path = Directory + "/config/" + name;
2016-07-09 11:21:54 +02:00
cmsys::SystemTools::CopyFileIfDifferent(InstallerWindowIcon.data(),
path.data());
xout.Element("InstallerWindowIcon", name);
}
2015-04-27 22:25:09 +02:00
// Logo
2016-07-09 11:21:54 +02:00
if (!Logo.empty()) {
2015-04-27 22:25:09 +02:00
std::string name = cmSystemTools::GetFilenameName(Logo);
std::string path = Directory + "/config/" + name;
cmsys::SystemTools::CopyFileIfDifferent(Logo.data(), path.data());
2016-07-09 11:21:54 +02:00
xout.Element("Logo", name);
}
2015-04-27 22:25:09 +02:00
2015-08-17 11:37:30 +02:00
// Start menu
2016-07-09 11:21:54 +02:00
if (!IsVersionLess("2.0")) {
xout.Element("StartMenuDir", StartMenuDir);
}
2015-08-17 11:37:30 +02:00
// Target dir
2016-07-09 11:21:54 +02:00
if (!TargetDir.empty()) {
xout.Element("TargetDir", TargetDir);
}
2015-04-27 22:25:09 +02:00
2015-08-17 11:37:30 +02:00
// Admin target dir
2016-07-09 11:21:54 +02:00
if (!AdminTargetDir.empty()) {
xout.Element("AdminTargetDir", AdminTargetDir);
}
2015-04-27 22:25:09 +02:00
// Remote repositories
2016-07-09 11:21:54 +02:00
if (!RemoteRepositories.empty()) {
xout.StartElement("RemoteRepositories");
for (RepositoriesVector::iterator rit = RemoteRepositories.begin();
rit != RemoteRepositories.end(); ++rit) {
(*rit)->WriteRepositoryConfig(xout);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
xout.EndElement();
}
2015-04-27 22:25:09 +02:00
2015-08-17 11:37:30 +02:00
// Maintenance tool
2016-07-09 11:21:54 +02:00
if (!IsVersionLess("2.0") && !MaintenanceToolName.empty()) {
xout.Element("MaintenanceToolName", MaintenanceToolName);
}
2015-08-17 11:37:30 +02:00
// Maintenance tool ini file
2016-07-09 11:21:54 +02:00
if (!IsVersionLess("2.0") && !MaintenanceToolIniFile.empty()) {
xout.Element("MaintenanceToolIniFile", MaintenanceToolIniFile);
}
2015-08-17 11:37:30 +02:00
// Different allows
2016-07-09 11:21:54 +02:00
if (IsVersionLess("2.0")) {
2015-08-17 11:37:30 +02:00
// CPack IFW default policy
2016-07-09 11:21:54 +02:00
xout.Comment("CPack IFW default policy for QtIFW less 2.0");
xout.Element("AllowNonAsciiCharacters", "true");
xout.Element("AllowSpaceInPath", "true");
} else {
if (!AllowNonAsciiCharacters.empty()) {
xout.Element("AllowNonAsciiCharacters", AllowNonAsciiCharacters);
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
if (!AllowSpaceInPath.empty()) {
xout.Element("AllowSpaceInPath", AllowSpaceInPath);
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
// Control script (copy to config dir)
2016-07-09 11:21:54 +02:00
if (!IsVersionLess("2.0") && !ControlScript.empty()) {
2015-08-17 11:37:30 +02:00
std::string name = cmSystemTools::GetFilenameName(ControlScript);
std::string path = Directory + "/config/" + name;
cmsys::SystemTools::CopyFileIfDifferent(ControlScript.data(), path.data());
2016-07-09 11:21:54 +02:00
xout.Element("ControlScript", name);
}
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
// Resources (copy to resources dir)
if (!Resources.empty()) {
std::vector<std::string> resources;
cmCPackIFWResourcesParser parser(this);
for (size_t i = 0; i < Resources.size(); i++) {
if (parser.ParseResource(i)) {
std::string name = cmSystemTools::GetFilenameName(Resources[i]);
std::string path = Directory + "/resources/" + name;
cmsys::SystemTools::CopyFileIfDifferent(Resources[i].data(),
path.data());
resources.push_back(name);
} else {
cmCPackLogger(cmCPackLog::LOG_WARNING, "Can't copy resources from \""
<< Resources[i] << "\". Resource will be skipped."
<< std::endl);
}
}
Resources = resources;
}
2016-07-09 11:21:54 +02:00
xout.EndElement();
xout.EndDocument();
2015-04-27 22:25:09 +02:00
}
void cmCPackIFWInstaller::GeneratePackageFiles()
{
2016-07-09 11:21:54 +02:00
if (Packages.empty() || Generator->IsOnePackage()) {
2015-04-27 22:25:09 +02:00
// Generate default package
cmCPackIFWPackage package;
package.Generator = Generator;
package.Installer = this;
// Check package group
2016-07-09 11:21:54 +02:00
if (const char* option = GetOption("CPACK_IFW_PACKAGE_GROUP")) {
2015-04-27 22:25:09 +02:00
package.ConfigureFromGroup(option);
package.ForcedInstallation = "true";
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
package.ConfigureFromOptions();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
package.GeneratePackageFile();
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Generate packages meta information
2016-07-09 11:21:54 +02:00
for (PackagesMap::iterator pit = Packages.begin(); pit != Packages.end();
++pit) {
2015-04-27 22:25:09 +02:00
cmCPackIFWPackage* package = pit->second;
package->GeneratePackageFile();
2015-08-17 11:37:30 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmCPackIFWInstaller::WriteGeneratedByToStrim(cmXMLWriter& xout)
2015-08-17 11:37:30 +02:00
{
2016-10-30 18:24:19 +01:00
if (Generator) {
2016-07-09 11:21:54 +02:00
Generator->WriteGeneratedByToStrim(xout);
2016-10-30 18:24:19 +01:00
}
2015-04-27 22:25:09 +02:00
}