cmCPackDragNDropGenerator.cxx 18 KB

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