cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. // Create 1 MB dummy padding file in staging area when we need to remount
  334. // image, so we have enough space for storing changes ...
  335. if (remount_image) {
  336. std::ostringstream dummy_padding;
  337. dummy_padding << staging.str() << "/.dummy-padding-file";
  338. if (!this->CreateEmptyFile(dummy_padding, 1048576)) {
  339. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error creating dummy padding file."
  340. << std::endl);
  341. return 0;
  342. }
  343. }
  344. // Create a temporary read-write disk image ...
  345. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  346. temp_image += "/temp.dmg";
  347. std::ostringstream temp_image_command;
  348. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  349. temp_image_command << " create";
  350. temp_image_command << " -ov";
  351. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  352. temp_image_command << " -volname \"" << cpack_dmg_volume_name << "\"";
  353. temp_image_command << " -format UDRW";
  354. temp_image_command << " \"" << temp_image << "\"";
  355. if (!this->RunCommand(temp_image_command)) {
  356. cmCPackLogger(cmCPackLog::LOG_ERROR,
  357. "Error generating temporary disk image." << std::endl);
  358. return 0;
  359. }
  360. if (remount_image) {
  361. // Store that we have a failure so that we always unmount the image
  362. // before we exit.
  363. bool had_error = false;
  364. std::ostringstream attach_command;
  365. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  366. attach_command << " attach";
  367. attach_command << " \"" << temp_image << "\"";
  368. std::string attach_output;
  369. if (!this->RunCommand(attach_command, &attach_output)) {
  370. cmCPackLogger(cmCPackLog::LOG_ERROR,
  371. "Error attaching temporary disk image." << std::endl);
  372. return 0;
  373. }
  374. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  375. mountpoint_regex.find(attach_output.c_str());
  376. std::string const temp_mount = mountpoint_regex.match(1);
  377. // Remove dummy padding file so we have enough space on RW image ...
  378. std::ostringstream dummy_padding;
  379. dummy_padding << temp_mount << "/.dummy-padding-file";
  380. if (!cmSystemTools::RemoveFile(dummy_padding.str())) {
  381. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error removing dummy padding file."
  382. << std::endl);
  383. had_error = true;
  384. }
  385. // Optionally set the custom icon flag for the image ...
  386. if (!had_error && !cpack_package_icon.empty()) {
  387. std::ostringstream setfile_command;
  388. setfile_command << this->GetOption("CPACK_COMMAND_SETFILE");
  389. setfile_command << " -a C";
  390. setfile_command << " \"" << temp_mount << "\"";
  391. if (!this->RunCommand(setfile_command)) {
  392. cmCPackLogger(cmCPackLog::LOG_ERROR,
  393. "Error assigning custom icon to temporary disk image."
  394. << std::endl);
  395. had_error = true;
  396. }
  397. }
  398. // Optionally we can execute a custom apple script to generate
  399. // the .DS_Store for the volume folder ...
  400. if (!had_error && !cpack_dmg_ds_store_setup_script.empty()) {
  401. std::ostringstream setup_script_command;
  402. setup_script_command << "osascript"
  403. << " \"" << cpack_dmg_ds_store_setup_script << "\""
  404. << " \"" << cpack_dmg_volume_name << "\"";
  405. std::string error;
  406. if (!this->RunCommand(setup_script_command, &error)) {
  407. cmCPackLogger(cmCPackLog::LOG_ERROR,
  408. "Error executing custom script on disk image."
  409. << std::endl
  410. << error << std::endl);
  411. had_error = true;
  412. }
  413. }
  414. std::ostringstream detach_command;
  415. detach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  416. detach_command << " detach";
  417. detach_command << " \"" << temp_mount << "\"";
  418. if (!this->RunCommand(detach_command)) {
  419. cmCPackLogger(cmCPackLog::LOG_ERROR,
  420. "Error detaching temporary disk image." << std::endl);
  421. return 0;
  422. }
  423. if (had_error) {
  424. return 0;
  425. }
  426. }
  427. if (!cpack_license_file.empty() || !slaDirectory.empty()) {
  428. // Use old hardcoded style if sla_dir is not set
  429. bool oldStyle = slaDirectory.empty();
  430. std::string sla_r = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  431. sla_r += "/sla.r";
  432. std::vector<std::string> languages;
  433. if (!oldStyle) {
  434. cmSystemTools::ExpandListArgument(cpack_dmg_languages, languages);
  435. }
  436. cmGeneratedFileStream ofs(sla_r.c_str());
  437. ofs << "#include <CoreServices/CoreServices.r>\n\n";
  438. if (oldStyle) {
  439. ofs << SLAHeader;
  440. ofs << "\n";
  441. } else {
  442. /*
  443. * LPic Layout
  444. * (https://github.com/pypt/dmg-add-license/blob/master/main.c)
  445. * as far as I can tell (no official documentation seems to exist):
  446. * struct LPic {
  447. * uint16_t default_language; // points to a resid, defaulting to 0,
  448. * // which is the first set language
  449. * uint16_t length;
  450. * struct {
  451. * uint16_t language_code;
  452. * uint16_t resid;
  453. * uint16_t encoding; // Encoding from TextCommon.h,
  454. * // forcing MacRoman (0) for now. Might need to
  455. * // allow overwrite per license by user later
  456. * } item[1];
  457. * }
  458. */
  459. // Create vector first for readability, then iterate to write to ofs
  460. std::vector<uint16_t> header_data;
  461. header_data.push_back(0);
  462. header_data.push_back(languages.size());
  463. for (size_t i = 0; i < languages.size(); ++i) {
  464. CFStringRef language_cfstring = CFStringCreateWithCString(
  465. NULL, languages[i].c_str(), kCFStringEncodingUTF8);
  466. CFStringRef iso_language =
  467. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  468. NULL, language_cfstring);
  469. if (!iso_language) {
  470. cmCPackLogger(cmCPackLog::LOG_ERROR, languages[i]
  471. << " is not a recognized language" << std::endl);
  472. }
  473. char* iso_language_cstr = (char*)malloc(65);
  474. CFStringGetCString(iso_language, iso_language_cstr, 64,
  475. kCFStringEncodingMacRoman);
  476. LangCode lang = 0;
  477. RegionCode region = 0;
  478. #ifdef HAVE_CoreServices
  479. OSStatus err =
  480. LocaleStringToLangAndRegionCodes(iso_language_cstr, &lang, &region);
  481. if (err != noErr)
  482. #endif
  483. {
  484. cmCPackLogger(cmCPackLog::LOG_ERROR,
  485. "No language/region code available for "
  486. << iso_language_cstr << std::endl);
  487. free(iso_language_cstr);
  488. return 0;
  489. }
  490. #ifdef HAVE_CoreServices
  491. free(iso_language_cstr);
  492. header_data.push_back(region);
  493. header_data.push_back(i);
  494. header_data.push_back(0);
  495. #endif
  496. }
  497. ofs << "data 'LPic' (5000) {\n";
  498. ofs << std::hex << std::uppercase << std::setfill('0');
  499. for (size_t i = 0; i < header_data.size(); ++i) {
  500. if (i % 8 == 0) {
  501. ofs << " $\"";
  502. }
  503. ofs << std::setw(4) << header_data[i];
  504. if (i % 8 == 7 || i == header_data.size() - 1) {
  505. ofs << "\"\n";
  506. } else {
  507. ofs << " ";
  508. }
  509. }
  510. ofs << "};\n\n";
  511. // Reset ofs options
  512. ofs << std::dec << std::nouppercase << std::setfill(' ');
  513. }
  514. bool have_write_license_error = false;
  515. std::string error;
  516. if (oldStyle) {
  517. if (!this->WriteLicense(ofs, 0, "", cpack_license_file, &error)) {
  518. have_write_license_error = true;
  519. }
  520. } else {
  521. for (size_t i = 0; i < languages.size() && !have_write_license_error;
  522. ++i) {
  523. if (singleLicense) {
  524. if (!this->WriteLicense(ofs, i + 5000, languages[i],
  525. cpack_license_file, &error)) {
  526. have_write_license_error = true;
  527. }
  528. } else {
  529. if (!this->WriteLicense(ofs, i + 5000, languages[i], "", &error)) {
  530. have_write_license_error = true;
  531. }
  532. }
  533. }
  534. }
  535. ofs.Close();
  536. if (have_write_license_error) {
  537. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error writing license file to SLA."
  538. << std::endl
  539. << error << std::endl);
  540. return 0;
  541. }
  542. // convert to UDCO
  543. std::string temp_udco = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  544. temp_udco += "/temp-udco.dmg";
  545. std::ostringstream udco_image_command;
  546. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  547. udco_image_command << " convert \"" << temp_image << "\"";
  548. udco_image_command << " -format UDCO";
  549. udco_image_command << " -ov -o \"" << temp_udco << "\"";
  550. if (!this->RunCommand(udco_image_command, &error)) {
  551. cmCPackLogger(cmCPackLog::LOG_ERROR,
  552. "Error converting to UDCO dmg for adding SLA."
  553. << std::endl
  554. << error << std::endl);
  555. return 0;
  556. }
  557. // unflatten dmg
  558. std::ostringstream unflatten_command;
  559. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  560. unflatten_command << " unflatten ";
  561. unflatten_command << "\"" << temp_udco << "\"";
  562. if (!this->RunCommand(unflatten_command, &error)) {
  563. cmCPackLogger(cmCPackLog::LOG_ERROR,
  564. "Error unflattening dmg for adding SLA." << std::endl
  565. << error
  566. << std::endl);
  567. return 0;
  568. }
  569. // Rez the SLA
  570. std::ostringstream embed_sla_command;
  571. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  572. const char* sysroot = this->GetOption("CPACK_OSX_SYSROOT");
  573. if (sysroot && sysroot[0] != '\0') {
  574. embed_sla_command << " -isysroot \"" << sysroot << "\"";
  575. }
  576. embed_sla_command << " \"" << sla_r << "\"";
  577. embed_sla_command << " -a -o ";
  578. embed_sla_command << "\"" << temp_udco << "\"";
  579. if (!this->RunCommand(embed_sla_command, &error)) {
  580. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding SLA." << std::endl
  581. << error
  582. << std::endl);
  583. return 0;
  584. }
  585. // flatten dmg
  586. std::ostringstream flatten_command;
  587. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  588. flatten_command << " flatten ";
  589. flatten_command << "\"" << temp_udco << "\"";
  590. if (!this->RunCommand(flatten_command, &error)) {
  591. cmCPackLogger(cmCPackLog::LOG_ERROR,
  592. "Error flattening dmg for adding SLA." << std::endl
  593. << error
  594. << std::endl);
  595. return 0;
  596. }
  597. temp_image = temp_udco;
  598. }
  599. // Create the final compressed read-only disk image ...
  600. std::ostringstream final_image_command;
  601. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  602. final_image_command << " convert \"" << temp_image << "\"";
  603. final_image_command << " -format ";
  604. final_image_command << cpack_dmg_format;
  605. final_image_command << " -imagekey";
  606. final_image_command << " zlib-level=9";
  607. final_image_command << " -o \"" << output_file << "\"";
  608. if (!this->RunCommand(final_image_command)) {
  609. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error compressing disk image."
  610. << std::endl);
  611. return 0;
  612. }
  613. return 1;
  614. }
  615. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  616. {
  617. return true;
  618. }
  619. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  620. const std::string& componentName)
  621. {
  622. // we want to group components together that go in the same dmg package
  623. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  624. // we have 3 mutually exclusive modes to work in
  625. // 1. all components in one package
  626. // 2. each group goes in its own package with left over
  627. // components in their own package
  628. // 3. ignore groups - if grouping is defined, it is ignored
  629. // and each component goes in its own package
  630. if (this->componentPackageMethod == ONE_PACKAGE) {
  631. return "ALL_IN_ONE";
  632. }
  633. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  634. // We have to find the name of the COMPONENT GROUP
  635. // the current COMPONENT belongs to.
  636. std::string groupVar =
  637. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  638. const char* _groupName = GetOption(groupVar);
  639. if (_groupName) {
  640. std::string groupName = _groupName;
  641. groupName =
  642. GetComponentPackageFileName(package_file_name, groupName, true);
  643. return groupName;
  644. }
  645. }
  646. return GetComponentPackageFileName(package_file_name, componentName, false);
  647. }
  648. bool cmCPackDragNDropGenerator::WriteLicense(
  649. cmGeneratedFileStream& outputStream, int licenseNumber,
  650. std::string licenseLanguage, std::string licenseFile, std::string* error)
  651. {
  652. if (!licenseFile.empty() && !singleLicense) {
  653. licenseNumber = 5002;
  654. licenseLanguage = "English";
  655. }
  656. // License header
  657. outputStream << "data 'TEXT' (" << licenseNumber << ", \"" << licenseLanguage
  658. << "\") {\n";
  659. // License body
  660. std::string actual_license = !licenseFile.empty()
  661. ? licenseFile
  662. : (slaDirectory + "/" + licenseLanguage + ".license.txt");
  663. cmsys::ifstream license_ifs;
  664. license_ifs.open(actual_license.c_str());
  665. if (license_ifs.is_open()) {
  666. while (license_ifs.good()) {
  667. std::string line;
  668. std::getline(license_ifs, line);
  669. if (!line.empty()) {
  670. EscapeQuotesAndBackslashes(line);
  671. std::vector<std::string> lines;
  672. if (!this->BreakLongLine(line, lines, error)) {
  673. return false;
  674. }
  675. for (size_t i = 0; i < lines.size(); ++i) {
  676. outputStream << " \"" << lines[i] << "\"\n";
  677. }
  678. }
  679. outputStream << " \"\\n\"\n";
  680. }
  681. license_ifs.close();
  682. }
  683. // End of License
  684. outputStream << "};\n\n";
  685. if (!licenseFile.empty() && !singleLicense) {
  686. outputStream << SLASTREnglish;
  687. } else {
  688. // Menu header
  689. outputStream << "resource 'STR#' (" << licenseNumber << ", \""
  690. << licenseLanguage << "\") {\n";
  691. outputStream << " {\n";
  692. // Menu body
  693. cmsys::ifstream menu_ifs;
  694. menu_ifs.open(
  695. (slaDirectory + "/" + licenseLanguage + ".menu.txt").c_str());
  696. if (menu_ifs.is_open()) {
  697. size_t lines_written = 0;
  698. while (menu_ifs.good()) {
  699. // Lines written from original file, not from broken up lines
  700. std::string line;
  701. std::getline(menu_ifs, line);
  702. if (!line.empty()) {
  703. EscapeQuotesAndBackslashes(line);
  704. std::vector<std::string> lines;
  705. if (!this->BreakLongLine(line, lines, error)) {
  706. return false;
  707. }
  708. for (size_t i = 0; i < lines.size(); ++i) {
  709. std::string comma;
  710. // We need a comma after every complete string,
  711. // but not on the very last line
  712. if (lines_written != 8 && i == lines.size() - 1) {
  713. comma = ",";
  714. } else {
  715. comma = "";
  716. }
  717. outputStream << " \"" << lines[i] << "\"" << comma << "\n";
  718. }
  719. ++lines_written;
  720. }
  721. }
  722. menu_ifs.close();
  723. }
  724. // End of menu
  725. outputStream << " }\n";
  726. outputStream << "};\n";
  727. outputStream << "\n";
  728. }
  729. return true;
  730. }
  731. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  732. std::vector<std::string>& lines,
  733. std::string* error)
  734. {
  735. const size_t max_line_length = 512;
  736. for (size_t i = 0; i < line.size(); i += max_line_length) {
  737. size_t line_length = max_line_length;
  738. if (i + line_length > line.size()) {
  739. line_length = line.size() - i;
  740. } else
  741. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  742. line_length = line_length - 1;
  743. }
  744. if (line_length == 0) {
  745. *error = "Please make sure there are no words "
  746. "(or character sequences not broken up by spaces or newlines) "
  747. "in your license file which are more than 512 characters long.";
  748. return false;
  749. }
  750. lines.push_back(line.substr(i, line_length));
  751. }
  752. return true;
  753. }
  754. void cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  755. {
  756. std::string::size_type backslash_pos = line.find('\\');
  757. while (backslash_pos != std::string::npos) {
  758. line.replace(backslash_pos, 1, "\\\\");
  759. backslash_pos = line.find('\\', backslash_pos + 2);
  760. }
  761. std::string::size_type quote_pos = line.find('\"');
  762. while (quote_pos != std::string::npos) {
  763. line.replace(quote_pos, 1, "\\\"");
  764. quote_pos = line.find('\"', quote_pos + 2);
  765. }
  766. }