cmCPackOSXX11Generator.cxx 9.9 KB

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