cmCPackDragNDropGenerator.cxx 30 KB

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