cmCPackDragNDropGenerator.cxx 14 KB

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