cmCPackDragNDropGenerator.cxx 30 KB

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