cmCPackDragNDropGenerator.cxx 31 KB

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