Commit 85baac15 authored by Nils Gladitz's avatar Nils Gladitz Committed by David Cole

CPack: Add a WiX Generator (#11575)

This new CPack generator produces an *.msi installer file.
Requires having the WiX Toolset installed in order to work
properly.

Download the WiX Toolset installer "WiX36.exe" here:

  http://wix.codeplex.com/releases/view/93929
parent 581b0c0d
......@@ -51,3 +51,27 @@ if("${CPACK_GENERATOR}" STREQUAL "PackageMaker")
set(CPACK_PACKAGE_DEFAULT_LOCATION "/usr")
endif()
endif()
if("${CPACK_GENERATOR}" STREQUAL "WIX")
# Reset CPACK_PACKAGE_VERSION to deal with WiX restriction.
# But the file names still use the full CMake_VERSION value:
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-@CMake_VERSION@-${CPACK_SYSTEM_NAME}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-@CMake_VERSION@-Source")
if(NOT CPACK_WIX_SIZEOF_VOID_P)
set(CPACK_WIX_SIZEOF_VOID_P "@CMAKE_SIZEOF_VOID_P@")
endif()
set(CPACK_PACKAGE_VERSION
"@CMake_VERSION_MAJOR@.@CMake_VERSION_MINOR@.@CMake_VERSION_PATCH@")
# WIX installers require at most a 4 component version number, where
# each component is an integer between 0 and 65534 inclusive
set(tweak "@CMake_VERSION_TWEAK@")
if(tweak MATCHES "^[0-9]+$")
if(tweak GREATER 0 AND tweak LESS 65535)
set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}.${tweak}")
endif()
endif()
endif()
......@@ -438,6 +438,7 @@ if(NOT CPACK_GENERATOR)
endif()
else()
option(CPACK_BINARY_NSIS "Enable to build NSIS packages" ON)
option(CPACK_BINARY_WIX "Enable to build WiX packages" OFF)
option(CPACK_BINARY_ZIP "Enable to build ZIP packages" OFF)
endif()
......@@ -453,6 +454,7 @@ if(NOT CPACK_GENERATOR)
cpack_optional_append(CPACK_GENERATOR CPACK_BINARY_TGZ TGZ)
cpack_optional_append(CPACK_GENERATOR CPACK_BINARY_TBZ2 TBZ2)
cpack_optional_append(CPACK_GENERATOR CPACK_BINARY_TZ TZ)
cpack_optional_append(CPACK_GENERATOR CPACK_BINARY_WIX WIX)
cpack_optional_append(CPACK_GENERATOR CPACK_BINARY_ZIP ZIP)
endif()
......@@ -483,7 +485,7 @@ endif()
mark_as_advanced(CPACK_BINARY_CYGWIN CPACK_BINARY_PACKAGEMAKER CPACK_BINARY_OSXX11
CPACK_BINARY_STGZ CPACK_BINARY_TGZ CPACK_BINARY_TBZ2
CPACK_BINARY_DEB CPACK_BINARY_RPM CPACK_BINARY_TZ
CPACK_BINARY_NSIS CPACK_BINARY_ZIP CPACK_BINARY_BUNDLE
CPACK_BINARY_NSIS CPACK_BINARY_WIX CPACK_BINARY_ZIP CPACK_BINARY_BUNDLE
CPACK_SOURCE_CYGWIN CPACK_SOURCE_TBZ2 CPACK_SOURCE_TGZ
CPACK_SOURCE_TZ CPACK_SOURCE_ZIP CPACK_BINARY_DRAGNDROP)
......@@ -522,6 +524,9 @@ cpack_set_if_not_set(CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
cpack_set_if_not_set(CPACK_NSIS_INSTALLER_ICON_CODE "")
cpack_set_if_not_set(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "")
# WiX specific variables
cpack_set_if_not_set(CPACK_WIX_SIZEOF_VOID_P "${CMAKE_SIZEOF_VOID_P}")
if(DEFINED CPACK_COMPONENTS_ALL)
if(CPACK_MONOLITHIC_INSTALL)
message("CPack warning: both CPACK_COMPONENTS_ALL and CPACK_MONOLITHIC_INSTALL have been set.\nDefaulting to a monolithic installation.")
......
##section Variables specific to CPack WiX generator
##end
##module
# - CPack WiX generator specific options
#
# The following variables are specific to the installers built
# on Windows using WiX.
##end
##variable
# CPACK_WIX_UPGRADE_GUID - Upgrade GUID (Product/@UpgradeCode)
#
# Will be automatically generated unless explicitly provided.
#
# It should be explicitly set to a constant generated
# gloabally unique identifier (GUID) to allow your installers
# to replace existing installations that use the same GUID.
#
# You may for example explicitly set this variable in
# your CMakeLists.txt to the value that has been generated per default.
# You should not use GUIDs that you did not generate yourself or which may
# belong to other projects.
#
# A GUID shall have the following fixed length syntax:
# XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
# (each X represents an uppercase hexadecimal digit)
##end
##variable
# CPACK_WIX_PRODUCT_GUID - Product GUID (Product/@Id)
#
# Will be automatically generated unless explicitly provided.
#
# If explicitly provided this will set the Product Id of your installer.
#
# The installer will abort if it detects a pre-existing installation that uses
# the same GUID.
#
# The GUID shall use the syntax described for CPACK_WIX_UPGRADE_GUID.
##end
##variable
# CPACK_WIX_LICENSE_RTF - RTF License File
#
# If CPACK_RESOURCE_FILE_LICENSE has an .rtf extension
# it is used as-is.
#
# If CPACK_RESOURCE_FILE_LICENSE has an .txt extension
# it is implicitly converted to RTF by the WiX Generator.
#
# With CPACK_WIX_LICENSE_RTF you can override the license file used
# by the WiX Generator in case CPACK_RESOURCE_FILE_LICENSE
# is in an unsupported format or the .txt -> .rtf
# conversion does not work as expected.
##end
#=============================================================================
# Copyright 2012 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
if(NOT CPACK_WIX_ROOT)
file(TO_CMAKE_PATH "$ENV{WIX}" CPACK_WIX_ROOT)
endif()
find_program(CPACK_WIX_CANDLE_EXECUTABLE candle
PATHS "${CPACK_WIX_ROOT}/bin")
if(NOT CPACK_WIX_CANDLE_EXECUTABLE)
message(FATAL_ERROR "Could not find the WiX candle executable.")
endif()
find_program(CPACK_WIX_LIGHT_EXECUTABLE light
PATHS "${CPACK_WIX_ROOT}/bin")
if(NOT CPACK_WIX_LIGHT_EXECUTABLE)
message(FATAL_ERROR "Could not find the WiX light executable.")
endif()
<?xml version="1.0" encoding="UTF-8"?>
<?include "cpack_variables.wxi"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
RequiredVersion="3.6.3303.0">
<Product Id="$(var.CPACK_WIX_PRODUCT_GUID)"
Name="$(var.CPACK_PACKAGE_NAME)"
Language="1033"
Version="$(var.CPACK_PACKAGE_VERSION)"
Manufacturer="$(var.CPACK_PACKAGE_VENDOR)"
UpgradeCode="$(var.CPACK_WIX_UPGRADE_GUID)">
<Package InstallerVersion="301" Compressed="yes"/>
<Media Id="1" Cabinet="media1.cab" EmbedCab="yes"/>
<MajorUpgrade
Schedule="afterInstallInitialize"
AllowSameVersionUpgrades="yes"
DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit."/>
<WixVariable Id="WixUILicenseRtf" Value="$(var.CPACK_WIX_LICENSE_RTF)"/>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALL_ROOT"/>
<FeatureRef Id="ProductFeature"/>
<UIRef Id="WixUI_InstallDir" />
</Product>
</Wix>
......@@ -499,6 +499,14 @@ if(UNIX)
)
endif()
if(WIN32)
set(CPACK_SRCS ${CPACK_SRCS}
CPack/WiX/cmCPackWIXGenerator.cxx
CPack/WiX/cmWIXSourceWriter.cxx
CPack/WiX/cmWIXRichTextFormatWriter.cxx
)
endif()
if(APPLE)
set(CPACK_SRCS ${CPACK_SRCS}
CPack/cmCPackBundleGenerator.cxx
......
This diff is collapsed.
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2012 Kitware, Inc.
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#ifndef cmCPackWIXGenerator_h
#define cmCPackWIXGenerator_h
#include <CPack/cmCPackGenerator.h>
#include <string>
#include <map>
class cmWIXSourceWriter;
/** \class cmCPackWIXGenerator
* \brief A generator for WIX files
*/
class cmCPackWIXGenerator : public cmCPackGenerator
{
public:
cmCPackTypeMacro(cmCPackWIXGenerator, cmCPackGenerator);
protected:
virtual int InitializeInternal();
virtual int PackageFiles();
virtual const char* GetOutputExtension()
{
return ".msi";
}
virtual enum CPackSetDestdirSupport SupportsSetDestdir() const
{
return SETDESTDIR_UNSUPPORTED;
}
virtual bool SupportsAbsoluteDestination() const
{
return false;
}
virtual bool SupportsComponentInstallation() const
{
return false;
}
private:
bool InitializeWiXConfiguration();
bool PackageFilesImpl();
bool CreateWiXVariablesIncludeFile();
void CopyDefinition(
cmWIXSourceWriter &source, const std::string &name);
void AddDefinition(cmWIXSourceWriter& source,
const std::string& name, const std::string& value);
bool CreateWiXSourceFiles();
bool CreateLicenseFile();
bool RunWiXCommand(const std::string& command);
bool RunCandleCommand(
const std::string& sourceFile, const std::string& objectFile);
bool RunLightCommand(const std::string& objectFiles);
void AddDirectoryAndFileDefinitons(const std::string& topdir,
const std::string& directoryId,
cmWIXSourceWriter& directoryDefinitions,
cmWIXSourceWriter& fileDefinitions,
cmWIXSourceWriter& featureDefinitions,
std::size_t& directoryCounter,
std::size_t& fileCounter);
bool RequireOption(const std::string& name, std::string& value) const;
std::string GetArchitecture() const;
static std::string GenerateGUID();
static std::string QuotePath(const std::string& path);
static std::string GetRightmostExtension(const std::string& filename);
std::vector<std::string> wixSources;
};
#endif
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2012 Kitware, Inc.
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmWIXRichTextFormatWriter.h"
#include <cmVersion.h>
cmWIXRichTextFormatWriter::cmWIXRichTextFormatWriter(
const std::string& filename):
file(filename.c_str(), std::ios::binary)
{
StartGroup();
WriteHeader();
WriteDocumentPrefix();
}
cmWIXRichTextFormatWriter::~cmWIXRichTextFormatWriter()
{
EndGroup();
/* I haven't seen this in the RTF spec but
* wordpad terminates its RTF like this */
file << "\r\n";
file.put(0);
}
void cmWIXRichTextFormatWriter::AddText(const std::string& text)
{
typedef unsigned char rtf_byte_t;
for(std::size_t i = 0; i < text.size(); ++i)
{
rtf_byte_t c = rtf_byte_t(text[i]);
switch(c)
{
case '\\':
file << "\\\\";
break;
case '{':
file << "\\{";
break;
case '}':
file << "\\}";
break;
case '\n':
file << "\\par\r\n";
break;
case '\r':
continue;
default:
{
if(c <= 0x7F)
{
file << c;
}
else
{
file << "[NON-ASCII-" << int(c) << "]";
}
}
break;
}
}
}
void cmWIXRichTextFormatWriter::WriteHeader()
{
ControlWord("rtf1");
ControlWord("ansi");
ControlWord("ansicpg1252");
ControlWord("deff0");
ControlWord("deflang1031");
WriteFontTable();
WriteGenerator();
}
void cmWIXRichTextFormatWriter::WriteFontTable()
{
StartGroup();
ControlWord("fonttbl");
StartGroup();
ControlWord("f0");
ControlWord("fswiss");
ControlWord("fcharset0 Arial;");
EndGroup();
EndGroup();
}
void cmWIXRichTextFormatWriter::WriteGenerator()
{
StartGroup();
NewControlWord("generator");
file << " CPack WiX Generator (" << cmVersion::GetCMakeVersion() << ");";
EndGroup();
}
void cmWIXRichTextFormatWriter::WriteDocumentPrefix()
{
ControlWord("viewkind4");
ControlWord("uc1");
ControlWord("pard");
ControlWord("f0");
ControlWord("fs20");
}
void cmWIXRichTextFormatWriter::ControlWord(const std::string& keyword)
{
file << "\\" << keyword;
}
void cmWIXRichTextFormatWriter::NewControlWord(const std::string& keyword)
{
file << "\\*\\" << keyword;
}
void cmWIXRichTextFormatWriter::StartGroup()
{
file.put('{');
}
void cmWIXRichTextFormatWriter::EndGroup()
{
file.put('}');
}
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2012 Kitware, Inc.
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#ifndef cmWIXRichTextFormatWriter_h
#define cmWIXRichTextFormatWriter_h
#include <fstream>
/** \class cmWIXRichtTextFormatWriter
* \brief Helper class to generate Rich Text Format (RTF) documents
* from plain text (e.g. for license and welcome text)
*/
class cmWIXRichTextFormatWriter
{
public:
cmWIXRichTextFormatWriter(const std::string& filename);
~cmWIXRichTextFormatWriter();
void AddText(const std::string& text);
private:
void WriteHeader();
void WriteFontTable();
void WriteGenerator();
void WriteDocumentPrefix();
void ControlWord(const std::string& keyword);
void NewControlWord(const std::string& keyword);
void StartGroup();
void EndGroup();
std::ofstream file;
};
#endif
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2012 Kitware, Inc.
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmWIXSourceWriter.h"
#include <CPack/cmCPackGenerator.h>
#include <windows.h>
cmWIXSourceWriter::cmWIXSourceWriter(cmCPackLog* logger,
const std::string& filename,
bool isIncludeFile):
Logger(logger),
file(filename.c_str()),
state(DEFAULT)
{
WriteXMLDeclaration();
if(isIncludeFile)
{
BeginElement("Include");
}
else
{
BeginElement("Wix");
}
AddAttribute("xmlns", "http://schemas.microsoft.com/wix/2006/wi");
}
cmWIXSourceWriter::~cmWIXSourceWriter()
{
while(elements.size())
{
EndElement();
}
}
void cmWIXSourceWriter::BeginElement(const std::string& name)
{
if(state == BEGIN)
{
file << ">";
}
file << "\n";
Indent(elements.size());
file << "<" << name;
elements.push_back(name);
state = BEGIN;
}
void cmWIXSourceWriter::EndElement()
{
if(elements.empty())
{
cmCPackLogger(cmCPackLog::LOG_ERROR,
"can not end WiX element with no open elements" << std::endl);
return;
}
if(state == DEFAULT)
{
file << "\n";
Indent(elements.size()-1);
file << "</" << elements.back() << ">";
}
else
{
file << "/>";
}
elements.pop_back();
state = DEFAULT;
}
void cmWIXSourceWriter::AddProcessingInstruction(
const std::string& target, const std::string& content)
{
if(state == BEGIN)
{
file << ">";
}
file << "\n";
Indent(elements.size());
file << "<?" << target << " " << content << "?>";
state = DEFAULT;
}
void cmWIXSourceWriter::AddAttribute(
const std::string& key, const std::string& value)
{
std::string utf8 = WindowsCodepageToUtf8(value);
file << " " << key << "=\"" << EscapeAttributeValue(utf8) << '"';
}
std::string cmWIXSourceWriter::WindowsCodepageToUtf8(const std::string& value)
{
if(value.empty())
{
return std::string();
}
int characterCount = MultiByteToWideChar(
CP_ACP, 0, value.c_str(), value.size(), 0, 0);
if(characterCount == 0)
{
return std::string();
}
std::vector<wchar_t> utf16(characterCount);
MultiByteToWideChar(
CP_ACP, 0, value.c_str(), value.size(), &utf16[0], utf16.size());
int utf8ByteCount =
WideCharToMultiByte(CP_UTF8, 0, &utf16[0], utf16.size(), 0, 0, 0, 0);
if(utf8ByteCount == 0)
{
return std::string();
}
std::vector<char> utf8(utf8ByteCount);
WideCharToMultiByte(CP_UTF8, 0, &utf16[0], utf16.size(),
&utf8[0], utf8.size(), 0, 0);
return std::string(&utf8[0], utf8.size());
}
void cmWIXSourceWriter::WriteXMLDeclaration()
{
file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
}
void cmWIXSourceWriter::Indent(std::size_t count)
{
for(std::size_t i = 0; i < count; ++i)
{
file << " ";
}
}
std::string cmWIXSourceWriter::EscapeAttributeValue(
const std::string& value)
{
std::string result;
result.reserve(value.size());
char c = 0;
for(std::size_t i = 0 ; i < value.size(); ++i)
{
c = value[i];
switch(c)
{
case '<':
result += "&lt;";
break;
case '&':
result +="&amp;";
break;
case '"':
result += "&quot;";
break;
default:
result += c;
break;
}
}
return result;
}
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2012 Kitware, Inc.
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#ifndef cmWIXSourceWriter_h
#define cmWIXSourceWriter_h
#include <vector>
#include <string>
#include <fstream>
#include <CPack/cmCPackLog.h>
/** \class cmWIXSourceWriter
* \brief Helper class to generate XML WiX source files
*/
class cmWIXSourceWriter
{
public:
cmWIXSourceWriter(cmCPackLog* logger,
const std::string& filename, bool isIncludeFile = false);
~cmWIXSourceWriter();
void BeginElement(const std::string& name);
void EndElement();
void AddProcessingInstruction(
const std::string& target, const std::string& content);
void AddAttribute(
const std::string& key, const std::string& value);
static std::string WindowsCodepageToUtf8(const std::string& value);
private:
enum State
{
DEFAULT,
BEGIN
};
void WriteXMLDeclaration();
void Indent(std::size_t count);
static std::string EscapeAttributeValue(const std::string& value);
std::ofstream file;
std::vector<std::string> elements;
State state;
cmCPackLog* Logger;
};