cmCPackOSXX11Generator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackOSXX11Generator.h"
  4. #include <sstream>
  5. #include <sys/stat.h>
  6. #include "cmCPackGenerator.h"
  7. #include "cmCPackLog.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmSystemTools.h"
  10. cmCPackOSXX11Generator::cmCPackOSXX11Generator()
  11. {
  12. }
  13. cmCPackOSXX11Generator::~cmCPackOSXX11Generator()
  14. {
  15. }
  16. int cmCPackOSXX11Generator::PackageFiles()
  17. {
  18. // TODO: Use toplevel ?
  19. // It is used! Is this an obsolete comment?
  20. const char* cpackPackageExecutables =
  21. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  22. if (cpackPackageExecutables) {
  23. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  24. << cpackPackageExecutables << "." << std::endl);
  25. std::ostringstream str;
  26. std::ostringstream deleteStr;
  27. std::vector<std::string> cpackPackageExecutablesVector;
  28. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  29. cpackPackageExecutablesVector);
  30. if (cpackPackageExecutablesVector.size() % 2 != 0) {
  31. cmCPackLogger(
  32. cmCPackLog::LOG_ERROR,
  33. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  34. "<icon name>."
  35. << std::endl);
  36. return 0;
  37. }
  38. std::vector<std::string>::iterator it;
  39. for (it = cpackPackageExecutablesVector.begin();
  40. it != cpackPackageExecutablesVector.end(); ++it) {
  41. std::string cpackExecutableName = *it;
  42. ++it;
  43. this->SetOptionIfNotSet("CPACK_EXECUTABLE_NAME",
  44. cpackExecutableName.c_str());
  45. }
  46. }
  47. // Disk image directories
  48. std::string diskImageDirectory = toplevel;
  49. std::string diskImageBackgroundImageDir =
  50. diskImageDirectory + "/.background";
  51. // App bundle directories
  52. std::string packageDirFileName = toplevel;
  53. packageDirFileName += "/";
  54. packageDirFileName += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  55. packageDirFileName += ".app";
  56. std::string contentsDirectory = packageDirFileName + "/Contents";
  57. std::string resourcesDirectory = contentsDirectory + "/Resources";
  58. std::string appDirectory = contentsDirectory + "/MacOS";
  59. std::string scriptDirectory = resourcesDirectory + "/Scripts";
  60. std::string resourceFileName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  61. resourceFileName += ".rsrc";
  62. const char* dir = resourcesDirectory.c_str();
  63. const char* appdir = appDirectory.c_str();
  64. const char* scrDir = scriptDirectory.c_str();
  65. const char* contDir = contentsDirectory.c_str();
  66. const char* rsrcFile = resourceFileName.c_str();
  67. const char* iconFile = this->GetOption("CPACK_PACKAGE_ICON");
  68. if (iconFile) {
  69. std::string iconFileName = cmsys::SystemTools::GetFilenameName(iconFile);
  70. if (!cmSystemTools::FileExists(iconFile)) {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find icon file: "
  72. << iconFile
  73. << ". Please check CPACK_PACKAGE_ICON setting."
  74. << std::endl);
  75. return 0;
  76. }
  77. std::string destFileName = resourcesDirectory + "/" + iconFileName;
  78. this->ConfigureFile(iconFile, destFileName.c_str(), true);
  79. this->SetOptionIfNotSet("CPACK_APPLE_GUI_ICON", iconFileName.c_str());
  80. }
  81. std::string applicationsLinkName = diskImageDirectory + "/Applications";
  82. cmSystemTools::CreateSymlink("/Applications", applicationsLinkName);
  83. if (!this->CopyResourcePlistFile("VolumeIcon.icns", diskImageDirectory,
  84. ".VolumeIcon.icns", true) ||
  85. !this->CopyResourcePlistFile("DS_Store", diskImageDirectory, ".DS_Store",
  86. true) ||
  87. !this->CopyResourcePlistFile("background.png",
  88. diskImageBackgroundImageDir,
  89. "background.png", true) ||
  90. !this->CopyResourcePlistFile("RuntimeScript", dir) ||
  91. !this->CopyResourcePlistFile("OSXX11.Info.plist", contDir,
  92. "Info.plist") ||
  93. !this->CopyResourcePlistFile("OSXX11.main.scpt", scrDir, "main.scpt",
  94. true) ||
  95. !this->CopyResourcePlistFile("OSXScriptLauncher.rsrc", dir, rsrcFile,
  96. true) ||
  97. !this->CopyResourcePlistFile("OSXScriptLauncher", appdir,
  98. this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  99. true)) {
  100. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
  101. << std::endl);
  102. return 0;
  103. }
  104. // Two of the files need to have execute permission, so ensure they do:
  105. std::string runTimeScript = dir;
  106. runTimeScript += "/";
  107. runTimeScript += "RuntimeScript";
  108. std::string appScriptName = appdir;
  109. appScriptName += "/";
  110. appScriptName += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  111. mode_t mode;
  112. if (cmsys::SystemTools::GetPermissions(runTimeScript.c_str(), mode)) {
  113. mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
  114. cmsys::SystemTools::SetPermissions(runTimeScript.c_str(), mode);
  115. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  116. "Setting: " << runTimeScript << " to permission: " << mode
  117. << std::endl);
  118. }
  119. if (cmsys::SystemTools::GetPermissions(appScriptName.c_str(), mode)) {
  120. mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
  121. cmsys::SystemTools::SetPermissions(appScriptName.c_str(), mode);
  122. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  123. "Setting: " << appScriptName << " to permission: " << mode
  124. << std::endl);
  125. }
  126. std::string output;
  127. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  128. tmpFile += "/hdiutilOutput.log";
  129. std::ostringstream dmgCmd;
  130. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
  131. << "\" create -ov -format UDZO -srcfolder \"" << diskImageDirectory
  132. << "\" \"" << packageFileNames[0] << "\"";
  133. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress disk image using command: "
  134. << dmgCmd.str() << std::endl);
  135. // since we get random dashboard failures with this one
  136. // try running it more than once
  137. int retVal = 1;
  138. int numTries = 10;
  139. bool res = false;
  140. while (numTries > 0) {
  141. res =
  142. cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output, &output,
  143. &retVal, 0, this->GeneratorVerbose, 0);
  144. if (res && !retVal) {
  145. numTries = -1;
  146. break;
  147. }
  148. cmSystemTools::Delay(500);
  149. numTries--;
  150. }
  151. if (!res || retVal) {
  152. cmGeneratedFileStream ofs(tmpFile.c_str());
  153. ofs << "# Run command: " << dmgCmd.str() << std::endl
  154. << "# Output:" << std::endl
  155. << output << std::endl;
  156. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  157. << dmgCmd.str() << std::endl
  158. << "Please check " << tmpFile << " for errors"
  159. << std::endl);
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. int cmCPackOSXX11Generator::InitializeInternal()
  165. {
  166. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackOSXX11Generator::Initialize()"
  167. << std::endl);
  168. std::vector<std::string> path;
  169. std::string pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  170. if (pkgPath.empty()) {
  171. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  172. << std::endl);
  173. return 0;
  174. }
  175. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  176. pkgPath.c_str());
  177. return this->Superclass::InitializeInternal();
  178. }
  179. /*
  180. bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
  181. {
  182. std::string uname = cmSystemTools::UpperCase(name);
  183. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  184. const char* inFileName = this->GetOption(cpackVar.c_str());
  185. if ( !inFileName )
  186. {
  187. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  188. << " not specified. It should point to "
  189. << (name ? name : "(NULL)")
  190. << ".rtf, " << name
  191. << ".html, or " << name << ".txt file" << std::endl);
  192. return false;
  193. }
  194. if ( !cmSystemTools::FileExists(inFileName) )
  195. {
  196. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  197. << (name ? name : "(NULL)")
  198. << " resource file: " << inFileName << std::endl);
  199. return false;
  200. }
  201. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  202. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  203. {
  204. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  205. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  206. << std::endl);
  207. return false;
  208. }
  209. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  210. destFileName += "/Resources/";
  211. destFileName += name + ext;
  212. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  213. << (inFileName ? inFileName : "(NULL)")
  214. << " to " << destFileName << std::endl);
  215. this->ConfigureFile(inFileName, destFileName.c_str());
  216. return true;
  217. }
  218. */
  219. bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  220. const std::string& name, const std::string& dir,
  221. const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
  222. {
  223. std::string inFName = "CPack.";
  224. inFName += name;
  225. inFName += ".in";
  226. std::string inFileName = this->FindTemplate(inFName.c_str());
  227. if (inFileName.empty()) {
  228. cmCPackLogger(cmCPackLog::LOG_ERROR,
  229. "Cannot find input file: " << inFName << std::endl);
  230. return false;
  231. }
  232. if (!outputFileName) {
  233. outputFileName = name.c_str();
  234. }
  235. std::string destFileName = dir;
  236. destFileName += "/";
  237. destFileName += outputFileName;
  238. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  239. << inFileName << " to " << destFileName << std::endl);
  240. this->ConfigureFile(inFileName.c_str(), destFileName.c_str(), copyOnly);
  241. return true;
  242. }
  243. const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
  244. {
  245. this->InstallPrefix = "/";
  246. this->InstallPrefix += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  247. this->InstallPrefix += ".app/Contents/Resources";
  248. return this->InstallPrefix.c_str();
  249. }