cmCPackDragNDropGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "cmCPackDragNDropGenerator.h"
  11. #include "cmCPackLog.h"
  12. #include "cmSystemTools.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. static const char* SLAHeader =
  16. "data 'LPic' (5000) {\n"
  17. " $\"0002 0011 0003 0001 0000 0000 0002 0000\"\n"
  18. " $\"0008 0003 0000 0001 0004 0000 0004 0005\"\n"
  19. " $\"0000 000E 0006 0001 0005 0007 0000 0007\"\n"
  20. " $\"0008 0000 0047 0009 0000 0034 000A 0001\"\n"
  21. " $\"0035 000B 0001 0020 000C 0000 0011 000D\"\n"
  22. " $\"0000 005B 0004 0000 0033 000F 0001 000C\"\n"
  23. " $\"0010 0000 000B 000E 0000\"\n"
  24. "};\n"
  25. "\n";
  26. static const char* SLASTREnglish =
  27. "resource 'STR#' (5002, \"English\") {\n"
  28. " {\n"
  29. " \"English\",\n"
  30. " \"Agree\",\n"
  31. " \"Disagree\",\n"
  32. " \"Print\",\n"
  33. " \"Save...\",\n"
  34. " \"You agree to the License Agreement terms when you click \"\n"
  35. " \"the \\\"Agree\\\" button.\",\n"
  36. " \"Software License Agreement\",\n"
  37. " \"This text cannot be saved. This disk may be full or locked, "
  38. "or the \"\n"
  39. " \"file may be locked.\",\n"
  40. " \"Unable to print. Make sure you have selected a printer.\"\n"
  41. " }\n"
  42. "};\n"
  43. "\n";
  44. //----------------------------------------------------------------------
  45. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  46. {
  47. }
  48. //----------------------------------------------------------------------
  49. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  50. {
  51. }
  52. //----------------------------------------------------------------------
  53. int cmCPackDragNDropGenerator::InitializeInternal()
  54. {
  55. const std::string hdiutil_path = cmSystemTools::FindProgram("hdiutil",
  56. std::vector<std::string>(), false);
  57. if(hdiutil_path.empty())
  58. {
  59. cmCPackLogger(cmCPackLog::LOG_ERROR,
  60. "Cannot locate hdiutil command"
  61. << std::endl);
  62. return 0;
  63. }
  64. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  65. const std::string setfile_path = cmSystemTools::FindProgram("SetFile",
  66. std::vector<std::string>(1, "/Developer/Tools"), false);
  67. if(setfile_path.empty())
  68. {
  69. cmCPackLogger(cmCPackLog::LOG_ERROR,
  70. "Cannot locate SetFile command"
  71. << std::endl);
  72. return 0;
  73. }
  74. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  75. const std::string rez_path = cmSystemTools::FindProgram("Rez",
  76. std::vector<std::string>(1, "/Developer/Tools"), false);
  77. if(rez_path.empty())
  78. {
  79. cmCPackLogger(cmCPackLog::LOG_ERROR,
  80. "Cannot locate Rez command"
  81. << std::endl);
  82. return 0;
  83. }
  84. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  85. return this->Superclass::InitializeInternal();
  86. }
  87. //----------------------------------------------------------------------
  88. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  89. {
  90. return ".dmg";
  91. }
  92. //----------------------------------------------------------------------
  93. int cmCPackDragNDropGenerator::CompressFiles(const char* outFileName,
  94. const char* toplevel, const std::vector<std::string>& files)
  95. {
  96. (void) files;
  97. return this->CreateDMG(toplevel, outFileName);
  98. }
  99. //----------------------------------------------------------------------
  100. bool cmCPackDragNDropGenerator::CopyFile(cmOStringStream& source,
  101. cmOStringStream& target)
  102. {
  103. if(!cmSystemTools::CopyFileIfDifferent(
  104. source.str().c_str(),
  105. target.str().c_str()))
  106. {
  107. cmCPackLogger(cmCPackLog::LOG_ERROR,
  108. "Error copying "
  109. << source.str()
  110. << " to "
  111. << target.str()
  112. << std::endl);
  113. return false;
  114. }
  115. return true;
  116. }
  117. //----------------------------------------------------------------------
  118. bool cmCPackDragNDropGenerator::RunCommand(cmOStringStream& command,
  119. std::string* output)
  120. {
  121. int exit_code = 1;
  122. bool result = cmSystemTools::RunSingleCommand(
  123. command.str().c_str(),
  124. output,
  125. &exit_code,
  126. 0,
  127. this->GeneratorVerbose,
  128. 0);
  129. if(!result || exit_code)
  130. {
  131. cmCPackLogger(cmCPackLog::LOG_ERROR,
  132. "Error executing: "
  133. << command.str()
  134. << std::endl);
  135. return false;
  136. }
  137. return true;
  138. }
  139. //----------------------------------------------------------------------
  140. int cmCPackDragNDropGenerator::CreateDMG(const std::string& toplevel,
  141. const std::string& outFileName)
  142. {
  143. // Get optional arguments ...
  144. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  145. ? this->GetOption("CPACK_PACKAGE_ICON") : "";
  146. const std::string cpack_dmg_volume_name =
  147. this->GetOption("CPACK_DMG_VOLUME_NAME")
  148. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  149. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  150. const std::string cpack_dmg_format =
  151. this->GetOption("CPACK_DMG_FORMAT")
  152. ? this->GetOption("CPACK_DMG_FORMAT") : "UDZO";
  153. // Get optional arguments ...
  154. std::string cpack_license_file =
  155. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") ?
  156. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") : "";
  157. const std::string cpack_dmg_background_image =
  158. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  159. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE") : "";
  160. const std::string cpack_dmg_ds_store =
  161. this->GetOption("CPACK_DMG_DS_STORE")
  162. ? this->GetOption("CPACK_DMG_DS_STORE") : "";
  163. // only put license on dmg if is user provided
  164. if(!cpack_license_file.empty() &&
  165. cpack_license_file.find("CPack.GenericLicense.txt") != std::string::npos)
  166. {
  167. cpack_license_file = "";
  168. }
  169. // The staging directory contains everything that will end-up inside the
  170. // final disk image ...
  171. cmOStringStream staging;
  172. staging << toplevel;
  173. // Add a symlink to /Applications so users can drag-and-drop the bundle
  174. // into it
  175. cmOStringStream application_link;
  176. application_link << staging.str() << "/Applications";
  177. cmSystemTools::CreateSymlink("/Applications",
  178. application_link.str().c_str());
  179. // Optionally add a custom volume icon ...
  180. if(!cpack_package_icon.empty())
  181. {
  182. cmOStringStream package_icon_source;
  183. package_icon_source << cpack_package_icon;
  184. cmOStringStream package_icon_destination;
  185. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  186. if(!this->CopyFile(package_icon_source, package_icon_destination))
  187. {
  188. cmCPackLogger(cmCPackLog::LOG_ERROR,
  189. "Error copying disk volume icon. "
  190. "Check the value of CPACK_PACKAGE_ICON."
  191. << std::endl);
  192. return 0;
  193. }
  194. }
  195. // Optionally add a custom .DS_Store file
  196. // (e.g. for setting background/layout) ...
  197. if(!cpack_dmg_ds_store.empty())
  198. {
  199. cmOStringStream package_settings_source;
  200. package_settings_source << cpack_dmg_ds_store;
  201. cmOStringStream package_settings_destination;
  202. package_settings_destination << staging.str() << "/.DS_Store";
  203. if(!this->CopyFile(package_settings_source, package_settings_destination))
  204. {
  205. cmCPackLogger(cmCPackLog::LOG_ERROR,
  206. "Error copying disk volume settings file. "
  207. "Check the value of CPACK_DMG_DS_STORE."
  208. << std::endl);
  209. return 0;
  210. }
  211. }
  212. // Optionally add a custom background image ...
  213. if(!cpack_dmg_background_image.empty())
  214. {
  215. cmOStringStream package_background_source;
  216. package_background_source << cpack_dmg_background_image;
  217. cmOStringStream package_background_destination;
  218. package_background_destination << staging.str() << "/background.png";
  219. if(!this->CopyFile(package_background_source,
  220. package_background_destination))
  221. {
  222. cmCPackLogger(cmCPackLog::LOG_ERROR,
  223. "Error copying disk volume background image. "
  224. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  225. << std::endl);
  226. return 0;
  227. }
  228. cmOStringStream temp_background_hiding_command;
  229. temp_background_hiding_command << this->GetOption("CPACK_COMMAND_SETFILE");
  230. temp_background_hiding_command << " -a V \"";
  231. temp_background_hiding_command << package_background_destination.str();
  232. temp_background_hiding_command << "\"";
  233. if(!this->RunCommand(temp_background_hiding_command))
  234. {
  235. cmCPackLogger(cmCPackLog::LOG_ERROR,
  236. "Error setting attributes on disk volume background image."
  237. << std::endl);
  238. return 0;
  239. }
  240. }
  241. // Create a temporary read-write disk image ...
  242. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  243. temp_image += "/temp.dmg";
  244. cmOStringStream temp_image_command;
  245. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  246. temp_image_command << " create";
  247. temp_image_command << " -ov";
  248. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  249. temp_image_command << " -volname \""
  250. << cpack_dmg_volume_name << "\"";
  251. temp_image_command << " -format UDRW";
  252. temp_image_command << " \"" << temp_image << "\"";
  253. if(!this->RunCommand(temp_image_command))
  254. {
  255. cmCPackLogger(cmCPackLog::LOG_ERROR,
  256. "Error generating temporary disk image."
  257. << std::endl);
  258. return 0;
  259. }
  260. // Optionally set the custom icon flag for the image ...
  261. if(!cpack_package_icon.empty())
  262. {
  263. cmOStringStream temp_mount;
  264. cmOStringStream attach_command;
  265. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  266. attach_command << " attach";
  267. attach_command << " \"" << temp_image << "\"";
  268. std::string attach_output;
  269. if(!this->RunCommand(attach_command, &attach_output))
  270. {
  271. cmCPackLogger(cmCPackLog::LOG_ERROR,
  272. "Error attaching temporary disk image."
  273. << std::endl);
  274. return 0;
  275. }
  276. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  277. mountpoint_regex.find(attach_output.c_str());
  278. temp_mount << mountpoint_regex.match(1);
  279. cmOStringStream setfile_command;
  280. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  281. setfile_command << " -a C";
  282. setfile_command << " \"" << temp_mount.str() << "\"";
  283. if(!this->RunCommand(setfile_command))
  284. {
  285. cmCPackLogger(cmCPackLog::LOG_ERROR,
  286. "Error assigning custom icon to temporary disk image."
  287. << std::endl);
  288. return 0;
  289. }
  290. cmOStringStream detach_command;
  291. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  292. detach_command << " detach";
  293. detach_command << " \"" << temp_mount.str() << "\"";
  294. if(!this->RunCommand(detach_command))
  295. {
  296. cmCPackLogger(cmCPackLog::LOG_ERROR,
  297. "Error detaching temporary disk image."
  298. << std::endl);
  299. return 0;
  300. }
  301. }
  302. if(!cpack_license_file.empty())
  303. {
  304. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  305. sla_r += "/sla.r";
  306. std::ifstream ifs;
  307. ifs.open(cpack_license_file.c_str());
  308. if(ifs.is_open())
  309. {
  310. cmGeneratedFileStream osf(sla_r.c_str());
  311. osf << SLAHeader;
  312. osf << "\n";
  313. osf << "data 'TEXT' (5002, \"English\") {\n";
  314. while(ifs.good())
  315. {
  316. std::string line;
  317. std::getline(ifs, line);
  318. osf << " \"" << line << "\\n\"\n";
  319. }
  320. osf << "};\n";
  321. osf << "\n";
  322. osf << SLASTREnglish;
  323. ifs.close();
  324. osf.close();
  325. }
  326. // convert to UDCO
  327. std::string temp_udco = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  328. temp_udco += "/temp-udco.dmg";
  329. cmOStringStream udco_image_command;
  330. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  331. udco_image_command << " convert \"" << temp_image << "\"";
  332. udco_image_command << " -format UDCO";
  333. udco_image_command << " -o \"" << temp_udco << "\"";
  334. std::string error;
  335. if(!this->RunCommand(udco_image_command, &error))
  336. {
  337. cmCPackLogger(cmCPackLog::LOG_ERROR,
  338. "Error converting to UDCO dmg for adding SLA." << std::endl
  339. << error
  340. << std::endl);
  341. return 0;
  342. }
  343. // unflatten dmg
  344. cmOStringStream unflatten_command;
  345. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  346. unflatten_command << " unflatten ";
  347. unflatten_command << "\"" << temp_udco << "\"";
  348. if(!this->RunCommand(unflatten_command, &error))
  349. {
  350. cmCPackLogger(cmCPackLog::LOG_ERROR,
  351. "Error unflattening dmg for adding SLA." << std::endl
  352. << error
  353. << std::endl);
  354. return 0;
  355. }
  356. // Rez the SLA
  357. cmOStringStream embed_sla_command;
  358. embed_sla_command << "/bin/bash -c \""; // need expansion of "*.r"
  359. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  360. embed_sla_command << " /Developer/Headers/FlatCarbon/*.r ";
  361. embed_sla_command << "'" << sla_r << "'";
  362. embed_sla_command << " -a -o ";
  363. embed_sla_command << "'" << temp_udco << "'\"";
  364. if(!this->RunCommand(embed_sla_command, &error))
  365. {
  366. cmCPackLogger(cmCPackLog::LOG_ERROR,
  367. "Error adding SLA." << std::endl
  368. << error
  369. << std::endl);
  370. return 0;
  371. }
  372. // flatten dmg
  373. cmOStringStream flatten_command;
  374. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  375. flatten_command << " flatten ";
  376. flatten_command << "\"" << temp_udco << "\"";
  377. if(!this->RunCommand(flatten_command, &error))
  378. {
  379. cmCPackLogger(cmCPackLog::LOG_ERROR,
  380. "Error flattening dmg for adding SLA." << std::endl
  381. << error
  382. << std::endl);
  383. return 0;
  384. }
  385. temp_image = temp_udco;
  386. }
  387. // Create the final compressed read-only disk image ...
  388. cmOStringStream final_image_command;
  389. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  390. final_image_command << " convert \"" << temp_image << "\"";
  391. final_image_command << " -format ";
  392. final_image_command << cpack_dmg_format;
  393. final_image_command << " -imagekey";
  394. final_image_command << " zlib-level=9";
  395. final_image_command << " -o \"" << outFileName << "\"";
  396. if(!this->RunCommand(final_image_command))
  397. {
  398. cmCPackLogger(cmCPackLog::LOG_ERROR,
  399. "Error compressing disk image."
  400. << std::endl);
  401. return 0;
  402. }
  403. return 1;
  404. }