cmCPackDragNDropGenerator.cxx 31 KB

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