cmCPackOSXX11Generator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "cmProperty.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.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. cmProp 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",
  43. cpackExecutableName.c_str());
  44. }
  45. }
  46. // Disk image directories
  47. std::string diskImageDirectory = toplevel;
  48. std::string diskImageBackgroundImageDir =
  49. diskImageDirectory + "/.background";
  50. // App bundle directories
  51. std::string packageDirFileName = cmStrCat(
  52. toplevel, '/', this->GetOption("CPACK_PACKAGE_FILE_NAME"), ".app");
  53. std::string contentsDirectory = packageDirFileName + "/Contents";
  54. std::string resourcesDirectory = contentsDirectory + "/Resources";
  55. std::string appDirectory = contentsDirectory + "/MacOS";
  56. std::string scriptDirectory = resourcesDirectory + "/Scripts";
  57. std::string resourceFileName =
  58. cmStrCat(this->GetOption("CPACK_PACKAGE_FILE_NAME"), ".rsrc");
  59. const char* dir = resourcesDirectory.c_str();
  60. const char* appdir = appDirectory.c_str();
  61. const char* scrDir = scriptDirectory.c_str();
  62. const char* contDir = contentsDirectory.c_str();
  63. const char* rsrcFile = resourceFileName.c_str();
  64. cmProp iconFile = this->GetOption("CPACK_PACKAGE_ICON");
  65. if (iconFile) {
  66. std::string iconFileName = cmsys::SystemTools::GetFilenameName(iconFile);
  67. if (!cmSystemTools::FileExists(iconFile)) {
  68. cmCPackLogger(cmCPackLog::LOG_ERROR,
  69. "Cannot find icon file: "
  70. << iconFile
  71. << ". Please check CPACK_PACKAGE_ICON setting."
  72. << std::endl);
  73. return 0;
  74. }
  75. std::string destFileName = resourcesDirectory + "/" + iconFileName;
  76. this->ConfigureFile(iconFile, destFileName, true);
  77. this->SetOptionIfNotSet("CPACK_APPLE_GUI_ICON", iconFileName.c_str());
  78. }
  79. std::string applicationsLinkName = diskImageDirectory + "/Applications";
  80. cmSystemTools::CreateSymlink("/Applications", applicationsLinkName);
  81. if (!this->CopyResourcePlistFile("VolumeIcon.icns", diskImageDirectory,
  82. ".VolumeIcon.icns", true) ||
  83. !this->CopyResourcePlistFile("DS_Store", diskImageDirectory, ".DS_Store",
  84. true) ||
  85. !this->CopyResourcePlistFile("background.png",
  86. diskImageBackgroundImageDir,
  87. "background.png", true) ||
  88. !this->CopyResourcePlistFile("RuntimeScript", dir) ||
  89. !this->CopyResourcePlistFile("OSXX11.Info.plist", contDir,
  90. "Info.plist") ||
  91. !this->CopyResourcePlistFile("OSXX11.main.scpt", scrDir, "main.scpt",
  92. true) ||
  93. !this->CopyResourcePlistFile("OSXScriptLauncher.rsrc", dir, rsrcFile,
  94. true) ||
  95. !this->CopyResourcePlistFile(
  96. "OSXScriptLauncher", appdir,
  97. this->GetOption("CPACK_PACKAGE_FILE_NAME").GetCStr(), true)) {
  98. cmCPackLogger(cmCPackLog::LOG_ERROR,
  99. "Problem copying the resource files" << std::endl);
  100. return 0;
  101. }
  102. // Two of the files need to have execute permission, so ensure they do:
  103. std::string runTimeScript = cmStrCat(dir, "/RuntimeScript");
  104. std::string appScriptName =
  105. cmStrCat(appdir, '/', this->GetOption("CPACK_PACKAGE_FILE_NAME"));
  106. mode_t mode;
  107. if (cmsys::SystemTools::GetPermissions(runTimeScript.c_str(), mode)) {
  108. mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
  109. cmsys::SystemTools::SetPermissions(runTimeScript.c_str(), mode);
  110. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  111. "Setting: " << runTimeScript << " to permission: " << mode
  112. << std::endl);
  113. }
  114. if (cmsys::SystemTools::GetPermissions(appScriptName.c_str(), mode)) {
  115. mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
  116. cmsys::SystemTools::SetPermissions(appScriptName.c_str(), mode);
  117. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  118. "Setting: " << appScriptName << " to permission: " << mode
  119. << std::endl);
  120. }
  121. std::string output;
  122. std::string tmpFile = cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  123. "/hdiutilOutput.log");
  124. std::ostringstream dmgCmd;
  125. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
  126. << "\" create -ov -fs HFS+ -format UDZO -srcfolder \""
  127. << diskImageDirectory << "\" \"" << packageFileNames[0] << "\"";
  128. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  129. "Compress disk image using command: " << dmgCmd.str()
  130. << std::endl);
  131. // since we get random dashboard failures with this one
  132. // try running it more than once
  133. int retVal = 1;
  134. int numTries = 10;
  135. bool res = false;
  136. while (numTries > 0) {
  137. res = cmSystemTools::RunSingleCommand(
  138. dmgCmd.str(), &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  139. cmDuration::zero());
  140. if (res && !retVal) {
  141. numTries = -1;
  142. break;
  143. }
  144. cmSystemTools::Delay(500);
  145. numTries--;
  146. }
  147. if (!res || retVal) {
  148. cmGeneratedFileStream ofs(tmpFile);
  149. ofs << "# Run command: " << dmgCmd.str() << std::endl
  150. << "# Output:" << std::endl
  151. << output << std::endl;
  152. cmCPackLogger(cmCPackLog::LOG_ERROR,
  153. "Problem running hdiutil command: "
  154. << dmgCmd.str() << std::endl
  155. << "Please check " << tmpFile << " for errors"
  156. << std::endl);
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. int cmCPackOSXX11Generator::InitializeInternal()
  162. {
  163. cmCPackLogger(cmCPackLog::LOG_WARNING,
  164. "The OSXX11 generator is deprecated "
  165. "and will be removed in a future version.\n");
  166. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  167. "cmCPackOSXX11Generator::Initialize()" << 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,
  172. "Cannot find hdiutil compiler" << 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 = cmStrCat(
  210. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/Resources/", name, ext );
  211. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  212. << (inFileName ? inFileName : "(NULL)")
  213. << " to " << destFileName << std::endl);
  214. this->ConfigureFile(inFileName, destFileName);
  215. return true;
  216. }
  217. */
  218. bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  219. const std::string& name, const std::string& dir,
  220. const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
  221. {
  222. std::string inFName = cmStrCat("CPack.", name, ".in");
  223. std::string inFileName = this->FindTemplate(inFName.c_str());
  224. if (inFileName.empty()) {
  225. cmCPackLogger(cmCPackLog::LOG_ERROR,
  226. "Cannot find input file: " << inFName << std::endl);
  227. return false;
  228. }
  229. if (!outputFileName) {
  230. outputFileName = name.c_str();
  231. }
  232. std::string destFileName = cmStrCat(dir, '/', outputFileName);
  233. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  234. "Configure file: " << inFileName << " to " << destFileName
  235. << std::endl);
  236. this->ConfigureFile(inFileName, destFileName, copyOnly);
  237. return true;
  238. }
  239. const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
  240. {
  241. this->InstallPrefix =
  242. cmStrCat('/', this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  243. ".app/Contents/Resources");
  244. return this->InstallPrefix.c_str();
  245. }