cmCPackOSXX11Generator.cxx 10.3 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
#include "cmCPackLog.h"
9
#include "cmDuration.h"
10 11
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
12
#include "cm_sys_stat.h"
13

wahikihiki's avatar
wahikihiki committed
14
cmCPackOSXX11Generator::cmCPackOSXX11Generator() = default;
15

wahikihiki's avatar
wahikihiki committed
16
cmCPackOSXX11Generator::~cmCPackOSXX11Generator() = default;
17

18
int cmCPackOSXX11Generator::PackageFiles()
19
{
20 21
  // TODO: Use toplevel ?
  //       It is used! Is this an obsolete comment?
22

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

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

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

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

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

93
  if (!this->CopyResourcePlistFile("VolumeIcon.icns", diskImageDirectory,
94
                                   ".VolumeIcon.icns", true) ||
95 96
      !this->CopyResourcePlistFile("DS_Store", diskImageDirectory, ".DS_Store",
                                   true) ||
97
      !this->CopyResourcePlistFile("background.png",
98
                                   diskImageBackgroundImageDir,
99 100 101 102 103 104 105 106 107 108 109
                                   "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)) {
110 111
    cmCPackLogger(cmCPackLog::LOG_ERROR,
                  "Problem copying the resource files" << std::endl);
112
    return 0;
113
  }
114

115 116 117 118 119 120 121 122 123 124
  // 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;
125
  if (cmsys::SystemTools::GetPermissions(runTimeScript.c_str(), mode)) {
126 127
    mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
    cmsys::SystemTools::SetPermissions(runTimeScript.c_str(), mode);
128 129 130 131
    cmCPackLogger(cmCPackLog::LOG_OUTPUT,
                  "Setting: " << runTimeScript << " to permission: " << mode
                              << std::endl);
  }
132

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

141 142 143
  std::string output;
  std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  tmpFile += "/hdiutilOutput.log";
144
  std::ostringstream dmgCmd;
145
  dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
146 147
         << "\" create -ov -fs HFS+ -format UDZO -srcfolder \""
         << diskImageDirectory << "\" \"" << packageFileNames[0] << "\"";
148 149 150
  cmCPackLogger(cmCPackLog::LOG_VERBOSE,
                "Compress disk image using command: " << 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) {
157
    res = cmSystemTools::RunSingleCommand(
158 159
      dmgCmd.str(), &output, &output, &retVal, nullptr, this->GeneratorVerbose,
      cmDuration::zero());
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);
169
    ofs << "# Run command: " << dmgCmd.str() << std::endl
170
        << "# Output:" << std::endl
171
        << output << std::endl;
172 173
    cmCPackLogger(cmCPackLog::LOG_ERROR,
                  "Problem running hdiutil command: "
174 175
                    << dmgCmd.str() << std::endl
                    << "Please check " << tmpFile << " for errors"
176
                    << std::endl);
177
    return 0;
178
  }
179 180 181 182 183 184

  return 1;
}

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

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

/*
201
bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
202 203 204 205 206 207 208
{
  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()
209
                  << " not specified. It should point to "
210 211 212 213 214 215 216
                  << (name ? name : "(NULL)")
                  << ".rtf, " << name
                  << ".html, or " << name << ".txt file" << std::endl);
    return false;
    }
  if ( !cmSystemTools::FileExists(inFileName) )
    {
217
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                  << (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;


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

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

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

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

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

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