cmCPackDragNDropGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. const std::string cpack_dmg_filesystem =
  238. this->GetOption("CPACK_DMG_FILESYSTEM")
  239. ? this->GetOption("CPACK_DMG_FILESYSTEM")
  240. : "HFS+";
  241. // Get optional arguments ...
  242. std::string cpack_license_file =
  243. this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  244. ? this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  245. : "";
  246. const std::string cpack_dmg_background_image =
  247. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  248. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  249. : "";
  250. const std::string cpack_dmg_ds_store = this->GetOption("CPACK_DMG_DS_STORE")
  251. ? this->GetOption("CPACK_DMG_DS_STORE")
  252. : "";
  253. const std::string cpack_dmg_languages =
  254. this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  255. ? this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  256. : "";
  257. const std::string cpack_dmg_ds_store_setup_script =
  258. this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  259. ? this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  260. : "";
  261. const bool cpack_dmg_disable_applications_symlink =
  262. this->IsOn("CPACK_DMG_DISABLE_APPLICATIONS_SYMLINK");
  263. // only put license on dmg if is user provided
  264. if (!cpack_license_file.empty() &&
  265. cpack_license_file.find("CPack.GenericLicense.txt") !=
  266. std::string::npos) {
  267. cpack_license_file = "";
  268. }
  269. // use sla_dir if both sla_dir and license_file are set
  270. if (!cpack_license_file.empty() && !slaDirectory.empty() && !singleLicense) {
  271. cpack_license_file = "";
  272. }
  273. // The staging directory contains everything that will end-up inside the
  274. // final disk image ...
  275. std::ostringstream staging;
  276. staging << src_dir;
  277. // Add a symlink to /Applications so users can drag-and-drop the bundle
  278. // into it unless this behavior was disabled
  279. if (!cpack_dmg_disable_applications_symlink) {
  280. std::ostringstream application_link;
  281. application_link << staging.str() << "/Applications";
  282. cmSystemTools::CreateSymlink("/Applications", application_link.str());
  283. }
  284. // Optionally add a custom volume icon ...
  285. if (!cpack_package_icon.empty()) {
  286. std::ostringstream package_icon_source;
  287. package_icon_source << cpack_package_icon;
  288. std::ostringstream package_icon_destination;
  289. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  290. if (!this->CopyFile(package_icon_source, package_icon_destination)) {
  291. cmCPackLogger(cmCPackLog::LOG_ERROR,
  292. "Error copying disk volume icon. "
  293. "Check the value of CPACK_PACKAGE_ICON."
  294. << std::endl);
  295. return 0;
  296. }
  297. }
  298. // Optionally add a custom .DS_Store file
  299. // (e.g. for setting background/layout) ...
  300. if (!cpack_dmg_ds_store.empty()) {
  301. std::ostringstream package_settings_source;
  302. package_settings_source << cpack_dmg_ds_store;
  303. std::ostringstream package_settings_destination;
  304. package_settings_destination << staging.str() << "/.DS_Store";
  305. if (!this->CopyFile(package_settings_source,
  306. package_settings_destination)) {
  307. cmCPackLogger(cmCPackLog::LOG_ERROR,
  308. "Error copying disk volume settings file. "
  309. "Check the value of CPACK_DMG_DS_STORE."
  310. << std::endl);
  311. return 0;
  312. }
  313. }
  314. // Optionally add a custom background image ...
  315. // Make sure the background file type is the same as the custom image
  316. // and that the file is hidden so it doesn't show up.
  317. if (!cpack_dmg_background_image.empty()) {
  318. const std::string extension =
  319. cmSystemTools::GetFilenameLastExtension(cpack_dmg_background_image);
  320. std::ostringstream package_background_source;
  321. package_background_source << cpack_dmg_background_image;
  322. std::ostringstream package_background_destination;
  323. package_background_destination << staging.str()
  324. << "/.background/background" << extension;
  325. if (!this->CopyFile(package_background_source,
  326. package_background_destination)) {
  327. cmCPackLogger(cmCPackLog::LOG_ERROR,
  328. "Error copying disk volume background image. "
  329. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  330. << std::endl);
  331. return 0;
  332. }
  333. }
  334. bool remount_image =
  335. !cpack_package_icon.empty() || !cpack_dmg_ds_store_setup_script.empty();
  336. std::string temp_image_format = "UDZO";
  337. // Create 1 MB dummy padding file in staging area when we need to remount
  338. // image, so we have enough space for storing changes ...
  339. if (remount_image) {
  340. std::ostringstream dummy_padding;
  341. dummy_padding << staging.str() << "/.dummy-padding-file";
  342. if (!this->CreateEmptyFile(dummy_padding, 1048576)) {
  343. cmCPackLogger(cmCPackLog::LOG_ERROR,
  344. "Error creating dummy padding file." << std::endl);
  345. return 0;
  346. }
  347. temp_image_format = "UDRW";
  348. }
  349. // Create a temporary read-write disk image ...
  350. std::string temp_image =
  351. cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/temp.dmg");
  352. std::string create_error;
  353. std::ostringstream temp_image_command;
  354. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  355. temp_image_command << " create";
  356. temp_image_command << " -ov";
  357. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  358. temp_image_command << " -volname \"" << cpack_dmg_volume_name << "\"";
  359. temp_image_command << " -fs \"" << cpack_dmg_filesystem << "\"";
  360. temp_image_command << " -format " << temp_image_format;
  361. temp_image_command << " \"" << temp_image << "\"";
  362. if (!this->RunCommand(temp_image_command, &create_error)) {
  363. cmCPackLogger(cmCPackLog::LOG_ERROR,
  364. "Error generating temporary disk image." << std::endl
  365. << create_error
  366. << std::endl);
  367. return 0;
  368. }
  369. if (remount_image) {
  370. // Store that we have a failure so that we always unmount the image
  371. // before we exit.
  372. bool had_error = false;
  373. std::ostringstream attach_command;
  374. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  375. attach_command << " attach";
  376. attach_command << " \"" << temp_image << "\"";
  377. std::string attach_output;
  378. if (!this->RunCommand(attach_command, &attach_output)) {
  379. cmCPackLogger(cmCPackLog::LOG_ERROR,
  380. "Error attaching temporary disk image." << std::endl);
  381. return 0;
  382. }
  383. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  384. mountpoint_regex.find(attach_output.c_str());
  385. std::string const temp_mount = mountpoint_regex.match(1);
  386. std::string const temp_mount_name =
  387. temp_mount.substr(sizeof("/Volumes/") - 1);
  388. // Remove dummy padding file so we have enough space on RW image ...
  389. std::ostringstream dummy_padding;
  390. dummy_padding << temp_mount << "/.dummy-padding-file";
  391. if (!cmSystemTools::RemoveFile(dummy_padding.str())) {
  392. cmCPackLogger(cmCPackLog::LOG_ERROR,
  393. "Error removing dummy padding file." << std::endl);
  394. had_error = true;
  395. }
  396. // Optionally set the custom icon flag for the image ...
  397. if (!had_error && !cpack_package_icon.empty()) {
  398. std::string error;
  399. std::ostringstream setfile_command;
  400. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  401. setfile_command << " -a C";
  402. setfile_command << " \"" << temp_mount << "\"";
  403. if (!this->RunCommand(setfile_command, &error)) {
  404. cmCPackLogger(cmCPackLog::LOG_ERROR,
  405. "Error assigning custom icon to temporary disk image."
  406. << std::endl
  407. << error << std::endl);
  408. had_error = true;
  409. }
  410. }
  411. // Optionally we can execute a custom apple script to generate
  412. // the .DS_Store for the volume folder ...
  413. if (!had_error && !cpack_dmg_ds_store_setup_script.empty()) {
  414. std::ostringstream setup_script_command;
  415. setup_script_command << "osascript"
  416. << " \"" << cpack_dmg_ds_store_setup_script << "\""
  417. << " \"" << temp_mount_name << "\"";
  418. std::string error;
  419. if (!this->RunCommand(setup_script_command, &error)) {
  420. cmCPackLogger(cmCPackLog::LOG_ERROR,
  421. "Error executing custom script on disk image."
  422. << std::endl
  423. << error << std::endl);
  424. had_error = true;
  425. }
  426. }
  427. std::ostringstream detach_command;
  428. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  429. detach_command << " detach";
  430. detach_command << " \"" << temp_mount << "\"";
  431. if (!this->RunCommand(detach_command)) {
  432. cmCPackLogger(cmCPackLog::LOG_ERROR,
  433. "Error detaching temporary disk image." << std::endl);
  434. return 0;
  435. }
  436. if (had_error) {
  437. return 0;
  438. }
  439. }
  440. // Create the final compressed read-only disk image ...
  441. std::ostringstream final_image_command;
  442. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  443. final_image_command << " convert \"" << temp_image << "\"";
  444. final_image_command << " -format ";
  445. final_image_command << cpack_dmg_format;
  446. final_image_command << " -imagekey";
  447. final_image_command << " zlib-level=9";
  448. final_image_command << " -o \"" << output_file << "\"";
  449. std::string convert_error;
  450. if (!this->RunCommand(final_image_command, &convert_error)) {
  451. cmCPackLogger(cmCPackLog::LOG_ERROR,
  452. "Error compressing disk image." << std::endl
  453. << convert_error
  454. << std::endl);
  455. return 0;
  456. }
  457. if (!cpack_license_file.empty() || !slaDirectory.empty()) {
  458. // Use old hardcoded style if sla_dir is not set
  459. bool oldStyle = slaDirectory.empty();
  460. std::string sla_xml =
  461. cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/sla.xml");
  462. std::vector<std::string> languages;
  463. if (!oldStyle) {
  464. cmExpandList(cpack_dmg_languages, languages);
  465. }
  466. std::vector<uint16_t> header_data;
  467. if (oldStyle) {
  468. header_data = std::vector<uint16_t>(
  469. DefaultLpic,
  470. DefaultLpic + (sizeof(DefaultLpic) / sizeof(*DefaultLpic)));
  471. } else {
  472. /*
  473. * LPic Layout
  474. * (https://github.com/pypt/dmg-add-license/blob/master/main.c)
  475. * as far as I can tell (no official documentation seems to exist):
  476. * struct LPic {
  477. * uint16_t default_language; // points to a resid, defaulting to 0,
  478. * // which is the first set language
  479. * uint16_t length;
  480. * struct {
  481. * uint16_t language_code;
  482. * uint16_t resid;
  483. * uint16_t encoding; // Encoding from TextCommon.h,
  484. * // forcing MacRoman (0) for now. Might need to
  485. * // allow overwrite per license by user later
  486. * } item[1];
  487. * }
  488. */
  489. header_data.push_back(0);
  490. header_data.push_back(languages.size());
  491. for (size_t i = 0; i < languages.size(); ++i) {
  492. CFStringRef language_cfstring = CFStringCreateWithCString(
  493. nullptr, languages[i].c_str(), kCFStringEncodingUTF8);
  494. CFStringRef iso_language =
  495. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  496. nullptr, language_cfstring);
  497. if (!iso_language) {
  498. cmCPackLogger(cmCPackLog::LOG_ERROR,
  499. languages[i] << " is not a recognized language"
  500. << std::endl);
  501. }
  502. char iso_language_cstr[65];
  503. CFStringGetCString(iso_language, iso_language_cstr,
  504. sizeof(iso_language_cstr) - 1,
  505. kCFStringEncodingMacRoman);
  506. LangCode lang = 0;
  507. RegionCode region = 0;
  508. #ifdef HAVE_CoreServices
  509. OSStatus err =
  510. LocaleStringToLangAndRegionCodes(iso_language_cstr, &lang, &region);
  511. if (err != noErr)
  512. #endif
  513. {
  514. cmCPackLogger(cmCPackLog::LOG_ERROR,
  515. "No language/region code available for "
  516. << iso_language_cstr << std::endl);
  517. return 0;
  518. }
  519. #ifdef HAVE_CoreServices
  520. header_data.push_back(region);
  521. header_data.push_back(i);
  522. header_data.push_back(0);
  523. #endif
  524. }
  525. }
  526. RezDoc rez;
  527. {
  528. RezDict lpic = { {}, 5000, {} };
  529. lpic.Data.reserve(header_data.size() * sizeof(header_data[0]));
  530. for (uint16_t x : header_data) {
  531. // LPic header is big-endian.
  532. char* d = reinterpret_cast<char*>(&x);
  533. #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
  534. lpic.Data.push_back(d[1]);
  535. lpic.Data.push_back(d[0]);
  536. #else
  537. lpic.Data.push_back(d[0]);
  538. lpic.Data.push_back(d[1]);
  539. #endif
  540. }
  541. rez.LPic.Entries.emplace_back(std::move(lpic));
  542. }
  543. bool have_write_license_error = false;
  544. std::string error;
  545. if (oldStyle) {
  546. if (!this->WriteLicense(rez, 0, "", cpack_license_file, &error)) {
  547. have_write_license_error = true;
  548. }
  549. } else {
  550. for (size_t i = 0; i < languages.size() && !have_write_license_error;
  551. ++i) {
  552. if (singleLicense) {
  553. if (!this->WriteLicense(rez, i + 5000, languages[i],
  554. cpack_license_file, &error)) {
  555. have_write_license_error = true;
  556. }
  557. } else {
  558. if (!this->WriteLicense(rez, i + 5000, languages[i], "", &error)) {
  559. have_write_license_error = true;
  560. }
  561. }
  562. }
  563. }
  564. if (have_write_license_error) {
  565. cmCPackLogger(cmCPackLog::LOG_ERROR,
  566. "Error writing license file to SLA." << std::endl
  567. << error
  568. << std::endl);
  569. return 0;
  570. }
  571. this->WriteRezXML(sla_xml, rez);
  572. // Create the final compressed read-only disk image ...
  573. std::ostringstream embed_sla_command;
  574. embed_sla_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  575. embed_sla_command << " udifrez";
  576. embed_sla_command << " -xml";
  577. embed_sla_command << " \"" << sla_xml << "\"";
  578. embed_sla_command << " FIXME_WHY_IS_THIS_ARGUMENT_NEEDED";
  579. embed_sla_command << " \"" << output_file << "\"";
  580. std::string embed_error;
  581. if (!this->RunCommand(embed_sla_command, &embed_error)) {
  582. cmCPackLogger(cmCPackLog::LOG_ERROR,
  583. "Error compressing disk image." << std::endl
  584. << embed_error
  585. << std::endl);
  586. return 0;
  587. }
  588. }
  589. return 1;
  590. }
  591. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  592. {
  593. return true;
  594. }
  595. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  596. const std::string& componentName)
  597. {
  598. // we want to group components together that go in the same dmg package
  599. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  600. // we have 3 mutually exclusive modes to work in
  601. // 1. all components in one package
  602. // 2. each group goes in its own package with left over
  603. // components in their own package
  604. // 3. ignore groups - if grouping is defined, it is ignored
  605. // and each component goes in its own package
  606. if (this->componentPackageMethod == ONE_PACKAGE) {
  607. return "ALL_IN_ONE";
  608. }
  609. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  610. // We have to find the name of the COMPONENT GROUP
  611. // the current COMPONENT belongs to.
  612. std::string groupVar =
  613. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  614. const char* _groupName = GetOption(groupVar);
  615. if (_groupName) {
  616. std::string groupName = _groupName;
  617. groupName =
  618. GetComponentPackageFileName(package_file_name, groupName, true);
  619. return groupName;
  620. }
  621. }
  622. std::string componentFileName =
  623. "CPACK_DMG_" + cmSystemTools::UpperCase(componentName) + "_FILE_NAME";
  624. if (this->IsSet(componentFileName)) {
  625. return this->GetOption(componentFileName);
  626. }
  627. return GetComponentPackageFileName(package_file_name, componentName, false);
  628. }
  629. void cmCPackDragNDropGenerator::WriteRezXML(std::string const& file,
  630. RezDoc const& rez)
  631. {
  632. cmGeneratedFileStream fxml(file);
  633. cmXMLWriter xml(fxml);
  634. xml.StartDocument();
  635. xml.StartElement("plist");
  636. xml.Attribute("version", "1.0");
  637. xml.StartElement("dict");
  638. this->WriteRezArray(xml, rez.LPic);
  639. this->WriteRezArray(xml, rez.Menu);
  640. this->WriteRezArray(xml, rez.Text);
  641. this->WriteRezArray(xml, rez.RTF);
  642. xml.EndElement(); // dict
  643. xml.EndElement(); // plist
  644. xml.EndDocument();
  645. fxml.Close();
  646. }
  647. void cmCPackDragNDropGenerator::WriteRezArray(cmXMLWriter& xml,
  648. RezArray const& array)
  649. {
  650. if (array.Entries.empty()) {
  651. return;
  652. }
  653. xml.StartElement("key");
  654. xml.Content(array.Key);
  655. xml.EndElement(); // key
  656. xml.StartElement("array");
  657. for (RezDict const& dict : array.Entries) {
  658. this->WriteRezDict(xml, dict);
  659. }
  660. xml.EndElement(); // array
  661. }
  662. void cmCPackDragNDropGenerator::WriteRezDict(cmXMLWriter& xml,
  663. RezDict const& dict)
  664. {
  665. std::vector<char> base64buf(dict.Data.size() * 3 / 2 + 5);
  666. size_t base64len =
  667. cmsysBase64_Encode(dict.Data.data(), dict.Data.size(),
  668. reinterpret_cast<unsigned char*>(base64buf.data()), 0);
  669. std::string base64data(base64buf.data(), base64len);
  670. /* clang-format off */
  671. xml.StartElement("dict");
  672. xml.StartElement("key"); xml.Content("Attributes"); xml.EndElement();
  673. xml.StartElement("string"); xml.Content("0x0000"); xml.EndElement();
  674. xml.StartElement("key"); xml.Content("Data"); xml.EndElement();
  675. xml.StartElement("data"); xml.Content(base64data); xml.EndElement();
  676. xml.StartElement("key"); xml.Content("ID"); xml.EndElement();
  677. xml.StartElement("string"); xml.Content(dict.ID); xml.EndElement();
  678. xml.StartElement("key"); xml.Content("Name"); xml.EndElement();
  679. xml.StartElement("string"); xml.Content(dict.Name); xml.EndElement();
  680. xml.EndElement(); // dict
  681. /* clang-format on */
  682. }
  683. bool cmCPackDragNDropGenerator::WriteLicense(RezDoc& rez, size_t licenseNumber,
  684. std::string licenseLanguage,
  685. const std::string& licenseFile,
  686. std::string* error)
  687. {
  688. if (!licenseFile.empty() && !singleLicense) {
  689. licenseNumber = 5002;
  690. licenseLanguage = "English";
  691. }
  692. // License file
  693. RezArray* licenseArray = &rez.Text;
  694. std::string actual_license;
  695. if (!licenseFile.empty()) {
  696. if (cmHasLiteralSuffix(licenseFile, ".rtf")) {
  697. licenseArray = &rez.RTF;
  698. }
  699. actual_license = licenseFile;
  700. } else {
  701. std::string license_wo_ext =
  702. slaDirectory + "/" + licenseLanguage + ".license";
  703. if (cmSystemTools::FileExists(license_wo_ext + ".txt")) {
  704. actual_license = license_wo_ext + ".txt";
  705. } else {
  706. licenseArray = &rez.RTF;
  707. actual_license = license_wo_ext + ".rtf";
  708. }
  709. }
  710. // License body
  711. {
  712. RezDict license = { licenseLanguage, licenseNumber, {} };
  713. std::vector<std::string> lines;
  714. if (!this->ReadFile(actual_license, lines, error)) {
  715. return false;
  716. }
  717. this->EncodeLicense(license, lines);
  718. licenseArray->Entries.emplace_back(std::move(license));
  719. }
  720. // Menu body
  721. {
  722. RezDict menu = { licenseLanguage, licenseNumber, {} };
  723. if (!licenseFile.empty() && !singleLicense) {
  724. this->EncodeMenu(menu, DefaultMenu);
  725. } else {
  726. std::vector<std::string> lines;
  727. std::string actual_menu =
  728. slaDirectory + "/" + licenseLanguage + ".menu.txt";
  729. if (!this->ReadFile(actual_menu, lines, error)) {
  730. return false;
  731. }
  732. this->EncodeMenu(menu, lines);
  733. }
  734. rez.Menu.Entries.emplace_back(std::move(menu));
  735. }
  736. return true;
  737. }
  738. void cmCPackDragNDropGenerator::EncodeLicense(
  739. RezDict& dict, std::vector<std::string> const& lines)
  740. {
  741. // License text uses CR newlines.
  742. for (std::string const& l : lines) {
  743. dict.Data.insert(dict.Data.end(), l.begin(), l.end());
  744. dict.Data.push_back('\r');
  745. }
  746. dict.Data.push_back('\r');
  747. }
  748. void cmCPackDragNDropGenerator::EncodeMenu(
  749. RezDict& dict, std::vector<std::string> const& lines)
  750. {
  751. // Menu resources start with a big-endian uint16_t for number of lines:
  752. {
  753. uint16_t numLines = static_cast<uint16_t>(lines.size());
  754. char* d = reinterpret_cast<char*>(&numLines);
  755. #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
  756. dict.Data.push_back(d[1]);
  757. dict.Data.push_back(d[0]);
  758. #else
  759. dict.Data.push_back(d[0]);
  760. dict.Data.push_back(d[1]);
  761. #endif
  762. }
  763. // Each line starts with a uint8_t length, plus the bytes themselves:
  764. for (std::string const& l : lines) {
  765. dict.Data.push_back(static_cast<unsigned char>(l.length()));
  766. dict.Data.insert(dict.Data.end(), l.begin(), l.end());
  767. }
  768. }
  769. bool cmCPackDragNDropGenerator::ReadFile(std::string const& file,
  770. std::vector<std::string>& lines,
  771. std::string* error)
  772. {
  773. cmsys::ifstream ifs(file);
  774. std::string line;
  775. while (std::getline(ifs, line)) {
  776. if (!this->BreakLongLine(line, lines, error)) {
  777. return false;
  778. }
  779. }
  780. return true;
  781. }
  782. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  783. std::vector<std::string>& lines,
  784. std::string* error)
  785. {
  786. const size_t max_line_length = 255;
  787. size_t line_length = max_line_length;
  788. for (size_t i = 0; i < line.size(); i += line_length) {
  789. line_length = max_line_length;
  790. if (i + line_length > line.size()) {
  791. line_length = line.size() - i;
  792. } else {
  793. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  794. line_length = line_length - 1;
  795. }
  796. }
  797. if (line_length == 0) {
  798. *error = "Please make sure there are no words "
  799. "(or character sequences not broken up by spaces or newlines) "
  800. "in your license file which are more than 255 characters long.";
  801. return false;
  802. }
  803. lines.push_back(line.substr(i, line_length));
  804. }
  805. return true;
  806. }