cmCPackDragNDropGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackDragNDropGenerator.h"
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <map>
  8. #include <CoreFoundation/CoreFoundation.h>
  9. #include <cm3p/kwiml/abi.h>
  10. #include "cmsys/Base64.h"
  11. #include "cmsys/FStream.hxx"
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmCPackGenerator.h"
  14. #include "cmCPackLog.h"
  15. #include "cmDuration.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. #include "cmXMLWriter.h"
  20. #ifdef HAVE_CoreServices
  21. // For the old LocaleStringToLangAndRegionCodes() function, to convert
  22. // to the old Script Manager RegionCode values needed for the 'LPic' data
  23. // structure used for generating multi-lingual SLAs.
  24. # include <CoreServices/CoreServices.h>
  25. #endif
  26. static const uint16_t DefaultLpic[] = {
  27. /* clang-format off */
  28. 0x0002, 0x0011, 0x0003, 0x0001, 0x0000, 0x0000, 0x0002, 0x0000,
  29. 0x0008, 0x0003, 0x0000, 0x0001, 0x0004, 0x0000, 0x0004, 0x0005,
  30. 0x0000, 0x000E, 0x0006, 0x0001, 0x0005, 0x0007, 0x0000, 0x0007,
  31. 0x0008, 0x0000, 0x0047, 0x0009, 0x0000, 0x0034, 0x000A, 0x0001,
  32. 0x0035, 0x000B, 0x0001, 0x0020, 0x000C, 0x0000, 0x0011, 0x000D,
  33. 0x0000, 0x005B, 0x0004, 0x0000, 0x0033, 0x000F, 0x0001, 0x000C,
  34. 0x0010, 0x0000, 0x000B, 0x000E, 0x0000
  35. /* clang-format on */
  36. };
  37. static const std::vector<std::string> DefaultMenu = {
  38. { "English", "Agree", "Disagree", "Print", "Save...",
  39. // NOLINTNEXTLINE(bugprone-suspicious-missing-comma)
  40. "You agree to the License Agreement terms when "
  41. "you click the \"Agree\" button.",
  42. "Software License Agreement",
  43. "This text cannot be saved. "
  44. "This disk may be full or locked, or the file may be locked.",
  45. "Unable to print. Make sure you have selected a printer." }
  46. };
  47. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  48. : singleLicense(false)
  49. {
  50. // default to one package file for components
  51. this->componentPackageMethod = ONE_PACKAGE;
  52. }
  53. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator() = default;
  54. int cmCPackDragNDropGenerator::InitializeInternal()
  55. {
  56. // Starting with Xcode 4.3, look in "/Applications/Xcode.app" first:
  57. //
  58. std::vector<std::string> paths;
  59. paths.emplace_back("/Applications/Xcode.app/Contents/Developer/Tools");
  60. paths.emplace_back("/Developer/Tools");
  61. const std::string hdiutil_path =
  62. cmSystemTools::FindProgram("hdiutil", std::vector<std::string>(), false);
  63. if (hdiutil_path.empty()) {
  64. cmCPackLogger(cmCPackLog::LOG_ERROR,
  65. "Cannot locate hdiutil command" << std::endl);
  66. return 0;
  67. }
  68. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  69. const std::string setfile_path =
  70. cmSystemTools::FindProgram("SetFile", paths, false);
  71. if (setfile_path.empty()) {
  72. cmCPackLogger(cmCPackLog::LOG_ERROR,
  73. "Cannot locate SetFile command" << 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", paths, false);
  78. if (rez_path.empty()) {
  79. cmCPackLogger(cmCPackLog::LOG_ERROR,
  80. "Cannot locate Rez command" << std::endl);
  81. return 0;
  82. }
  83. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  84. if (this->IsSet("CPACK_DMG_SLA_DIR")) {
  85. slaDirectory = this->GetOption("CPACK_DMG_SLA_DIR");
  86. if (!slaDirectory.empty() && this->IsSet("CPACK_RESOURCE_FILE_LICENSE")) {
  87. std::string license_file =
  88. this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  89. if (!license_file.empty() &&
  90. (license_file.find("CPack.GenericLicense.txt") ==
  91. std::string::npos)) {
  92. cmCPackLogger(
  93. cmCPackLog::LOG_OUTPUT,
  94. "Both CPACK_DMG_SLA_DIR and CPACK_RESOURCE_FILE_LICENSE specified, "
  95. "using CPACK_RESOURCE_FILE_LICENSE as a license for all languages."
  96. << std::endl);
  97. singleLicense = true;
  98. }
  99. }
  100. if (!this->IsSet("CPACK_DMG_SLA_LANGUAGES")) {
  101. cmCPackLogger(cmCPackLog::LOG_ERROR,
  102. "CPACK_DMG_SLA_DIR set but no languages defined "
  103. "(set CPACK_DMG_SLA_LANGUAGES)"
  104. << std::endl);
  105. return 0;
  106. }
  107. if (!cmSystemTools::FileExists(slaDirectory, false)) {
  108. cmCPackLogger(cmCPackLog::LOG_ERROR,
  109. "CPACK_DMG_SLA_DIR does not exist" << std::endl);
  110. return 0;
  111. }
  112. std::vector<std::string> languages =
  113. cmExpandedList(this->GetOption("CPACK_DMG_SLA_LANGUAGES"));
  114. if (languages.empty()) {
  115. cmCPackLogger(cmCPackLog::LOG_ERROR,
  116. "CPACK_DMG_SLA_LANGUAGES set but empty" << std::endl);
  117. return 0;
  118. }
  119. for (auto const& language : languages) {
  120. std::string license = slaDirectory + "/" + language + ".license.txt";
  121. std::string license_rtf = slaDirectory + "/" + language + ".license.rtf";
  122. if (!singleLicense) {
  123. if (!cmSystemTools::FileExists(license) &&
  124. !cmSystemTools::FileExists(license_rtf)) {
  125. cmCPackLogger(cmCPackLog::LOG_ERROR,
  126. "Missing license file "
  127. << language << ".license.txt"
  128. << " / " << language << ".license.rtf" << std::endl);
  129. return 0;
  130. }
  131. }
  132. std::string menu = slaDirectory + "/" + language + ".menu.txt";
  133. if (!cmSystemTools::FileExists(menu)) {
  134. cmCPackLogger(cmCPackLog::LOG_ERROR,
  135. "Missing menu file " << language << ".menu.txt"
  136. << std::endl);
  137. return 0;
  138. }
  139. }
  140. }
  141. return this->Superclass::InitializeInternal();
  142. }
  143. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  144. {
  145. return ".dmg";
  146. }
  147. int cmCPackDragNDropGenerator::PackageFiles()
  148. {
  149. // gather which directories to make dmg files for
  150. // multiple directories occur if packaging components or groups separately
  151. // monolith
  152. if (this->Components.empty()) {
  153. return this->CreateDMG(toplevel, packageFileNames[0]);
  154. }
  155. // component install
  156. std::vector<std::string> package_files;
  157. std::map<std::string, cmCPackComponent>::iterator compIt;
  158. for (compIt = this->Components.begin(); compIt != this->Components.end();
  159. ++compIt) {
  160. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  161. package_files.push_back(name);
  162. }
  163. std::sort(package_files.begin(), package_files.end());
  164. package_files.erase(std::unique(package_files.begin(), package_files.end()),
  165. package_files.end());
  166. // loop to create dmg files
  167. packageFileNames.clear();
  168. for (auto const& package_file : package_files) {
  169. std::string full_package_name = std::string(toplevel) + std::string("/");
  170. if (package_file == "ALL_IN_ONE") {
  171. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  172. } else {
  173. full_package_name += package_file;
  174. }
  175. full_package_name += std::string(GetOutputExtension());
  176. packageFileNames.push_back(full_package_name);
  177. std::string src_dir = cmStrCat(toplevel, '/', package_file);
  178. if (0 == this->CreateDMG(src_dir, full_package_name)) {
  179. return 0;
  180. }
  181. }
  182. return 1;
  183. }
  184. bool cmCPackDragNDropGenerator::CopyFile(std::ostringstream& source,
  185. std::ostringstream& target)
  186. {
  187. if (!cmSystemTools::CopyFileIfDifferent(source.str(), target.str())) {
  188. cmCPackLogger(cmCPackLog::LOG_ERROR,
  189. "Error copying " << source.str() << " to " << target.str()
  190. << std::endl);
  191. return false;
  192. }
  193. return true;
  194. }
  195. bool cmCPackDragNDropGenerator::CreateEmptyFile(std::ostringstream& target,
  196. size_t size)
  197. {
  198. cmsys::ofstream fout(target.str().c_str(), std::ios::out | std::ios::binary);
  199. if (!fout) {
  200. return false;
  201. }
  202. // Seek to desired size - 1 byte
  203. fout.seekp(size - 1, std::ios::beg);
  204. char byte = 0;
  205. // Write one byte to ensure file grows
  206. fout.write(&byte, 1);
  207. return true;
  208. }
  209. bool cmCPackDragNDropGenerator::RunCommand(std::ostringstream& command,
  210. std::string* output)
  211. {
  212. int exit_code = 1;
  213. bool result = cmSystemTools::RunSingleCommand(
  214. command.str(), output, output, &exit_code, nullptr, this->GeneratorVerbose,
  215. cmDuration::zero());
  216. if (!result || exit_code) {
  217. cmCPackLogger(cmCPackLog::LOG_ERROR,
  218. "Error executing: " << command.str() << std::endl);
  219. return false;
  220. }
  221. return true;
  222. }
  223. int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir,
  224. const std::string& output_file)
  225. {
  226. // Get optional arguments ...
  227. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  228. ? this->GetOption("CPACK_PACKAGE_ICON")
  229. : "";
  230. const std::string cpack_dmg_volume_name =
  231. this->GetOption("CPACK_DMG_VOLUME_NAME")
  232. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  233. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  234. const std::string cpack_dmg_format = this->GetOption("CPACK_DMG_FORMAT")
  235. ? this->GetOption("CPACK_DMG_FORMAT")
  236. : "UDZO";
  237. // Get optional arguments ...
  238. std::string cpack_license_file =
  239. this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  240. ? this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  241. : "";
  242. const std::string cpack_dmg_background_image =
  243. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  244. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  245. : "";
  246. const std::string cpack_dmg_ds_store = this->GetOption("CPACK_DMG_DS_STORE")
  247. ? this->GetOption("CPACK_DMG_DS_STORE")
  248. : "";
  249. const std::string cpack_dmg_languages =
  250. this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  251. ? this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  252. : "";
  253. const std::string cpack_dmg_ds_store_setup_script =
  254. this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  255. ? this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  256. : "";
  257. const bool cpack_dmg_disable_applications_symlink =
  258. this->IsOn("CPACK_DMG_DISABLE_APPLICATIONS_SYMLINK");
  259. // only put license on dmg if is user provided
  260. if (!cpack_license_file.empty() &&
  261. cpack_license_file.find("CPack.GenericLicense.txt") !=
  262. std::string::npos) {
  263. cpack_license_file = "";
  264. }
  265. // use sla_dir if both sla_dir and license_file are set
  266. if (!cpack_license_file.empty() && !slaDirectory.empty() && !singleLicense) {
  267. cpack_license_file = "";
  268. }
  269. // The staging directory contains everything that will end-up inside the
  270. // final disk image ...
  271. std::ostringstream staging;
  272. staging << src_dir;
  273. // Add a symlink to /Applications so users can drag-and-drop the bundle
  274. // into it unless this behaviour was disabled
  275. if (!cpack_dmg_disable_applications_symlink) {
  276. std::ostringstream application_link;
  277. application_link << staging.str() << "/Applications";
  278. cmSystemTools::CreateSymlink("/Applications", application_link.str());
  279. }
  280. // Optionally add a custom volume icon ...
  281. if (!cpack_package_icon.empty()) {
  282. std::ostringstream package_icon_source;
  283. package_icon_source << cpack_package_icon;
  284. std::ostringstream package_icon_destination;
  285. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  286. if (!this->CopyFile(package_icon_source, package_icon_destination)) {
  287. cmCPackLogger(cmCPackLog::LOG_ERROR,
  288. "Error copying disk volume icon. "
  289. "Check the value of CPACK_PACKAGE_ICON."
  290. << std::endl);
  291. return 0;
  292. }
  293. }
  294. // Optionally add a custom .DS_Store file
  295. // (e.g. for setting background/layout) ...
  296. if (!cpack_dmg_ds_store.empty()) {
  297. std::ostringstream package_settings_source;
  298. package_settings_source << cpack_dmg_ds_store;
  299. std::ostringstream package_settings_destination;
  300. package_settings_destination << staging.str() << "/.DS_Store";
  301. if (!this->CopyFile(package_settings_source,
  302. package_settings_destination)) {
  303. cmCPackLogger(cmCPackLog::LOG_ERROR,
  304. "Error copying disk volume settings file. "
  305. "Check the value of CPACK_DMG_DS_STORE."
  306. << std::endl);
  307. return 0;
  308. }
  309. }
  310. // Optionally add a custom background image ...
  311. // Make sure the background file type is the same as the custom image
  312. // and that the file is hidden so it doesn't show up.
  313. if (!cpack_dmg_background_image.empty()) {
  314. const std::string extension =
  315. cmSystemTools::GetFilenameLastExtension(cpack_dmg_background_image);
  316. std::ostringstream package_background_source;
  317. package_background_source << cpack_dmg_background_image;
  318. std::ostringstream package_background_destination;
  319. package_background_destination << staging.str()
  320. << "/.background/background" << extension;
  321. if (!this->CopyFile(package_background_source,
  322. package_background_destination)) {
  323. cmCPackLogger(cmCPackLog::LOG_ERROR,
  324. "Error copying disk volume background image. "
  325. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  326. << std::endl);
  327. return 0;
  328. }
  329. }
  330. bool remount_image =
  331. !cpack_package_icon.empty() || !cpack_dmg_ds_store_setup_script.empty();
  332. std::string temp_image_format = "UDZO";
  333. // Create 1 MB dummy padding file in staging area when we need to remount
  334. // image, so we have enough space for storing changes ...
  335. if (remount_image) {
  336. std::ostringstream dummy_padding;
  337. dummy_padding << staging.str() << "/.dummy-padding-file";
  338. if (!this->CreateEmptyFile(dummy_padding, 1048576)) {
  339. cmCPackLogger(cmCPackLog::LOG_ERROR,
  340. "Error creating dummy padding file." << std::endl);
  341. return 0;
  342. }
  343. temp_image_format = "UDRW";
  344. }
  345. // Create a temporary read-write disk image ...
  346. std::string temp_image =
  347. cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/temp.dmg");
  348. std::string create_error;
  349. std::ostringstream temp_image_command;
  350. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  351. temp_image_command << " create";
  352. temp_image_command << " -ov";
  353. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  354. temp_image_command << " -volname \"" << cpack_dmg_volume_name << "\"";
  355. temp_image_command << " -fs HFS+";
  356. temp_image_command << " -format " << temp_image_format;
  357. temp_image_command << " \"" << temp_image << "\"";
  358. if (!this->RunCommand(temp_image_command, &create_error)) {
  359. cmCPackLogger(cmCPackLog::LOG_ERROR,
  360. "Error generating temporary disk image." << std::endl
  361. << create_error
  362. << std::endl);
  363. return 0;
  364. }
  365. if (remount_image) {
  366. // Store that we have a failure so that we always unmount the image
  367. // before we exit.
  368. bool had_error = false;
  369. std::ostringstream attach_command;
  370. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  371. attach_command << " attach";
  372. attach_command << " \"" << temp_image << "\"";
  373. std::string attach_output;
  374. if (!this->RunCommand(attach_command, &attach_output)) {
  375. cmCPackLogger(cmCPackLog::LOG_ERROR,
  376. "Error attaching temporary disk image." << std::endl);
  377. return 0;
  378. }
  379. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  380. mountpoint_regex.find(attach_output.c_str());
  381. std::string const temp_mount = mountpoint_regex.match(1);
  382. std::string const temp_mount_name =
  383. temp_mount.substr(sizeof("/Volumes/") - 1);
  384. // Remove dummy padding file so we have enough space on RW image ...
  385. std::ostringstream dummy_padding;
  386. dummy_padding << temp_mount << "/.dummy-padding-file";
  387. if (!cmSystemTools::RemoveFile(dummy_padding.str())) {
  388. cmCPackLogger(cmCPackLog::LOG_ERROR,
  389. "Error removing dummy padding file." << std::endl);
  390. had_error = true;
  391. }
  392. // Optionally set the custom icon flag for the image ...
  393. if (!had_error && !cpack_package_icon.empty()) {
  394. std::string error;
  395. std::ostringstream setfile_command;
  396. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  397. setfile_command << " -a C";
  398. setfile_command << " \"" << temp_mount << "\"";
  399. if (!this->RunCommand(setfile_command, &error)) {
  400. cmCPackLogger(cmCPackLog::LOG_ERROR,
  401. "Error assigning custom icon to temporary disk image."
  402. << std::endl
  403. << error << std::endl);
  404. had_error = true;
  405. }
  406. }
  407. // Optionally we can execute a custom apple script to generate
  408. // the .DS_Store for the volume folder ...
  409. if (!had_error && !cpack_dmg_ds_store_setup_script.empty()) {
  410. std::ostringstream setup_script_command;
  411. setup_script_command << "osascript"
  412. << " \"" << cpack_dmg_ds_store_setup_script << "\""
  413. << " \"" << temp_mount_name << "\"";
  414. std::string error;
  415. if (!this->RunCommand(setup_script_command, &error)) {
  416. cmCPackLogger(cmCPackLog::LOG_ERROR,
  417. "Error executing custom script on disk image."
  418. << std::endl
  419. << error << std::endl);
  420. had_error = true;
  421. }
  422. }
  423. std::ostringstream detach_command;
  424. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  425. detach_command << " detach";
  426. detach_command << " \"" << temp_mount << "\"";
  427. if (!this->RunCommand(detach_command)) {
  428. cmCPackLogger(cmCPackLog::LOG_ERROR,
  429. "Error detaching temporary disk image." << std::endl);
  430. return 0;
  431. }
  432. if (had_error) {
  433. return 0;
  434. }
  435. }
  436. // Create the final compressed read-only disk image ...
  437. std::ostringstream 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. std::string convert_error;
  446. if (!this->RunCommand(final_image_command, &convert_error)) {
  447. cmCPackLogger(cmCPackLog::LOG_ERROR,
  448. "Error compressing disk image." << std::endl
  449. << convert_error
  450. << std::endl);
  451. return 0;
  452. }
  453. if (!cpack_license_file.empty() || !slaDirectory.empty()) {
  454. // Use old hardcoded style if sla_dir is not set
  455. bool oldStyle = slaDirectory.empty();
  456. std::string sla_xml =
  457. cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/sla.xml");
  458. std::vector<std::string> languages;
  459. if (!oldStyle) {
  460. cmExpandList(cpack_dmg_languages, languages);
  461. }
  462. std::vector<uint16_t> header_data;
  463. if (oldStyle) {
  464. header_data = std::vector<uint16_t>(
  465. DefaultLpic,
  466. DefaultLpic + (sizeof(DefaultLpic) / sizeof(*DefaultLpic)));
  467. } else {
  468. /*
  469. * LPic Layout
  470. * (https://github.com/pypt/dmg-add-license/blob/master/main.c)
  471. * as far as I can tell (no official documentation seems to exist):
  472. * struct LPic {
  473. * uint16_t default_language; // points to a resid, defaulting to 0,
  474. * // which is the first set language
  475. * uint16_t length;
  476. * struct {
  477. * uint16_t language_code;
  478. * uint16_t resid;
  479. * uint16_t encoding; // Encoding from TextCommon.h,
  480. * // forcing MacRoman (0) for now. Might need to
  481. * // allow overwrite per license by user later
  482. * } item[1];
  483. * }
  484. */
  485. header_data.push_back(0);
  486. header_data.push_back(languages.size());
  487. for (size_t i = 0; i < languages.size(); ++i) {
  488. CFStringRef language_cfstring = CFStringCreateWithCString(
  489. nullptr, languages[i].c_str(), kCFStringEncodingUTF8);
  490. CFStringRef iso_language =
  491. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  492. nullptr, language_cfstring);
  493. if (!iso_language) {
  494. cmCPackLogger(cmCPackLog::LOG_ERROR,
  495. languages[i] << " is not a recognized language"
  496. << std::endl);
  497. }
  498. char iso_language_cstr[65];
  499. CFStringGetCString(iso_language, iso_language_cstr,
  500. sizeof(iso_language_cstr) - 1,
  501. kCFStringEncodingMacRoman);
  502. LangCode lang = 0;
  503. RegionCode region = 0;
  504. #ifdef HAVE_CoreServices
  505. OSStatus err =
  506. LocaleStringToLangAndRegionCodes(iso_language_cstr, &lang, &region);
  507. if (err != noErr)
  508. #endif
  509. {
  510. cmCPackLogger(cmCPackLog::LOG_ERROR,
  511. "No language/region code available for "
  512. << iso_language_cstr << std::endl);
  513. return 0;
  514. }
  515. #ifdef HAVE_CoreServices
  516. header_data.push_back(region);
  517. header_data.push_back(i);
  518. header_data.push_back(0);
  519. #endif
  520. }
  521. }
  522. RezDoc rez;
  523. {
  524. RezDict lpic = { {}, 5000, {} };
  525. lpic.Data.reserve(header_data.size() * sizeof(header_data[0]));
  526. for (uint16_t x : header_data) {
  527. // LPic header is big-endian.
  528. char* d = reinterpret_cast<char*>(&x);
  529. #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
  530. lpic.Data.push_back(d[1]);
  531. lpic.Data.push_back(d[0]);
  532. #else
  533. lpic.Data.push_back(d[0]);
  534. lpic.Data.push_back(d[1]);
  535. #endif
  536. }
  537. rez.LPic.Entries.emplace_back(std::move(lpic));
  538. }
  539. bool have_write_license_error = false;
  540. std::string error;
  541. if (oldStyle) {
  542. if (!this->WriteLicense(rez, 0, "", cpack_license_file, &error)) {
  543. have_write_license_error = true;
  544. }
  545. } else {
  546. for (size_t i = 0; i < languages.size() && !have_write_license_error;
  547. ++i) {
  548. if (singleLicense) {
  549. if (!this->WriteLicense(rez, i + 5000, languages[i],
  550. cpack_license_file, &error)) {
  551. have_write_license_error = true;
  552. }
  553. } else {
  554. if (!this->WriteLicense(rez, i + 5000, languages[i], "", &error)) {
  555. have_write_license_error = true;
  556. }
  557. }
  558. }
  559. }
  560. if (have_write_license_error) {
  561. cmCPackLogger(cmCPackLog::LOG_ERROR,
  562. "Error writing license file to SLA." << std::endl
  563. << error
  564. << std::endl);
  565. return 0;
  566. }
  567. this->WriteRezXML(sla_xml, rez);
  568. // Create the final compressed read-only disk image ...
  569. std::ostringstream embed_sla_command;
  570. embed_sla_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  571. embed_sla_command << " udifrez";
  572. embed_sla_command << " -xml";
  573. embed_sla_command << " \"" << sla_xml << "\"";
  574. embed_sla_command << " FIXME_WHY_IS_THIS_ARGUMENT_NEEDED";
  575. embed_sla_command << " \"" << output_file << "\"";
  576. std::string embed_error;
  577. if (!this->RunCommand(embed_sla_command, &embed_error)) {
  578. cmCPackLogger(cmCPackLog::LOG_ERROR,
  579. "Error compressing disk image." << std::endl
  580. << embed_error
  581. << std::endl);
  582. return 0;
  583. }
  584. }
  585. return 1;
  586. }
  587. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  588. {
  589. return true;
  590. }
  591. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  592. const std::string& componentName)
  593. {
  594. // we want to group components together that go in the same dmg package
  595. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  596. // we have 3 mutually exclusive modes to work in
  597. // 1. all components in one package
  598. // 2. each group goes in its own package with left over
  599. // components in their own package
  600. // 3. ignore groups - if grouping is defined, it is ignored
  601. // and each component goes in its own package
  602. if (this->componentPackageMethod == ONE_PACKAGE) {
  603. return "ALL_IN_ONE";
  604. }
  605. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  606. // We have to find the name of the COMPONENT GROUP
  607. // the current COMPONENT belongs to.
  608. std::string groupVar =
  609. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  610. const char* _groupName = GetOption(groupVar);
  611. if (_groupName) {
  612. std::string groupName = _groupName;
  613. groupName =
  614. GetComponentPackageFileName(package_file_name, groupName, true);
  615. return groupName;
  616. }
  617. }
  618. std::string componentFileName =
  619. "CPACK_DMG_" + cmSystemTools::UpperCase(componentName) + "_FILE_NAME";
  620. if (this->IsSet(componentFileName)) {
  621. return this->GetOption(componentFileName);
  622. }
  623. return GetComponentPackageFileName(package_file_name, componentName, false);
  624. }
  625. void cmCPackDragNDropGenerator::WriteRezXML(std::string const& file,
  626. RezDoc const& rez)
  627. {
  628. cmGeneratedFileStream fxml(file);
  629. cmXMLWriter xml(fxml);
  630. xml.StartDocument();
  631. xml.StartElement("plist");
  632. xml.Attribute("version", "1.0");
  633. xml.StartElement("dict");
  634. this->WriteRezArray(xml, rez.LPic);
  635. this->WriteRezArray(xml, rez.Menu);
  636. this->WriteRezArray(xml, rez.Text);
  637. this->WriteRezArray(xml, rez.RTF);
  638. xml.EndElement(); // dict
  639. xml.EndElement(); // plist
  640. xml.EndDocument();
  641. fxml.Close();
  642. }
  643. void cmCPackDragNDropGenerator::WriteRezArray(cmXMLWriter& xml,
  644. RezArray const& array)
  645. {
  646. if (array.Entries.empty()) {
  647. return;
  648. }
  649. xml.StartElement("key");
  650. xml.Content(array.Key);
  651. xml.EndElement(); // key
  652. xml.StartElement("array");
  653. for (RezDict const& dict : array.Entries) {
  654. this->WriteRezDict(xml, dict);
  655. }
  656. xml.EndElement(); // array
  657. }
  658. void cmCPackDragNDropGenerator::WriteRezDict(cmXMLWriter& xml,
  659. RezDict const& dict)
  660. {
  661. std::vector<char> base64buf(dict.Data.size() * 3 / 2 + 5);
  662. size_t base64len =
  663. cmsysBase64_Encode(dict.Data.data(), dict.Data.size(),
  664. reinterpret_cast<unsigned char*>(base64buf.data()), 0);
  665. std::string base64data(base64buf.data(), base64len);
  666. /* clang-format off */
  667. xml.StartElement("dict");
  668. xml.StartElement("key"); xml.Content("Attributes"); xml.EndElement();
  669. xml.StartElement("string"); xml.Content("0x0000"); xml.EndElement();
  670. xml.StartElement("key"); xml.Content("Data"); xml.EndElement();
  671. xml.StartElement("data"); xml.Content(base64data); xml.EndElement();
  672. xml.StartElement("key"); xml.Content("ID"); xml.EndElement();
  673. xml.StartElement("string"); xml.Content(dict.ID); xml.EndElement();
  674. xml.StartElement("key"); xml.Content("Name"); xml.EndElement();
  675. xml.StartElement("string"); xml.Content(dict.Name); xml.EndElement();
  676. xml.EndElement(); // dict
  677. /* clang-format on */
  678. }
  679. bool cmCPackDragNDropGenerator::WriteLicense(RezDoc& rez, size_t licenseNumber,
  680. std::string licenseLanguage,
  681. const std::string& licenseFile,
  682. std::string* error)
  683. {
  684. if (!licenseFile.empty() && !singleLicense) {
  685. licenseNumber = 5002;
  686. licenseLanguage = "English";
  687. }
  688. // License file
  689. RezArray* licenseArray = &rez.Text;
  690. std::string actual_license;
  691. if (!licenseFile.empty()) {
  692. if (cmHasLiteralSuffix(licenseFile, ".rtf")) {
  693. licenseArray = &rez.RTF;
  694. }
  695. actual_license = licenseFile;
  696. } else {
  697. std::string license_wo_ext =
  698. slaDirectory + "/" + licenseLanguage + ".license";
  699. if (cmSystemTools::FileExists(license_wo_ext + ".txt")) {
  700. actual_license = license_wo_ext + ".txt";
  701. } else {
  702. licenseArray = &rez.RTF;
  703. actual_license = license_wo_ext + ".rtf";
  704. }
  705. }
  706. // License body
  707. {
  708. RezDict license = { licenseLanguage, licenseNumber, {} };
  709. std::vector<std::string> lines;
  710. if (!this->ReadFile(actual_license, lines, error)) {
  711. return false;
  712. }
  713. this->EncodeLicense(license, lines);
  714. licenseArray->Entries.emplace_back(std::move(license));
  715. }
  716. // Menu body
  717. {
  718. RezDict menu = { licenseLanguage, licenseNumber, {} };
  719. if (!licenseFile.empty() && !singleLicense) {
  720. this->EncodeMenu(menu, DefaultMenu);
  721. } else {
  722. std::vector<std::string> lines;
  723. std::string actual_menu =
  724. slaDirectory + "/" + licenseLanguage + ".menu.txt";
  725. if (!this->ReadFile(actual_menu, lines, error)) {
  726. return false;
  727. }
  728. this->EncodeMenu(menu, lines);
  729. }
  730. rez.Menu.Entries.emplace_back(std::move(menu));
  731. }
  732. return true;
  733. }
  734. void cmCPackDragNDropGenerator::EncodeLicense(
  735. RezDict& dict, std::vector<std::string> const& lines)
  736. {
  737. // License text uses CR newlines.
  738. for (std::string const& l : lines) {
  739. dict.Data.insert(dict.Data.end(), l.begin(), l.end());
  740. dict.Data.push_back('\r');
  741. }
  742. dict.Data.push_back('\r');
  743. }
  744. void cmCPackDragNDropGenerator::EncodeMenu(
  745. RezDict& dict, std::vector<std::string> const& lines)
  746. {
  747. // Menu resources start with a big-endian uint16_t for number of lines:
  748. {
  749. uint16_t numLines = static_cast<uint16_t>(lines.size());
  750. char* d = reinterpret_cast<char*>(&numLines);
  751. #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
  752. dict.Data.push_back(d[1]);
  753. dict.Data.push_back(d[0]);
  754. #else
  755. dict.Data.push_back(d[0]);
  756. dict.Data.push_back(d[1]);
  757. #endif
  758. }
  759. // Each line starts with a uint8_t length, plus the bytes themselves:
  760. for (std::string const& l : lines) {
  761. dict.Data.push_back(static_cast<unsigned char>(l.length()));
  762. dict.Data.insert(dict.Data.end(), l.begin(), l.end());
  763. }
  764. }
  765. bool cmCPackDragNDropGenerator::ReadFile(std::string const& file,
  766. std::vector<std::string>& lines,
  767. std::string* error)
  768. {
  769. cmsys::ifstream ifs(file);
  770. std::string line;
  771. while (std::getline(ifs, line)) {
  772. if (!this->BreakLongLine(line, lines, error)) {
  773. return false;
  774. }
  775. }
  776. return true;
  777. }
  778. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  779. std::vector<std::string>& lines,
  780. std::string* error)
  781. {
  782. const size_t max_line_length = 255;
  783. size_t line_length = max_line_length;
  784. for (size_t i = 0; i < line.size(); i += line_length) {
  785. line_length = max_line_length;
  786. if (i + line_length > line.size()) {
  787. line_length = line.size() - i;
  788. } else {
  789. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  790. line_length = line_length - 1;
  791. }
  792. }
  793. if (line_length == 0) {
  794. *error = "Please make sure there are no words "
  795. "(or character sequences not broken up by spaces or newlines) "
  796. "in your license file which are more than 255 characters long.";
  797. return false;
  798. }
  799. lines.push_back(line.substr(i, line_length));
  800. }
  801. return true;
  802. }