cmCPackDragNDropGenerator.cxx 30 KB

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