cmCPackOSXX11Generator.cxx 11 KB

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