cmCPackOSXX11Generator.cxx 10.2 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
#include "cmCPackOSXX11Generator.h"

Daniel Pfeifer's avatar
Daniel Pfeifer committed
5
6
7
#include <sstream>

#include "cmCPackGenerator.h"
8
9
10
#include "cmCPackLog.h"
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
11
#include "cm_sys_stat.h"
12
13
14
15
16
17
18
19
20

cmCPackOSXX11Generator::cmCPackOSXX11Generator()
{
}

cmCPackOSXX11Generator::~cmCPackOSXX11Generator()
{
}

21
int cmCPackOSXX11Generator::PackageFiles()
22
{
23
24
  // TODO: Use toplevel ?
  //       It is used! Is this an obsolete comment?
25

26
27
28
  const char* cpackPackageExecutables =
    this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  if (cpackPackageExecutables) {
29
    cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
30
                    << cpackPackageExecutables << "." << std::endl);
31
32
    std::ostringstream str;
    std::ostringstream deleteStr;
33
34
    std::vector<std::string> cpackPackageExecutablesVector;
    cmSystemTools::ExpandListArgument(cpackPackageExecutables,
35
36
37
38
                                      cpackPackageExecutablesVector);
    if (cpackPackageExecutablesVector.size() % 2 != 0) {
      cmCPackLogger(
        cmCPackLog::LOG_ERROR,
39
        "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
40
41
        "<icon name>."
          << std::endl);
42
      return 0;
43
    }
44
    std::vector<std::string>::iterator it;
45
46
    for (it = cpackPackageExecutablesVector.begin();
         it != cpackPackageExecutablesVector.end(); ++it) {
47
      std::string cpackExecutableName = *it;
48
      ++it;
49
      this->SetOptionIfNotSet("CPACK_EXECUTABLE_NAME",
50
                              cpackExecutableName.c_str());
51
    }
52
  }
53

54
55
  // Disk image directories
  std::string diskImageDirectory = toplevel;
56
57
  std::string diskImageBackgroundImageDir =
    diskImageDirectory + "/.background";
58
59

  // App bundle directories
60
  std::string packageDirFileName = toplevel;
61
62
  packageDirFileName += "/";
  packageDirFileName += this->GetOption("CPACK_PACKAGE_FILE_NAME");
63
64
65
66
  packageDirFileName += ".app";
  std::string contentsDirectory = packageDirFileName + "/Contents";
  std::string resourcesDirectory = contentsDirectory + "/Resources";
  std::string appDirectory = contentsDirectory + "/MacOS";
67
68
69
  std::string scriptDirectory = resourcesDirectory + "/Scripts";
  std::string resourceFileName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  resourceFileName += ".rsrc";
70
71
72

  const char* dir = resourcesDirectory.c_str();
  const char* appdir = appDirectory.c_str();
73
  const char* scrDir = scriptDirectory.c_str();
74
  const char* contDir = contentsDirectory.c_str();
75
  const char* rsrcFile = resourceFileName.c_str();
76
  const char* iconFile = this->GetOption("CPACK_PACKAGE_ICON");
77
78
79
  if (iconFile) {
    std::string iconFileName = cmsys::SystemTools::GetFilenameName(iconFile);
    if (!cmSystemTools::FileExists(iconFile)) {
80
      cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find icon file: "
81
82
83
                      << iconFile
                      << ". Please check CPACK_PACKAGE_ICON setting."
                      << std::endl);
84
      return 0;
85
    }
86
87
88
    std::string destFileName = resourcesDirectory + "/" + iconFileName;
    this->ConfigureFile(iconFile, destFileName.c_str(), true);
    this->SetOptionIfNotSet("CPACK_APPLE_GUI_ICON", iconFileName.c_str());
89
  }
90
91

  std::string applicationsLinkName = diskImageDirectory + "/Applications";
92
  cmSystemTools::CreateSymlink("/Applications", applicationsLinkName);
93

94
  if (!this->CopyResourcePlistFile("VolumeIcon.icns", diskImageDirectory,
95
                                   ".VolumeIcon.icns", true) ||
96
97
      !this->CopyResourcePlistFile("DS_Store", diskImageDirectory, ".DS_Store",
                                   true) ||
98
      !this->CopyResourcePlistFile("background.png",
99
                                   diskImageBackgroundImageDir,
100
101
102
103
104
105
106
107
108
109
110
                                   "background.png", true) ||
      !this->CopyResourcePlistFile("RuntimeScript", dir) ||
      !this->CopyResourcePlistFile("OSXX11.Info.plist", contDir,
                                   "Info.plist") ||
      !this->CopyResourcePlistFile("OSXX11.main.scpt", scrDir, "main.scpt",
                                   true) ||
      !this->CopyResourcePlistFile("OSXScriptLauncher.rsrc", dir, rsrcFile,
                                   true) ||
      !this->CopyResourcePlistFile("OSXScriptLauncher", appdir,
                                   this->GetOption("CPACK_PACKAGE_FILE_NAME"),
                                   true)) {
111
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
112
                    << std::endl);
113
    return 0;
114
  }
115

116
117
118
119
120
121
122
123
124
125
  // Two of the files need to have execute permission, so ensure they do:
  std::string runTimeScript = dir;
  runTimeScript += "/";
  runTimeScript += "RuntimeScript";

  std::string appScriptName = appdir;
  appScriptName += "/";
  appScriptName += this->GetOption("CPACK_PACKAGE_FILE_NAME");

  mode_t mode;
126
  if (cmsys::SystemTools::GetPermissions(runTimeScript.c_str(), mode)) {
127
128
    mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
    cmsys::SystemTools::SetPermissions(runTimeScript.c_str(), mode);
129
130
131
132
    cmCPackLogger(cmCPackLog::LOG_OUTPUT,
                  "Setting: " << runTimeScript << " to permission: " << mode
                              << std::endl);
  }
133

134
  if (cmsys::SystemTools::GetPermissions(appScriptName.c_str(), mode)) {
135
136
    mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
    cmsys::SystemTools::SetPermissions(appScriptName.c_str(), mode);
137
138
139
140
    cmCPackLogger(cmCPackLog::LOG_OUTPUT,
                  "Setting: " << appScriptName << " to permission: " << mode
                              << std::endl);
  }
141

142
143
144
  std::string output;
  std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  tmpFile += "/hdiutilOutput.log";
145
  std::ostringstream dmgCmd;
146
  dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
147
148
         << "\" create -ov -fs HFS+ -format UDZO -srcfolder \""
         << diskImageDirectory << "\" \"" << packageFileNames[0] << "\"";
149
  cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress disk image using command: "
150
                  << dmgCmd.str() << std::endl);
151
152
  // since we get random dashboard failures with this one
  // try running it more than once
153
154
  int retVal = 1;
  int numTries = 10;
155
  bool res = false;
156
  while (numTries > 0) {
Matthias Männich's avatar
Matthias Männich committed
157
158
159
    res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
                                          &output, &retVal, nullptr,
                                          this->GeneratorVerbose, 0);
160
    if (res && !retVal) {
161
      numTries = -1;
162
      break;
163
    }
164
    cmSystemTools::Delay(500);
165
    numTries--;
166
167
  }
  if (!res || retVal) {
168
    cmGeneratedFileStream ofs(tmpFile.c_str());
169
    ofs << "# Run command: " << dmgCmd.str() << std::endl
170
        << "# Output:" << std::endl
171
        << output << std::endl;
172
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
173
174
                    << dmgCmd.str() << std::endl
                    << "Please check " << tmpFile << " for errors"
175
                    << std::endl);
176
    return 0;
177
  }
178
179
180
181
182
183

  return 1;
}

int cmCPackOSXX11Generator::InitializeInternal()
{
184
185
  cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackOSXX11Generator::Initialize()"
                  << std::endl);
186
187
  std::vector<std::string> path;
  std::string pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
188
  if (pkgPath.empty()) {
189
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
190
                    << std::endl);
191
    return 0;
192
  }
193
  this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
194
195
196
197
198
199
                          pkgPath.c_str());

  return this->Superclass::InitializeInternal();
}

/*
200
bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
201
202
203
204
205
206
207
{
  std::string uname = cmSystemTools::UpperCase(name);
  std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  const char* inFileName = this->GetOption(cpackVar.c_str());
  if ( !inFileName )
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
208
                  << " not specified. It should point to "
209
210
211
212
213
214
215
                  << (name ? name : "(NULL)")
                  << ".rtf, " << name
                  << ".html, or " << name << ".txt file" << std::endl);
    return false;
    }
  if ( !cmSystemTools::FileExists(inFileName) )
    {
216
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                  << (name ? name : "(NULL)")
                  << " resource file: " << inFileName << std::endl);
    return false;
    }
  std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
      << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
      << std::endl);
    return false;
    }

  std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  destFileName += "/Resources/";
  destFileName += name + ext;


235
  cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
236
                << (inFileName ? inFileName : "(NULL)")
237
                << " to " << destFileName << std::endl);
238
239
240
241
242
  this->ConfigureFile(inFileName, destFileName.c_str());
  return true;
}
*/

243
244
245
bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  const std::string& name, const std::string& dir,
  const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
246
247
248
249
250
{
  std::string inFName = "CPack.";
  inFName += name;
  inFName += ".in";
  std::string inFileName = this->FindTemplate(inFName.c_str());
251
252
253
  if (inFileName.empty()) {
    cmCPackLogger(cmCPackLog::LOG_ERROR,
                  "Cannot find input file: " << inFName << std::endl);
254
    return false;
255
  }
256

257
  if (!outputFileName) {
258
    outputFileName = name.c_str();
259
  }
260
261
262
263
264

  std::string destFileName = dir;
  destFileName += "/";
  destFileName += outputFileName;

265
266
  cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
                  << inFileName << " to " << destFileName << std::endl);
267
268
269
270
  this->ConfigureFile(inFileName.c_str(), destFileName.c_str(), copyOnly);
  return true;
}

271
const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
272
273
274
275
276
277
{
  this->InstallPrefix = "/";
  this->InstallPrefix += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  this->InstallPrefix += ".app/Contents/Resources";
  return this->InstallPrefix.c_str();
}