cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 "cmSystemTools.h"
  9. #include "cmsys/FStream.hxx"
  10. #include "cmsys/RegularExpression.hxx"
  11. #include <algorithm>
  12. #include <iomanip>
  13. #include <map>
  14. #include <stdlib.h>
  15. #include <CoreFoundation/CoreFoundation.h>
  16. #ifdef HAVE_CoreServices
  17. // For the old LocaleStringToLangAndRegionCodes() function, to convert
  18. // to the old Script Manager RegionCode values needed for the 'LPic' data
  19. // structure used for generating multi-lingual SLAs.
  20. # include <CoreServices/CoreServices.h>
  21. #endif
  22. static const char* SLAHeader =
  23. "data 'LPic' (5000) {\n"
  24. " $\"0002 0011 0003 0001 0000 0000 0002 0000\"\n"
  25. " $\"0008 0003 0000 0001 0004 0000 0004 0005\"\n"
  26. " $\"0000 000E 0006 0001 0005 0007 0000 0007\"\n"
  27. " $\"0008 0000 0047 0009 0000 0034 000A 0001\"\n"
  28. " $\"0035 000B 0001 0020 000C 0000 0011 000D\"\n"
  29. " $\"0000 005B 0004 0000 0033 000F 0001 000C\"\n"
  30. " $\"0010 0000 000B 000E 0000\"\n"
  31. "};\n"
  32. "\n";
  33. static const char* SLASTREnglish =
  34. "resource 'STR#' (5002, \"English\") {\n"
  35. " {\n"
  36. " \"English\",\n"
  37. " \"Agree\",\n"
  38. " \"Disagree\",\n"
  39. " \"Print\",\n"
  40. " \"Save...\",\n"
  41. " \"You agree to the License Agreement terms when you click \"\n"
  42. " \"the \\\"Agree\\\" button.\",\n"
  43. " \"Software License Agreement\",\n"
  44. " \"This text cannot be saved. This disk may be full or locked, "
  45. "or the \"\n"
  46. " \"file may be locked.\",\n"
  47. " \"Unable to print. Make sure you have selected a printer.\"\n"
  48. " }\n"
  49. "};\n"
  50. "\n";
  51. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  52. : singleLicense(false)
  53. {
  54. // default to one package file for components
  55. this->componentPackageMethod = ONE_PACKAGE;
  56. }
  57. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  58. {
  59. }
  60. int cmCPackDragNDropGenerator::InitializeInternal()
  61. {
  62. // Starting with Xcode 4.3, look in "/Applications/Xcode.app" first:
  63. //
  64. std::vector<std::string> paths;
  65. paths.push_back("/Applications/Xcode.app/Contents/Developer/Tools");
  66. paths.push_back("/Developer/Tools");
  67. const std::string hdiutil_path =
  68. cmSystemTools::FindProgram("hdiutil", std::vector<std::string>(), false);
  69. if (hdiutil_path.empty()) {
  70. cmCPackLogger(cmCPackLog::LOG_ERROR,
  71. "Cannot locate hdiutil command" << std::endl);
  72. return 0;
  73. }
  74. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  75. const std::string setfile_path =
  76. cmSystemTools::FindProgram("SetFile", paths, false);
  77. if (setfile_path.empty()) {
  78. cmCPackLogger(cmCPackLog::LOG_ERROR,
  79. "Cannot locate SetFile command" << std::endl);
  80. return 0;
  81. }
  82. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  83. const std::string rez_path = cmSystemTools::FindProgram("Rez", paths, false);
  84. if (rez_path.empty()) {
  85. cmCPackLogger(cmCPackLog::LOG_ERROR,
  86. "Cannot locate Rez command" << std::endl);
  87. return 0;
  88. }
  89. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  90. if (this->IsSet("CPACK_DMG_SLA_DIR")) {
  91. slaDirectory = this->GetOption("CPACK_DMG_SLA_DIR");
  92. if (!slaDirectory.empty() && this->IsSet("CPACK_RESOURCE_FILE_LICENSE")) {
  93. std::string license_file =
  94. this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  95. if (!license_file.empty() &&
  96. (license_file.find("CPack.GenericLicense.txt") ==
  97. std::string::npos)) {
  98. cmCPackLogger(
  99. cmCPackLog::LOG_OUTPUT,
  100. "Both CPACK_DMG_SLA_DIR and CPACK_RESOURCE_FILE_LICENSE specified, "
  101. "using CPACK_RESOURCE_FILE_LICENSE as a license for all languages."
  102. << std::endl);
  103. singleLicense = true;
  104. }
  105. }
  106. if (!this->IsSet("CPACK_DMG_SLA_LANGUAGES")) {
  107. cmCPackLogger(cmCPackLog::LOG_ERROR,
  108. "CPACK_DMG_SLA_DIR set but no languages defined "
  109. "(set CPACK_DMG_SLA_LANGUAGES)"
  110. << std::endl);
  111. return 0;
  112. }
  113. if (!cmSystemTools::FileExists(slaDirectory, false)) {
  114. cmCPackLogger(cmCPackLog::LOG_ERROR,
  115. "CPACK_DMG_SLA_DIR does not exist" << std::endl);
  116. return 0;
  117. }
  118. std::vector<std::string> languages;
  119. cmSystemTools::ExpandListArgument(
  120. this->GetOption("CPACK_DMG_SLA_LANGUAGES"), languages);
  121. if (languages.empty()) {
  122. cmCPackLogger(cmCPackLog::LOG_ERROR,
  123. "CPACK_DMG_SLA_LANGUAGES set but empty" << std::endl);
  124. return 0;
  125. }
  126. for (auto const& language : languages) {
  127. std::string license = slaDirectory + "/" + language + ".license.txt";
  128. if (!singleLicense && !cmSystemTools::FileExists(license)) {
  129. cmCPackLogger(cmCPackLog::LOG_ERROR,
  130. "Missing license file " << language << ".license.txt"
  131. << std::endl);
  132. return 0;
  133. }
  134. std::string menu = slaDirectory + "/" + language + ".menu.txt";
  135. if (!cmSystemTools::FileExists(menu)) {
  136. cmCPackLogger(cmCPackLog::LOG_ERROR,
  137. "Missing menu file " << language << ".menu.txt"
  138. << std::endl);
  139. return 0;
  140. }
  141. }
  142. }
  143. return this->Superclass::InitializeInternal();
  144. }
  145. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  146. {
  147. return ".dmg";
  148. }
  149. int cmCPackDragNDropGenerator::PackageFiles()
  150. {
  151. // gather which directories to make dmg files for
  152. // multiple directories occur if packaging components or groups separately
  153. // monolith
  154. if (this->Components.empty()) {
  155. return this->CreateDMG(toplevel, packageFileNames[0]);
  156. }
  157. // component install
  158. std::vector<std::string> package_files;
  159. std::map<std::string, cmCPackComponent>::iterator compIt;
  160. for (compIt = this->Components.begin(); compIt != this->Components.end();
  161. ++compIt) {
  162. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  163. package_files.push_back(name);
  164. }
  165. std::sort(package_files.begin(), package_files.end());
  166. package_files.erase(std::unique(package_files.begin(), package_files.end()),
  167. package_files.end());
  168. // loop to create dmg files
  169. packageFileNames.clear();
  170. for (auto const& package_file : package_files) {
  171. std::string full_package_name = std::string(toplevel) + std::string("/");
  172. if (package_file == "ALL_IN_ONE") {
  173. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  174. } else {
  175. full_package_name += package_file;
  176. }
  177. full_package_name += std::string(GetOutputExtension());
  178. packageFileNames.push_back(full_package_name);
  179. std::string src_dir = toplevel;
  180. src_dir += "/";
  181. src_dir += package_file;
  182. if (0 == this->CreateDMG(src_dir, full_package_name)) {
  183. return 0;
  184. }
  185. }
  186. return 1;
  187. }
  188. bool cmCPackDragNDropGenerator::CopyFile(std::ostringstream& source,
  189. std::ostringstream& target)
  190. {
  191. if (!cmSystemTools::CopyFileIfDifferent(source.str().c_str(),
  192. target.str().c_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().c_str(), output, output, &exit_code, nullptr,
  220. this->GeneratorVerbose, 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 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  352. temp_image += "/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 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  445. sla_r += "/sla.r";
  446. std::vector<std::string> languages;
  447. if (!oldStyle) {
  448. cmSystemTools::ExpandListArgument(cpack_dmg_languages, languages);
  449. }
  450. cmGeneratedFileStream ofs(sla_r.c_str());
  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 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  561. temp_udzo += "/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. 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. for (size_t i = 0; i < line.size(); i += max_line_length) {
  759. size_t line_length = max_line_length;
  760. if (i + line_length > line.size()) {
  761. line_length = line.size() - i;
  762. } else {
  763. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  764. line_length = line_length - 1;
  765. }
  766. }
  767. if (line_length == 0) {
  768. *error = "Please make sure there are no words "
  769. "(or character sequences not broken up by spaces or newlines) "
  770. "in your license file which are more than 512 characters long.";
  771. return false;
  772. }
  773. lines.push_back(line.substr(i, line_length));
  774. }
  775. return true;
  776. }
  777. void cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  778. {
  779. std::string::size_type backslash_pos = line.find('\\');
  780. while (backslash_pos != std::string::npos) {
  781. line.replace(backslash_pos, 1, "\\\\");
  782. backslash_pos = line.find('\\', backslash_pos + 2);
  783. }
  784. std::string::size_type quote_pos = line.find('\"');
  785. while (quote_pos != std::string::npos) {
  786. line.replace(quote_pos, 1, "\\\"");
  787. quote_pos = line.find('\"', quote_pos + 2);
  788. }
  789. }