summaryrefslogtreecommitdiffstats
path: root/Source/CPack/cmCPackZIPGenerator.cxx
blob: 34db2f31d5d27ac8f05b4b9194ac3c188bdde92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "cmCPackZIPGenerator.h"

#include "cmake.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmSystemTools.h"
#include "cmMakefile.h"
#include "cmGeneratedFileStream.h"
#include "cmCPackLog.h"

#include <cmsys/SystemTools.hxx>

//----------------------------------------------------------------------
cmCPackZIPGenerator::cmCPackZIPGenerator()
{
}

//----------------------------------------------------------------------
cmCPackZIPGenerator::~cmCPackZIPGenerator()
{
}

//----------------------------------------------------------------------
int cmCPackZIPGenerator::InitializeInternal()
{
  this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  std::vector<std::string> path;
  std::string pkgPath = "c:/Program Files/WinZip";
  path.push_back(pkgPath);
  pkgPath = cmSystemTools::FindProgram("wzzip", path, false);
  this->ZipStyle = cmCPackZIPGenerator::StyleUnkown;
  bool found = false;
  if ( pkgPath.empty() )
    {
    cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find WinZip" << std::endl);
    }
  else
    {
    this->ZipStyle = cmCPackZIPGenerator::StyleWinZip;
    found = true;
    }

  if ( !found )
    {
    pkgPath = "c:/Program Files/7-Zip";
    path.push_back(pkgPath);
    pkgPath = cmSystemTools::FindProgram("7z", path, false);

    if ( pkgPath.empty() )
      {
      cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find 7ZIP"
        << std::endl);
      }
    else
      {
      this->ZipStyle = cmCPackZIPGenerator::Style7Zip;
      found = true;
      }
    }

  if ( !found )
    {
    path.erase(path.begin(), path.end());
    pkgPath = "c:/cygwin/bin";
    path.push_back(pkgPath);
    pkgPath = cmSystemTools::FindProgram("zip", path, false);
    if ( pkgPath.empty() )
      {
      cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find unix ZIP"
        << std::endl);
      }
    else
      {
      this->ZipStyle = cmCPackZIPGenerator::StyleUnixZip;
      found = true;
      }
    }
  if ( !found )
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find a suitable ZIP program"
      << std::endl);
    return 0;
    }
  this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found ZIP program: "
    << pkgPath.c_str()
    << std::endl);
  return this->Superclass::InitializeInternal();
}

//----------------------------------------------------------------------
int cmCPackZIPGenerator::CompressFiles(const char* outFileName,
  const char* toplevel, const std::vector<std::string>& files)
{
  std::string tempFileName;
  tempFileName = toplevel;
  tempFileName += "/winZip.filelist";
  bool needQuotesInFile = false;
  cmOStringStream dmgCmd;
  switch ( this->ZipStyle )
    {
  case cmCPackZIPGenerator::StyleWinZip:
    dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
           << "\" -P \"" << outFileName
           << "\" @winZip.filelist";
    needQuotesInFile = true;
    break;
  case cmCPackZIPGenerator::Style7Zip:
    // this is the zip generator, so tell 7zip to generate zip files
    dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
           << "\" a -tzip \"" << outFileName
           << "\" @winZip.filelist";
    needQuotesInFile = true;
    break;
  case cmCPackZIPGenerator::StyleUnixZip:
    dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
      << "\" -r \"" << outFileName
      << "\" . -i@winZip.filelist";
    break;
  default:
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Unknown ZIP style"
      << std::endl);
    return 0;
    }
  if(tempFileName.size())
    {
    cmGeneratedFileStream out(tempFileName.c_str());
    std::vector<std::string>::const_iterator fileIt;
    for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
      {
      if ( needQuotesInFile )
        {
        out << "\"";
        }
      out << cmSystemTools::RelativePath(toplevel, fileIt->c_str());
      if ( needQuotesInFile )
        {
        out << "\"";
        }
      out << std::endl;
      }
    }
  else
    {
    std::vector<std::string>::const_iterator fileIt;
    for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
      {
      dmgCmd << " \""
             << cmSystemTools::RelativePath(toplevel, fileIt->c_str())
             << "\"";
      }
    }
  std::string output;
  int retVal = -1;
  int res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
    &retVal, toplevel, this->GeneratorVerbose, 0);
  if ( !res || retVal )
    {
    std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
    tmpFile += "/CompressZip.log";
    cmGeneratedFileStream ofs(tmpFile.c_str());
    ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
      << "# Output:" << std::endl
      << output.c_str() << std::endl;
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
      << dmgCmd.str().c_str() << std::endl
      << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
    return 0;
    }
  return 1;
}