cmake/Source/CPack/WiX/cmCPackWIXGenerator.cxx

1142 lines
34 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. */
2013-03-16 19:13:01 +02:00
#include "cmCPackWIXGenerator.h"
2017-07-20 19:35:53 +02:00
#include "cmCPackComponentGroup.h"
#include "cmCPackLog.h"
#include "cmCryptoHash.h"
#include "cmGeneratedFileStream.h"
#include "cmInstalledFile.h"
#include "cmSystemTools.h"
#include "cmUuid.h"
2017-04-14 19:02:05 +02:00
#include <algorithm>
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
#include "cmWIXDirectoriesSourceWriter.h"
#include "cmWIXFeaturesSourceWriter.h"
#include "cmWIXFilesSourceWriter.h"
2013-03-16 19:13:01 +02:00
#include "cmWIXRichTextFormatWriter.h"
2016-07-09 11:21:54 +02:00
#include "cmWIXSourceWriter.h"
2013-03-16 19:13:01 +02:00
2017-07-20 19:35:53 +02:00
#include "cmsys/Directory.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
#include "cmsys/SystemTools.hxx"
2013-03-16 19:13:01 +02:00
#include <rpc.h> // for GUID generation
2016-07-09 11:21:54 +02:00
cmCPackWIXGenerator::cmCPackWIXGenerator()
: Patch(0)
2016-10-30 18:24:19 +01:00
, ComponentGuidType(cmWIXSourceWriter::WIX_GENERATED_GUID)
2014-08-03 19:52:23 +02:00
{
}
2015-04-27 22:25:09 +02:00
cmCPackWIXGenerator::~cmCPackWIXGenerator()
{
2016-07-09 11:21:54 +02:00
if (this->Patch) {
2015-04-27 22:25:09 +02:00
delete this->Patch;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2013-03-16 19:13:01 +02:00
int cmCPackWIXGenerator::InitializeInternal()
{
componentPackageMethod = ONE_PACKAGE;
2015-04-27 22:25:09 +02:00
this->Patch = new cmWIXPatch(this->Logger);
2013-03-16 19:13:01 +02:00
return this->Superclass::InitializeInternal();
}
2015-04-27 22:25:09 +02:00
bool cmCPackWIXGenerator::RunWiXCommand(std::string const& command)
2013-03-16 19:13:01 +02:00
{
2015-04-27 22:25:09 +02:00
std::string logFileName = this->CPackTopLevel + "/wix.log";
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Running WiX command: " << command
<< std::endl);
2013-03-16 19:13:01 +02:00
std::string output;
int returnValue = 0;
2016-07-09 11:21:54 +02:00
bool status = cmSystemTools::RunSingleCommand(command.c_str(), &output,
&output, &returnValue, 0,
cmSystemTools::OUTPUT_NONE);
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
cmsys::ofstream logFile(logFileName.c_str(), std::ios::app);
2013-03-16 19:13:01 +02:00
logFile << command << std::endl;
logFile << output;
logFile.close();
2016-07-09 11:21:54 +02:00
if (!status || returnValue) {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running WiX candle. "
"Please check '"
<< logFileName << "' for errors." << std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
bool cmCPackWIXGenerator::RunCandleCommand(std::string const& sourceFile,
std::string const& objectFile)
2013-03-16 19:13:01 +02:00
{
std::string executable;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_WIX_CANDLE_EXECUTABLE", executable)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-10-30 18:24:19 +01:00
std::ostringstream command;
2013-03-16 19:13:01 +02:00
command << QuotePath(executable);
command << " -nologo";
command << " -arch " << GetArchitecture();
command << " -out " << QuotePath(objectFile);
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& ext : CandleExtensions) {
command << " -ext " << QuotePath(ext);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
AddCustomFlags("CPACK_WIX_CANDLE_EXTRA_FLAGS", command);
2013-03-16 19:13:01 +02:00
command << " " << QuotePath(sourceFile);
return RunWiXCommand(command.str());
}
2015-04-27 22:25:09 +02:00
bool cmCPackWIXGenerator::RunLightCommand(std::string const& objectFiles)
2013-03-16 19:13:01 +02:00
{
std::string executable;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_WIX_LIGHT_EXECUTABLE", executable)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-10-30 18:24:19 +01:00
std::ostringstream command;
2013-03-16 19:13:01 +02:00
command << QuotePath(executable);
command << " -nologo";
command << " -out " << QuotePath(packageFileNames.at(0));
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& ext : this->LightExtensions) {
command << " -ext " << QuotePath(ext);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2013-11-03 12:27:13 +02:00
const char* const cultures = GetOption("CPACK_WIX_CULTURES");
2016-07-09 11:21:54 +02:00
if (cultures) {
2013-11-03 12:27:13 +02:00
command << " -cultures:" << cultures;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
AddCustomFlags("CPACK_WIX_LIGHT_EXTRA_FLAGS", command);
2013-03-16 19:13:01 +02:00
command << " " << objectFiles;
return RunWiXCommand(command.str());
}
int cmCPackWIXGenerator::PackageFiles()
{
2016-07-09 11:21:54 +02:00
if (!PackageFilesImpl() || cmSystemTools::GetErrorOccuredFlag()) {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Fatal WiX Generator Error"
<< std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
bool cmCPackWIXGenerator::InitializeWiXConfiguration()
{
2016-07-09 11:21:54 +02:00
if (!ReadListFile("CPackWIX.cmake")) {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while executing CPackWIX.cmake"
<< std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_PRODUCT_GUID") == 0) {
2013-03-16 19:13:01 +02:00
std::string guid = GenerateGUID();
SetOption("CPACK_WIX_PRODUCT_GUID", guid.c_str());
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
"CPACK_WIX_PRODUCT_GUID implicitly set to " << guid << " . "
<< std::endl);
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_UPGRADE_GUID") == 0) {
2013-03-16 19:13:01 +02:00
std::string guid = GenerateGUID();
SetOption("CPACK_WIX_UPGRADE_GUID", guid.c_str());
2016-07-09 11:21:54 +02:00
cmCPackLogger(
cmCPackLog::LOG_WARNING, "CPACK_WIX_UPGRADE_GUID implicitly set to "
<< guid << " . "
"Please refer to the documentation on how and why "
"you might want to set this explicitly."
<< std::endl);
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_TOPLEVEL_DIRECTORY", this->CPackTopLevel)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_LICENSE_RTF") == 0) {
2015-04-27 22:25:09 +02:00
std::string licenseFilename = this->CPackTopLevel + "/License.rtf";
2013-03-16 19:13:01 +02:00
SetOption("CPACK_WIX_LICENSE_RTF", licenseFilename.c_str());
2016-07-09 11:21:54 +02:00
if (!CreateLicenseFile()) {
2013-03-16 19:13:01 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_PACKAGE_VENDOR") == 0) {
2014-08-03 19:52:23 +02:00
std::string defaultVendor = "Humanity";
SetOption("CPACK_PACKAGE_VENDOR", defaultVendor.c_str());
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
"CPACK_PACKAGE_VENDOR implicitly set to "
<< defaultVendor << " . " << std::endl);
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_UI_REF") == 0) {
2014-08-03 19:52:23 +02:00
std::string defaultRef = "WixUI_InstallDir";
2016-07-09 11:21:54 +02:00
if (!this->Components.empty()) {
2014-08-03 19:52:23 +02:00
defaultRef = "WixUI_FeatureTree";
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
SetOption("CPACK_WIX_UI_REF", defaultRef.c_str());
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
const char* packageContact = GetOption("CPACK_PACKAGE_CONTACT");
2016-07-09 11:21:54 +02:00
if (packageContact != 0 && GetOption("CPACK_WIX_PROPERTY_ARPCONTACT") == 0) {
2015-04-27 22:25:09 +02:00
SetOption("CPACK_WIX_PROPERTY_ARPCONTACT", packageContact);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
CollectExtensions("CPACK_WIX_EXTENSIONS", this->CandleExtensions);
CollectExtensions("CPACK_WIX_CANDLE_EXTENSIONS", this->CandleExtensions);
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
this->LightExtensions.insert("WixUIExtension");
CollectExtensions("CPACK_WIX_EXTENSIONS", this->LightExtensions);
CollectExtensions("CPACK_WIX_LIGHT_EXTENSIONS", this->LightExtensions);
2014-08-03 19:52:23 +02:00
const char* patchFilePath = GetOption("CPACK_WIX_PATCH_FILE");
2016-07-09 11:21:54 +02:00
if (patchFilePath) {
2016-03-13 13:35:51 +01:00
std::vector<std::string> patchFilePaths;
cmSystemTools::ExpandListArgument(patchFilePath, patchFilePaths);
2018-01-26 17:06:56 +01:00
for (std::string const& p : patchFilePaths) {
if (!this->Patch->LoadFragments(p)) {
2016-03-13 13:35:51 +01:00
return false;
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-10-30 18:24:19 +01:00
// if install folder is supposed to be set absolutely, the default
// component guid "*" cannot be used
if (cmSystemTools::IsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
this->ComponentGuidType = cmWIXSourceWriter::CMAKE_GENERATED_GUID;
}
2013-03-16 19:13:01 +02:00
return true;
}
bool cmCPackWIXGenerator::PackageFilesImpl()
{
2016-07-09 11:21:54 +02:00
if (!InitializeWiXConfiguration()) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
CreateWiXVariablesIncludeFile();
CreateWiXPropertiesIncludeFile();
2015-08-17 11:37:30 +02:00
CreateWiXProductFragmentIncludeFile();
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!CreateWiXSourceFiles()) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
AppendUserSuppliedExtraSources();
2015-08-17 11:37:30 +02:00
std::set<std::string> usedBaseNames;
2016-10-30 18:24:19 +01:00
std::ostringstream objectFiles;
2018-01-26 17:06:56 +01:00
for (std::string const& sourceFilename : this->WixSources) {
2015-08-17 11:37:30 +02:00
std::string baseName =
cmSystemTools::GetFilenameWithoutLastExtension(sourceFilename);
unsigned int counter = 0;
std::string uniqueBaseName = baseName;
2016-07-09 11:21:54 +02:00
while (usedBaseNames.find(uniqueBaseName) != usedBaseNames.end()) {
2016-10-30 18:24:19 +01:00
std::ostringstream tmp;
2015-08-17 11:37:30 +02:00
tmp << baseName << ++counter;
uniqueBaseName = tmp.str();
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
usedBaseNames.insert(uniqueBaseName);
2013-03-16 19:13:01 +02:00
std::string objectFilename =
2015-08-17 11:37:30 +02:00
this->CPackTopLevel + "/" + uniqueBaseName + ".wixobj";
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!RunCandleCommand(sourceFilename, objectFilename)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
objectFiles << " " << QuotePath(objectFilename);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
AppendUserSuppliedExtraObjects(objectFiles);
2013-03-16 19:13:01 +02:00
return RunLightCommand(objectFiles.str());
}
2014-08-03 19:52:23 +02:00
void cmCPackWIXGenerator::AppendUserSuppliedExtraSources()
{
2016-07-09 11:21:54 +02:00
const char* cpackWixExtraSources = GetOption("CPACK_WIX_EXTRA_SOURCES");
if (!cpackWixExtraSources)
return;
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
cmSystemTools::ExpandListArgument(cpackWixExtraSources, this->WixSources);
2014-08-03 19:52:23 +02:00
}
void cmCPackWIXGenerator::AppendUserSuppliedExtraObjects(std::ostream& stream)
{
2016-07-09 11:21:54 +02:00
const char* cpackWixExtraObjects = GetOption("CPACK_WIX_EXTRA_OBJECTS");
if (!cpackWixExtraObjects)
return;
2014-08-03 19:52:23 +02:00
std::vector<std::string> expandedExtraObjects;
2016-07-09 11:21:54 +02:00
cmSystemTools::ExpandListArgument(cpackWixExtraObjects,
expandedExtraObjects);
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& obj : expandedExtraObjects) {
stream << " " << QuotePath(obj);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2015-04-27 22:25:09 +02:00
void cmCPackWIXGenerator::CreateWiXVariablesIncludeFile()
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
std::string includeFilename = this->CPackTopLevel + "/cpack_variables.wxi";
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
cmWIXSourceWriter includeFile(this->Logger, includeFilename,
this->ComponentGuidType,
cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
2013-03-16 19:13:01 +02:00
CopyDefinition(includeFile, "CPACK_WIX_PRODUCT_GUID");
CopyDefinition(includeFile, "CPACK_WIX_UPGRADE_GUID");
CopyDefinition(includeFile, "CPACK_PACKAGE_VENDOR");
CopyDefinition(includeFile, "CPACK_PACKAGE_NAME");
CopyDefinition(includeFile, "CPACK_PACKAGE_VERSION");
CopyDefinition(includeFile, "CPACK_WIX_LICENSE_RTF");
CopyDefinition(includeFile, "CPACK_WIX_PRODUCT_ICON");
CopyDefinition(includeFile, "CPACK_WIX_UI_BANNER");
CopyDefinition(includeFile, "CPACK_WIX_UI_DIALOG");
2013-11-03 12:27:13 +02:00
SetOptionIfNotSet("CPACK_WIX_PROGRAM_MENU_FOLDER",
2016-07-09 11:21:54 +02:00
GetOption("CPACK_PACKAGE_NAME"));
2013-11-03 12:27:13 +02:00
CopyDefinition(includeFile, "CPACK_WIX_PROGRAM_MENU_FOLDER");
2014-08-03 19:52:23 +02:00
CopyDefinition(includeFile, "CPACK_WIX_UI_REF");
2015-04-27 22:25:09 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
void cmCPackWIXGenerator::CreateWiXPropertiesIncludeFile()
{
2016-07-09 11:21:54 +02:00
std::string includeFilename = this->CPackTopLevel + "/properties.wxi";
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
cmWIXSourceWriter includeFile(this->Logger, includeFilename,
this->ComponentGuidType,
cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
2015-04-27 22:25:09 +02:00
std::string prefix = "CPACK_WIX_PROPERTY_";
std::vector<std::string> options = GetOptions();
2018-01-26 17:06:56 +01:00
for (std::string const& name : options) {
2016-07-09 11:21:54 +02:00
if (name.length() > prefix.length() &&
name.substr(0, prefix.length()) == prefix) {
2015-04-27 22:25:09 +02:00
std::string id = name.substr(prefix.length());
std::string value = GetOption(name.c_str());
includeFile.BeginElement("Property");
includeFile.AddAttribute("Id", id);
includeFile.AddAttribute("Value", value);
includeFile.EndElement("Property");
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_PROPERTY_ARPINSTALLLOCATION") == 0) {
2015-04-27 22:25:09 +02:00
includeFile.BeginElement("Property");
includeFile.AddAttribute("Id", "INSTALL_ROOT");
includeFile.AddAttribute("Secure", "yes");
includeFile.BeginElement("RegistrySearch");
includeFile.AddAttribute("Id", "FindInstallLocation");
includeFile.AddAttribute("Root", "HKLM");
2016-07-09 11:21:54 +02:00
includeFile.AddAttribute(
"Key", "Software\\Microsoft\\Windows\\"
"CurrentVersion\\Uninstall\\[WIX_UPGRADE_DETECTED]");
2015-04-27 22:25:09 +02:00
includeFile.AddAttribute("Name", "InstallLocation");
includeFile.AddAttribute("Type", "raw");
includeFile.EndElement("RegistrySearch");
includeFile.EndElement("Property");
includeFile.BeginElement("SetProperty");
includeFile.AddAttribute("Id", "ARPINSTALLLOCATION");
includeFile.AddAttribute("Value", "[INSTALL_ROOT]");
includeFile.AddAttribute("After", "CostFinalize");
includeFile.EndElement("SetProperty");
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2015-08-17 11:37:30 +02:00
void cmCPackWIXGenerator::CreateWiXProductFragmentIncludeFile()
{
2016-07-09 11:21:54 +02:00
std::string includeFilename = this->CPackTopLevel + "/product_fragment.wxi";
2015-08-17 11:37:30 +02:00
2016-10-30 18:24:19 +01:00
cmWIXSourceWriter includeFile(this->Logger, includeFilename,
this->ComponentGuidType,
cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
2015-08-17 11:37:30 +02:00
2016-07-09 11:21:54 +02:00
this->Patch->ApplyFragment("#PRODUCT", includeFile);
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
void cmCPackWIXGenerator::CopyDefinition(cmWIXSourceWriter& source,
std::string const& name)
2013-03-16 19:13:01 +02:00
{
const char* value = GetOption(name.c_str());
2016-07-09 11:21:54 +02:00
if (value) {
2013-03-16 19:13:01 +02:00
AddDefinition(source, name, value);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
void cmCPackWIXGenerator::AddDefinition(cmWIXSourceWriter& source,
2016-07-09 11:21:54 +02:00
std::string const& name,
std::string const& value)
2013-03-16 19:13:01 +02:00
{
2016-10-30 18:24:19 +01:00
std::ostringstream tmp;
2013-03-16 19:13:01 +02:00
tmp << name << "=\"" << value << '"';
2017-04-14 19:02:05 +02:00
source.AddProcessingInstruction("define", tmp.str());
2013-03-16 19:13:01 +02:00
}
bool cmCPackWIXGenerator::CreateWiXSourceFiles()
{
2016-10-30 18:24:19 +01:00
// if install folder is supposed to be set absolutely, the default
// component guid "*" cannot be used
2013-03-16 19:13:01 +02:00
std::string directoryDefinitionsFilename =
2015-04-27 22:25:09 +02:00
this->CPackTopLevel + "/directories.wxs";
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
this->WixSources.push_back(directoryDefinitionsFilename);
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
cmWIXDirectoriesSourceWriter directoryDefinitions(
2016-10-30 18:24:19 +01:00
this->Logger, directoryDefinitionsFilename, this->ComponentGuidType);
2013-03-16 19:13:01 +02:00
directoryDefinitions.BeginElement("Fragment");
2015-04-27 22:25:09 +02:00
std::string installRoot;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_PACKAGE_INSTALL_DIRECTORY", installRoot)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
directoryDefinitions.BeginElement("Directory");
directoryDefinitions.AddAttribute("Id", "TARGETDIR");
directoryDefinitions.AddAttribute("Name", "SourceDir");
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
size_t installRootSize =
2017-07-20 19:35:53 +02:00
directoryDefinitions.BeginInstallationPrefixDirectory(GetRootFolderId(),
installRoot);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
std::string fileDefinitionsFilename = this->CPackTopLevel + "/files.wxs";
2015-04-27 22:25:09 +02:00
this->WixSources.push_back(fileDefinitionsFilename);
2013-03-16 19:13:01 +02:00
2016-10-30 18:24:19 +01:00
cmWIXFilesSourceWriter fileDefinitions(this->Logger, fileDefinitionsFilename,
this->ComponentGuidType);
2013-03-16 19:13:01 +02:00
fileDefinitions.BeginElement("Fragment");
std::string featureDefinitionsFilename =
2016-07-09 11:21:54 +02:00
this->CPackTopLevel + "/features.wxs";
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
this->WixSources.push_back(featureDefinitionsFilename);
2016-10-30 18:24:19 +01:00
cmWIXFeaturesSourceWriter featureDefinitions(
this->Logger, featureDefinitionsFilename, this->ComponentGuidType);
2013-03-16 19:13:01 +02:00
featureDefinitions.BeginElement("Fragment");
featureDefinitions.BeginElement("Feature");
featureDefinitions.AddAttribute("Id", "ProductFeature");
2014-08-03 19:52:23 +02:00
featureDefinitions.AddAttribute("Display", "expand");
2016-03-13 13:35:51 +01:00
featureDefinitions.AddAttribute("Absent", "disallow");
2014-08-03 19:52:23 +02:00
featureDefinitions.AddAttribute("ConfigurableDirectory", "INSTALL_ROOT");
std::string cpackPackageName;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_PACKAGE_NAME", cpackPackageName)) {
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-10-30 18:24:19 +01:00
std::string featureTitle = cpackPackageName;
if (const char* title = GetOption("CPACK_WIX_ROOT_FEATURE_TITLE")) {
featureTitle = title;
}
featureDefinitions.AddAttribute("Title", featureTitle);
if (const char* desc = GetOption("CPACK_WIX_ROOT_FEATURE_DESCRIPTION")) {
featureDefinitions.AddAttribute("Description", desc);
}
2013-03-16 19:13:01 +02:00
featureDefinitions.AddAttribute("Level", "1");
2015-08-17 11:37:30 +02:00
this->Patch->ApplyFragment("#PRODUCTFEATURE", featureDefinitions);
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
const char* package = GetOption("CPACK_WIX_CMAKE_PACKAGE_REGISTRY");
2016-07-09 11:21:54 +02:00
if (package) {
2015-04-27 22:25:09 +02:00
featureDefinitions.CreateCMakePackageRegistryEntry(
2016-07-09 11:21:54 +02:00
package, GetOption("CPACK_WIX_UPGRADE_GUID"));
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!CreateFeatureHierarchy(featureDefinitions)) {
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
featureDefinitions.EndElement("Feature");
2013-11-03 12:27:13 +02:00
2015-08-17 11:37:30 +02:00
std::set<cmWIXShortcuts::Type> emittedShortcutTypes;
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
cmWIXShortcuts globalShortcuts;
2016-07-09 11:21:54 +02:00
if (Components.empty()) {
AddComponentsToFeature(toplevel, "ProductFeature", directoryDefinitions,
fileDefinitions, featureDefinitions,
globalShortcuts);
2015-08-17 11:37:30 +02:00
globalShortcuts.AddShortcutTypes(emittedShortcutTypes);
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Components) {
cmCPackComponent const& component = i.second;
2014-08-03 19:52:23 +02:00
std::string componentPath = toplevel;
componentPath += "/";
componentPath += component.Name;
2018-01-26 17:06:56 +01:00
std::string const componentFeatureId = "CM_C_" + component.Name;
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
cmWIXShortcuts featureShortcuts;
2014-08-03 19:52:23 +02:00
AddComponentsToFeature(componentPath, componentFeatureId,
2016-07-09 11:21:54 +02:00
directoryDefinitions, fileDefinitions,
featureDefinitions, featureShortcuts);
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
featureShortcuts.AddShortcutTypes(emittedShortcutTypes);
2016-07-09 11:21:54 +02:00
if (!CreateShortcuts(component.Name, componentFeatureId,
featureShortcuts, false, fileDefinitions,
featureDefinitions)) {
2015-08-17 11:37:30 +02:00
return false;
2013-11-03 12:27:13 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
bool emitUninstallShortcut =
emittedShortcutTypes.find(cmWIXShortcuts::START_MENU) !=
emittedShortcutTypes.end();
2015-08-17 11:37:30 +02:00
2016-07-09 11:21:54 +02:00
if (!CreateShortcuts(std::string(), "ProductFeature", globalShortcuts,
emitUninstallShortcut, fileDefinitions,
featureDefinitions)) {
2015-08-17 11:37:30 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
featureDefinitions.EndElement("Fragment");
fileDefinitions.EndElement("Fragment");
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
directoryDefinitions.EndInstallationPrefixDirectory(installRootSize);
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
if (emittedShortcutTypes.find(cmWIXShortcuts::START_MENU) !=
emittedShortcutTypes.end()) {
2015-04-27 22:25:09 +02:00
directoryDefinitions.EmitStartMenuFolder(
GetOption("CPACK_WIX_PROGRAM_MENU_FOLDER"));
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
if (emittedShortcutTypes.find(cmWIXShortcuts::DESKTOP) !=
emittedShortcutTypes.end()) {
2015-04-27 22:25:09 +02:00
directoryDefinitions.EmitDesktopFolder();
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
if (emittedShortcutTypes.find(cmWIXShortcuts::STARTUP) !=
emittedShortcutTypes.end()) {
2015-08-17 11:37:30 +02:00
directoryDefinitions.EmitStartupFolder();
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2014-08-03 19:52:23 +02:00
directoryDefinitions.EndElement("Directory");
directoryDefinitions.EndElement("Fragment");
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!GenerateMainSourceFileFromTemplate()) {
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->Patch->CheckForUnappliedFragments();
}
2017-07-20 19:35:53 +02:00
std::string cmCPackWIXGenerator::GetRootFolderId() const
2015-04-27 22:25:09 +02:00
{
2016-10-30 18:24:19 +01:00
if (cmSystemTools::IsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
return "";
}
2017-07-20 19:35:53 +02:00
std::string result = "ProgramFiles<64>Folder";
const char* rootFolderId = GetOption("CPACK_WIX_ROOT_FOLDER_ID");
if (rootFolderId) {
result = rootFolderId;
}
2016-07-09 11:21:54 +02:00
if (GetArchitecture() == "x86") {
2017-07-20 19:35:53 +02:00
cmSystemTools::ReplaceString(result, "<64>", "");
2016-07-09 11:21:54 +02:00
} else {
2017-07-20 19:35:53 +02:00
cmSystemTools::ReplaceString(result, "<64>", "64");
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
return result;
2015-04-27 22:25:09 +02:00
}
bool cmCPackWIXGenerator::GenerateMainSourceFileFromTemplate()
{
2013-03-16 19:13:01 +02:00
std::string wixTemplate = FindTemplate("WIX.template.in");
2016-07-09 11:21:54 +02:00
if (GetOption("CPACK_WIX_TEMPLATE") != 0) {
2013-11-03 12:27:13 +02:00
wixTemplate = GetOption("CPACK_WIX_TEMPLATE");
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (wixTemplate.empty()) {
2013-03-16 19:13:01 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Could not find CPack WiX template file WIX.template.in"
<< std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
std::string mainSourceFilePath = this->CPackTopLevel + "/main.wxs";
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (!ConfigureFile(wixTemplate.c_str(), mainSourceFilePath.c_str())) {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Failed creating '"
<< mainSourceFilePath << "'' from template." << std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
this->WixSources.push_back(mainSourceFilePath);
2014-08-03 19:52:23 +02:00
return true;
}
bool cmCPackWIXGenerator::CreateFeatureHierarchy(
2015-04-27 22:25:09 +02:00
cmWIXFeaturesSourceWriter& featureDefinitions)
2014-08-03 19:52:23 +02:00
{
2018-01-26 17:06:56 +01:00
for (auto const& i : ComponentGroups) {
cmCPackComponentGroup const& group = i.second;
2016-07-09 11:21:54 +02:00
if (group.ParentGroup == 0) {
2016-10-30 18:24:19 +01:00
featureDefinitions.EmitFeatureForComponentGroup(group, *this->Patch);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Components) {
cmCPackComponent const& component = i.second;
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (!component.Group) {
2016-10-30 18:24:19 +01:00
featureDefinitions.EmitFeatureForComponent(component, *this->Patch);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
return true;
}
bool cmCPackWIXGenerator::AddComponentsToFeature(
2016-07-09 11:21:54 +02:00
std::string const& rootPath, std::string const& featureId,
2015-04-27 22:25:09 +02:00
cmWIXDirectoriesSourceWriter& directoryDefinitions,
cmWIXFilesSourceWriter& fileDefinitions,
2016-07-09 11:21:54 +02:00
cmWIXFeaturesSourceWriter& featureDefinitions, cmWIXShortcuts& shortcuts)
2014-08-03 19:52:23 +02:00
{
featureDefinitions.BeginElement("FeatureRef");
featureDefinitions.AddAttribute("Id", featureId);
std::vector<std::string> cpackPackageExecutablesList;
2016-07-09 11:21:54 +02:00
const char* cpackPackageExecutables = GetOption("CPACK_PACKAGE_EXECUTABLES");
if (cpackPackageExecutables) {
cmSystemTools::ExpandListArgument(cpackPackageExecutables,
cpackPackageExecutablesList);
if (cpackPackageExecutablesList.size() % 2 != 0) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
"<text label>."
<< std::endl);
return false;
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::vector<std::string> cpackPackageDesktopLinksList;
2016-07-09 11:21:54 +02:00
const char* cpackPackageDesktopLinks =
2014-08-03 19:52:23 +02:00
GetOption("CPACK_CREATE_DESKTOP_LINKS");
2016-07-09 11:21:54 +02:00
if (cpackPackageDesktopLinks) {
cmSystemTools::ExpandListArgument(cpackPackageDesktopLinks,
cpackPackageDesktopLinksList);
}
2014-08-03 19:52:23 +02:00
2017-07-20 19:35:53 +02:00
AddDirectoryAndFileDefinitions(
rootPath, "INSTALL_ROOT", directoryDefinitions, fileDefinitions,
featureDefinitions, cpackPackageExecutablesList,
cpackPackageDesktopLinksList, shortcuts);
2014-08-03 19:52:23 +02:00
featureDefinitions.EndElement("FeatureRef");
return true;
}
2015-08-17 11:37:30 +02:00
bool cmCPackWIXGenerator::CreateShortcuts(
2016-07-09 11:21:54 +02:00
std::string const& cpackComponentName, std::string const& featureId,
cmWIXShortcuts const& shortcuts, bool emitUninstallShortcut,
2015-08-17 11:37:30 +02:00
cmWIXFilesSourceWriter& fileDefinitions,
cmWIXFeaturesSourceWriter& featureDefinitions)
{
2016-07-09 11:21:54 +02:00
if (!shortcuts.empty(cmWIXShortcuts::START_MENU)) {
if (!this->CreateShortcutsOfSpecificType(
cmWIXShortcuts::START_MENU, cpackComponentName, featureId, "",
shortcuts, emitUninstallShortcut, fileDefinitions,
featureDefinitions)) {
2015-08-17 11:37:30 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2016-07-09 11:21:54 +02:00
if (!shortcuts.empty(cmWIXShortcuts::DESKTOP)) {
if (!this->CreateShortcutsOfSpecificType(
cmWIXShortcuts::DESKTOP, cpackComponentName, featureId, "DESKTOP",
shortcuts, false, fileDefinitions, featureDefinitions)) {
2015-08-17 11:37:30 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2016-07-09 11:21:54 +02:00
if (!shortcuts.empty(cmWIXShortcuts::STARTUP)) {
if (!this->CreateShortcutsOfSpecificType(
cmWIXShortcuts::STARTUP, cpackComponentName, featureId, "STARTUP",
shortcuts, false, fileDefinitions, featureDefinitions)) {
2015-08-17 11:37:30 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return true;
}
bool cmCPackWIXGenerator::CreateShortcutsOfSpecificType(
2016-07-09 11:21:54 +02:00
cmWIXShortcuts::Type type, std::string const& cpackComponentName,
std::string const& featureId, std::string const& idPrefix,
cmWIXShortcuts const& shortcuts, bool emitUninstallShortcut,
2015-04-27 22:25:09 +02:00
cmWIXFilesSourceWriter& fileDefinitions,
cmWIXFeaturesSourceWriter& featureDefinitions)
2014-08-03 19:52:23 +02:00
{
2015-08-17 11:37:30 +02:00
std::string directoryId;
2016-07-09 11:21:54 +02:00
switch (type) {
2015-08-17 11:37:30 +02:00
case cmWIXShortcuts::START_MENU:
directoryId = "PROGRAM_MENU_FOLDER";
break;
case cmWIXShortcuts::DESKTOP:
directoryId = "DesktopFolder";
break;
case cmWIXShortcuts::STARTUP:
directoryId = "StartupFolder";
break;
default:
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
featureDefinitions.BeginElement("FeatureRef");
featureDefinitions.AddAttribute("Id", featureId);
std::string cpackVendor;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_PACKAGE_VENDOR", cpackVendor)) {
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string cpackPackageName;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_PACKAGE_NAME", cpackPackageName)) {
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string idSuffix;
2016-07-09 11:21:54 +02:00
if (!cpackComponentName.empty()) {
idSuffix += "_";
idSuffix += cpackComponentName;
}
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
std::string componentId = "CM_SHORTCUT";
2016-07-09 11:21:54 +02:00
if (idPrefix.size()) {
2015-08-17 11:37:30 +02:00
componentId += "_" + idPrefix;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
componentId += idSuffix;
2014-08-03 19:52:23 +02:00
fileDefinitions.BeginElement("DirectoryRef");
2015-08-17 11:37:30 +02:00
fileDefinitions.AddAttribute("Id", directoryId);
2015-04-27 22:25:09 +02:00
2014-08-03 19:52:23 +02:00
fileDefinitions.BeginElement("Component");
fileDefinitions.AddAttribute("Id", componentId);
2016-10-30 18:24:19 +01:00
fileDefinitions.AddAttribute(
"Guid", fileDefinitions.CreateGuidFromComponentId(componentId));
2014-08-03 19:52:23 +02:00
2016-03-13 13:35:51 +01:00
this->Patch->ApplyFragment(componentId, fileDefinitions);
2016-07-09 11:21:54 +02:00
std::string registryKey =
std::string("Software\\") + cpackVendor + "\\" + cpackPackageName;
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
shortcuts.EmitShortcuts(type, registryKey, cpackComponentName,
fileDefinitions);
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (type == cmWIXShortcuts::START_MENU) {
fileDefinitions.EmitRemoveFolder("CM_REMOVE_PROGRAM_MENU_FOLDER" +
idSuffix);
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (emitUninstallShortcut) {
2015-04-27 22:25:09 +02:00
fileDefinitions.EmitUninstallShortcut(cpackPackageName);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
fileDefinitions.EndElement("Component");
fileDefinitions.EndElement("DirectoryRef");
2015-04-27 22:25:09 +02:00
featureDefinitions.EmitComponentRef(componentId);
2014-08-03 19:52:23 +02:00
featureDefinitions.EndElement("FeatureRef");
2013-03-16 19:13:01 +02:00
return true;
}
bool cmCPackWIXGenerator::CreateLicenseFile()
{
std::string licenseSourceFilename;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_RESOURCE_FILE_LICENSE", licenseSourceFilename)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::string licenseDestinationFilename;
2016-07-09 11:21:54 +02:00
if (!RequireOption("CPACK_WIX_LICENSE_RTF", licenseDestinationFilename)) {
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::string extension = GetRightmostExtension(licenseSourceFilename);
2016-07-09 11:21:54 +02:00
if (extension == ".rtf") {
cmSystemTools::CopyAFile(licenseSourceFilename.c_str(),
licenseDestinationFilename.c_str());
} else if (extension == ".txt") {
2013-03-16 19:13:01 +02:00
cmWIXRichTextFormatWriter rtfWriter(licenseDestinationFilename);
2014-08-03 19:52:23 +02:00
cmsys::ifstream licenseSource(licenseSourceFilename.c_str());
2013-03-16 19:13:01 +02:00
std::string line;
2016-07-09 11:21:54 +02:00
while (std::getline(licenseSource, line)) {
2013-03-16 19:13:01 +02:00
rtfWriter.AddText(line);
rtfWriter.AddText("\n");
}
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"unsupported WiX License file extension '"
<< extension << "'" << std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
2017-07-20 19:35:53 +02:00
void cmCPackWIXGenerator::AddDirectoryAndFileDefinitions(
2016-07-09 11:21:54 +02:00
std::string const& topdir, std::string const& directoryId,
2015-04-27 22:25:09 +02:00
cmWIXDirectoriesSourceWriter& directoryDefinitions,
cmWIXFilesSourceWriter& fileDefinitions,
cmWIXFeaturesSourceWriter& featureDefinitions,
2015-08-17 11:37:30 +02:00
std::vector<std::string> const& packageExecutables,
std::vector<std::string> const& desktopExecutables,
cmWIXShortcuts& shortcuts)
2013-03-16 19:13:01 +02:00
{
cmsys::Directory dir;
dir.Load(topdir.c_str());
2015-04-27 22:25:09 +02:00
std::string relativeDirectoryPath =
cmSystemTools::RelativePath(toplevel.c_str(), topdir.c_str());
2016-07-09 11:21:54 +02:00
if (relativeDirectoryPath.empty()) {
2015-04-27 22:25:09 +02:00
relativeDirectoryPath = ".";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-03-13 13:35:51 +01:00
cmInstalledFile const* directoryInstalledFile = this->GetInstalledFile(
2016-07-09 11:21:54 +02:00
this->RelativePathWithoutComponentPrefix(relativeDirectoryPath));
2015-04-27 22:25:09 +02:00
bool emptyDirectory = dir.GetNumberOfFiles() == 2;
bool createDirectory = false;
2016-07-09 11:21:54 +02:00
if (emptyDirectory) {
2015-04-27 22:25:09 +02:00
createDirectory = true;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (directoryInstalledFile) {
if (directoryInstalledFile->HasProperty("CPACK_WIX_ACL")) {
2015-04-27 22:25:09 +02:00
createDirectory = true;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (createDirectory) {
2015-04-27 22:25:09 +02:00
std::string componentId = fileDefinitions.EmitComponentCreateFolder(
directoryId, GenerateGUID(), directoryInstalledFile);
featureDefinitions.EmitComponentRef(componentId);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (emptyDirectory) {
2015-04-27 22:25:09 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
for (size_t i = 0; i < dir.GetNumberOfFiles(); ++i) {
2013-03-16 19:13:01 +02:00
std::string fileName = dir.GetFile(static_cast<unsigned long>(i));
2016-07-09 11:21:54 +02:00
if (fileName == "." || fileName == "..") {
2013-03-16 19:13:01 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::string fullPath = topdir + "/" + fileName;
2016-07-09 11:21:54 +02:00
std::string relativePath =
cmSystemTools::RelativePath(toplevel.c_str(), fullPath.c_str());
2014-08-03 19:52:23 +02:00
std::string id = PathToId(relativePath);
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileIsDirectory(fullPath.c_str())) {
2014-08-03 19:52:23 +02:00
std::string subDirectoryId = std::string("CM_D") + id;
2013-03-16 19:13:01 +02:00
directoryDefinitions.BeginElement("Directory");
directoryDefinitions.AddAttribute("Id", subDirectoryId);
directoryDefinitions.AddAttribute("Name", fileName);
2017-07-20 19:35:53 +02:00
this->Patch->ApplyFragment(subDirectoryId, directoryDefinitions);
2013-03-16 19:13:01 +02:00
2017-07-20 19:35:53 +02:00
AddDirectoryAndFileDefinitions(
2016-07-09 11:21:54 +02:00
fullPath, subDirectoryId, directoryDefinitions, fileDefinitions,
featureDefinitions, packageExecutables, desktopExecutables, shortcuts);
2014-08-03 19:52:23 +02:00
directoryDefinitions.EndElement("Directory");
2016-07-09 11:21:54 +02:00
} else {
2016-03-13 13:35:51 +01:00
cmInstalledFile const* installedFile = this->GetInstalledFile(
2016-07-09 11:21:54 +02:00
this->RelativePathWithoutComponentPrefix(relativePath));
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (installedFile) {
2015-08-17 11:37:30 +02:00
shortcuts.CreateFromProperties(id, directoryId, *installedFile);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2015-04-27 22:25:09 +02:00
std::string componentId = fileDefinitions.EmitComponentFile(
directoryId, id, fullPath, *(this->Patch), installedFile);
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
featureDefinitions.EmitComponentRef(componentId);
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
for (size_t j = 0; j < packageExecutables.size(); ++j) {
2014-08-03 19:52:23 +02:00
std::string const& executableName = packageExecutables[j++];
std::string const& textLabel = packageExecutables[j];
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
if (cmSystemTools::LowerCase(fileName) ==
cmSystemTools::LowerCase(executableName) + ".exe") {
2015-08-17 11:37:30 +02:00
cmWIXShortcut shortcut;
2016-07-09 11:21:54 +02:00
shortcut.label = textLabel;
2014-08-03 19:52:23 +02:00
shortcut.workingDirectoryId = directoryId;
2015-08-17 11:37:30 +02:00
shortcuts.insert(cmWIXShortcuts::START_MENU, id, shortcut);
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (!desktopExecutables.empty() &&
std::find(desktopExecutables.begin(), desktopExecutables.end(),
executableName) != desktopExecutables.end()) {
shortcuts.insert(cmWIXShortcuts::DESKTOP, id, shortcut);
2013-11-03 12:27:13 +02:00
}
}
2013-03-16 19:13:01 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
bool cmCPackWIXGenerator::RequireOption(std::string const& name,
std::string& value) const
2013-03-16 19:13:01 +02:00
{
const char* tmp = GetOption(name.c_str());
2016-07-09 11:21:54 +02:00
if (tmp) {
2013-03-16 19:13:01 +02:00
value = tmp;
return true;
2016-07-09 11:21:54 +02:00
} else {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Required variable "
<< name << " not set" << std::endl);
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
std::string cmCPackWIXGenerator::GetArchitecture() const
{
std::string void_p_size;
RequireOption("CPACK_WIX_SIZEOF_VOID_P", void_p_size);
2016-07-09 11:21:54 +02:00
if (void_p_size == "8") {
2013-03-16 19:13:01 +02:00
return "x64";
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
return "x86";
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
std::string cmCPackWIXGenerator::GenerateGUID()
{
UUID guid;
UuidCreate(&guid);
2016-07-09 11:21:54 +02:00
unsigned short* tmp = 0;
2014-08-03 19:52:23 +02:00
UuidToStringW(&guid, &tmp);
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
std::string result =
cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(tmp));
RpcStringFreeW(&tmp);
2013-03-16 19:13:01 +02:00
return cmSystemTools::UpperCase(result);
}
2015-04-27 22:25:09 +02:00
std::string cmCPackWIXGenerator::QuotePath(std::string const& path)
2013-03-16 19:13:01 +02:00
{
return std::string("\"") + path + '"';
}
std::string cmCPackWIXGenerator::GetRightmostExtension(
2015-04-27 22:25:09 +02:00
std::string const& filename)
2013-03-16 19:13:01 +02:00
{
std::string extension;
std::string::size_type i = filename.rfind(".");
2016-07-09 11:21:54 +02:00
if (i != std::string::npos) {
2013-03-16 19:13:01 +02:00
extension = filename.substr(i);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return cmSystemTools::LowerCase(extension);
}
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
std::string cmCPackWIXGenerator::PathToId(std::string const& path)
2014-08-03 19:52:23 +02:00
{
id_map_t::const_iterator i = PathToIdMap.find(path);
2016-07-09 11:21:54 +02:00
if (i != PathToIdMap.end())
return i->second;
2014-08-03 19:52:23 +02:00
std::string id = CreateNewIdForPath(path);
return id;
}
2015-04-27 22:25:09 +02:00
std::string cmCPackWIXGenerator::CreateNewIdForPath(std::string const& path)
2014-08-03 19:52:23 +02:00
{
std::vector<std::string> components;
cmSystemTools::SplitPath(path.c_str(), components, false);
size_t replacementCount = 0;
std::string identifier;
std::string currentComponent;
2016-07-09 11:21:54 +02:00
for (size_t i = 1; i < components.size(); ++i) {
if (i != 1)
identifier += '.';
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
currentComponent =
NormalizeComponentForId(components[i], replacementCount);
2014-08-03 19:52:23 +02:00
identifier += currentComponent;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string idPrefix = "P";
size_t replacementPercent = replacementCount * 100 / identifier.size();
2016-07-09 11:21:54 +02:00
if (replacementPercent > 33 || identifier.size() > 60) {
2014-08-03 19:52:23 +02:00
identifier = CreateHashedId(path, currentComponent);
idPrefix = "H";
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-10-30 18:24:19 +01:00
std::ostringstream result;
2014-08-03 19:52:23 +02:00
result << idPrefix << "_" << identifier;
size_t ambiguityCount = ++IdAmbiguityCounter[identifier];
2016-07-09 11:21:54 +02:00
if (ambiguityCount > 999) {
2014-08-03 19:52:23 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error while trying to generate a unique Id for '"
<< path << "'" << std::endl);
2014-08-03 19:52:23 +02:00
return std::string();
2016-07-09 11:21:54 +02:00
} else if (ambiguityCount > 1) {
2014-08-03 19:52:23 +02:00
result << "_" << ambiguityCount;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string resultString = result.str();
PathToIdMap[path] = resultString;
return resultString;
}
std::string cmCPackWIXGenerator::CreateHashedId(
2015-04-27 22:25:09 +02:00
std::string const& path, std::string const& normalizedFilename)
2014-08-03 19:52:23 +02:00
{
2017-04-14 19:02:05 +02:00
cmCryptoHash sha1(cmCryptoHash::AlgoSHA1);
std::string const hash = sha1.HashString(path);
2014-08-03 19:52:23 +02:00
std::string identifier;
identifier += hash.substr(0, 7) + "_";
const size_t maxFileNameLength = 52;
2016-07-09 11:21:54 +02:00
if (normalizedFilename.length() > maxFileNameLength) {
2014-08-03 19:52:23 +02:00
identifier += normalizedFilename.substr(0, maxFileNameLength - 3);
identifier += "...";
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
identifier += normalizedFilename;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
return identifier;
}
std::string cmCPackWIXGenerator::NormalizeComponentForId(
2015-04-27 22:25:09 +02:00
std::string const& component, size_t& replacementCount)
2014-08-03 19:52:23 +02:00
{
std::string result;
result.resize(component.size());
2016-07-09 11:21:54 +02:00
for (size_t i = 0; i < component.size(); ++i) {
2014-08-03 19:52:23 +02:00
char c = component[i];
2016-07-09 11:21:54 +02:00
if (IsLegalIdCharacter(c)) {
2014-08-03 19:52:23 +02:00
result[i] = c;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
result[i] = '_';
2016-07-09 11:21:54 +02:00
++replacementCount;
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
return result;
}
bool cmCPackWIXGenerator::IsLegalIdCharacter(char c)
{
2016-07-09 11:21:54 +02:00
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') || c == '_' || c == '.';
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmCPackWIXGenerator::CollectExtensions(std::string const& variableName,
extension_set_t& extensions)
2014-08-03 19:52:23 +02:00
{
2016-07-09 11:21:54 +02:00
const char* variableContent = GetOption(variableName.c_str());
if (!variableContent)
return;
2014-08-03 19:52:23 +02:00
std::vector<std::string> list;
cmSystemTools::ExpandListArgument(variableContent, list);
2015-04-27 22:25:09 +02:00
extensions.insert(list.begin(), list.end());
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmCPackWIXGenerator::AddCustomFlags(std::string const& variableName,
std::ostream& stream)
2014-08-03 19:52:23 +02:00
{
2016-07-09 11:21:54 +02:00
const char* variableContent = GetOption(variableName.c_str());
if (!variableContent)
return;
2014-08-03 19:52:23 +02:00
std::vector<std::string> list;
cmSystemTools::ExpandListArgument(variableContent, list);
2018-01-26 17:06:56 +01:00
for (std::string const& i : list) {
stream << " " << QuotePath(i);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-03-13 13:35:51 +01:00
std::string cmCPackWIXGenerator::RelativePathWithoutComponentPrefix(
std::string const& path)
{
2016-07-09 11:21:54 +02:00
if (this->Components.empty()) {
2016-03-13 13:35:51 +01:00
return path;
2016-07-09 11:21:54 +02:00
}
2016-03-13 13:35:51 +01:00
std::string::size_type pos = path.find('/');
return path.substr(pos + 1);
}