cmCPackOSXX11Generator.cxx 10 KB

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