cmCPackDragNDropGenerator.cxx 31 KB

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