cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. #include <iomanip>
  17. #include <CoreFoundation/CoreFoundation.h>
  18. #ifdef HAVE_CoreServices
  19. // For the old LocaleStringToLangAndRegionCodes() function, to convert
  20. // to the old Script Manager RegionCode values needed for the 'LPic' data
  21. // structure used for generating multi-lingual SLAs.
  22. #include <CoreServices/CoreServices.h>
  23. #endif
  24. static const char* SLAHeader =
  25. "data 'LPic' (5000) {\n"
  26. " $\"0002 0011 0003 0001 0000 0000 0002 0000\"\n"
  27. " $\"0008 0003 0000 0001 0004 0000 0004 0005\"\n"
  28. " $\"0000 000E 0006 0001 0005 0007 0000 0007\"\n"
  29. " $\"0008 0000 0047 0009 0000 0034 000A 0001\"\n"
  30. " $\"0035 000B 0001 0020 000C 0000 0011 000D\"\n"
  31. " $\"0000 005B 0004 0000 0033 000F 0001 000C\"\n"
  32. " $\"0010 0000 000B 000E 0000\"\n"
  33. "};\n"
  34. "\n";
  35. static const char* SLASTREnglish =
  36. "resource 'STR#' (5002, \"English\") {\n"
  37. " {\n"
  38. " \"English\",\n"
  39. " \"Agree\",\n"
  40. " \"Disagree\",\n"
  41. " \"Print\",\n"
  42. " \"Save...\",\n"
  43. " \"You agree to the License Agreement terms when you click \"\n"
  44. " \"the \\\"Agree\\\" button.\",\n"
  45. " \"Software License Agreement\",\n"
  46. " \"This text cannot be saved. This disk may be full or locked, "
  47. "or the \"\n"
  48. " \"file may be locked.\",\n"
  49. " \"Unable to print. Make sure you have selected a printer.\"\n"
  50. " }\n"
  51. "};\n"
  52. "\n";
  53. //----------------------------------------------------------------------
  54. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  55. : singleLicense(false)
  56. {
  57. // default to one package file for components
  58. this->componentPackageMethod = ONE_PACKAGE;
  59. }
  60. //----------------------------------------------------------------------
  61. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  62. {
  63. }
  64. //----------------------------------------------------------------------
  65. int cmCPackDragNDropGenerator::InitializeInternal()
  66. {
  67. // Starting with Xcode 4.3, look in "/Applications/Xcode.app" first:
  68. //
  69. std::vector<std::string> paths;
  70. paths.push_back("/Applications/Xcode.app/Contents/Developer/Tools");
  71. paths.push_back("/Developer/Tools");
  72. const std::string hdiutil_path = cmSystemTools::FindProgram("hdiutil",
  73. std::vector<std::string>(), false);
  74. if(hdiutil_path.empty())
  75. {
  76. cmCPackLogger(cmCPackLog::LOG_ERROR,
  77. "Cannot locate hdiutil command"
  78. << std::endl);
  79. return 0;
  80. }
  81. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  82. const std::string setfile_path = cmSystemTools::FindProgram("SetFile",
  83. paths, false);
  84. if(setfile_path.empty())
  85. {
  86. cmCPackLogger(cmCPackLog::LOG_ERROR,
  87. "Cannot locate SetFile command"
  88. << std::endl);
  89. return 0;
  90. }
  91. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  92. const std::string rez_path = cmSystemTools::FindProgram("Rez",
  93. paths, false);
  94. if(rez_path.empty())
  95. {
  96. cmCPackLogger(cmCPackLog::LOG_ERROR,
  97. "Cannot locate Rez command"
  98. << std::endl);
  99. return 0;
  100. }
  101. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  102. if(this->IsSet("CPACK_DMG_SLA_DIR"))
  103. {
  104. slaDirectory = this->GetOption("CPACK_DMG_SLA_DIR");
  105. if(!slaDirectory.empty() && this->IsSet("CPACK_RESOURCE_FILE_LICENSE"))
  106. {
  107. std::string license_file =
  108. this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  109. if(!license_file.empty() &&
  110. (license_file.find("CPack.GenericLicense.txt") == std::string::npos))
  111. {
  112. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  113. "Both CPACK_DMG_SLA_DIR and CPACK_RESOURCE_FILE_LICENSE specified, "
  114. "using CPACK_RESOURCE_FILE_LICENSE as a license for all languages."
  115. << std::endl);
  116. singleLicense = true;
  117. }
  118. }
  119. if(!this->IsSet("CPACK_DMG_SLA_LANGUAGES"))
  120. {
  121. cmCPackLogger(cmCPackLog::LOG_ERROR,
  122. "CPACK_DMG_SLA_DIR set but no languages defined "
  123. "(set CPACK_DMG_SLA_LANGUAGES)"
  124. << std::endl);
  125. return 0;
  126. }
  127. if(!cmSystemTools::FileExists(slaDirectory, false))
  128. {
  129. cmCPackLogger(cmCPackLog::LOG_ERROR,
  130. "CPACK_DMG_SLA_DIR does not exist"
  131. << std::endl);
  132. return 0;
  133. }
  134. std::vector<std::string> languages;
  135. cmSystemTools::ExpandListArgument(
  136. this->GetOption("CPACK_DMG_SLA_LANGUAGES"), languages);
  137. if(languages.empty())
  138. {
  139. cmCPackLogger(cmCPackLog::LOG_ERROR,
  140. "CPACK_DMG_SLA_LANGUAGES set but empty"
  141. << std::endl);
  142. return 0;
  143. }
  144. for(size_t i = 0; i < languages.size(); ++i)
  145. {
  146. std::string license = slaDirectory + "/" + languages[i] + ".license.txt";
  147. if (!singleLicense && !cmSystemTools::FileExists(license))
  148. {
  149. cmCPackLogger(cmCPackLog::LOG_ERROR,
  150. "Missing license file " << languages[i] << ".license.txt"
  151. << std::endl);
  152. return 0;
  153. }
  154. std::string menu = slaDirectory + "/" + languages[i] + ".menu.txt";
  155. if (!cmSystemTools::FileExists(menu))
  156. {
  157. cmCPackLogger(cmCPackLog::LOG_ERROR,
  158. "Missing menu file " << languages[i] << ".menu.txt"
  159. << std::endl);
  160. return 0;
  161. }
  162. }
  163. }
  164. return this->Superclass::InitializeInternal();
  165. }
  166. //----------------------------------------------------------------------
  167. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  168. {
  169. return ".dmg";
  170. }
  171. //----------------------------------------------------------------------
  172. int cmCPackDragNDropGenerator::PackageFiles()
  173. {
  174. // gather which directories to make dmg files for
  175. // multiple directories occur if packaging components or groups separately
  176. // monolith
  177. if(this->Components.empty())
  178. {
  179. return this->CreateDMG(toplevel, packageFileNames[0]);
  180. }
  181. // component install
  182. std::vector<std::string> package_files;
  183. std::map<std::string, cmCPackComponent>::iterator compIt;
  184. for (compIt=this->Components.begin();
  185. compIt!=this->Components.end(); ++compIt )
  186. {
  187. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  188. package_files.push_back(name);
  189. }
  190. std::sort(package_files.begin(), package_files.end());
  191. package_files.erase(std::unique(package_files.begin(),
  192. package_files.end()),
  193. package_files.end());
  194. // loop to create dmg files
  195. packageFileNames.clear();
  196. for(size_t i=0; i<package_files.size(); i++)
  197. {
  198. std::string full_package_name = std::string(toplevel) + std::string("/");
  199. if(package_files[i] == "ALL_IN_ONE")
  200. {
  201. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  202. }
  203. else
  204. {
  205. full_package_name += package_files[i];
  206. }
  207. full_package_name += std::string(GetOutputExtension());
  208. packageFileNames.push_back(full_package_name);
  209. std::string src_dir = toplevel;
  210. src_dir += "/";
  211. src_dir += package_files[i];
  212. if(0 == this->CreateDMG(src_dir, full_package_name))
  213. {
  214. return 0;
  215. }
  216. }
  217. return 1;
  218. }
  219. //----------------------------------------------------------------------
  220. bool cmCPackDragNDropGenerator::CopyFile(std::ostringstream& source,
  221. std::ostringstream& target)
  222. {
  223. if(!cmSystemTools::CopyFileIfDifferent(
  224. source.str().c_str(),
  225. target.str().c_str()))
  226. {
  227. cmCPackLogger(cmCPackLog::LOG_ERROR,
  228. "Error copying "
  229. << source.str()
  230. << " to "
  231. << target.str()
  232. << std::endl);
  233. return false;
  234. }
  235. return true;
  236. }
  237. //----------------------------------------------------------------------
  238. bool cmCPackDragNDropGenerator::CreateEmptyFile(std::ostringstream& target,
  239. size_t size)
  240. {
  241. cmsys::ofstream fout(target.str().c_str(),
  242. std::ios::out | std::ios::binary);
  243. if(!fout)
  244. {
  245. return false;
  246. }
  247. else
  248. {
  249. // Seek to desired size - 1 byte
  250. fout.seekp(size - 1, std::ios_base::beg);
  251. char byte = 0;
  252. // Write one byte to ensure file grows
  253. fout.write(&byte, 1);
  254. }
  255. return true;
  256. }
  257. //----------------------------------------------------------------------
  258. bool cmCPackDragNDropGenerator::RunCommand(std::ostringstream& command,
  259. std::string* output)
  260. {
  261. int exit_code = 1;
  262. bool result = cmSystemTools::RunSingleCommand(
  263. command.str().c_str(),
  264. output, output,
  265. &exit_code,
  266. 0,
  267. this->GeneratorVerbose,
  268. 0);
  269. if(!result || exit_code)
  270. {
  271. cmCPackLogger(cmCPackLog::LOG_ERROR,
  272. "Error executing: "
  273. << command.str()
  274. << std::endl);
  275. return false;
  276. }
  277. return true;
  278. }
  279. //----------------------------------------------------------------------
  280. int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir,
  281. const std::string& output_file)
  282. {
  283. // Get optional arguments ...
  284. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  285. ? this->GetOption("CPACK_PACKAGE_ICON") : "";
  286. const std::string cpack_dmg_volume_name =
  287. this->GetOption("CPACK_DMG_VOLUME_NAME")
  288. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  289. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  290. const std::string cpack_dmg_format =
  291. this->GetOption("CPACK_DMG_FORMAT")
  292. ? this->GetOption("CPACK_DMG_FORMAT") : "UDZO";
  293. // Get optional arguments ...
  294. std::string cpack_license_file =
  295. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") ?
  296. this->GetOption("CPACK_RESOURCE_FILE_LICENSE") : "";
  297. const std::string cpack_dmg_background_image =
  298. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  299. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE") : "";
  300. const std::string cpack_dmg_ds_store =
  301. this->GetOption("CPACK_DMG_DS_STORE")
  302. ? this->GetOption("CPACK_DMG_DS_STORE") : "";
  303. const std::string cpack_dmg_languages =
  304. this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  305. ? this->GetOption("CPACK_DMG_SLA_LANGUAGES") : "";
  306. const std::string cpack_dmg_ds_store_setup_script =
  307. this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  308. ? this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT") : "";
  309. // only put license on dmg if is user provided
  310. if(!cpack_license_file.empty() &&
  311. cpack_license_file.find("CPack.GenericLicense.txt") != std::string::npos)
  312. {
  313. cpack_license_file = "";
  314. }
  315. // use sla_dir if both sla_dir and license_file are set
  316. if(!cpack_license_file.empty() &&
  317. !slaDirectory.empty() && !singleLicense)
  318. {
  319. cpack_license_file = "";
  320. }
  321. // The staging directory contains everything that will end-up inside the
  322. // final disk image ...
  323. std::ostringstream staging;
  324. staging << src_dir;
  325. // Add a symlink to /Applications so users can drag-and-drop the bundle
  326. // into it
  327. std::ostringstream application_link;
  328. application_link << staging.str() << "/Applications";
  329. cmSystemTools::CreateSymlink("/Applications",
  330. application_link.str().c_str());
  331. // Optionally add a custom volume icon ...
  332. if(!cpack_package_icon.empty())
  333. {
  334. std::ostringstream package_icon_source;
  335. package_icon_source << cpack_package_icon;
  336. std::ostringstream package_icon_destination;
  337. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  338. if(!this->CopyFile(package_icon_source, package_icon_destination))
  339. {
  340. cmCPackLogger(cmCPackLog::LOG_ERROR,
  341. "Error copying disk volume icon. "
  342. "Check the value of CPACK_PACKAGE_ICON."
  343. << std::endl);
  344. return 0;
  345. }
  346. }
  347. // Optionally add a custom .DS_Store file
  348. // (e.g. for setting background/layout) ...
  349. if(!cpack_dmg_ds_store.empty())
  350. {
  351. std::ostringstream package_settings_source;
  352. package_settings_source << cpack_dmg_ds_store;
  353. std::ostringstream package_settings_destination;
  354. package_settings_destination << staging.str() << "/.DS_Store";
  355. if(!this->CopyFile(package_settings_source, package_settings_destination))
  356. {
  357. cmCPackLogger(cmCPackLog::LOG_ERROR,
  358. "Error copying disk volume settings file. "
  359. "Check the value of CPACK_DMG_DS_STORE."
  360. << std::endl);
  361. return 0;
  362. }
  363. }
  364. // Optionally add a custom background image ...
  365. // Make sure the background file type is the same as the custom image
  366. // and that the file is hidden so it doesn't show up.
  367. if(!cpack_dmg_background_image.empty())
  368. {
  369. const std::string extension =
  370. cmSystemTools::GetFilenameLastExtension(cpack_dmg_background_image);
  371. std::ostringstream package_background_source;
  372. package_background_source << cpack_dmg_background_image;
  373. std::ostringstream package_background_destination;
  374. package_background_destination << staging.str()
  375. << "/.background/background" << extension;
  376. if(!this->CopyFile(package_background_source,
  377. package_background_destination))
  378. {
  379. cmCPackLogger(cmCPackLog::LOG_ERROR,
  380. "Error copying disk volume background image. "
  381. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  382. << std::endl);
  383. return 0;
  384. }
  385. }
  386. bool remount_image = !cpack_package_icon.empty() ||
  387. !cpack_dmg_ds_store_setup_script.empty();
  388. // Create 1 MB dummy padding file in staging area when we need to remount
  389. // image, so we have enough space for storing changes ...
  390. if(remount_image)
  391. {
  392. std::ostringstream dummy_padding;
  393. dummy_padding << staging.str() << "/.dummy-padding-file";
  394. if(!this->CreateEmptyFile(dummy_padding, 1048576))
  395. {
  396. cmCPackLogger(cmCPackLog::LOG_ERROR,
  397. "Error creating dummy padding file."
  398. << std::endl);
  399. return 0;
  400. }
  401. }
  402. // Create a temporary read-write disk image ...
  403. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  404. temp_image += "/temp.dmg";
  405. std::ostringstream temp_image_command;
  406. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  407. temp_image_command << " create";
  408. temp_image_command << " -ov";
  409. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  410. temp_image_command << " -volname \""
  411. << cpack_dmg_volume_name << "\"";
  412. temp_image_command << " -format UDRW";
  413. temp_image_command << " \"" << temp_image << "\"";
  414. if(!this->RunCommand(temp_image_command))
  415. {
  416. cmCPackLogger(cmCPackLog::LOG_ERROR,
  417. "Error generating temporary disk image."
  418. << std::endl);
  419. return 0;
  420. }
  421. if(remount_image)
  422. {
  423. // Store that we have a failure so that we always unmount the image
  424. // before we exit.
  425. bool had_error = false;
  426. std::ostringstream attach_command;
  427. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  428. attach_command << " attach";
  429. attach_command << " \"" << temp_image << "\"";
  430. std::string attach_output;
  431. if(!this->RunCommand(attach_command, &attach_output))
  432. {
  433. cmCPackLogger(cmCPackLog::LOG_ERROR,
  434. "Error attaching temporary disk image."
  435. << std::endl);
  436. return 0;
  437. }
  438. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  439. mountpoint_regex.find(attach_output.c_str());
  440. std::ostringstream temp_mount;
  441. temp_mount << mountpoint_regex.match(1);
  442. // Remove dummy padding file so we have enough space on RW image ...
  443. std::ostringstream dummy_padding;
  444. dummy_padding << temp_mount.str() << "/.dummy-padding-file";
  445. if(!cmSystemTools::RemoveFile(dummy_padding.str()))
  446. {
  447. cmCPackLogger(cmCPackLog::LOG_ERROR,
  448. "Error removing dummy padding file."
  449. << std::endl);
  450. had_error = true;
  451. }
  452. // Optionally set the custom icon flag for the image ...
  453. if(!had_error && !cpack_package_icon.empty())
  454. {
  455. std::ostringstream setfile_command;
  456. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  457. setfile_command << " -a C";
  458. setfile_command << " \"" << temp_mount.str() << "\"";
  459. if(!this->RunCommand(setfile_command))
  460. {
  461. cmCPackLogger(cmCPackLog::LOG_ERROR,
  462. "Error assigning custom icon to temporary disk image."
  463. << std::endl);
  464. had_error = true;
  465. }
  466. }
  467. // Optionally we can execute a custom apple script to generate
  468. // the .DS_Store for the volume folder ...
  469. if(!had_error && !cpack_dmg_ds_store_setup_script.empty())
  470. {
  471. std::ostringstream setup_script_command;
  472. setup_script_command << "osascript"
  473. << " \"" << cpack_dmg_ds_store_setup_script << "\""
  474. << " \"" << cpack_dmg_volume_name << "\"";
  475. std::string error;
  476. if(!this->RunCommand(setup_script_command, &error))
  477. {
  478. cmCPackLogger(cmCPackLog::LOG_ERROR,
  479. "Error executing custom script on disk image." << std::endl
  480. << error
  481. << std::endl);
  482. had_error = true;
  483. }
  484. }
  485. std::ostringstream detach_command;
  486. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  487. detach_command << " detach";
  488. detach_command << " \"" << temp_mount.str() << "\"";
  489. if(!this->RunCommand(detach_command))
  490. {
  491. cmCPackLogger(cmCPackLog::LOG_ERROR,
  492. "Error detaching temporary disk image."
  493. << std::endl);
  494. return 0;
  495. }
  496. if(had_error)
  497. {
  498. return 0;
  499. }
  500. }
  501. if(!cpack_license_file.empty() || !slaDirectory.empty())
  502. {
  503. // Use old hardcoded style if sla_dir is not set
  504. bool oldStyle = slaDirectory.empty();
  505. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  506. sla_r += "/sla.r";
  507. std::vector<std::string> languages;
  508. if(!oldStyle)
  509. {
  510. cmSystemTools::ExpandListArgument(cpack_dmg_languages, languages);
  511. }
  512. cmGeneratedFileStream ofs(sla_r.c_str());
  513. ofs << "#include <CoreServices/CoreServices.r>\n\n";
  514. if(oldStyle)
  515. {
  516. ofs << SLAHeader;
  517. ofs << "\n";
  518. }
  519. else
  520. {
  521. /*
  522. * LPic Layout
  523. * (https://github.com/pypt/dmg-add-license/blob/master/main.c)
  524. * as far as I can tell (no official documentation seems to exist):
  525. * struct LPic {
  526. * uint16_t default_language; // points to a resid, defaulting to 0,
  527. * // which is the first set language
  528. * uint16_t length;
  529. * struct {
  530. * uint16_t language_code;
  531. * uint16_t resid;
  532. * uint16_t encoding; // Encoding from TextCommon.h,
  533. * // forcing MacRoman (0) for now. Might need to
  534. * // allow overwrite per license by user later
  535. * } item[1];
  536. * }
  537. */
  538. // Create vector first for readability, then iterate to write to ofs
  539. std::vector<uint16_t> header_data;
  540. header_data.push_back(0);
  541. header_data.push_back(languages.size());
  542. for(size_t i = 0; i < languages.size(); ++i)
  543. {
  544. CFStringRef language_cfstring = CFStringCreateWithCString(
  545. NULL, languages[i].c_str(), kCFStringEncodingUTF8);
  546. CFStringRef iso_language =
  547. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  548. NULL, language_cfstring);
  549. if (!iso_language)
  550. {
  551. cmCPackLogger(cmCPackLog::LOG_ERROR,
  552. languages[i] << " is not a recognized language"
  553. << std::endl);
  554. }
  555. char *iso_language_cstr = (char *) malloc(65);
  556. CFStringGetCString(iso_language, iso_language_cstr, 64,
  557. kCFStringEncodingMacRoman);
  558. LangCode lang = 0;
  559. RegionCode region = 0;
  560. #ifdef HAVE_CoreServices
  561. OSStatus err = LocaleStringToLangAndRegionCodes(iso_language_cstr,
  562. &lang, &region);
  563. if (err != noErr)
  564. #endif
  565. {
  566. cmCPackLogger(cmCPackLog::LOG_ERROR,
  567. "No language/region code available for " << iso_language_cstr
  568. << std::endl);
  569. free(iso_language_cstr);
  570. return 0;
  571. }
  572. #ifdef HAVE_CoreServices
  573. free(iso_language_cstr);
  574. header_data.push_back(region);
  575. header_data.push_back(i);
  576. header_data.push_back(0);
  577. #endif
  578. }
  579. ofs << "data 'LPic' (5000) {\n";
  580. ofs << std::hex << std::uppercase << std::setfill('0');
  581. for(size_t i = 0; i < header_data.size(); ++i)
  582. {
  583. if(i % 8 == 0)
  584. {
  585. ofs << " $\"";
  586. }
  587. ofs << std::setw(4) << header_data[i];
  588. if(i % 8 == 7 || i == header_data.size() - 1)
  589. {
  590. ofs << "\"\n";
  591. }
  592. else
  593. {
  594. ofs << " ";
  595. }
  596. }
  597. ofs << "};\n\n";
  598. // Reset ofs options
  599. ofs << std::dec << std::nouppercase << std::setfill(' ');
  600. }
  601. bool have_write_license_error = false;
  602. std::string error;
  603. if(oldStyle)
  604. {
  605. if(!this->WriteLicense(ofs, 0, "", cpack_license_file, &error))
  606. {
  607. have_write_license_error = true;
  608. }
  609. }
  610. else
  611. {
  612. for(size_t i = 0; i < languages.size() && !have_write_license_error; ++i)
  613. {
  614. if(singleLicense)
  615. {
  616. if(!this->WriteLicense(ofs, i + 5000, languages[i],
  617. cpack_license_file, &error))
  618. {
  619. have_write_license_error = true;
  620. }
  621. }
  622. else
  623. {
  624. if(!this->WriteLicense(ofs, i + 5000, languages[i], "", &error))
  625. {
  626. have_write_license_error = true;
  627. }
  628. }
  629. }
  630. }
  631. ofs.Close();
  632. if(have_write_license_error)
  633. {
  634. cmCPackLogger(cmCPackLog::LOG_ERROR,
  635. "Error writing license file to SLA." << std::endl
  636. << error
  637. << std::endl);
  638. return 0;
  639. }
  640. // convert to UDCO
  641. std::string temp_udco = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  642. temp_udco += "/temp-udco.dmg";
  643. std::ostringstream udco_image_command;
  644. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  645. udco_image_command << " convert \"" << temp_image << "\"";
  646. udco_image_command << " -format UDCO";
  647. udco_image_command << " -ov -o \"" << temp_udco << "\"";
  648. if(!this->RunCommand(udco_image_command, &error))
  649. {
  650. cmCPackLogger(cmCPackLog::LOG_ERROR,
  651. "Error converting to UDCO dmg for adding SLA." << std::endl
  652. << error
  653. << std::endl);
  654. return 0;
  655. }
  656. // unflatten dmg
  657. std::ostringstream unflatten_command;
  658. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  659. unflatten_command << " unflatten ";
  660. unflatten_command << "\"" << temp_udco << "\"";
  661. if(!this->RunCommand(unflatten_command, &error))
  662. {
  663. cmCPackLogger(cmCPackLog::LOG_ERROR,
  664. "Error unflattening dmg for adding SLA." << std::endl
  665. << error
  666. << std::endl);
  667. return 0;
  668. }
  669. // Rez the SLA
  670. std::ostringstream embed_sla_command;
  671. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  672. const char* sysroot = this->GetOption("CPACK_OSX_SYSROOT");
  673. if(sysroot && sysroot[0] != '\0')
  674. {
  675. embed_sla_command << " -isysroot \"" << sysroot << "\"";
  676. }
  677. embed_sla_command << " \"" << sla_r << "\"";
  678. embed_sla_command << " -a -o ";
  679. embed_sla_command << "\"" << temp_udco << "\"";
  680. if(!this->RunCommand(embed_sla_command, &error))
  681. {
  682. cmCPackLogger(cmCPackLog::LOG_ERROR,
  683. "Error adding SLA." << std::endl
  684. << error
  685. << std::endl);
  686. return 0;
  687. }
  688. // flatten dmg
  689. std::ostringstream flatten_command;
  690. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  691. flatten_command << " flatten ";
  692. flatten_command << "\"" << temp_udco << "\"";
  693. if(!this->RunCommand(flatten_command, &error))
  694. {
  695. cmCPackLogger(cmCPackLog::LOG_ERROR,
  696. "Error flattening dmg for adding SLA." << std::endl
  697. << error
  698. << std::endl);
  699. return 0;
  700. }
  701. temp_image = temp_udco;
  702. }
  703. // Create the final compressed read-only disk image ...
  704. std::ostringstream final_image_command;
  705. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  706. final_image_command << " convert \"" << temp_image << "\"";
  707. final_image_command << " -format ";
  708. final_image_command << cpack_dmg_format;
  709. final_image_command << " -imagekey";
  710. final_image_command << " zlib-level=9";
  711. final_image_command << " -o \"" << output_file << "\"";
  712. if(!this->RunCommand(final_image_command))
  713. {
  714. cmCPackLogger(cmCPackLog::LOG_ERROR,
  715. "Error compressing disk image."
  716. << std::endl);
  717. return 0;
  718. }
  719. return 1;
  720. }
  721. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  722. {
  723. return true;
  724. }
  725. std::string
  726. cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  727. const std::string& componentName)
  728. {
  729. // we want to group components together that go in the same dmg package
  730. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  731. // we have 3 mutually exclusive modes to work in
  732. // 1. all components in one package
  733. // 2. each group goes in its own package with left over
  734. // components in their own package
  735. // 3. ignore groups - if grouping is defined, it is ignored
  736. // and each component goes in its own package
  737. if(this->componentPackageMethod == ONE_PACKAGE)
  738. {
  739. return "ALL_IN_ONE";
  740. }
  741. if(this->componentPackageMethod == ONE_PACKAGE_PER_GROUP)
  742. {
  743. // We have to find the name of the COMPONENT GROUP
  744. // the current COMPONENT belongs to.
  745. std::string groupVar = "CPACK_COMPONENT_" +
  746. cmSystemTools::UpperCase(componentName) + "_GROUP";
  747. const char* _groupName = GetOption(groupVar.c_str());
  748. if (_groupName)
  749. {
  750. std::string groupName = _groupName;
  751. groupName = GetComponentPackageFileName(package_file_name,
  752. groupName, true);
  753. return groupName;
  754. }
  755. }
  756. return GetComponentPackageFileName(package_file_name, componentName, false);
  757. }
  758. bool
  759. cmCPackDragNDropGenerator::WriteLicense(cmGeneratedFileStream& outputStream,
  760. int licenseNumber, std::string licenseLanguage, std::string licenseFile,
  761. std::string *error)
  762. {
  763. if(!licenseFile.empty() && !singleLicense)
  764. {
  765. licenseNumber = 5002;
  766. licenseLanguage = "English";
  767. }
  768. // License header
  769. outputStream << "data 'TEXT' (" << licenseNumber << ", \""
  770. << licenseLanguage << "\") {\n";
  771. // License body
  772. std::string actual_license = !licenseFile.empty() ? licenseFile :
  773. (slaDirectory + "/" + licenseLanguage + ".license.txt");
  774. cmsys::ifstream license_ifs;
  775. license_ifs.open(actual_license.c_str());
  776. if(license_ifs.is_open())
  777. {
  778. while(license_ifs.good())
  779. {
  780. std::string line;
  781. std::getline(license_ifs, line);
  782. if(!line.empty())
  783. {
  784. EscapeQuotesAndBackslashes(line);
  785. std::vector<std::string> lines;
  786. if(!this->BreakLongLine(line, lines, error))
  787. {
  788. return false;
  789. }
  790. for(size_t i = 0; i < lines.size(); ++i)
  791. {
  792. outputStream << " \"" << lines[i] << "\"\n";
  793. }
  794. }
  795. outputStream << " \"\\n\"\n";
  796. }
  797. license_ifs.close();
  798. }
  799. // End of License
  800. outputStream << "};\n\n";
  801. if(!licenseFile.empty() && !singleLicense)
  802. {
  803. outputStream << SLASTREnglish;
  804. }
  805. else
  806. {
  807. // Menu header
  808. outputStream << "resource 'STR#' (" << licenseNumber << ", \""
  809. << licenseLanguage << "\") {\n";
  810. outputStream << " {\n";
  811. // Menu body
  812. cmsys::ifstream menu_ifs;
  813. menu_ifs.open((slaDirectory+"/"+licenseLanguage+".menu.txt").c_str());
  814. if(menu_ifs.is_open())
  815. {
  816. size_t lines_written = 0;
  817. while(menu_ifs.good())
  818. {
  819. // Lines written from original file, not from broken up lines
  820. std::string line;
  821. std::getline(menu_ifs, line);
  822. if(!line.empty())
  823. {
  824. EscapeQuotesAndBackslashes(line);
  825. std::vector<std::string> lines;
  826. if(!this->BreakLongLine(line, lines, error))
  827. {
  828. return false;
  829. }
  830. for(size_t i = 0; i < lines.size(); ++i)
  831. {
  832. std::string comma;
  833. // We need a comma after every complete string,
  834. // but not on the very last line
  835. if(lines_written != 8 && i == lines.size() - 1)
  836. {
  837. comma = ",";
  838. }
  839. else
  840. {
  841. comma = "";
  842. }
  843. outputStream << " \"" << lines[i] << "\"" << comma << "\n";
  844. }
  845. ++lines_written;
  846. }
  847. }
  848. menu_ifs.close();
  849. }
  850. //End of menu
  851. outputStream << " }\n";
  852. outputStream << "};\n";
  853. outputStream << "\n";
  854. }
  855. return true;
  856. }
  857. bool
  858. cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  859. std::vector<std::string>& lines, std::string *error)
  860. {
  861. const size_t max_line_length = 512;
  862. for(size_t i = 0; i < line.size(); i += max_line_length)
  863. {
  864. size_t line_length = max_line_length;
  865. if(i + line_length > line.size())
  866. {
  867. line_length = line.size() - i;
  868. }
  869. else while(line_length > 0 && line[i + line_length - 1] != ' ')
  870. {
  871. line_length = line_length - 1;
  872. }
  873. if(line_length == 0)
  874. {
  875. *error = "Please make sure there are no words "
  876. "(or character sequences not broken up by spaces or newlines) "
  877. "in your license file which are more than 512 characters long.";
  878. return false;
  879. }
  880. lines.push_back(line.substr(i, line_length));
  881. }
  882. return true;
  883. }
  884. void
  885. cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  886. {
  887. std::string::size_type backslash_pos = line.find('\\');
  888. while(backslash_pos != std::string::npos)
  889. {
  890. line.replace(backslash_pos, 1, "\\\\");
  891. backslash_pos = line.find('\\', backslash_pos + 2);
  892. }
  893. std::string::size_type quote_pos = line.find('\"');
  894. while(quote_pos != std::string::npos)
  895. {
  896. line.replace(quote_pos, 1, "\\\"");
  897. quote_pos = line.find('\"', quote_pos + 2);
  898. }
  899. }