cmCPackOSXX11Generator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 \""
  143. << diskImageDirectory.c_str() << "\" \"" << packageFileNames[0]
  144. << "\"";
  145. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress disk image using command: "
  146. << dmgCmd.str().c_str() << std::endl);
  147. // since we get random dashboard failures with this one
  148. // try running it more than once
  149. int retVal = 1;
  150. int numTries = 10;
  151. bool res = false;
  152. while (numTries > 0) {
  153. res =
  154. cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output, &output,
  155. &retVal, 0, this->GeneratorVerbose, 0);
  156. if (res && !retVal) {
  157. numTries = -1;
  158. break;
  159. }
  160. cmSystemTools::Delay(500);
  161. numTries--;
  162. }
  163. if (!res || retVal) {
  164. cmGeneratedFileStream ofs(tmpFile.c_str());
  165. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  166. << "# Output:" << std::endl
  167. << output.c_str() << std::endl;
  168. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  169. << dmgCmd.str().c_str() << std::endl
  170. << "Please check " << tmpFile.c_str() << " for errors"
  171. << std::endl);
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. int cmCPackOSXX11Generator::InitializeInternal()
  177. {
  178. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackOSXX11Generator::Initialize()"
  179. << std::endl);
  180. std::vector<std::string> path;
  181. std::string pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  182. if (pkgPath.empty()) {
  183. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  184. << std::endl);
  185. return 0;
  186. }
  187. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  188. pkgPath.c_str());
  189. return this->Superclass::InitializeInternal();
  190. }
  191. /*
  192. bool cmCPackOSXX11Generator::CopyCreateResourceFile(const std::string& name)
  193. {
  194. std::string uname = cmSystemTools::UpperCase(name);
  195. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  196. const char* inFileName = this->GetOption(cpackVar.c_str());
  197. if ( !inFileName )
  198. {
  199. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  200. << " not specified. It should point to "
  201. << (name ? name : "(NULL)")
  202. << ".rtf, " << name
  203. << ".html, or " << name << ".txt file" << std::endl);
  204. return false;
  205. }
  206. if ( !cmSystemTools::FileExists(inFileName) )
  207. {
  208. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  209. << (name ? name : "(NULL)")
  210. << " resource file: " << inFileName << std::endl);
  211. return false;
  212. }
  213. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  214. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  215. {
  216. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  217. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  218. << std::endl);
  219. return false;
  220. }
  221. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  222. destFileName += "/Resources/";
  223. destFileName += name + ext;
  224. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  225. << (inFileName ? inFileName : "(NULL)")
  226. << " to " << destFileName.c_str() << std::endl);
  227. this->ConfigureFile(inFileName, destFileName.c_str());
  228. return true;
  229. }
  230. */
  231. bool cmCPackOSXX11Generator::CopyResourcePlistFile(
  232. const std::string& name, const std::string& dir,
  233. const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
  234. {
  235. std::string inFName = "CPack.";
  236. inFName += name;
  237. inFName += ".in";
  238. std::string inFileName = this->FindTemplate(inFName.c_str());
  239. if (inFileName.empty()) {
  240. cmCPackLogger(cmCPackLog::LOG_ERROR,
  241. "Cannot find input file: " << inFName << std::endl);
  242. return false;
  243. }
  244. if (!outputFileName) {
  245. outputFileName = name.c_str();
  246. }
  247. std::string destFileName = dir;
  248. destFileName += "/";
  249. destFileName += outputFileName;
  250. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  251. "Configure file: " << inFileName.c_str() << " to "
  252. << destFileName.c_str() << std::endl);
  253. this->ConfigureFile(inFileName.c_str(), destFileName.c_str(), copyOnly);
  254. return true;
  255. }
  256. const char* cmCPackOSXX11Generator::GetPackagingInstallPrefix()
  257. {
  258. this->InstallPrefix = "/";
  259. this->InstallPrefix += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  260. this->InstallPrefix += ".app/Contents/Resources";
  261. return this->InstallPrefix.c_str();
  262. }