cmCPackDragNDropGenerator.cxx 31 KB

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