cmCPackOSXX11Generator.cxx 11 KB

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