cmCPackOSXX11Generator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "cmCPackLog.h"
  5. #include "cmGeneratedFileStream.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmSystemTools.h"
  9. #include "cmake.h"
  10. #include <cmsys/Glob.hxx>
  11. #include <cmsys/SystemTools.hxx>
  12. #include <sys/stat.h>
  13. cmCPackOSXX11Generator::cmCPackOSXX11Generator()
  14. {
  15. }
  16. cmCPackOSXX11Generator::~cmCPackOSXX11Generator()
  17. {
  18. }
  19. int cmCPackOSXX11Generator::PackageFiles()
  20. {
  21. // TODO: Use toplevel ?
  22. // It is used! Is this an obsolete comment?
  23. const char* cpackPackageExecutables =
  24. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  25. if (cpackPackageExecutables) {
  26. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  27. << cpackPackageExecutables << "." << std::endl);
  28. std::ostringstream str;
  29. std::ostringstream deleteStr;
  30. std::vector<std::string> cpackPackageExecutablesVector;
  31. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  32. cpackPackageExecutablesVector);
  33. if (cpackPackageExecutablesVector.size() % 2 != 0) {
  34. cmCPackLogger(
  35. cmCPackLog::LOG_ERROR,
  36. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  37. "<icon name>."
  38. << std::endl);
  39. return 0;
  40. }
  41. std::vector<std::string>::iterator it;
  42. for (it = cpackPackageExecutablesVector.begin();
  43. it != cpackPackageExecutablesVector.end(); ++it) {
  44. std::string cpackExecutableName = *it;
  45. ++it;
  46. this->SetOptionIfNotSet("CPACK_EXECUTABLE_NAME",
  47. cpackExecutableName.c_str());
  48. }
  49. }
  50. // Disk image directories
  51. std::string diskImageDirectory = toplevel;
  52. std::string diskImageBackgroundImageDir =
  53. diskImageDirectory + "/.background";
  54. // App bundle directories
  55. std::string packageDirFileName = toplevel;
  56. packageDirFileName += "/";
  57. packageDirFileName += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  58. packageDirFileName += ".app";
  59. std::string contentsDirectory = packageDirFileName + "/Contents";
  60. std::string resourcesDirectory = contentsDirectory + "/Resources";
  61. std::string appDirectory = contentsDirectory + "/MacOS";
  62. std::string scriptDirectory = resourcesDirectory + "/Scripts";
  63. std::string resourceFileName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  64. resourceFileName += ".rsrc";
  65. const char* dir = resourcesDirectory.c_str();
  66. const char* appdir = appDirectory.c_str();
  67. const char* scrDir = scriptDirectory.c_str();
  68. const char* contDir = contentsDirectory.c_str();
  69. const char* rsrcFile = resourceFileName.c_str();
  70. const char* iconFile = this->GetOption("CPACK_PACKAGE_ICON");
  71. if (iconFile) {
  72. std::string iconFileName = cmsys::SystemTools::GetFilenameName(iconFile);
  73. if (!cmSystemTools::FileExists(iconFile)) {
  74. cmCPackLogger(cmCPackLog::LOG_ERROR, "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, "Problem copying the resource files"
  104. << 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 -format UDZO -srcfolder \"" << diskImageDirectory
  135. << "\" \"" << packageFileNames[0] << "\"";
  136. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress disk image using command: "
  137. << dmgCmd.str() << std::endl);
  138. // since we get random dashboard failures with this one
  139. // try running it more than once
  140. int retVal = 1;
  141. int numTries = 10;
  142. bool res = false;
  143. while (numTries > 0) {
  144. res =
  145. cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output, &output,
  146. &retVal, 0, this->GeneratorVerbose, 0);
  147. if (res && !retVal) {
  148. numTries = -1;
  149. break;
  150. }
  151. cmSystemTools::Delay(500);
  152. numTries--;
  153. }
  154. if (!res || retVal) {
  155. cmGeneratedFileStream ofs(tmpFile.c_str());
  156. ofs << "# Run command: " << dmgCmd.str() << std::endl
  157. << "# Output:" << std::endl
  158. << output << std::endl;
  159. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  160. << dmgCmd.str() << std::endl
  161. << "Please check " << tmpFile << " for errors"
  162. << std::endl);
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. int cmCPackOSXX11Generator::InitializeInternal()
  168. {
  169. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackOSXX11Generator::Initialize()"
  170. << std::endl);
  171. std::vector<std::string> path;
  172. std::string pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  173. if (pkgPath.empty()) {
  174. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  175. << std::endl);
  176. return 0;
  177. }
  178. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  179. pkgPath.c_str());
  180. return this->Superclass::InitializeInternal();
  181. }
  182. /*
  183. bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
  184. {
  185. std::string uname = cmSystemTools::UpperCase(name);
  186. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  187. const char* inFileName = this->GetOption(cpackVar.c_str());
  188. if ( !inFileName )
  189. {
  190. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  191. << " not specified. It should point to "
  192. << (name ? name : "(NULL)")
  193. << ".rtf, " << name
  194. << ".html, or " << name << ".txt file" << std::endl);
  195. return false;
  196. }
  197. if ( !cmSystemTools::FileExists(inFileName) )
  198. {
  199. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  200. << (name ? name : "(NULL)")
  201. << " resource file: " << inFileName << std::endl);
  202. return false;
  203. }
  204. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  205. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  206. {
  207. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  208. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  209. << std::endl);
  210. return false;
  211. }
  212. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  213. destFileName += "/Resources/";
  214. destFileName += name + ext;
  215. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  216. << (inFileName ? inFileName : "(NULL)")
  217. << " to " << destFileName << std::endl);
  218. this->ConfigureFile(inFileName, destFileName.c_str());
  219. return true;
  220. }
  221. */
  222. bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  223. const std::string& name, const std::string& dir,
  224. const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
  225. {
  226. std::string inFName = "CPack.";
  227. inFName += name;
  228. inFName += ".in";
  229. std::string inFileName = this->FindTemplate(inFName.c_str());
  230. if (inFileName.empty()) {
  231. cmCPackLogger(cmCPackLog::LOG_ERROR,
  232. "Cannot find input file: " << inFName << std::endl);
  233. return false;
  234. }
  235. if (!outputFileName) {
  236. outputFileName = name.c_str();
  237. }
  238. std::string destFileName = dir;
  239. destFileName += "/";
  240. destFileName += outputFileName;
  241. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  242. << inFileName << " to " << destFileName << std::endl);
  243. this->ConfigureFile(inFileName.c_str(), destFileName.c_str(), copyOnly);
  244. return true;
  245. }
  246. const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
  247. {
  248. this->InstallPrefix = "/";
  249. this->InstallPrefix += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  250. this->InstallPrefix += ".app/Contents/Resources";
  251. return this->InstallPrefix.c_str();
  252. }