cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 "cmGeneratedFileStream.h"
  7. #include "cmSystemTools.h"
  8. #include "cmsys/FStream.hxx"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <algorithm>
  11. #include <iomanip>
  12. #include <map>
  13. #include <stdlib.h>
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #ifdef HAVE_CoreServices
  16. // For the old LocaleStringToLangAndRegionCodes() function, to convert
  17. // to the old Script Manager RegionCode values needed for the 'LPic' data
  18. // structure used for generating multi-lingual SLAs.
  19. #include <CoreServices/CoreServices.h>
  20. #endif
  21. static const char* SLAHeader =
  22. "data 'LPic' (5000) {\n"
  23. " $\"0002 0011 0003 0001 0000 0000 0002 0000\"\n"
  24. " $\"0008 0003 0000 0001 0004 0000 0004 0005\"\n"
  25. " $\"0000 000E 0006 0001 0005 0007 0000 0007\"\n"
  26. " $\"0008 0000 0047 0009 0000 0034 000A 0001\"\n"
  27. " $\"0035 000B 0001 0020 000C 0000 0011 000D\"\n"
  28. " $\"0000 005B 0004 0000 0033 000F 0001 000C\"\n"
  29. " $\"0010 0000 000B 000E 0000\"\n"
  30. "};\n"
  31. "\n";
  32. static const char* SLASTREnglish =
  33. "resource 'STR#' (5002, \"English\") {\n"
  34. " {\n"
  35. " \"English\",\n"
  36. " \"Agree\",\n"
  37. " \"Disagree\",\n"
  38. " \"Print\",\n"
  39. " \"Save...\",\n"
  40. " \"You agree to the License Agreement terms when you click \"\n"
  41. " \"the \\\"Agree\\\" button.\",\n"
  42. " \"Software License Agreement\",\n"
  43. " \"This text cannot be saved. This disk may be full or locked, "
  44. "or the \"\n"
  45. " \"file may be locked.\",\n"
  46. " \"Unable to print. Make sure you have selected a printer.\"\n"
  47. " }\n"
  48. "};\n"
  49. "\n";
  50. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  51. : singleLicense(false)
  52. {
  53. // default to one package file for components
  54. this->componentPackageMethod = ONE_PACKAGE;
  55. }
  56. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  57. {
  58. }
  59. int cmCPackDragNDropGenerator::InitializeInternal()
  60. {
  61. // Starting with Xcode 4.3, look in "/Applications/Xcode.app" first:
  62. //
  63. std::vector<std::string> paths;
  64. paths.push_back("/Applications/Xcode.app/Contents/Developer/Tools");
  65. paths.push_back("/Developer/Tools");
  66. const std::string hdiutil_path =
  67. cmSystemTools::FindProgram("hdiutil", std::vector<std::string>(), false);
  68. if (hdiutil_path.empty()) {
  69. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate hdiutil command"
  70. << std::endl);
  71. return 0;
  72. }
  73. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  74. const std::string setfile_path =
  75. cmSystemTools::FindProgram("SetFile", paths, false);
  76. if (setfile_path.empty()) {
  77. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate SetFile command"
  78. << std::endl);
  79. return 0;
  80. }
  81. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  82. const std::string rez_path = cmSystemTools::FindProgram("Rez", paths, false);
  83. if (rez_path.empty()) {
  84. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate Rez command"
  85. << std::endl);
  86. return 0;
  87. }
  88. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  89. if (this->IsSet("CPACK_DMG_SLA_DIR")) {
  90. slaDirectory = this->GetOption("CPACK_DMG_SLA_DIR");
  91. if (!slaDirectory.empty() && this->IsSet("CPACK_RESOURCE_FILE_LICENSE")) {
  92. std::string license_file =
  93. this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  94. if (!license_file.empty() &&
  95. (license_file.find("CPack.GenericLicense.txt") ==
  96. std::string::npos)) {
  97. cmCPackLogger(
  98. cmCPackLog::LOG_OUTPUT,
  99. "Both CPACK_DMG_SLA_DIR and CPACK_RESOURCE_FILE_LICENSE specified, "
  100. "using CPACK_RESOURCE_FILE_LICENSE as a license for all languages."
  101. << std::endl);
  102. singleLicense = true;
  103. }
  104. }
  105. if (!this->IsSet("CPACK_DMG_SLA_LANGUAGES")) {
  106. cmCPackLogger(cmCPackLog::LOG_ERROR,
  107. "CPACK_DMG_SLA_DIR set but no languages defined "
  108. "(set CPACK_DMG_SLA_LANGUAGES)"
  109. << std::endl);
  110. return 0;
  111. }
  112. if (!cmSystemTools::FileExists(slaDirectory, false)) {
  113. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_DMG_SLA_DIR does not exist"
  114. << std::endl);
  115. return 0;
  116. }
  117. std::vector<std::string> languages;
  118. cmSystemTools::ExpandListArgument(
  119. this->GetOption("CPACK_DMG_SLA_LANGUAGES"), languages);
  120. if (languages.empty()) {
  121. cmCPackLogger(cmCPackLog::LOG_ERROR,
  122. "CPACK_DMG_SLA_LANGUAGES set but empty" << std::endl);
  123. return 0;
  124. }
  125. for (size_t i = 0; i < languages.size(); ++i) {
  126. std::string license = slaDirectory + "/" + languages[i] + ".license.txt";
  127. if (!singleLicense && !cmSystemTools::FileExists(license)) {
  128. cmCPackLogger(cmCPackLog::LOG_ERROR, "Missing license file "
  129. << languages[i] << ".license.txt" << std::endl);
  130. return 0;
  131. }
  132. std::string menu = slaDirectory + "/" + languages[i] + ".menu.txt";
  133. if (!cmSystemTools::FileExists(menu)) {
  134. cmCPackLogger(cmCPackLog::LOG_ERROR, "Missing menu file "
  135. << languages[i] << ".menu.txt" << std::endl);
  136. return 0;
  137. }
  138. }
  139. }
  140. return this->Superclass::InitializeInternal();
  141. }
  142. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  143. {
  144. return ".dmg";
  145. }
  146. int cmCPackDragNDropGenerator::PackageFiles()
  147. {
  148. // gather which directories to make dmg files for
  149. // multiple directories occur if packaging components or groups separately
  150. // monolith
  151. if (this->Components.empty()) {
  152. return this->CreateDMG(toplevel, packageFileNames[0]);
  153. }
  154. // component install
  155. std::vector<std::string> package_files;
  156. std::map<std::string, cmCPackComponent>::iterator compIt;
  157. for (compIt = this->Components.begin(); compIt != this->Components.end();
  158. ++compIt) {
  159. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  160. package_files.push_back(name);
  161. }
  162. std::sort(package_files.begin(), package_files.end());
  163. package_files.erase(std::unique(package_files.begin(), package_files.end()),
  164. package_files.end());
  165. // loop to create dmg files
  166. packageFileNames.clear();
  167. for (size_t i = 0; i < package_files.size(); i++) {
  168. std::string full_package_name = std::string(toplevel) + std::string("/");
  169. if (package_files[i] == "ALL_IN_ONE") {
  170. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  171. } else {
  172. full_package_name += package_files[i];
  173. }
  174. full_package_name += std::string(GetOutputExtension());
  175. packageFileNames.push_back(full_package_name);
  176. std::string src_dir = toplevel;
  177. src_dir += "/";
  178. src_dir += package_files[i];
  179. if (0 == this->CreateDMG(src_dir, full_package_name)) {
  180. return 0;
  181. }
  182. }
  183. return 1;
  184. }
  185. bool cmCPackDragNDropGenerator::CopyFile(std::ostringstream& source,
  186. std::ostringstream& target)
  187. {
  188. if (!cmSystemTools::CopyFileIfDifferent(source.str().c_str(),
  189. target.str().c_str())) {
  190. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error copying "
  191. << source.str() << " to " << target.str() << std::endl);
  192. return false;
  193. }
  194. return true;
  195. }
  196. bool cmCPackDragNDropGenerator::CreateEmptyFile(std::ostringstream& target,
  197. size_t size)
  198. {
  199. cmsys::ofstream fout(target.str().c_str(), std::ios::out | std::ios::binary);
  200. if (!fout) {
  201. return false;
  202. } else {
  203. // Seek to desired size - 1 byte
  204. fout.seekp(size - 1, std::ios::beg);
  205. char byte = 0;
  206. // Write one byte to ensure file grows
  207. fout.write(&byte, 1);
  208. }
  209. return true;
  210. }
  211. bool cmCPackDragNDropGenerator::RunCommand(std::ostringstream& command,
  212. std::string* output)
  213. {
  214. int exit_code = 1;
  215. bool result =
  216. cmSystemTools::RunSingleCommand(command.str().c_str(), output, output,
  217. &exit_code, 0, this->GeneratorVerbose, 0);
  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::ostringstream temp_image_command;
  351. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  352. temp_image_command << " create";
  353. temp_image_command << " -ov";
  354. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  355. temp_image_command << " -volname \"" << cpack_dmg_volume_name << "\"";
  356. temp_image_command << " -format " << temp_image_format;
  357. temp_image_command << " \"" << temp_image << "\"";
  358. if (!this->RunCommand(temp_image_command)) {
  359. cmCPackLogger(cmCPackLog::LOG_ERROR,
  360. "Error generating temporary disk image." << std::endl);
  361. return 0;
  362. }
  363. if (remount_image) {
  364. // Store that we have a failure so that we always unmount the image
  365. // before we exit.
  366. bool had_error = false;
  367. std::ostringstream attach_command;
  368. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  369. attach_command << " attach";
  370. attach_command << " \"" << temp_image << "\"";
  371. std::string attach_output;
  372. if (!this->RunCommand(attach_command, &attach_output)) {
  373. cmCPackLogger(cmCPackLog::LOG_ERROR,
  374. "Error attaching temporary disk image." << std::endl);
  375. return 0;
  376. }
  377. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  378. mountpoint_regex.find(attach_output.c_str());
  379. std::string const temp_mount = mountpoint_regex.match(1);
  380. // Remove dummy padding file so we have enough space on RW image ...
  381. std::ostringstream dummy_padding;
  382. dummy_padding << temp_mount << "/.dummy-padding-file";
  383. if (!cmSystemTools::RemoveFile(dummy_padding.str())) {
  384. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error removing dummy padding file."
  385. << std::endl);
  386. had_error = true;
  387. }
  388. // Optionally set the custom icon flag for the image ...
  389. if (!had_error && !cpack_package_icon.empty()) {
  390. std::ostringstream setfile_command;
  391. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  392. setfile_command << " -a C";
  393. setfile_command << " \"" << temp_mount << "\"";
  394. if (!this->RunCommand(setfile_command)) {
  395. cmCPackLogger(cmCPackLog::LOG_ERROR,
  396. "Error assigning custom icon to temporary disk image."
  397. << std::endl);
  398. had_error = true;
  399. }
  400. }
  401. // Optionally we can execute a custom apple script to generate
  402. // the .DS_Store for the volume folder ...
  403. if (!had_error && !cpack_dmg_ds_store_setup_script.empty()) {
  404. std::ostringstream setup_script_command;
  405. setup_script_command << "osascript"
  406. << " \"" << cpack_dmg_ds_store_setup_script << "\""
  407. << " \"" << cpack_dmg_volume_name << "\"";
  408. std::string error;
  409. if (!this->RunCommand(setup_script_command, &error)) {
  410. cmCPackLogger(cmCPackLog::LOG_ERROR,
  411. "Error executing custom script on disk image."
  412. << std::endl
  413. << error << std::endl);
  414. had_error = true;
  415. }
  416. }
  417. std::ostringstream detach_command;
  418. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  419. detach_command << " detach";
  420. detach_command << " \"" << temp_mount << "\"";
  421. if (!this->RunCommand(detach_command)) {
  422. cmCPackLogger(cmCPackLog::LOG_ERROR,
  423. "Error detaching temporary disk image." << std::endl);
  424. return 0;
  425. }
  426. if (had_error) {
  427. return 0;
  428. }
  429. }
  430. if (!cpack_license_file.empty() || !slaDirectory.empty()) {
  431. // Use old hardcoded style if sla_dir is not set
  432. bool oldStyle = slaDirectory.empty();
  433. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  434. sla_r += "/sla.r";
  435. std::vector<std::string> languages;
  436. if (!oldStyle) {
  437. cmSystemTools::ExpandListArgument(cpack_dmg_languages, languages);
  438. }
  439. cmGeneratedFileStream ofs(sla_r.c_str());
  440. ofs << "#include <CoreServices/CoreServices.r>\n\n";
  441. if (oldStyle) {
  442. ofs << SLAHeader;
  443. ofs << "\n";
  444. } else {
  445. /*
  446. * LPic Layout
  447. * (https://github.com/pypt/dmg-add-license/blob/master/main.c)
  448. * as far as I can tell (no official documentation seems to exist):
  449. * struct LPic {
  450. * uint16_t default_language; // points to a resid, defaulting to 0,
  451. * // which is the first set language
  452. * uint16_t length;
  453. * struct {
  454. * uint16_t language_code;
  455. * uint16_t resid;
  456. * uint16_t encoding; // Encoding from TextCommon.h,
  457. * // forcing MacRoman (0) for now. Might need to
  458. * // allow overwrite per license by user later
  459. * } item[1];
  460. * }
  461. */
  462. // Create vector first for readability, then iterate to write to ofs
  463. std::vector<uint16_t> header_data;
  464. header_data.push_back(0);
  465. header_data.push_back(languages.size());
  466. for (size_t i = 0; i < languages.size(); ++i) {
  467. CFStringRef language_cfstring = CFStringCreateWithCString(
  468. NULL, languages[i].c_str(), kCFStringEncodingUTF8);
  469. CFStringRef iso_language =
  470. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  471. NULL, language_cfstring);
  472. if (!iso_language) {
  473. cmCPackLogger(cmCPackLog::LOG_ERROR, languages[i]
  474. << " is not a recognized language" << std::endl);
  475. }
  476. char* iso_language_cstr = (char*)malloc(65);
  477. CFStringGetCString(iso_language, iso_language_cstr, 64,
  478. kCFStringEncodingMacRoman);
  479. LangCode lang = 0;
  480. RegionCode region = 0;
  481. #ifdef HAVE_CoreServices
  482. OSStatus err =
  483. LocaleStringToLangAndRegionCodes(iso_language_cstr, &lang, &region);
  484. if (err != noErr)
  485. #endif
  486. {
  487. cmCPackLogger(cmCPackLog::LOG_ERROR,
  488. "No language/region code available for "
  489. << iso_language_cstr << std::endl);
  490. free(iso_language_cstr);
  491. return 0;
  492. }
  493. #ifdef HAVE_CoreServices
  494. free(iso_language_cstr);
  495. header_data.push_back(region);
  496. header_data.push_back(i);
  497. header_data.push_back(0);
  498. #endif
  499. }
  500. ofs << "data 'LPic' (5000) {\n";
  501. ofs << std::hex << std::uppercase << std::setfill('0');
  502. for (size_t i = 0; i < header_data.size(); ++i) {
  503. if (i % 8 == 0) {
  504. ofs << " $\"";
  505. }
  506. ofs << std::setw(4) << header_data[i];
  507. if (i % 8 == 7 || i == header_data.size() - 1) {
  508. ofs << "\"\n";
  509. } else {
  510. ofs << " ";
  511. }
  512. }
  513. ofs << "};\n\n";
  514. // Reset ofs options
  515. ofs << std::dec << std::nouppercase << std::setfill(' ');
  516. }
  517. bool have_write_license_error = false;
  518. std::string error;
  519. if (oldStyle) {
  520. if (!this->WriteLicense(ofs, 0, "", cpack_license_file, &error)) {
  521. have_write_license_error = true;
  522. }
  523. } else {
  524. for (size_t i = 0; i < languages.size() && !have_write_license_error;
  525. ++i) {
  526. if (singleLicense) {
  527. if (!this->WriteLicense(ofs, i + 5000, languages[i],
  528. cpack_license_file, &error)) {
  529. have_write_license_error = true;
  530. }
  531. } else {
  532. if (!this->WriteLicense(ofs, i + 5000, languages[i], "", &error)) {
  533. have_write_license_error = true;
  534. }
  535. }
  536. }
  537. }
  538. ofs.Close();
  539. if (have_write_license_error) {
  540. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error writing license file to SLA."
  541. << std::endl
  542. << error << std::endl);
  543. return 0;
  544. }
  545. if (temp_image_format != "UDZO") {
  546. temp_image_format = "UDZO";
  547. // convert to UDZO to enable unflatten/flatten
  548. std::string temp_udzo = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  549. temp_udzo += "/temp-udzo.dmg";
  550. std::ostringstream udco_image_command;
  551. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  552. udco_image_command << " convert \"" << temp_image << "\"";
  553. udco_image_command << " -format UDZO";
  554. udco_image_command << " -ov -o \"" << temp_udzo << "\"";
  555. if (!this->RunCommand(udco_image_command, &error)) {
  556. cmCPackLogger(cmCPackLog::LOG_ERROR,
  557. "Error converting to UDCO dmg for adding SLA."
  558. << std::endl
  559. << error << std::endl);
  560. return 0;
  561. }
  562. temp_image = temp_udzo;
  563. }
  564. // unflatten dmg
  565. std::ostringstream unflatten_command;
  566. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  567. unflatten_command << " unflatten ";
  568. unflatten_command << "\"" << temp_image << "\"";
  569. if (!this->RunCommand(unflatten_command, &error)) {
  570. cmCPackLogger(cmCPackLog::LOG_ERROR,
  571. "Error unflattening dmg for adding SLA." << std::endl
  572. << error
  573. << std::endl);
  574. return 0;
  575. }
  576. // Rez the SLA
  577. std::ostringstream embed_sla_command;
  578. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  579. const char* sysroot = this->GetOption("CPACK_OSX_SYSROOT");
  580. if (sysroot && sysroot[0] != '\0') {
  581. embed_sla_command << " -isysroot \"" << sysroot << "\"";
  582. }
  583. embed_sla_command << " \"" << sla_r << "\"";
  584. embed_sla_command << " -a -o ";
  585. embed_sla_command << "\"" << temp_image << "\"";
  586. if (!this->RunCommand(embed_sla_command, &error)) {
  587. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding SLA." << std::endl
  588. << error
  589. << std::endl);
  590. return 0;
  591. }
  592. // flatten dmg
  593. std::ostringstream flatten_command;
  594. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  595. flatten_command << " flatten ";
  596. flatten_command << "\"" << temp_image << "\"";
  597. if (!this->RunCommand(flatten_command, &error)) {
  598. cmCPackLogger(cmCPackLog::LOG_ERROR,
  599. "Error flattening dmg for adding SLA." << std::endl
  600. << error
  601. << std::endl);
  602. return 0;
  603. }
  604. }
  605. // Create the final compressed read-only disk image ...
  606. std::ostringstream final_image_command;
  607. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  608. final_image_command << " convert \"" << temp_image << "\"";
  609. final_image_command << " -format ";
  610. final_image_command << cpack_dmg_format;
  611. final_image_command << " -imagekey";
  612. final_image_command << " zlib-level=9";
  613. final_image_command << " -o \"" << output_file << "\"";
  614. if (!this->RunCommand(final_image_command)) {
  615. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error compressing disk image."
  616. << std::endl);
  617. return 0;
  618. }
  619. return 1;
  620. }
  621. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  622. {
  623. return true;
  624. }
  625. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  626. const std::string& componentName)
  627. {
  628. // we want to group components together that go in the same dmg package
  629. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  630. // we have 3 mutually exclusive modes to work in
  631. // 1. all components in one package
  632. // 2. each group goes in its own package with left over
  633. // components in their own package
  634. // 3. ignore groups - if grouping is defined, it is ignored
  635. // and each component goes in its own package
  636. if (this->componentPackageMethod == ONE_PACKAGE) {
  637. return "ALL_IN_ONE";
  638. }
  639. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  640. // We have to find the name of the COMPONENT GROUP
  641. // the current COMPONENT belongs to.
  642. std::string groupVar =
  643. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  644. const char* _groupName = GetOption(groupVar);
  645. if (_groupName) {
  646. std::string groupName = _groupName;
  647. groupName =
  648. GetComponentPackageFileName(package_file_name, groupName, true);
  649. return groupName;
  650. }
  651. }
  652. return GetComponentPackageFileName(package_file_name, componentName, false);
  653. }
  654. bool cmCPackDragNDropGenerator::WriteLicense(
  655. cmGeneratedFileStream& outputStream, int licenseNumber,
  656. std::string licenseLanguage, std::string licenseFile, std::string* error)
  657. {
  658. if (!licenseFile.empty() && !singleLicense) {
  659. licenseNumber = 5002;
  660. licenseLanguage = "English";
  661. }
  662. // License header
  663. outputStream << "data 'TEXT' (" << licenseNumber << ", \"" << licenseLanguage
  664. << "\") {\n";
  665. // License body
  666. std::string actual_license = !licenseFile.empty()
  667. ? licenseFile
  668. : (slaDirectory + "/" + licenseLanguage + ".license.txt");
  669. cmsys::ifstream license_ifs;
  670. license_ifs.open(actual_license.c_str());
  671. if (license_ifs.is_open()) {
  672. while (license_ifs.good()) {
  673. std::string line;
  674. std::getline(license_ifs, line);
  675. if (!line.empty()) {
  676. EscapeQuotesAndBackslashes(line);
  677. std::vector<std::string> lines;
  678. if (!this->BreakLongLine(line, lines, error)) {
  679. return false;
  680. }
  681. for (size_t i = 0; i < lines.size(); ++i) {
  682. outputStream << " \"" << lines[i] << "\"\n";
  683. }
  684. }
  685. outputStream << " \"\\n\"\n";
  686. }
  687. license_ifs.close();
  688. }
  689. // End of License
  690. outputStream << "};\n\n";
  691. if (!licenseFile.empty() && !singleLicense) {
  692. outputStream << SLASTREnglish;
  693. } else {
  694. // Menu header
  695. outputStream << "resource 'STR#' (" << licenseNumber << ", \""
  696. << licenseLanguage << "\") {\n";
  697. outputStream << " {\n";
  698. // Menu body
  699. cmsys::ifstream menu_ifs;
  700. menu_ifs.open(
  701. (slaDirectory + "/" + licenseLanguage + ".menu.txt").c_str());
  702. if (menu_ifs.is_open()) {
  703. size_t lines_written = 0;
  704. while (menu_ifs.good()) {
  705. // Lines written from original file, not from broken up lines
  706. std::string line;
  707. std::getline(menu_ifs, line);
  708. if (!line.empty()) {
  709. EscapeQuotesAndBackslashes(line);
  710. std::vector<std::string> lines;
  711. if (!this->BreakLongLine(line, lines, error)) {
  712. return false;
  713. }
  714. for (size_t i = 0; i < lines.size(); ++i) {
  715. std::string comma;
  716. // We need a comma after every complete string,
  717. // but not on the very last line
  718. if (lines_written != 8 && i == lines.size() - 1) {
  719. comma = ",";
  720. } else {
  721. comma = "";
  722. }
  723. outputStream << " \"" << lines[i] << "\"" << comma << "\n";
  724. }
  725. ++lines_written;
  726. }
  727. }
  728. menu_ifs.close();
  729. }
  730. // End of menu
  731. outputStream << " }\n";
  732. outputStream << "};\n";
  733. outputStream << "\n";
  734. }
  735. return true;
  736. }
  737. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  738. std::vector<std::string>& lines,
  739. std::string* error)
  740. {
  741. const size_t max_line_length = 512;
  742. for (size_t i = 0; i < line.size(); i += max_line_length) {
  743. size_t line_length = max_line_length;
  744. if (i + line_length > line.size()) {
  745. line_length = line.size() - i;
  746. } else
  747. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  748. line_length = line_length - 1;
  749. }
  750. if (line_length == 0) {
  751. *error = "Please make sure there are no words "
  752. "(or character sequences not broken up by spaces or newlines) "
  753. "in your license file which are more than 512 characters long.";
  754. return false;
  755. }
  756. lines.push_back(line.substr(i, line_length));
  757. }
  758. return true;
  759. }
  760. void cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  761. {
  762. std::string::size_type backslash_pos = line.find('\\');
  763. while (backslash_pos != std::string::npos) {
  764. line.replace(backslash_pos, 1, "\\\\");
  765. backslash_pos = line.find('\\', backslash_pos + 2);
  766. }
  767. std::string::size_type quote_pos = line.find('\"');
  768. while (quote_pos != std::string::npos) {
  769. line.replace(quote_pos, 1, "\\\"");
  770. quote_pos = line.find('\"', quote_pos + 2);
  771. }
  772. }