cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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, "Cannot locate hdiutil command"
  71. << 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, "Cannot locate SetFile command"
  79. << 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, "Cannot locate Rez command"
  86. << 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, "CPACK_DMG_SLA_DIR does not exist"
  115. << 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, "Missing license file "
  130. << language << ".license.txt" << std::endl);
  131. return 0;
  132. }
  133. std::string menu = slaDirectory + "/" + language + ".menu.txt";
  134. if (!cmSystemTools::FileExists(menu)) {
  135. cmCPackLogger(cmCPackLog::LOG_ERROR, "Missing menu file "
  136. << language << ".menu.txt" << 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().c_str(),
  190. target.str().c_str())) {
  191. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error copying "
  192. << source.str() << " to " << target.str() << 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().c_str(), output, output, &exit_code, nullptr,
  217. this->GeneratorVerbose, cmDuration::zero());
  218. if (!result || exit_code) {
  219. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error executing: " << command.str()
  220. << 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, "Error creating dummy padding file."
  342. << 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, "Error removing dummy padding file."
  391. << 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. cmSystemTools::ExpandListArgument(cpack_dmg_languages, languages);
  446. }
  447. cmGeneratedFileStream ofs(sla_r.c_str());
  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, languages[i]
  482. << " is not a recognized language" << std::endl);
  483. }
  484. char iso_language_cstr[65];
  485. CFStringGetCString(iso_language, iso_language_cstr,
  486. sizeof(iso_language_cstr) - 1,
  487. kCFStringEncodingMacRoman);
  488. LangCode lang = 0;
  489. RegionCode region = 0;
  490. #ifdef HAVE_CoreServices
  491. OSStatus err =
  492. LocaleStringToLangAndRegionCodes(iso_language_cstr, &lang, &region);
  493. if (err != noErr)
  494. #endif
  495. {
  496. cmCPackLogger(cmCPackLog::LOG_ERROR,
  497. "No language/region code available for "
  498. << iso_language_cstr << std::endl);
  499. return 0;
  500. }
  501. #ifdef HAVE_CoreServices
  502. header_data.push_back(region);
  503. header_data.push_back(i);
  504. header_data.push_back(0);
  505. #endif
  506. }
  507. ofs << "data 'LPic' (5000) {\n";
  508. ofs << std::hex << std::uppercase << std::setfill('0');
  509. for (size_t i = 0; i < header_data.size(); ++i) {
  510. if (i % 8 == 0) {
  511. ofs << " $\"";
  512. }
  513. ofs << std::setw(4) << header_data[i];
  514. if (i % 8 == 7 || i == header_data.size() - 1) {
  515. ofs << "\"\n";
  516. } else {
  517. ofs << " ";
  518. }
  519. }
  520. ofs << "};\n\n";
  521. // Reset ofs options
  522. ofs << std::dec << std::nouppercase << std::setfill(' ');
  523. }
  524. bool have_write_license_error = false;
  525. std::string error;
  526. if (oldStyle) {
  527. if (!this->WriteLicense(ofs, 0, "", cpack_license_file, &error)) {
  528. have_write_license_error = true;
  529. }
  530. } else {
  531. for (size_t i = 0; i < languages.size() && !have_write_license_error;
  532. ++i) {
  533. if (singleLicense) {
  534. if (!this->WriteLicense(ofs, i + 5000, languages[i],
  535. cpack_license_file, &error)) {
  536. have_write_license_error = true;
  537. }
  538. } else {
  539. if (!this->WriteLicense(ofs, i + 5000, languages[i], "", &error)) {
  540. have_write_license_error = true;
  541. }
  542. }
  543. }
  544. }
  545. ofs.Close();
  546. if (have_write_license_error) {
  547. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error writing license file to SLA."
  548. << std::endl
  549. << error << 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 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  556. temp_udzo += "/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, "Error adding SLA." << std::endl
  595. << error
  596. << 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, "Error compressing disk image."
  624. << std::endl
  625. << convert_error << std::endl);
  626. return 0;
  627. }
  628. return 1;
  629. }
  630. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  631. {
  632. return true;
  633. }
  634. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  635. const std::string& componentName)
  636. {
  637. // we want to group components together that go in the same dmg package
  638. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  639. // we have 3 mutually exclusive modes to work in
  640. // 1. all components in one package
  641. // 2. each group goes in its own package with left over
  642. // components in their own package
  643. // 3. ignore groups - if grouping is defined, it is ignored
  644. // and each component goes in its own package
  645. if (this->componentPackageMethod == ONE_PACKAGE) {
  646. return "ALL_IN_ONE";
  647. }
  648. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  649. // We have to find the name of the COMPONENT GROUP
  650. // the current COMPONENT belongs to.
  651. std::string groupVar =
  652. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  653. const char* _groupName = GetOption(groupVar);
  654. if (_groupName) {
  655. std::string groupName = _groupName;
  656. groupName =
  657. GetComponentPackageFileName(package_file_name, groupName, true);
  658. return groupName;
  659. }
  660. }
  661. return GetComponentPackageFileName(package_file_name, componentName, false);
  662. }
  663. bool cmCPackDragNDropGenerator::WriteLicense(
  664. cmGeneratedFileStream& outputStream, int licenseNumber,
  665. std::string licenseLanguage, const std::string& licenseFile,
  666. std::string* error)
  667. {
  668. if (!licenseFile.empty() && !singleLicense) {
  669. licenseNumber = 5002;
  670. licenseLanguage = "English";
  671. }
  672. // License header
  673. outputStream << "data 'TEXT' (" << licenseNumber << ", \"" << licenseLanguage
  674. << "\") {\n";
  675. // License body
  676. std::string actual_license = !licenseFile.empty()
  677. ? licenseFile
  678. : (slaDirectory + "/" + licenseLanguage + ".license.txt");
  679. cmsys::ifstream license_ifs;
  680. license_ifs.open(actual_license.c_str());
  681. if (license_ifs.is_open()) {
  682. while (license_ifs.good()) {
  683. std::string line;
  684. std::getline(license_ifs, line);
  685. if (!line.empty()) {
  686. EscapeQuotesAndBackslashes(line);
  687. std::vector<std::string> lines;
  688. if (!this->BreakLongLine(line, lines, error)) {
  689. return false;
  690. }
  691. for (auto const& l : lines) {
  692. outputStream << " \"" << l << "\"\n";
  693. }
  694. }
  695. outputStream << " \"\\n\"\n";
  696. }
  697. license_ifs.close();
  698. }
  699. // End of License
  700. outputStream << "};\n\n";
  701. if (!licenseFile.empty() && !singleLicense) {
  702. outputStream << SLASTREnglish;
  703. } else {
  704. // Menu header
  705. outputStream << "resource 'STR#' (" << licenseNumber << ", \""
  706. << licenseLanguage << "\") {\n";
  707. outputStream << " {\n";
  708. // Menu body
  709. cmsys::ifstream menu_ifs;
  710. menu_ifs.open(
  711. (slaDirectory + "/" + licenseLanguage + ".menu.txt").c_str());
  712. if (menu_ifs.is_open()) {
  713. size_t lines_written = 0;
  714. while (menu_ifs.good()) {
  715. // Lines written from original file, not from broken up lines
  716. std::string line;
  717. std::getline(menu_ifs, line);
  718. if (!line.empty()) {
  719. EscapeQuotesAndBackslashes(line);
  720. std::vector<std::string> lines;
  721. if (!this->BreakLongLine(line, lines, error)) {
  722. return false;
  723. }
  724. for (size_t i = 0; i < lines.size(); ++i) {
  725. std::string comma;
  726. // We need a comma after every complete string,
  727. // but not on the very last line
  728. if (lines_written != 8 && i == lines.size() - 1) {
  729. comma = ",";
  730. } else {
  731. comma = "";
  732. }
  733. outputStream << " \"" << lines[i] << "\"" << comma << "\n";
  734. }
  735. ++lines_written;
  736. }
  737. }
  738. menu_ifs.close();
  739. }
  740. // End of menu
  741. outputStream << " }\n";
  742. outputStream << "};\n";
  743. outputStream << "\n";
  744. }
  745. return true;
  746. }
  747. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  748. std::vector<std::string>& lines,
  749. std::string* error)
  750. {
  751. const size_t max_line_length = 512;
  752. for (size_t i = 0; i < line.size(); i += max_line_length) {
  753. size_t line_length = max_line_length;
  754. if (i + line_length > line.size()) {
  755. line_length = line.size() - i;
  756. } else {
  757. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  758. line_length = line_length - 1;
  759. }
  760. }
  761. if (line_length == 0) {
  762. *error = "Please make sure there are no words "
  763. "(or character sequences not broken up by spaces or newlines) "
  764. "in your license file which are more than 512 characters long.";
  765. return false;
  766. }
  767. lines.push_back(line.substr(i, line_length));
  768. }
  769. return true;
  770. }
  771. void cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  772. {
  773. std::string::size_type backslash_pos = line.find('\\');
  774. while (backslash_pos != std::string::npos) {
  775. line.replace(backslash_pos, 1, "\\\\");
  776. backslash_pos = line.find('\\', backslash_pos + 2);
  777. }
  778. std::string::size_type quote_pos = line.find('\"');
  779. while (quote_pos != std::string::npos) {
  780. line.replace(quote_pos, 1, "\\\"");
  781. quote_pos = line.find('\"', quote_pos + 2);
  782. }
  783. }