cmCPackBundleGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackBundleGenerator.h"
  14. #include "cmCPackLog.h"
  15. #include "cmSystemTools.h"
  16. //----------------------------------------------------------------------
  17. cmCPackBundleGenerator::cmCPackBundleGenerator()
  18. {
  19. }
  20. //----------------------------------------------------------------------
  21. cmCPackBundleGenerator::~cmCPackBundleGenerator()
  22. {
  23. }
  24. //----------------------------------------------------------------------
  25. int cmCPackBundleGenerator::InitializeInternal()
  26. {
  27. const std::string hdiutil_path = cmSystemTools::FindProgram("hdiutil",
  28. std::vector<std::string>(), false);
  29. if(hdiutil_path.empty())
  30. {
  31. cmCPackLogger(cmCPackLog::LOG_ERROR,
  32. "Cannot locate hdiutil command"
  33. << std::endl);
  34. return 0;
  35. }
  36. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  37. const std::string setfile_path = cmSystemTools::FindProgram("SetFile",
  38. std::vector<std::string>(1, "/Developer/Tools"), false);
  39. if(setfile_path.empty())
  40. {
  41. cmCPackLogger(cmCPackLog::LOG_ERROR,
  42. "Cannot locate SetFile command"
  43. << std::endl);
  44. return 0;
  45. }
  46. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  47. return this->Superclass::InitializeInternal();
  48. }
  49. //----------------------------------------------------------------------
  50. const char* cmCPackBundleGenerator::GetOutputExtension()
  51. {
  52. return ".dmg";
  53. }
  54. //----------------------------------------------------------------------
  55. const char* cmCPackBundleGenerator::GetPackagingInstallPrefix()
  56. {
  57. this->InstallPrefix = "/";
  58. this->InstallPrefix += this->GetOption("CPACK_BUNDLE_NAME");
  59. this->InstallPrefix += ".app/Contents/Resources";
  60. return this->InstallPrefix.c_str();
  61. }
  62. //----------------------------------------------------------------------
  63. int cmCPackBundleGenerator::CompressFiles(const char* outFileName,
  64. const char* toplevel, const std::vector<std::string>& files)
  65. {
  66. (void) files;
  67. // Get required arguments ...
  68. const std::string cpack_bundle_name = this->GetOption("CPACK_BUNDLE_NAME")
  69. ? this->GetOption("CPACK_BUNDLE_NAME") : "";
  70. if(cpack_bundle_name.empty())
  71. {
  72. cmCPackLogger(cmCPackLog::LOG_ERROR,
  73. "CPACK_BUNDLE_NAME must be set."
  74. << std::endl);
  75. return 0;
  76. }
  77. const std::string cpack_bundle_plist = this->GetOption("CPACK_BUNDLE_PLIST")
  78. ? this->GetOption("CPACK_BUNDLE_PLIST") : "";
  79. if(cpack_bundle_plist.empty())
  80. {
  81. cmCPackLogger(cmCPackLog::LOG_ERROR,
  82. "CPACK_BUNDLE_PLIST must be set."
  83. << std::endl);
  84. return 0;
  85. }
  86. const std::string cpack_bundle_icon = this->GetOption("CPACK_BUNDLE_ICON")
  87. ? this->GetOption("CPACK_BUNDLE_ICON") : "";
  88. if(cpack_bundle_icon.empty())
  89. {
  90. cmCPackLogger(cmCPackLog::LOG_ERROR,
  91. "CPACK_BUNDLE_ICON must be set."
  92. << std::endl);
  93. return 0;
  94. }
  95. const std::string cpack_bundle_startup_command =
  96. this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND") ?
  97. this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND") : "";
  98. if(cpack_bundle_startup_command.empty())
  99. {
  100. cmCPackLogger(cmCPackLog::LOG_ERROR,
  101. "CPACK_BUNDLE_STARTUP_COMMAND must be set."
  102. << std::endl);
  103. return 0;
  104. }
  105. // Get optional arguments ...
  106. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  107. ? this->GetOption("CPACK_PACKAGE_ICON") : "";
  108. // The staging directory contains everything that will end-up inside the
  109. // final disk image ...
  110. cmOStringStream staging;
  111. staging << toplevel;
  112. cmOStringStream contents;
  113. contents << staging.str() << "/" << cpack_bundle_name
  114. << ".app/" << "Contents";
  115. cmOStringStream application;
  116. application << contents.str() << "/" << "MacOS";
  117. cmOStringStream resources;
  118. resources << contents.str() << "/" << "Resources";
  119. // Install a required, user-provided bundle metadata file ...
  120. cmOStringStream plist_source;
  121. plist_source << cpack_bundle_plist;
  122. cmOStringStream plist_target;
  123. plist_target << contents.str() << "/" << "Info.plist";
  124. if(!this->CopyFile(plist_source, plist_target))
  125. {
  126. cmCPackLogger(cmCPackLog::LOG_ERROR,
  127. "Error copying plist. Check the value of CPACK_BUNDLE_PLIST."
  128. << std::endl);
  129. return 0;
  130. }
  131. // Install a user-provided bundle icon ...
  132. cmOStringStream icon_source;
  133. icon_source << cpack_bundle_icon;
  134. cmOStringStream icon_target;
  135. icon_target << resources.str() << "/" << cpack_bundle_name << ".icns";
  136. if(!this->CopyFile(icon_source, icon_target))
  137. {
  138. cmCPackLogger(cmCPackLog::LOG_ERROR,
  139. "Error copying bundle icon. Check the value of CPACK_BUNDLE_ICON."
  140. << std::endl);
  141. return 0;
  142. }
  143. // Install a user-provided startup command (could be an executable or a
  144. // script) ...
  145. cmOStringStream command_source;
  146. command_source << cpack_bundle_startup_command;
  147. cmOStringStream command_target;
  148. command_target << application.str() << "/" << cpack_bundle_name;
  149. if(!this->CopyFile(command_source, command_target))
  150. {
  151. cmCPackLogger(cmCPackLog::LOG_ERROR,
  152. "Error copying startup command. Check the value of CPACK_BUNDLE_STARTUP_COMMAND."
  153. << std::endl);
  154. return 0;
  155. }
  156. cmSystemTools::SetPermissions(command_target.str().c_str(), 0777);
  157. // Add a symlink to /Applications so users can drag-and-drop the bundle
  158. // into it
  159. cmOStringStream application_link;
  160. application_link << staging.str() << "/Applications";
  161. cmSystemTools::CreateSymlink("/Applications",
  162. application_link.str().c_str());
  163. // Optionally add a custom volume icon ...
  164. if(!cpack_package_icon.empty())
  165. {
  166. cmOStringStream package_icon_source;
  167. package_icon_source << cpack_package_icon;
  168. cmOStringStream package_icon_destination;
  169. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  170. if(!this->CopyFile(package_icon_source, package_icon_destination))
  171. {
  172. cmCPackLogger(cmCPackLog::LOG_ERROR,
  173. "Error copying disk volume icon. Check the value of CPACK_PACKAGE_ICON."
  174. << std::endl);
  175. return 0;
  176. }
  177. }
  178. // Create a temporary read-write disk image ...
  179. cmOStringStream temp_image;
  180. temp_image << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "/temp.dmg";
  181. cmOStringStream temp_image_command;
  182. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  183. temp_image_command << " create";
  184. temp_image_command << " -ov";
  185. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  186. temp_image_command << " -volname \""
  187. << this->GetOption("CPACK_PACKAGE_FILE_NAME") << "\"";
  188. temp_image_command << " -format UDRW";
  189. temp_image_command << " \"" << temp_image.str() << "\"";
  190. if(!this->RunCommand(temp_image_command))
  191. {
  192. cmCPackLogger(cmCPackLog::LOG_ERROR,
  193. "Error generating temporary disk image."
  194. << std::endl);
  195. return 0;
  196. }
  197. // Optionally set the custom icon flag for the image ...
  198. if(!cpack_package_icon.empty())
  199. {
  200. cmOStringStream temp_mount;
  201. temp_mount << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "/mnt";
  202. cmSystemTools::MakeDirectory(temp_mount.str().c_str());
  203. cmOStringStream attach_command;
  204. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  205. attach_command << " attach";
  206. attach_command << " -mountpoint \"" << temp_mount.str() << "\"";
  207. attach_command << " \"" << temp_image.str() << "\"";
  208. if(!this->RunCommand(attach_command))
  209. {
  210. cmCPackLogger(cmCPackLog::LOG_ERROR,
  211. "Error attaching temporary disk image."
  212. << std::endl);
  213. return 0;
  214. }
  215. cmOStringStream setfile_command;
  216. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  217. setfile_command << " -a C";
  218. setfile_command << " \"" << temp_mount.str() << "\"";
  219. if(!this->RunCommand(setfile_command))
  220. {
  221. cmCPackLogger(cmCPackLog::LOG_ERROR,
  222. "Error assigning custom icon to temporary disk image."
  223. << std::endl);
  224. return 0;
  225. }
  226. cmOStringStream detach_command;
  227. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  228. detach_command << " detach";
  229. detach_command << " \"" << temp_mount.str() << "\"";
  230. if(!this->RunCommand(detach_command))
  231. {
  232. cmCPackLogger(cmCPackLog::LOG_ERROR,
  233. "Error detaching temporary disk image."
  234. << std::endl);
  235. return 0;
  236. }
  237. }
  238. // Create the final compressed read-only disk image ...
  239. cmOStringStream final_image_command;
  240. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  241. final_image_command << " convert \"" << temp_image.str() << "\"";
  242. final_image_command << " -format UDZO";
  243. final_image_command << " -imagekey";
  244. final_image_command << " zlib-level=9";
  245. final_image_command << " -o \"" << outFileName << "\"";
  246. if(!this->RunCommand(final_image_command))
  247. {
  248. cmCPackLogger(cmCPackLog::LOG_ERROR,
  249. "Error compressing disk image."
  250. << std::endl);
  251. return 0;
  252. }
  253. return 1;
  254. }
  255. //----------------------------------------------------------------------
  256. bool cmCPackBundleGenerator::CopyFile(cmOStringStream& source,
  257. cmOStringStream& target)
  258. {
  259. if(!cmSystemTools::CopyFileIfDifferent(
  260. source.str().c_str(),
  261. target.str().c_str()))
  262. {
  263. cmCPackLogger(cmCPackLog::LOG_ERROR,
  264. "Error copying "
  265. << source.str()
  266. << " to "
  267. << target.str()
  268. << std::endl);
  269. return false;
  270. }
  271. return true;
  272. }
  273. //----------------------------------------------------------------------
  274. bool cmCPackBundleGenerator::RunCommand(cmOStringStream& command)
  275. {
  276. std::string output;
  277. int exit_code = 1;
  278. bool result = cmSystemTools::RunSingleCommand(
  279. command.str().c_str(),
  280. &output,
  281. &exit_code,
  282. 0,
  283. this->GeneratorVerbose,
  284. 0);
  285. if(!result || exit_code)
  286. {
  287. cmCPackLogger(cmCPackLog::LOG_ERROR,
  288. "Error executing: "
  289. << command.str()
  290. << std::endl);
  291. return false;
  292. }
  293. return true;
  294. }