cmake/Source/CPack/WiX/cmCPackWIXGenerator.cxx

1243 lines
36 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"
2020-02-01 23:06:01 +01:00
#include <algorithm>
2020-08-30 11:54:41 +02:00
#include <cm/memory>
2020-02-01 23:06:01 +01:00
#include <cm/string_view>
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
2023-12-07 09:12:54 +01:00
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
#include "cmsys/Directory.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
#include "cmsys/SystemTools.hxx"
2017-07-20 19:35:53 +02:00
#include "cmCPackComponentGroup.h"
#include "cmCPackLog.h"
#include "cmCryptoHash.h"
#include "cmGeneratedFileStream.h"
#include "cmInstalledFile.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-07-20 19:35:53 +02:00
#include "cmSystemTools.h"
#include "cmUuid.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
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
2018-04-23 21:13:27 +02:00
#ifdef _WIN32
2018-08-09 18:06:22 +02:00
# include <rpc.h> // for GUID generation (windows only)
2018-04-23 21:13:27 +02:00
#else
2018-08-09 18:06:22 +02:00
# include <uuid/uuid.h> // for GUID generation (libuuid)
2018-04-23 21:13:27 +02:00
#endif
#include "cmCMakeToWixPath.h"
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
cmCPackWIXGenerator::cmCPackWIXGenerator()
2020-08-30 11:54:41 +02:00
: ComponentGuidType(cmWIXSourceWriter::WIX_GENERATED_GUID)
2014-08-03 19:52:23 +02:00
{
}
2020-08-30 11:54:41 +02:00
cmCPackWIXGenerator::~cmCPackWIXGenerator() = default;
2015-04-27 22:25:09 +02:00
2013-03-16 19:13:01 +02:00
int cmCPackWIXGenerator::InitializeInternal()
{
componentPackageMethod = ONE_PACKAGE;
2020-08-30 11:54:41 +02:00
this->Patch = cm::make_unique<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
{
2023-12-07 09:12:54 +01:00
std::string logFileName = cmStrCat(this->CPackTopLevel, "/wix.log");
2013-03-16 19:13:01 +02:00
2018-08-09 18:06:22 +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;
2023-12-07 09:12:54 +01:00
bool status =
cmSystemTools::RunSingleCommand(command, &output, &output, &returnValue,
nullptr, 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) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Problem running WiX candle. "
"Please check '"
2016-07-09 11:21:54 +02:00
<< 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
2022-08-04 22:12:04 +02:00
std::string arch;
if (cmValue archOpt = GetOption("CPACK_WIX_ARCHITECTURE")) {
arch = *archOpt;
} else {
arch = GetArchitecture();
cmCPackLogger(
cmCPackLog::LOG_VERBOSE,
"CPACK_WIX_ARCHITECTURE was not set. Invoking WiX with architecture "
<< arch << " . " << std::endl);
}
2016-10-30 18:24:19 +01:00
std::ostringstream command;
2023-12-07 09:12:54 +01:00
command << QuotePath(executable)
<< " -nologo"
" -arch "
<< arch << " -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
2020-08-30 11:54:41 +02:00
if (!cmHasSuffix(sourceFile, this->CPackTopLevel)) {
2023-12-07 09:12:54 +01:00
command << ' ' << QuotePath(cmStrCat("-I", this->CPackTopLevel));
2018-10-28 12:09:07 +01:00
}
2014-08-03 19:52:23 +02:00
AddCustomFlags("CPACK_WIX_CANDLE_EXTRA_FLAGS", command);
2023-12-07 09:12:54 +01:00
command << ' ' << QuotePath(sourceFile);
2013-03-16 19:13:01 +02:00
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;
2023-12-07 09:12:54 +01:00
command << QuotePath(executable)
<< " -nologo"
" -out "
<< QuotePath(CMakeToWixPath(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
2021-11-20 13:41:27 +01:00
cmValue 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);
2023-12-07 09:12:54 +01:00
command << ' ' << objectFiles;
2013-03-16 19:13:01 +02:00
return RunWiXCommand(command.str());
}
int cmCPackWIXGenerator::PackageFiles()
{
2022-08-04 22:12:04 +02:00
if (!PackageFilesImpl() || cmSystemTools::GetErrorOccurredFlag()) {
2018-08-09 18:06:22 +02:00
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()
{
2018-10-28 12:09:07 +01:00
if (!ReadListFile("Internal/CPack/CPackWIX.cmake")) {
2018-08-09 18:06:22 +02:00
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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_WIX_PRODUCT_GUID")) {
2013-03-16 19:13:01 +02:00
std::string guid = GenerateGUID();
2021-11-20 13:41:27 +01:00
SetOption("CPACK_WIX_PRODUCT_GUID", guid);
2013-03-16 19:13:01 +02:00
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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_WIX_UPGRADE_GUID")) {
2013-03-16 19:13:01 +02:00
std::string guid = GenerateGUID();
2021-11-20 13:41:27 +01:00
SetOption("CPACK_WIX_UPGRADE_GUID", guid);
2013-03-16 19:13:01 +02:00
2018-08-09 18:06:22 +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);
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 (!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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_WIX_LICENSE_RTF")) {
2023-12-07 09:12:54 +01:00
std::string licenseFilename =
cmStrCat(this->CPackTopLevel, "/License.rtf");
2021-11-20 13:41:27 +01:00
SetOption("CPACK_WIX_LICENSE_RTF", licenseFilename);
2013-03-16 19:13:01 +02:00
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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_PACKAGE_VENDOR")) {
2014-08-03 19:52:23 +02:00
std::string defaultVendor = "Humanity";
2021-11-20 13:41:27 +01:00
SetOption("CPACK_PACKAGE_VENDOR", defaultVendor);
2014-08-03 19:52:23 +02:00
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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_WIX_UI_REF")) {
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
2021-11-20 13:41:27 +01:00
SetOption("CPACK_WIX_UI_REF", defaultRef);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2021-11-20 13:41:27 +01:00
cmValue packageContact = GetOption("CPACK_PACKAGE_CONTACT");
if (packageContact && !GetOption("CPACK_WIX_PROPERTY_ARPCONTACT")) {
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
2022-03-29 21:10:50 +02:00
if (!cmIsOn(GetOption("CPACK_WIX_SKIP_WIX_UI_EXTENSION"))) {
this->LightExtensions.insert("WixUIExtension");
}
2015-04-27 22:25:09 +02:00
CollectExtensions("CPACK_WIX_EXTENSIONS", this->LightExtensions);
CollectExtensions("CPACK_WIX_LIGHT_EXTENSIONS", this->LightExtensions);
2021-09-14 00:13:48 +02:00
CollectXmlNamespaces("CPACK_WIX_CUSTOM_XMLNS", this->CustomXmlNamespaces);
2014-08-03 19:52:23 +02:00
2021-11-20 13:41:27 +01:00
cmValue patchFilePath = GetOption("CPACK_WIX_PATCH_FILE");
2016-07-09 11:21:54 +02:00
if (patchFilePath) {
2023-07-02 19:51:09 +02:00
cmList patchFilePaths{ patchFilePath };
2016-03-13 13:35:51 +01:00
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
2020-02-01 23:06:01 +01:00
if (cmIsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
2016-10-30 18:24:19 +01:00
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 =
2023-12-07 09:12:54 +01:00
cmStrCat(this->CPackTopLevel, '/', uniqueBaseName, ".wixobj");
2013-03-16 19:13:01 +02:00
2018-04-23 21:13:27 +02:00
if (!RunCandleCommand(CMakeToWixPath(sourceFilename),
CMakeToWixPath(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
2023-12-07 09:12:54 +01:00
objectFiles << ' ' << QuotePath(CMakeToWixPath(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()
{
2021-11-20 13:41:27 +01:00
cmValue cpackWixExtraSources = GetOption("CPACK_WIX_EXTRA_SOURCES");
2023-12-07 09:12:54 +01:00
if (!cpackWixExtraSources) {
2016-07-09 11:21:54 +02:00
return;
2023-12-07 09:12:54 +01:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
cmExpandList(cpackWixExtraSources, this->WixSources);
2014-08-03 19:52:23 +02:00
}
void cmCPackWIXGenerator::AppendUserSuppliedExtraObjects(std::ostream& stream)
{
2021-11-20 13:41:27 +01:00
cmValue cpackWixExtraObjects = GetOption("CPACK_WIX_EXTRA_OBJECTS");
2023-12-07 09:12:54 +01:00
if (!cpackWixExtraObjects) {
2016-07-09 11:21:54 +02:00
return;
2023-12-07 09:12:54 +01:00
}
2014-08-03 19:52:23 +02:00
2023-07-02 19:51:09 +02:00
cmList expandedExtraObjects{ cpackWixExtraObjects };
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
{
2023-12-07 09:12:54 +01:00
std::string includeFilename =
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(includeFile);
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");
2018-04-23 21:13:27 +02:00
CopyDefinition(includeFile, "CPACK_WIX_LICENSE_RTF", DefinitionType::PATH);
CopyDefinition(includeFile, "CPACK_WIX_PRODUCT_ICON", DefinitionType::PATH);
CopyDefinition(includeFile, "CPACK_WIX_UI_BANNER", DefinitionType::PATH);
CopyDefinition(includeFile, "CPACK_WIX_UI_DIALOG", DefinitionType::PATH);
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()
{
2023-12-07 09:12:54 +01:00
std::string includeFilename =
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(includeFile);
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) {
2020-08-30 11:54:41 +02:00
if (cmHasPrefix(name, prefix)) {
2015-04-27 22:25:09 +02:00
std::string id = name.substr(prefix.length());
2021-11-20 13:41:27 +01:00
std::string value = GetOption(name);
2015-04-27 22:25:09 +02:00
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
2021-11-20 13:41:27 +01:00
if (!GetOption("CPACK_WIX_PROPERTY_ARPINSTALLLOCATION")) {
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(
2018-08-09 18:06:22 +02:00
"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()
{
2023-12-07 09:12:54 +01:00
std::string includeFilename =
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(includeFile);
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,
2018-04-23 21:13:27 +02:00
std::string const& name,
DefinitionType type)
2013-03-16 19:13:01 +02:00
{
2021-11-20 13:41:27 +01:00
cmValue value = GetOption(name);
2016-07-09 11:21:54 +02:00
if (value) {
2018-04-23 21:13:27 +02:00
if (type == DefinitionType::PATH) {
2023-05-23 16:38:00 +02:00
AddDefinition(source, name, CMakeToWixPath(*value));
2018-04-23 21:13:27 +02:00
} else {
2023-05-23 16:38:00 +02:00
AddDefinition(source, name, *value);
2018-04-23 21:13:27 +02:00
}
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 =
2023-12-07 09:12:54 +01:00
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(directoryDefinitions);
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
2023-12-07 09:12:54 +01:00
std::string fileDefinitionsFilename =
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(fileDefinitions);
2013-03-16 19:13:01 +02:00
fileDefinitions.BeginElement("Fragment");
std::string featureDefinitionsFilename =
2023-12-07 09:12:54 +01:00
cmStrCat(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);
2021-09-14 00:13:48 +02:00
InjectXmlNamespaces(featureDefinitions);
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;
2021-11-20 13:41:27 +01:00
if (cmValue title = GetOption("CPACK_WIX_ROOT_FEATURE_TITLE")) {
featureTitle = *title;
2016-10-30 18:24:19 +01:00
}
featureDefinitions.AddAttribute("Title", featureTitle);
2021-11-20 13:41:27 +01:00
if (cmValue desc = GetOption("CPACK_WIX_ROOT_FEATURE_DESCRIPTION")) {
2023-05-23 16:38:00 +02:00
featureDefinitions.AddAttribute("Description", *desc);
2016-10-30 18:24:19 +01:00
}
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
2021-11-20 13:41:27 +01:00
cmValue 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(
2023-05-23 16:38:00 +02:00
*package, GetOption("CPACK_WIX_UPGRADE_GUID"));
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 (!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
2020-02-01 23:06:01 +01:00
std::string componentPath = cmStrCat(toplevel, '/', component.Name);
2014-08-03 19:52:23 +02:00
2023-12-07 09:12:54 +01:00
std::string const componentFeatureId = cmStrCat("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
2020-02-01 23:06:01 +01:00
bool emitUninstallShortcut = true;
2021-11-20 13:41:27 +01:00
cmValue cpackWixProgramMenuFolder =
2020-02-01 23:06:01 +01:00
GetOption("CPACK_WIX_PROGRAM_MENU_FOLDER");
2023-12-07 09:12:54 +01:00
if (cpackWixProgramMenuFolder && cpackWixProgramMenuFolder == "."_s) {
2020-02-01 23:06:01 +01:00
emitUninstallShortcut = false;
} else if (emittedShortcutTypes.find(cmWIXShortcuts::START_MENU) ==
emittedShortcutTypes.end()) {
emitUninstallShortcut = false;
}
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
{
2020-02-01 23:06:01 +01:00
if (cmIsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
2016-10-30 18:24:19 +01:00
return "";
}
2017-07-20 19:35:53 +02:00
std::string result = "ProgramFiles<64>Folder";
2021-11-20 13:41:27 +01:00
cmValue rootFolderId = GetOption("CPACK_WIX_ROOT_FOLDER_ID");
2017-07-20 19:35:53 +02:00
if (rootFolderId) {
2021-11-20 13:41:27 +01:00
result = *rootFolderId;
2017-07-20 19:35:53 +02:00
}
2023-12-07 09:12:54 +01:00
if (GetArchitecture() == "x86"_s) {
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");
2021-11-20 13:41:27 +01:00
if (cmValue wixtpl = GetOption("CPACK_WIX_TEMPLATE")) {
wixTemplate = *wixtpl;
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
2023-12-07 09:12:54 +01:00
std::string mainSourceFilePath = cmStrCat(this->CPackTopLevel, "/main.wxs");
2013-03-16 19:13:01 +02:00
2019-11-11 23:01:05 +01:00
if (!ConfigureFile(wixTemplate, mainSourceFilePath)) {
2018-08-09 18:06:22 +02:00
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;
2023-12-07 09:12:54 +01:00
if (group.ParentGroup == nullptr) {
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);
2023-07-02 19:51:09 +02:00
cmList cpackPackageExecutablesList;
2021-11-20 13:41:27 +01:00
cmValue cpackPackageExecutables = GetOption("CPACK_PACKAGE_EXECUTABLES");
2016-07-09 11:21:54 +02:00
if (cpackPackageExecutables) {
2023-07-02 19:51:09 +02:00
cpackPackageExecutablesList.assign(cpackPackageExecutables);
2016-07-09 11:21:54 +02:00
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
2023-07-02 19:51:09 +02:00
cmList cpackPackageDesktopLinksList;
2021-11-20 13:41:27 +01:00
cmValue cpackPackageDesktopLinks = GetOption("CPACK_CREATE_DESKTOP_LINKS");
2016-07-09 11:21:54 +02:00
if (cpackPackageDesktopLinks) {
2023-07-02 19:51:09 +02:00
cpackPackageDesktopLinksList.assign(cpackPackageDesktopLinks);
2016-07-09 11:21:54 +02:00
}
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) {
2020-02-01 23:06:01 +01:00
case cmWIXShortcuts::START_MENU: {
2021-11-20 13:41:27 +01:00
cmValue cpackWixProgramMenuFolder =
2020-02-01 23:06:01 +01:00
GetOption("CPACK_WIX_PROGRAM_MENU_FOLDER");
2023-12-07 09:12:54 +01:00
if (cpackWixProgramMenuFolder && cpackWixProgramMenuFolder == "."_s) {
2020-02-01 23:06:01 +01:00
directoryId = "ProgramMenuFolder";
} else {
directoryId = "PROGRAM_MENU_FOLDER";
}
} break;
2015-08-17 11:37:30 +02:00
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()) {
2023-12-07 09:12:54 +01:00
idSuffix += '_';
2016-07-09 11:21:54 +02:00
idSuffix += cpackComponentName;
}
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
std::string componentId = "CM_SHORTCUT";
2023-12-07 09:12:54 +01:00
if (!idPrefix.empty()) {
componentId += cmStrCat('_', 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 =
2023-12-07 09:12:54 +01:00
cmStrCat("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) {
2021-11-20 13:41:27 +01:00
cmValue cpackWixProgramMenuFolder =
2020-02-01 23:06:01 +01:00
GetOption("CPACK_WIX_PROGRAM_MENU_FOLDER");
2023-12-07 09:12:54 +01:00
if (cpackWixProgramMenuFolder && cpackWixProgramMenuFolder != "."_s) {
fileDefinitions.EmitRemoveFolder(
cmStrCat("CM_REMOVE_PROGRAM_MENU_FOLDER", idSuffix));
2020-02-01 23:06:01 +01:00
}
2016-07-09 11:21:54 +02:00
}
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);
2023-12-07 09:12:54 +01:00
if (extension == ".rtf"_s) {
2016-07-09 11:21:54 +02:00
cmSystemTools::CopyAFile(licenseSourceFilename.c_str(),
licenseDestinationFilename.c_str());
2023-12-07 09:12:54 +01:00
} else if (extension == ".txt"_s) {
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));
2023-12-07 09:12:54 +01:00
if (fileName == "."_s || fileName == ".."_s) {
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
2023-12-07 09:12:54 +01:00
std::string fullPath = cmStrCat(topdir, '/', fileName);
2013-03-16 19:13:01 +02:00
2023-12-07 09:12:54 +01:00
std::string relativePath = cmSystemTools::RelativePath(toplevel, fullPath);
2014-08-03 19:52:23 +02:00
std::string id = PathToId(relativePath);
2023-12-07 09:12:54 +01:00
if (cmSystemTools::FileIsDirectory(fullPath)) {
std::string subDirectoryId = cmStrCat("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) ==
2023-12-07 09:12:54 +01:00
cmStrCat(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
2020-08-30 11:54:41 +02:00
if (cm::contains(desktopExecutables, executableName)) {
2016-07-09 11:21:54 +02:00
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
{
2021-11-20 13:41:27 +01:00
cmValue tmp = GetOption(name);
2016-07-09 11:21:54 +02:00
if (tmp) {
2021-11-20 13:41:27 +01:00
value = *tmp;
2013-03-16 19:13:01 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Required variable " << name << " not set" << std::endl);
return false;
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);
2023-12-07 09:12:54 +01:00
if (void_p_size == "8"_s) {
2013-03-16 19:13:01 +02:00
return "x64";
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
return "x86";
2013-03-16 19:13:01 +02:00
}
std::string cmCPackWIXGenerator::GenerateGUID()
{
2018-04-23 21:13:27 +02:00
#ifdef _WIN32
2013-03-16 19:13:01 +02:00
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);
2018-04-23 21:13:27 +02:00
#else
uuid_t guid;
char guid_ch[37] = { 0 };
uuid_generate(guid);
uuid_unparse(guid, guid_ch);
std::string result = guid_ch;
#endif
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
{
2023-12-07 09:12:54 +01:00
return cmStrCat('"', path, '"');
2013-03-16 19:13:01 +02:00
}
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;
2023-12-07 09:12:54 +01:00
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
{
2023-12-07 09:12:54 +01:00
auto i = PathToIdMap.find(path);
if (i != PathToIdMap.end()) {
2016-07-09 11:21:54 +02:00
return i->second;
2023-12-07 09:12:54 +01:00
}
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;
2023-12-07 09:12:54 +01:00
cmSystemTools::SplitPath(path, components, false);
2014-08-03 19:52:23 +02:00
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) {
2023-12-07 09:12:54 +01:00
if (i != 1) {
2016-07-09 11:21:54 +02:00
identifier += '.';
2023-12-07 09:12:54 +01:00
}
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;
2023-12-07 09:12:54 +01:00
result << idPrefix << '_' << identifier;
2014-08-03 19:52:23 +02:00
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 '"
2023-12-07 09:12:54 +01:00
<< path << '\'' << std::endl);
2014-08-03 19:52:23 +02:00
return std::string();
2023-12-07 09:12:54 +01:00
}
if (ambiguityCount > 1) {
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
const size_t maxFileNameLength = 52;
2020-08-30 11:54:41 +02:00
std::string identifier =
cmStrCat(cm::string_view(hash).substr(0, 7), '_',
cm::string_view(normalizedFilename).substr(0, maxFileNameLength));
// if the name was truncated
2016-07-09 11:21:54 +02:00
if (normalizedFilename.length() > maxFileNameLength) {
2014-08-03 19:52:23 +02:00
identifier += "...";
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
{
2021-11-20 13:41:27 +01:00
cmValue variableContent = GetOption(variableName);
2023-12-07 09:12:54 +01:00
if (!variableContent) {
2016-07-09 11:21:54 +02:00
return;
2023-12-07 09:12:54 +01:00
}
2014-08-03 19:52:23 +02:00
2023-07-02 19:51:09 +02:00
cmList list{ variableContent };
2015-04-27 22:25:09 +02:00
extensions.insert(list.begin(), list.end());
2014-08-03 19:52:23 +02:00
}
2021-09-14 00:13:48 +02:00
void cmCPackWIXGenerator::CollectXmlNamespaces(std::string const& variableName,
xmlns_map_t& namespaces)
{
2021-11-20 13:41:27 +01:00
cmValue variableContent = GetOption(variableName);
2021-09-14 00:13:48 +02:00
if (!variableContent) {
return;
}
2023-07-02 19:51:09 +02:00
cmList list{ variableContent };
2021-09-14 00:13:48 +02:00
for (std::string const& str : list) {
auto pos = str.find('=');
if (pos != std::string::npos) {
auto name = str.substr(0, pos);
auto value = str.substr(pos + 1);
namespaces.emplace(std::make_pair(name, value));
} else {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Invalid element in CPACK_WIX_CUSTOM_XMLNS ignored: "
2023-12-07 09:12:54 +01:00
"\""
<< str << '"' << std::endl);
2021-09-14 00:13:48 +02:00
}
}
std::ostringstream oss;
for (auto& ns : namespaces) {
oss << " xmlns:" << ns.first << "=\""
<< cmWIXSourceWriter::EscapeAttributeValue(ns.second) << '"';
}
2021-11-20 13:41:27 +01:00
SetOption("CPACK_WIX_CUSTOM_XMLNS_EXPANDED", oss.str());
2021-09-14 00:13:48 +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
{
2021-11-20 13:41:27 +01:00
cmValue variableContent = GetOption(variableName);
2023-12-07 09:12:54 +01:00
if (!variableContent) {
2016-07-09 11:21:54 +02:00
return;
2023-12-07 09:12:54 +01:00
}
2014-08-03 19:52:23 +02:00
2023-07-02 19:51:09 +02:00
cmList list{ variableContent };
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& i : list) {
2023-12-07 09:12:54 +01:00
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);
}
2021-09-14 00:13:48 +02:00
void cmCPackWIXGenerator::InjectXmlNamespaces(cmWIXSourceWriter& sourceWriter)
{
for (auto& ns : this->CustomXmlNamespaces) {
2023-12-07 09:12:54 +01:00
sourceWriter.AddAttributeUnlessEmpty(cmStrCat("xmlns:", ns.first),
ns.second);
2021-09-14 00:13:48 +02:00
}
}