cmCPackOSXX11Generator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "cmCPackGenerator.h"
  6. #include "cmCPackLog.h"
  7. #include "cmDuration.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. #include "cm_sys_stat.h"
  12. cmCPackOSXX11Generator::cmCPackOSXX11Generator() = default;
  13. cmCPackOSXX11Generator::~cmCPackOSXX11Generator() = default;
  14. int cmCPackOSXX11Generator::PackageFiles()
  15. {
  16. // TODO: Use toplevel ?
  17. // It is used! Is this an obsolete comment?
  18. const char* cpackPackageExecutables =
  19. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  20. if (cpackPackageExecutables) {
  21. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  22. "The cpackPackageExecutables: " << cpackPackageExecutables
  23. << "." << std::endl);
  24. std::ostringstream str;
  25. std::ostringstream deleteStr;
  26. std::vector<std::string> cpackPackageExecutablesVector;
  27. cmExpandList(cpackPackageExecutables, cpackPackageExecutablesVector);
  28. if (cpackPackageExecutablesVector.size() % 2 != 0) {
  29. cmCPackLogger(
  30. cmCPackLog::LOG_ERROR,
  31. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  32. "<icon name>."
  33. << std::endl);
  34. return 0;
  35. }
  36. std::vector<std::string>::iterator it;
  37. for (it = cpackPackageExecutablesVector.begin();
  38. it != cpackPackageExecutablesVector.end(); ++it) {
  39. std::string cpackExecutableName = *it;
  40. ++it;
  41. this->SetOptionIfNotSet("CPACK_EXECUTABLE_NAME",
  42. cpackExecutableName.c_str());
  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. const char* 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.c_str());
  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("OSXScriptLauncher", appdir,
  95. this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  96. 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_DEBUG,
  163. "cmCPackOSXX11Generator::Initialize()" << std::endl);
  164. std::vector<std::string> path;
  165. std::string pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  166. if (pkgPath.empty()) {
  167. cmCPackLogger(cmCPackLog::LOG_ERROR,
  168. "Cannot find hdiutil compiler" << std::endl);
  169. return 0;
  170. }
  171. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  172. pkgPath.c_str());
  173. return this->Superclass::InitializeInternal();
  174. }
  175. /*
  176. bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
  177. {
  178. std::string uname = cmSystemTools::UpperCase(name);
  179. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  180. const char* inFileName = this->GetOption(cpackVar.c_str());
  181. if ( !inFileName )
  182. {
  183. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  184. << " not specified. It should point to "
  185. << (name ? name : "(NULL)")
  186. << ".rtf, " << name
  187. << ".html, or " << name << ".txt file" << std::endl);
  188. return false;
  189. }
  190. if ( !cmSystemTools::FileExists(inFileName) )
  191. {
  192. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  193. << (name ? name : "(NULL)")
  194. << " resource file: " << inFileName << std::endl);
  195. return false;
  196. }
  197. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  198. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  199. {
  200. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  201. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  202. << std::endl);
  203. return false;
  204. }
  205. std::string destFileName = cmStrCat(
  206. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/Resources/", name, ext );
  207. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  208. << (inFileName ? inFileName : "(NULL)")
  209. << " to " << destFileName << std::endl);
  210. this->ConfigureFile(inFileName, destFileName);
  211. return true;
  212. }
  213. */
  214. bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  215. const std::string& name, const std::string& dir,
  216. const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
  217. {
  218. std::string inFName = cmStrCat("Internal/CPack/CPack.", name, ".in");
  219. std::string inFileName = this->FindTemplate(inFName.c_str());
  220. if (inFileName.empty()) {
  221. cmCPackLogger(cmCPackLog::LOG_ERROR,
  222. "Cannot find input file: " << inFName << std::endl);
  223. return false;
  224. }
  225. if (!outputFileName) {
  226. outputFileName = name.c_str();
  227. }
  228. std::string destFileName = cmStrCat(dir, '/', outputFileName);
  229. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  230. "Configure file: " << inFileName << " to " << destFileName
  231. << std::endl);
  232. this->ConfigureFile(inFileName, destFileName, copyOnly);
  233. return true;
  234. }
  235. const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
  236. {
  237. this->InstallPrefix =
  238. cmStrCat('/', this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  239. ".app/Contents/Resources");
  240. return this->InstallPrefix.c_str();
  241. }