cmCPackDragNDropGenerator.cxx 18 KB

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