cmCPackDragNDropGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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::PackageFiles()
  94. {
  95. return this->CreateDMG();
  96. }
  97. //----------------------------------------------------------------------
  98. bool cmCPackDragNDropGenerator::CopyFile(cmOStringStream& source,
  99. cmOStringStream& target)
  100. {
  101. if(!cmSystemTools::CopyFileIfDifferent(
  102. source.str().c_str(),
  103. target.str().c_str()))
  104. {
  105. cmCPackLogger(cmCPackLog::LOG_ERROR,
  106. "Error copying "
  107. << source.str()
  108. << " to "
  109. << target.str()
  110. << std::endl);
  111. return false;
  112. }
  113. return true;
  114. }
  115. //----------------------------------------------------------------------
  116. bool cmCPackDragNDropGenerator::RunCommand(cmOStringStream& command,
  117. std::string* output)
  118. {
  119. int exit_code = 1;
  120. bool result = cmSystemTools::RunSingleCommand(
  121. command.str().c_str(),
  122. output,
  123. &exit_code,
  124. 0,
  125. this->GeneratorVerbose,
  126. 0);
  127. if(!result || exit_code)
  128. {
  129. cmCPackLogger(cmCPackLog::LOG_ERROR,
  130. "Error executing: "
  131. << command.str()
  132. << std::endl);
  133. return false;
  134. }
  135. return true;
  136. }
  137. //----------------------------------------------------------------------
  138. int cmCPackDragNDropGenerator::CreateDMG()
  139. {
  140. // Get optional arguments ...
  141. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  142. ? this->GetOption("CPACK_PACKAGE_ICON") : "";
  143. const std::string cpack_dmg_volume_name =
  144. this->GetOption("CPACK_DMG_VOLUME_NAME")
  145. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  146. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  147. const std::string cpack_dmg_format =
  148. this->GetOption("CPACK_DMG_FORMAT")
  149. ? this->GetOption("CPACK_DMG_FORMAT") : "UDZO";
  150. // Get optional arguments ...
  151. std::string cpack_license_file =
  152. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") ?
  153. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") : "";
  154. const std::string cpack_dmg_background_image =
  155. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  156. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE") : "";
  157. const std::string cpack_dmg_ds_store =
  158. this->GetOption("CPACK_DMG_DS_STORE")
  159. ? this->GetOption("CPACK_DMG_DS_STORE") : "";
  160. // only put license on dmg if is user provided
  161. if(!cpack_license_file.empty() &&
  162. cpack_license_file.find("CPack.GenericLicense.txt") != std::string::npos)
  163. {
  164. cpack_license_file = "";
  165. }
  166. // The staging directory contains everything that will end-up inside the
  167. // final disk image ...
  168. cmOStringStream staging;
  169. staging << toplevel;
  170. // Add a symlink to /Applications so users can drag-and-drop the bundle
  171. // into it
  172. cmOStringStream application_link;
  173. application_link << staging.str() << "/Applications";
  174. cmSystemTools::CreateSymlink("/Applications",
  175. application_link.str().c_str());
  176. // Optionally add a custom volume icon ...
  177. if(!cpack_package_icon.empty())
  178. {
  179. cmOStringStream package_icon_source;
  180. package_icon_source << cpack_package_icon;
  181. cmOStringStream package_icon_destination;
  182. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  183. if(!this->CopyFile(package_icon_source, package_icon_destination))
  184. {
  185. cmCPackLogger(cmCPackLog::LOG_ERROR,
  186. "Error copying disk volume icon. "
  187. "Check the value of CPACK_PACKAGE_ICON."
  188. << std::endl);
  189. return 0;
  190. }
  191. }
  192. // Optionally add a custom .DS_Store file
  193. // (e.g. for setting background/layout) ...
  194. if(!cpack_dmg_ds_store.empty())
  195. {
  196. cmOStringStream package_settings_source;
  197. package_settings_source << cpack_dmg_ds_store;
  198. cmOStringStream package_settings_destination;
  199. package_settings_destination << staging.str() << "/.DS_Store";
  200. if(!this->CopyFile(package_settings_source, package_settings_destination))
  201. {
  202. cmCPackLogger(cmCPackLog::LOG_ERROR,
  203. "Error copying disk volume settings file. "
  204. "Check the value of CPACK_DMG_DS_STORE."
  205. << std::endl);
  206. return 0;
  207. }
  208. }
  209. // Optionally add a custom background image ...
  210. if(!cpack_dmg_background_image.empty())
  211. {
  212. cmOStringStream package_background_source;
  213. package_background_source << cpack_dmg_background_image;
  214. cmOStringStream package_background_destination;
  215. package_background_destination << staging.str() << "/background.png";
  216. if(!this->CopyFile(package_background_source,
  217. package_background_destination))
  218. {
  219. cmCPackLogger(cmCPackLog::LOG_ERROR,
  220. "Error copying disk volume background image. "
  221. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  222. << std::endl);
  223. return 0;
  224. }
  225. cmOStringStream temp_background_hiding_command;
  226. temp_background_hiding_command << this->GetOption("CPACK_COMMAND_SETFILE");
  227. temp_background_hiding_command << " -a V \"";
  228. temp_background_hiding_command << package_background_destination.str();
  229. temp_background_hiding_command << "\"";
  230. if(!this->RunCommand(temp_background_hiding_command))
  231. {
  232. cmCPackLogger(cmCPackLog::LOG_ERROR,
  233. "Error setting attributes on disk volume background image."
  234. << std::endl);
  235. return 0;
  236. }
  237. }
  238. // Create a temporary read-write disk image ...
  239. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  240. temp_image += "/temp.dmg";
  241. cmOStringStream temp_image_command;
  242. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  243. temp_image_command << " create";
  244. temp_image_command << " -ov";
  245. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  246. temp_image_command << " -volname \""
  247. << cpack_dmg_volume_name << "\"";
  248. temp_image_command << " -format UDRW";
  249. temp_image_command << " \"" << temp_image << "\"";
  250. if(!this->RunCommand(temp_image_command))
  251. {
  252. cmCPackLogger(cmCPackLog::LOG_ERROR,
  253. "Error generating temporary disk image."
  254. << std::endl);
  255. return 0;
  256. }
  257. // Optionally set the custom icon flag for the image ...
  258. if(!cpack_package_icon.empty())
  259. {
  260. cmOStringStream temp_mount;
  261. cmOStringStream attach_command;
  262. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  263. attach_command << " attach";
  264. attach_command << " \"" << temp_image << "\"";
  265. std::string attach_output;
  266. if(!this->RunCommand(attach_command, &attach_output))
  267. {
  268. cmCPackLogger(cmCPackLog::LOG_ERROR,
  269. "Error attaching temporary disk image."
  270. << std::endl);
  271. return 0;
  272. }
  273. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  274. mountpoint_regex.find(attach_output.c_str());
  275. temp_mount << mountpoint_regex.match(1);
  276. cmOStringStream setfile_command;
  277. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  278. setfile_command << " -a C";
  279. setfile_command << " \"" << temp_mount.str() << "\"";
  280. if(!this->RunCommand(setfile_command))
  281. {
  282. cmCPackLogger(cmCPackLog::LOG_ERROR,
  283. "Error assigning custom icon to temporary disk image."
  284. << std::endl);
  285. return 0;
  286. }
  287. cmOStringStream detach_command;
  288. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  289. detach_command << " detach";
  290. detach_command << " \"" << temp_mount.str() << "\"";
  291. if(!this->RunCommand(detach_command))
  292. {
  293. cmCPackLogger(cmCPackLog::LOG_ERROR,
  294. "Error detaching temporary disk image."
  295. << std::endl);
  296. return 0;
  297. }
  298. }
  299. if(!cpack_license_file.empty())
  300. {
  301. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  302. sla_r += "/sla.r";
  303. std::ifstream ifs;
  304. ifs.open(cpack_license_file.c_str());
  305. if(ifs.is_open())
  306. {
  307. cmGeneratedFileStream osf(sla_r.c_str());
  308. osf << SLAHeader;
  309. osf << "\n";
  310. osf << "data 'TEXT' (5002, \"English\") {\n";
  311. while(ifs.good())
  312. {
  313. std::string line;
  314. std::getline(ifs, line);
  315. // escape quotes
  316. std::string::size_type pos = line.find('\"');
  317. while(pos != std::string::npos)
  318. {
  319. line.replace(pos, 1, "\\\"");
  320. pos = line.find('\"', pos+2);
  321. }
  322. osf << " \"" << line << "\\n\"\n";
  323. }
  324. osf << "};\n";
  325. osf << "\n";
  326. osf << SLASTREnglish;
  327. ifs.close();
  328. osf.close();
  329. }
  330. // convert to UDCO
  331. std::string temp_udco = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  332. temp_udco += "/temp-udco.dmg";
  333. cmOStringStream udco_image_command;
  334. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  335. udco_image_command << " convert \"" << temp_image << "\"";
  336. udco_image_command << " -format UDCO";
  337. udco_image_command << " -o \"" << temp_udco << "\"";
  338. std::string error;
  339. if(!this->RunCommand(udco_image_command, &error))
  340. {
  341. cmCPackLogger(cmCPackLog::LOG_ERROR,
  342. "Error converting to UDCO dmg for adding SLA." << std::endl
  343. << error
  344. << std::endl);
  345. return 0;
  346. }
  347. // unflatten dmg
  348. cmOStringStream unflatten_command;
  349. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  350. unflatten_command << " unflatten ";
  351. unflatten_command << "\"" << temp_udco << "\"";
  352. if(!this->RunCommand(unflatten_command, &error))
  353. {
  354. cmCPackLogger(cmCPackLog::LOG_ERROR,
  355. "Error unflattening dmg for adding SLA." << std::endl
  356. << error
  357. << std::endl);
  358. return 0;
  359. }
  360. // Rez the SLA
  361. cmOStringStream embed_sla_command;
  362. embed_sla_command << "/bin/bash -c \""; // need expansion of "*.r"
  363. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  364. embed_sla_command << " /Developer/Headers/FlatCarbon/*.r ";
  365. embed_sla_command << "'" << sla_r << "'";
  366. embed_sla_command << " -a -o ";
  367. embed_sla_command << "'" << temp_udco << "'\"";
  368. if(!this->RunCommand(embed_sla_command, &error))
  369. {
  370. cmCPackLogger(cmCPackLog::LOG_ERROR,
  371. "Error adding SLA." << std::endl
  372. << error
  373. << std::endl);
  374. return 0;
  375. }
  376. // flatten dmg
  377. cmOStringStream flatten_command;
  378. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  379. flatten_command << " flatten ";
  380. flatten_command << "\"" << temp_udco << "\"";
  381. if(!this->RunCommand(flatten_command, &error))
  382. {
  383. cmCPackLogger(cmCPackLog::LOG_ERROR,
  384. "Error flattening dmg for adding SLA." << std::endl
  385. << error
  386. << std::endl);
  387. return 0;
  388. }
  389. temp_image = temp_udco;
  390. }
  391. // Create the final compressed read-only disk image ...
  392. cmOStringStream final_image_command;
  393. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  394. final_image_command << " convert \"" << temp_image << "\"";
  395. final_image_command << " -format ";
  396. final_image_command << cpack_dmg_format;
  397. final_image_command << " -imagekey";
  398. final_image_command << " zlib-level=9";
  399. final_image_command << " -o \"" << packageFileNames[0] << "\"";
  400. if(!this->RunCommand(final_image_command))
  401. {
  402. cmCPackLogger(cmCPackLog::LOG_ERROR,
  403. "Error compressing disk image."
  404. << std::endl);
  405. return 0;
  406. }
  407. return 1;
  408. }