cmCPackOSXX11Generator.cxx 9.9 KB

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