cmCPackOSXX11Generator.cxx 10 KB

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