cmCPackDragNDropGenerator.cxx 31 KB

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