From 5358809a5c1010739923b3bec22a63ba4fe2f657 Mon Sep 17 00:00:00 2001
From: Andy Cedilnik <andy.cedilnik@kitware.com>
Date: Wed, 4 Jan 2006 15:14:09 -0500
Subject: ENH: More CPack stuff and fix zlib compression

---
 CMakeLists.txt                           | 11 +++++
 Modules/CPack.Description.plist.in       |  6 +--
 Modules/CPack.cmake                      | 48 +++++++++++++++++++++
 Modules/NSIS.template.in                 | 20 ++++-----
 Source/CPack/cmCPackGenericGenerator.cxx | 28 ++++++------
 Source/CPack/cmCPackSTGZGenerator.cxx    |  4 +-
 Source/CPack/cmCPackTGZGenerator.cxx     | 73 ++++++++++++++++++++++++++++----
 Source/CPack/cpack.cxx                   | 34 ++++++++++-----
 Templates/CPackConfig.cmake.in           | 10 +++++
 9 files changed, 188 insertions(+), 46 deletions(-)
 create mode 100644 Modules/CPack.cmake
 create mode 100644 Templates/CPackConfig.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 16c8633..1462303 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -178,3 +178,14 @@ ADD_CUSTOM_TARGET(uninstall
 
 # include support for making the release
 INCLUDE (${CMake_SOURCE_DIR}/Utilities/Release/Release.cmake)
+
+# If the cmake version includes cpack, use it
+IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 2.2)
+  SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "CMake is a build tool")
+  SET(CPACK_PACKAGE_VENDOR "Kitware")
+  SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt")
+  SET(CPACK_PACKAGE_VERSION_MAJOR "${CMake_VERSION_MAJOR}")
+  SET(CPACK_PACKAGE_VERSION_MINOR "${CMake_VERSION_MINOR}")
+  SET(CPACK_PACKAGE_VERSION_PATCH "${CMake_VERSION_PATCH}")
+  INCLUDE(CPack)
+ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 2.2)
diff --git a/Modules/CPack.Description.plist.in b/Modules/CPack.Description.plist.in
index 310eb68..3d11476 100644
--- a/Modules/CPack.Description.plist.in
+++ b/Modules/CPack.Description.plist.in
@@ -3,10 +3,10 @@
 <plist version="1.4">
 <dict>
 	<key>IFPkgDescriptionTitle</key>
-	<string>@CPACK_PROJECT_NAME@</string>
+	<string>@CPACK_PACKAGE_NAME@</string>
   <key>IFPkgDescriptionVersion</key>
-  <string>@CPACK_PROJECT_VERSION@-@CPACK_PROJECT_VERSION_PATCH@</string>
+  <string>@CPACK_PACKAGE_VERSION@</string>
   <key>IFPkgDescriptionDescription</key>
-  <string>@CPACK_PROJECT_DESCRIPTION@</string>
+  <string>@CPACK_PACKAGE_DESCRIPTION@</string>
 </dict>
 </plist>
diff --git a/Modules/CPack.cmake b/Modules/CPack.cmake
new file mode 100644
index 0000000..39c773c
--- /dev/null
+++ b/Modules/CPack.cmake
@@ -0,0 +1,48 @@
+# Pick a configuration file
+SET(cpack_input_file "${CMAKE_ROOT}/Templates/CPackConfig.cmake.in")
+IF(EXISTS "${CMAKE_SOURCE_DIR}/CPackConfig.cmake.in")
+  SET(cpack_input_file "${CMAKE_SOURCE_DIR}/CPackConfig.cmake.in")
+ENDIF(EXISTS "${CMAKE_SOURCE_DIR}/CPackConfig.cmake.in")
+
+# Macro for setting values if a user did not overwrite them
+MACRO(cpack_set_if_not_set name value)
+  IF(NOT "${name}")
+    SET(${name} "${value}")
+  ENDIF(NOT "${name}")
+ENDMACRO(cpack_set_if_not_set)
+
+# Set the package name
+cpack_set_if_not_set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
+cpack_set_if_not_set(CPACK_PACKAGE_VERSION_MAJOR "0")
+cpack_set_if_not_set(CPACK_PACKAGE_VERSION_MINOR "1")
+cpack_set_if_not_set(CPACK_PACKAGE_VERSION_PATCH "1")
+cpack_set_if_not_set(CPACK_PACKAGE_VENDOR "Humanity")
+cpack_set_if_not_set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_NAME} built using CMake")
+cpack_set_if_not_set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_ROOT}/Templates/CPack.GenericDescription.txt")
+
+# <project>-<major>.<minor>.<patch>-<release>-<platform>.<pkgtype>
+cpack_set_if_not_set(CPACK_PACKAGE_FILE_NAME
+  "${CPACK_PACKAGE_NAME}.${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}")
+
+IF(NOT EXISTS "${CPACK_PACKAGE_DESCRIPTION_FILE}")
+  MESSAGE(SEND_ERROR "CPack package description file: \"${CPACK_PACKAGE_DESCRIPTION_FILE}\" could not be found.")
+ENDIF(NOT EXISTS "${CPACK_PACKAGE_DESCRIPTION_FILE}")
+
+# Pick a generator
+IF(NOT CPACK_GENERATOR)
+  IF(UNIX)
+    IF(APPLE)
+      SET(CPACK_GENERATOR "PackageMaker")
+    ELSE(APPLE)
+      SET(CPACK_GENERATOR "TGZ")
+    ENDIF(APPLE)
+  ELSE(UNIX)
+    SET(CPACK_GENERATOR "NSIS")
+  ENDIF(UNIX)
+ENDIF(NOT CPACK_GENERATOR)
+
+# Set some other variables
+SET(CPACK_SOURCE_DIR "${CMAKE_SOURCE_DIR}")
+SET(CPACK_BINARY_DIR "${CMAKE_BINARY_DIR}")
+
+CONFIGURE_FILE("${cpack_input_file}" "${CMAKE_BINARY_DIR}/CPackConfig.cmake" @ONLY IMMEDIATE)
diff --git a/Modules/NSIS.template.in b/Modules/NSIS.template.in
index 4ede861..a0b3613 100644
--- a/Modules/NSIS.template.in
+++ b/Modules/NSIS.template.in
@@ -3,8 +3,8 @@
 ;--------------------------------
 ; You must define these values
 
-  !define VERSION "@CPACK_PROJECT_VERSION@"
-  !define PATCH  "@CPACK_PROJECT_VERSION_PATCH@"
+  !define VERSION "@CPACK_PACKAGE_VERSION@"
+  !define PATCH  "@CPACK_PACKAGE_VERSION_PATCH@"
   !define INST_DIR @CPACK_TEMPORARY_DIRECTORY@
 
 ;--------------------------------
@@ -19,7 +19,7 @@
   !include "MUI.nsh"
 
   ;Default installation folder
-  InstallDir "$PROGRAMFILES\@CPACK_PROJECT_NAME@ ${VERSION}"  
+  InstallDir "$PROGRAMFILES\@CPACK_PACKAGE_NAME@ ${VERSION}"  
 
 ;--------------------------------
 ; determine admin versus local install
@@ -51,8 +51,8 @@ FunctionEnd
 ;General
 
   ;Name and file
-  Name "@CPACK_PROJECT_NAME@ ${VERSION}"
-  OutFile "@CPACK_TOPLEVEL_DIRECTORY@/@CPACK_PROJECT_NAME@-${VERSION}-${PATCH}-win32.exe"
+  Name "@CPACK_PACKAGE_NAME@ ${VERSION}"
+  OutFile "@CPACK_TOPLEVEL_DIRECTORY@/@CPACK_PACKAGE_NAME@-${VERSION}-${PATCH}-win32.exe"
 
 ;--------------------------------
 ;Interface Settings
@@ -303,7 +303,7 @@ FunctionEnd
 ;--------------------------------
 ; Define some macro setting for the gui
 
-  !define MUI_HEADERIMAGE_BITMAP "@CPACK_PROJECT_ICON@"
+  !define MUI_HEADERIMAGE_BITMAP "@CPACK_PACKAGE_ICON@"
 
 ;--------------------------------
 ;Pages
@@ -312,7 +312,7 @@ FunctionEnd
   
   ;Start Menu Folder Page Configuration
   !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX" 
-  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\@CPACK_PROJECT_VENDOR@\@CPACK_PROJECT_NAME@ ${VERSION}" 
+  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}" 
   !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
   !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
 
@@ -349,7 +349,7 @@ Section "Dummy Section" SecDummy
   @CPACK_NSIS_EXTRA_COMMANDS@
 
   ;Store installation folder
-  WriteRegStr SHCTX "Software\@CPACK_PROJECT_VENDOR@\@CPACK_PROJECT_NAME@ ${VERSION}" "" $INSTDIR
+  WriteRegStr SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}" "" $INSTDIR
   
   ;Create uninstaller
   WriteUninstaller "$INSTDIR\Uninstall.exe"
@@ -409,7 +409,7 @@ Section "Uninstall"
   RMDir "$INSTDIR"
 
   ; Remove the registry entries.
-  DeleteRegKey SHCTX "Software\@CPACK_PROJECT_VENDOR@\@CPACK_PROJECT_NAME@ ${VERSION}"
+  DeleteRegKey SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}"
 
   !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
     
@@ -429,7 +429,7 @@ Section "Uninstall"
     StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
   startMenuDeleteLoopDone:
 
-  DeleteRegKey /ifempty SHCTX "Software\@CPACK_PROJECT_VENDOR@\@CPACK_PROJECT_NAME@ ${VERSION}"
+  DeleteRegKey /ifempty SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}"
 
   Push $INSTDIR\bin
   Call un.RemoveFromPath
diff --git a/Source/CPack/cmCPackGenericGenerator.cxx b/Source/CPack/cmCPackGenericGenerator.cxx
index 7647144..7c1eb51 100644
--- a/Source/CPack/cmCPackGenericGenerator.cxx
+++ b/Source/CPack/cmCPackGenericGenerator.cxx
@@ -45,15 +45,15 @@ cmCPackGenericGenerator::~cmCPackGenericGenerator()
 int cmCPackGenericGenerator::PrepareNames()
 {
   this->SetOption("CPACK_GENERATOR", m_Name.c_str());
-  std::string tempDirectory = this->GetOption("CPACK_PROJECT_DIRECTORY");
+  std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
   tempDirectory += "/_CPack_Packages/";
   tempDirectory += this->GetOption("CPACK_GENERATOR");
   std::string topDirectory = tempDirectory;
 
-  std::string outName = this->GetOption("CPACK_PROJECT_NAME");
+  std::string outName = this->GetOption("CPACK_PACKAGE_NAME");
   outName += "-";
-  outName += this->GetOption("CPACK_PROJECT_VERSION");
-  const char* patch = this->GetOption("CPACK_PROJECT_VERSION_PATCH");
+  outName += this->GetOption("CPACK_PACKAGE_VERSION");
+  const char* patch = this->GetOption("CPACK_PACKAGE_VERSION_PATCH");
   if ( patch && *patch )
     {
     outName += "-";
@@ -71,10 +71,10 @@ int cmCPackGenericGenerator::PrepareNames()
   outName += this->GetOutputExtension();
 
 
-  std::string installFile = this->GetOption("CPACK_PROJECT_DIRECTORY");
+  std::string installFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
   installFile += "/cmake_install.cmake";
 
-  std::string destFile = this->GetOption("CPACK_PROJECT_DIRECTORY");
+  std::string destFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
   destFile += "/" + outName;
 
   std::string outFile = topDirectory + "/" + outName;
@@ -91,8 +91,8 @@ int cmCPackGenericGenerator::PrepareNames()
     cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
   this->SetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY", installPrefix.c_str());
 
-  cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: CPACK_PROJECT_DESCRIPTION_FILE_NAME" << std::endl);
-  const char* descFileName = this->GetOption("CPACK_PROJECT_DESCRIPTION_FILE_NAME");
+  cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
+  const char* descFileName = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
   cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: " << descFileName << std::endl);
   if ( descFileName )
     {
@@ -109,16 +109,18 @@ int cmCPackGenericGenerator::PrepareNames()
       }
     cmOStringStream ostr;
     std::string line;
+
+    cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Read description file: " << descFileName << std::endl);
     while ( ifs && cmSystemTools::GetLineFromStream(ifs, line) )
       {
       ostr << cmSystemTools::MakeXMLSafe(line.c_str()) << std::endl;
       }
-    this->SetOption("CPACK_PROJECT_DESCRIPTION", ostr.str().c_str());
+    this->SetOption("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
     }
-  if ( !this->GetOption("CPACK_PROJECT_DESCRIPTION") )
+  if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
     {
     cmCPackLogger(cmCPackLog::LOG_ERROR,
-      "Project description not specified. Please specify CPACK_PROJECT_DESCRIPTION or CPACK_PROJECT_DESCRIPTION_FILE_NAME."
+      "Project description not specified. Please specify CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE_NAME."
       << std::endl);
     return 0;
     }
@@ -427,9 +429,9 @@ const char* cmCPackGenericGenerator::GetInstallPath()
     m_InstallPath = "c:/Program Files";
     }
   m_InstallPath += "/";
-  m_InstallPath += this->GetOption("CPACK_PROJECT_NAME");
+  m_InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
   m_InstallPath += "-";
-  m_InstallPath += this->GetOption("CPACK_PROJECT_VERSION");
+  m_InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
 #else
   m_InstallPath = "/usr/local/";
 #endif
diff --git a/Source/CPack/cmCPackSTGZGenerator.cxx b/Source/CPack/cmCPackSTGZGenerator.cxx
index ca021a4..64e4b76 100644
--- a/Source/CPack/cmCPackSTGZGenerator.cxx
+++ b/Source/CPack/cmCPackSTGZGenerator.cxx
@@ -41,7 +41,7 @@ int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
   cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
   *os
     << "#!/bin/sh" << std::endl
-    << "echo \"" << this->GetOption("ProjectName")
+    << "echo \"" << this->GetOption("CPACK_PACKAGE_NAME")
     << " - self-extracting archive.\"" << std::endl
     << "echo \"If you want to stop extracting, please press <ctrl-C>.\"" << std::endl
     << "read line" << std::endl
@@ -58,6 +58,6 @@ int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
     << "#-----------------------------------------------------------" << std::endl
     << "#      Start of TAR.GZ file" << std::endl
     << "#-----------------------------------------------------------" << std::endl;
-  return 1;
+  return this->Superclass::GenerateHeader(os);
 }
 
diff --git a/Source/CPack/cmCPackTGZGenerator.cxx b/Source/CPack/cmCPackTGZGenerator.cxx
index d4d8d96..edbb483 100644
--- a/Source/CPack/cmCPackTGZGenerator.cxx
+++ b/Source/CPack/cmCPackTGZGenerator.cxx
@@ -26,7 +26,7 @@
 #include "cmCPackLog.h"
 
 #include <cmsys/SystemTools.hxx>
-#include <cmzlib/zlib.h>
+#include <cmzlib/zutil.h>
 #include <libtar/libtar.h>
 #include <memory> // auto_ptr
 #include <fcntl.h>
@@ -52,15 +52,21 @@ cmCPackTGZGenerator::~cmCPackTGZGenerator()
 {
 }
 
+static const size_t cmCPackTGZ_Data_BlockSize = 16384;
+
 //----------------------------------------------------------------------
 class cmCPackTGZ_Data
 {
 public:
   cmCPackTGZ_Data(cmCPackTGZGenerator* gen) :
-    Name(0), OutputStream(0), Generator(gen) {}
+    Name(0), OutputStream(0), Generator(gen), m_CompressionLevel(Z_DEFAULT_COMPRESSION) {}
   const char *Name;
   std::ostream* OutputStream;
   cmCPackTGZGenerator* Generator;
+  char m_CompressedBuffer[cmCPackTGZ_Data_BlockSize];
+  int m_CompressionLevel;
+  z_stream m_ZLibStream;
+  uLong m_CRC;
 };
 
 //----------------------------------------------------------------------
@@ -76,6 +82,16 @@ int cmCPackTGZ_Data_Open(void *client_data, const char* pathname, int, mode_t)
 {
   cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
 
+  mydata->m_ZLibStream.zalloc = Z_NULL;
+  mydata->m_ZLibStream.zfree = Z_NULL;
+  mydata->m_ZLibStream.opaque = Z_NULL;
+  int strategy = Z_DEFAULT_STRATEGY;
+  if ( deflateInit2(&mydata->m_ZLibStream, mydata->m_CompressionLevel,
+      Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy) != Z_OK )
+    {
+    return -1;
+    }
+
   cmGeneratedFileStream* gf = new cmGeneratedFileStream(pathname);
   mydata->OutputStream = gf;
   if ( !*mydata->OutputStream )
@@ -83,13 +99,15 @@ int cmCPackTGZ_Data_Open(void *client_data, const char* pathname, int, mode_t)
     return -1;
     }
 
-  gf->SetCompression(true);
-  gf->SetCompressionExtraExtension(false);
+  gf->SetCompression(false);
 
   if ( !cmCPackTGZGeneratorForward::GenerateHeader(mydata->Generator,gf))
     {
     return -1;
     }
+
+  mydata->m_CRC = crc32(0L, Z_NULL, 0);
+
   return 0;
 }
 
@@ -98,11 +116,28 @@ ssize_t cmCPackTGZ_Data_Write(void *client_data, void *buff, size_t n)
 {
   cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
 
-  mydata->OutputStream->write(reinterpret_cast<const char*>(buff), n);
+  mydata->m_ZLibStream.avail_in = n;
+  mydata->m_ZLibStream.next_in  = reinterpret_cast<Bytef*>(buff);
+
+  do {
+    mydata->m_ZLibStream.avail_out = cmCPackTGZ_Data_BlockSize;
+    mydata->m_ZLibStream.next_out = reinterpret_cast<Bytef*>(mydata->m_CompressedBuffer);
+    int ret = deflate(&mydata->m_ZLibStream, (n?Z_NO_FLUSH:Z_FINISH));    /* no bad return value */
+    assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+
+    size_t compressedSize = cmCPackTGZ_Data_BlockSize - mydata->m_ZLibStream.avail_out;
+
+    mydata->OutputStream->write(reinterpret_cast<const char*>(mydata->m_CompressedBuffer), compressedSize);
+  } while ( mydata->m_ZLibStream.avail_out == 0 );
+
   if ( !*mydata->OutputStream )
     {
     return 0;
     }
+  if ( n )
+    {
+    mydata->m_CRC = crc32(mydata->m_CRC, reinterpret_cast<Bytef *>(buff), n);
+    }
   return n;
 }
 
@@ -111,6 +146,23 @@ int cmCPackTGZ_Data_Close(void *client_data)
 {
   cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
 
+  cmCPackTGZ_Data_Write(client_data, 0, 0);
+
+  char buffer[8];
+  int n;
+  uLong x = mydata->m_CRC;
+  for (n = 0; n < 4; n++) {
+    buffer[n] = (int)(x & 0xff);
+    x >>= 8;
+  }
+  x = mydata->m_ZLibStream.total_in;
+  for (n = 0; n < 4; n++) {
+    buffer[n+4] = (int)(x & 0xff);
+    x >>= 8;
+  }
+
+  mydata->OutputStream->write(buffer, 8);
+  (void)deflateEnd(&mydata->m_ZLibStream);
   delete mydata->OutputStream;
   mydata->OutputStream = 0;
   return (0);
@@ -152,11 +204,12 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName, const char* topl
   std::vector<std::string>::const_iterator fileIt;
   for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
     {
+    std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
     strncpy(pathname, fileIt->c_str(), sizeof(pathname));
     pathname[sizeof(pathname)-1] = 0;
-    strncpy(buf, pathname, sizeof(buf));
+    strncpy(buf, rp.c_str(), sizeof(buf));
     buf[sizeof(buf)-1] = 0;
-    if (tar_append_tree(t, buf, pathname) != 0)
+    if (tar_append_tree(t, pathname, buf) != 0)
       {
       cmCPackLogger(cmCPackLog::LOG_ERROR,
         "Problem with tar_append_tree(\"" << buf << "\", \"" << pathname << "\"): "
@@ -183,7 +236,11 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName, const char* topl
 //----------------------------------------------------------------------
 int cmCPackTGZGenerator::GenerateHeader(std::ostream* os)
 {
-  (void)os;
+  const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
+  char header[10];
+  sprintf(header, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
+    Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
+  os->write(header, 10);
   return 1;
 }
 
diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx
index ef22f82..419261f 100644
--- a/Source/CPack/cpack.cxx
+++ b/Source/CPack/cpack.cxx
@@ -216,7 +216,7 @@ int main (int argc, char *argv[])
   if ( cpackConfigFile.empty() )
     {
     cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
-    cpackConfigFile += "/CPack.cmake";
+    cpackConfigFile += "/CPackConfig.cmake";
     cpackConfigFileSpecified = false;
     }
 
@@ -243,11 +243,10 @@ int main (int argc, char *argv[])
       }
 
     if ( !generator.empty() )             { mf->AddDefinition("CPACK_GENERATOR",             generator.c_str()); }
-    if ( !cpackProjectName.empty() )      { mf->AddDefinition("CPACK_PROJECT_NAME",          cpackProjectName.c_str()); }
-    if ( !cpackProjectVersion.empty() )   { mf->AddDefinition("CPACK_PROJECT_VERSION",       cpackProjectVersion.c_str()); }
-    if ( !cpackProjectPatch.empty() )     { mf->AddDefinition("CPACK_PROJECT_VERSION_PATCH", cpackProjectPatch.c_str()); }
-    if ( !cpackProjectVendor.empty() )    { mf->AddDefinition("CPACK_PROJECT_VENDOR",        cpackProjectVendor.c_str()); }
-    if ( !cpackProjectDirectory.empty() ) { mf->AddDefinition("CPACK_PROJECT_DIRECTORY",     cpackProjectDirectory.c_str()); }
+    if ( !cpackProjectName.empty() )      { mf->AddDefinition("CPACK_PACKAGE_NAME",          cpackProjectName.c_str()); }
+    if ( !cpackProjectVersion.empty() )   { mf->AddDefinition("CPACK_PACKAGE_VERSION",       cpackProjectVersion.c_str()); }
+    if ( !cpackProjectVendor.empty() )    { mf->AddDefinition("CPACK_PACKAGE_VENDOR",        cpackProjectVendor.c_str()); }
+    if ( !cpackProjectDirectory.empty() ) { mf->AddDefinition("CPACK_PACKAGE_DIRECTORY",     cpackProjectDirectory.c_str()); }
     if ( !cpackBuildConfig.empty() )      { mf->AddDefinition("CPACK_BUILD_CONFIG",          cpackBuildConfig.c_str()); }
     cpackDefinitions::MapType::iterator cdit;
     for ( cdit = definitions.m_Map.begin(); cdit != definitions.m_Map.end(); ++cdit )
@@ -261,14 +260,18 @@ int main (int argc, char *argv[])
       cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack generator not specified" << std::endl);
       parsed = 0;
       }
-    if ( parsed && !mf->GetDefinition("CPACK_PROJECT_NAME") )
+    if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
       {
       cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project name not specified" << std::endl);
       parsed = 0;
       }
-    if ( parsed && !mf->GetDefinition("CPACK_PROJECT_VERSION"))
+    if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
+        || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") && mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
+        && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
       {
-      cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project version not specified" << std::endl);
+      cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project version not specified" << std::endl
+        << "Specify CPACK_PACKAGE_VERSION, or CPACK_PACKAGE_VERSION_MAJOR, CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
+        << std::endl);
       parsed = 0;
       }
     if ( parsed )
@@ -316,10 +319,21 @@ int main (int argc, char *argv[])
   cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
 #endif
 
-  const char* projName = mf->GetDefinition("CPACK_PROJECT_NAME");
+  const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
   cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: " << cpackGenerator->GetNameOfClass() << std::endl);
   cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: " << projName << std::endl);
 
+  const char* projVersion = mf->GetDefinition("CPACK_PACKAGE_VERSION");
+  if ( !projVersion )
+    {
+    const char* projVersionMajor = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
+    const char* projVersionMinor = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
+    const char* projVersionPatch = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
+    cmOStringStream ostr;
+    ostr << projVersionMajor << "." << projVersionMinor << "." << projVersionPatch;
+    mf->AddDefinition("CPACK_PACKAGE_VERSION", ostr.str().c_str());
+    }
+
   int res = cpackGenerator->ProcessGenerator();
   if ( !res )
     {
diff --git a/Templates/CPackConfig.cmake.in b/Templates/CPackConfig.cmake.in
new file mode 100644
index 0000000..efc35cb
--- /dev/null
+++ b/Templates/CPackConfig.cmake.in
@@ -0,0 +1,10 @@
+SET(CPACK_GENERATOR "@CPACK_GENERATOR@")
+SET(CPACK_PACKAGE_NAME "@CPACK_PACKAGE_NAME@")
+SET(CPACK_PACKAGE_VERSION_MAJOR "@CPACK_PACKAGE_VERSION_MAJOR@")
+SET(CPACK_PACKAGE_VERSION_MINOR "@CPACK_PACKAGE_VERSION_MINOR@")
+SET(CPACK_PACKAGE_VERSION_PATCH "@CPACK_PACKAGE_VERSION_PATCH@")
+SET(CPACK_PACKAGE_VENDOR "@CPACK_PACKAGE_VENDOR@")
+SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "@CPACK_PACKAGE_DESCRIPTION_SUMMARY@")
+SET(CPACK_PACKAGE_DESCRIPTION_FILE "@CPACK_PACKAGE_DESCRIPTION_FILE@")
+SET(CPACK_SOURCE_DIR "@CPACK_SOURCE_DIR@")
+SET(CPACK_BINARY_DIR "@CPACK_BINARY_DIR@")
-- 
cgit v0.12