cmCPackOSXX11Generator.cxx 9.9 KB

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