cmCPackDragNDropGenerator.cxx 30 KB

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