cmCPackOSXX11Generator.cxx 10 KB

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