cmCPackDragNDropGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. // default to one package file for components
  48. this->componentPackageMethod = ONE_PACKAGE;
  49. }
  50. //----------------------------------------------------------------------
  51. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  52. {
  53. }
  54. //----------------------------------------------------------------------
  55. int cmCPackDragNDropGenerator::InitializeInternal()
  56. {
  57. const std::string hdiutil_path = cmSystemTools::FindProgram("hdiutil",
  58. std::vector<std::string>(), false);
  59. if(hdiutil_path.empty())
  60. {
  61. cmCPackLogger(cmCPackLog::LOG_ERROR,
  62. "Cannot locate hdiutil command"
  63. << std::endl);
  64. return 0;
  65. }
  66. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  67. const std::string setfile_path = cmSystemTools::FindProgram("SetFile",
  68. std::vector<std::string>(1, "/Developer/Tools"), false);
  69. if(setfile_path.empty())
  70. {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR,
  72. "Cannot locate SetFile command"
  73. << std::endl);
  74. return 0;
  75. }
  76. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  77. const std::string rez_path = cmSystemTools::FindProgram("Rez",
  78. std::vector<std::string>(1, "/Developer/Tools"), false);
  79. if(rez_path.empty())
  80. {
  81. cmCPackLogger(cmCPackLog::LOG_ERROR,
  82. "Cannot locate Rez command"
  83. << std::endl);
  84. return 0;
  85. }
  86. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  87. return this->Superclass::InitializeInternal();
  88. }
  89. //----------------------------------------------------------------------
  90. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  91. {
  92. return ".dmg";
  93. }
  94. //----------------------------------------------------------------------
  95. int cmCPackDragNDropGenerator::PackageFiles()
  96. {
  97. // gather which directories to make dmg files for
  98. // multiple directories occur if packaging components or groups separately
  99. // monolith
  100. if(this->Components.empty())
  101. {
  102. return this->CreateDMG(toplevel, packageFileNames[0]);
  103. }
  104. // component install
  105. std::vector<std::string> package_files;
  106. std::map<std::string, cmCPackComponent>::iterator compIt;
  107. for (compIt=this->Components.begin();
  108. compIt!=this->Components.end(); ++compIt )
  109. {
  110. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  111. package_files.push_back(name);
  112. }
  113. std::sort(package_files.begin(), package_files.end());
  114. package_files.erase(std::unique(package_files.begin(),
  115. package_files.end()),
  116. package_files.end());
  117. // loop to create dmg files
  118. packageFileNames.clear();
  119. for(size_t i=0; i<package_files.size(); i++)
  120. {
  121. std::string full_package_name = std::string(toplevel) + std::string("/");
  122. if(package_files[i] == "ALL_IN_ONE")
  123. {
  124. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  125. }
  126. else
  127. {
  128. full_package_name += package_files[i];
  129. }
  130. full_package_name += std::string(GetOutputExtension());
  131. packageFileNames.push_back(full_package_name);
  132. std::string src_dir = toplevel;
  133. src_dir += "/";
  134. src_dir += package_files[i];
  135. if(0 == this->CreateDMG(src_dir, full_package_name))
  136. {
  137. return 0;
  138. }
  139. }
  140. return 1;
  141. }
  142. //----------------------------------------------------------------------
  143. bool cmCPackDragNDropGenerator::CopyFile(cmOStringStream& source,
  144. cmOStringStream& target)
  145. {
  146. if(!cmSystemTools::CopyFileIfDifferent(
  147. source.str().c_str(),
  148. target.str().c_str()))
  149. {
  150. cmCPackLogger(cmCPackLog::LOG_ERROR,
  151. "Error copying "
  152. << source.str()
  153. << " to "
  154. << target.str()
  155. << std::endl);
  156. return false;
  157. }
  158. return true;
  159. }
  160. //----------------------------------------------------------------------
  161. bool cmCPackDragNDropGenerator::RunCommand(cmOStringStream& command,
  162. std::string* output)
  163. {
  164. int exit_code = 1;
  165. bool result = cmSystemTools::RunSingleCommand(
  166. command.str().c_str(),
  167. output,
  168. &exit_code,
  169. 0,
  170. this->GeneratorVerbose,
  171. 0);
  172. if(!result || exit_code)
  173. {
  174. cmCPackLogger(cmCPackLog::LOG_ERROR,
  175. "Error executing: "
  176. << command.str()
  177. << std::endl);
  178. return false;
  179. }
  180. return true;
  181. }
  182. //----------------------------------------------------------------------
  183. int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir,
  184. const std::string& output_file)
  185. {
  186. // Get optional arguments ...
  187. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  188. ? this->GetOption("CPACK_PACKAGE_ICON") : "";
  189. const std::string cpack_dmg_volume_name =
  190. this->GetOption("CPACK_DMG_VOLUME_NAME")
  191. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  192. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  193. const std::string cpack_dmg_format =
  194. this->GetOption("CPACK_DMG_FORMAT")
  195. ? this->GetOption("CPACK_DMG_FORMAT") : "UDZO";
  196. // Get optional arguments ...
  197. std::string cpack_license_file =
  198. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") ?
  199. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") : "";
  200. const std::string cpack_dmg_background_image =
  201. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  202. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE") : "";
  203. const std::string cpack_dmg_ds_store =
  204. this->GetOption("CPACK_DMG_DS_STORE")
  205. ? this->GetOption("CPACK_DMG_DS_STORE") : "";
  206. // only put license on dmg if is user provided
  207. if(!cpack_license_file.empty() &&
  208. cpack_license_file.find("CPack.GenericLicense.txt") != std::string::npos)
  209. {
  210. cpack_license_file = "";
  211. }
  212. // The staging directory contains everything that will end-up inside the
  213. // final disk image ...
  214. cmOStringStream staging;
  215. staging << src_dir;
  216. // Add a symlink to /Applications so users can drag-and-drop the bundle
  217. // into it
  218. cmOStringStream application_link;
  219. application_link << staging.str() << "/Applications";
  220. cmSystemTools::CreateSymlink("/Applications",
  221. application_link.str().c_str());
  222. // Optionally add a custom volume icon ...
  223. if(!cpack_package_icon.empty())
  224. {
  225. cmOStringStream package_icon_source;
  226. package_icon_source << cpack_package_icon;
  227. cmOStringStream package_icon_destination;
  228. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  229. if(!this->CopyFile(package_icon_source, package_icon_destination))
  230. {
  231. cmCPackLogger(cmCPackLog::LOG_ERROR,
  232. "Error copying disk volume icon. "
  233. "Check the value of CPACK_PACKAGE_ICON."
  234. << std::endl);
  235. return 0;
  236. }
  237. }
  238. // Optionally add a custom .DS_Store file
  239. // (e.g. for setting background/layout) ...
  240. if(!cpack_dmg_ds_store.empty())
  241. {
  242. cmOStringStream package_settings_source;
  243. package_settings_source << cpack_dmg_ds_store;
  244. cmOStringStream package_settings_destination;
  245. package_settings_destination << staging.str() << "/.DS_Store";
  246. if(!this->CopyFile(package_settings_source, package_settings_destination))
  247. {
  248. cmCPackLogger(cmCPackLog::LOG_ERROR,
  249. "Error copying disk volume settings file. "
  250. "Check the value of CPACK_DMG_DS_STORE."
  251. << std::endl);
  252. return 0;
  253. }
  254. }
  255. // Optionally add a custom background image ...
  256. if(!cpack_dmg_background_image.empty())
  257. {
  258. cmOStringStream package_background_source;
  259. package_background_source << cpack_dmg_background_image;
  260. cmOStringStream package_background_destination;
  261. package_background_destination << staging.str() << "/background.png";
  262. if(!this->CopyFile(package_background_source,
  263. package_background_destination))
  264. {
  265. cmCPackLogger(cmCPackLog::LOG_ERROR,
  266. "Error copying disk volume background image. "
  267. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  268. << std::endl);
  269. return 0;
  270. }
  271. cmOStringStream temp_background_hiding_command;
  272. temp_background_hiding_command << this->GetOption("CPACK_COMMAND_SETFILE");
  273. temp_background_hiding_command << " -a V \"";
  274. temp_background_hiding_command << package_background_destination.str();
  275. temp_background_hiding_command << "\"";
  276. if(!this->RunCommand(temp_background_hiding_command))
  277. {
  278. cmCPackLogger(cmCPackLog::LOG_ERROR,
  279. "Error setting attributes on disk volume background image."
  280. << std::endl);
  281. return 0;
  282. }
  283. }
  284. // Create a temporary read-write disk image ...
  285. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  286. temp_image += "/temp.dmg";
  287. cmOStringStream temp_image_command;
  288. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  289. temp_image_command << " create";
  290. temp_image_command << " -ov";
  291. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  292. temp_image_command << " -volname \""
  293. << cpack_dmg_volume_name << "\"";
  294. temp_image_command << " -format UDRW";
  295. temp_image_command << " \"" << temp_image << "\"";
  296. if(!this->RunCommand(temp_image_command))
  297. {
  298. cmCPackLogger(cmCPackLog::LOG_ERROR,
  299. "Error generating temporary disk image."
  300. << std::endl);
  301. return 0;
  302. }
  303. // Optionally set the custom icon flag for the image ...
  304. if(!cpack_package_icon.empty())
  305. {
  306. cmOStringStream temp_mount;
  307. cmOStringStream attach_command;
  308. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  309. attach_command << " attach";
  310. attach_command << " \"" << temp_image << "\"";
  311. std::string attach_output;
  312. if(!this->RunCommand(attach_command, &attach_output))
  313. {
  314. cmCPackLogger(cmCPackLog::LOG_ERROR,
  315. "Error attaching temporary disk image."
  316. << std::endl);
  317. return 0;
  318. }
  319. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  320. mountpoint_regex.find(attach_output.c_str());
  321. temp_mount << mountpoint_regex.match(1);
  322. cmOStringStream setfile_command;
  323. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  324. setfile_command << " -a C";
  325. setfile_command << " \"" << temp_mount.str() << "\"";
  326. if(!this->RunCommand(setfile_command))
  327. {
  328. cmCPackLogger(cmCPackLog::LOG_ERROR,
  329. "Error assigning custom icon to temporary disk image."
  330. << std::endl);
  331. return 0;
  332. }
  333. cmOStringStream detach_command;
  334. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  335. detach_command << " detach";
  336. detach_command << " \"" << temp_mount.str() << "\"";
  337. if(!this->RunCommand(detach_command))
  338. {
  339. cmCPackLogger(cmCPackLog::LOG_ERROR,
  340. "Error detaching temporary disk image."
  341. << std::endl);
  342. return 0;
  343. }
  344. }
  345. if(!cpack_license_file.empty())
  346. {
  347. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  348. sla_r += "/sla.r";
  349. std::ifstream ifs;
  350. ifs.open(cpack_license_file.c_str());
  351. if(ifs.is_open())
  352. {
  353. cmGeneratedFileStream osf(sla_r.c_str());
  354. osf << "#include <CoreServices/CoreServices.r>\n\n";
  355. osf << SLAHeader;
  356. osf << "\n";
  357. osf << "data 'TEXT' (5002, \"English\") {\n";
  358. while(ifs.good())
  359. {
  360. std::string line;
  361. std::getline(ifs, line);
  362. // escape quotes
  363. std::string::size_type pos = line.find('\"');
  364. while(pos != std::string::npos)
  365. {
  366. line.replace(pos, 1, "\\\"");
  367. pos = line.find('\"', pos+2);
  368. }
  369. osf << " \"" << line << "\\n\"\n";
  370. }
  371. osf << "};\n";
  372. osf << "\n";
  373. osf << SLASTREnglish;
  374. ifs.close();
  375. osf.close();
  376. }
  377. // convert to UDCO
  378. std::string temp_udco = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  379. temp_udco += "/temp-udco.dmg";
  380. cmOStringStream udco_image_command;
  381. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  382. udco_image_command << " convert \"" << temp_image << "\"";
  383. udco_image_command << " -format UDCO";
  384. udco_image_command << " -o \"" << temp_udco << "\"";
  385. std::string error;
  386. if(!this->RunCommand(udco_image_command, &error))
  387. {
  388. cmCPackLogger(cmCPackLog::LOG_ERROR,
  389. "Error converting to UDCO dmg for adding SLA." << std::endl
  390. << error
  391. << std::endl);
  392. return 0;
  393. }
  394. // unflatten dmg
  395. cmOStringStream unflatten_command;
  396. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  397. unflatten_command << " unflatten ";
  398. unflatten_command << "\"" << temp_udco << "\"";
  399. if(!this->RunCommand(unflatten_command, &error))
  400. {
  401. cmCPackLogger(cmCPackLog::LOG_ERROR,
  402. "Error unflattening dmg for adding SLA." << std::endl
  403. << error
  404. << std::endl);
  405. return 0;
  406. }
  407. // Rez the SLA
  408. cmOStringStream embed_sla_command;
  409. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  410. embed_sla_command << " \"" << sla_r << "\"";
  411. embed_sla_command << " -a -o ";
  412. embed_sla_command << "\"" << temp_udco << "\"";
  413. if(!this->RunCommand(embed_sla_command, &error))
  414. {
  415. cmCPackLogger(cmCPackLog::LOG_ERROR,
  416. "Error adding SLA." << std::endl
  417. << error
  418. << std::endl);
  419. return 0;
  420. }
  421. // flatten dmg
  422. cmOStringStream flatten_command;
  423. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  424. flatten_command << " flatten ";
  425. flatten_command << "\"" << temp_udco << "\"";
  426. if(!this->RunCommand(flatten_command, &error))
  427. {
  428. cmCPackLogger(cmCPackLog::LOG_ERROR,
  429. "Error flattening dmg for adding SLA." << std::endl
  430. << error
  431. << std::endl);
  432. return 0;
  433. }
  434. temp_image = temp_udco;
  435. }
  436. // Create the final compressed read-only disk image ...
  437. cmOStringStream final_image_command;
  438. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  439. final_image_command << " convert \"" << temp_image << "\"";
  440. final_image_command << " -format ";
  441. final_image_command << cpack_dmg_format;
  442. final_image_command << " -imagekey";
  443. final_image_command << " zlib-level=9";
  444. final_image_command << " -o \"" << output_file << "\"";
  445. if(!this->RunCommand(final_image_command))
  446. {
  447. cmCPackLogger(cmCPackLog::LOG_ERROR,
  448. "Error compressing disk image."
  449. << std::endl);
  450. return 0;
  451. }
  452. return 1;
  453. }
  454. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  455. {
  456. return true;
  457. }
  458. std::string
  459. cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  460. const std::string& componentName)
  461. {
  462. // we want to group components together that go in the same dmg package
  463. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  464. // we have 3 mutually exclusive modes to work in
  465. // 1. all components in one package
  466. // 2. each group goes in its own package with left over
  467. // components in their own package
  468. // 3. ignore groups - if grouping is defined, it is ignored
  469. // and each component goes in its own package
  470. if(this->componentPackageMethod == ONE_PACKAGE)
  471. {
  472. return "ALL_IN_ONE";
  473. }
  474. if(this->componentPackageMethod == ONE_PACKAGE_PER_GROUP)
  475. {
  476. // We have to find the name of the COMPONENT GROUP
  477. // the current COMPONENT belongs to.
  478. std::string groupVar = "CPACK_COMPONENT_" +
  479. cmSystemTools::UpperCase(componentName) + "_GROUP";
  480. const char* _groupName = GetOption(groupVar.c_str());
  481. if (_groupName)
  482. {
  483. std::string groupName = _groupName;
  484. groupName = GetComponentPackageFileName(package_file_name,
  485. groupName, true);
  486. return groupName;
  487. }
  488. }
  489. return GetComponentPackageFileName(package_file_name, componentName, false);
  490. }