cmCPackDragNDropGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackDragNDropGenerator.h"
  4. #include "cmCPackGenerator.h"
  5. #include "cmCPackLog.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmSystemTools.h"
  8. #include "cmsys/FStream.hxx"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <algorithm>
  11. #include <iomanip>
  12. #include <map>
  13. #include <stdlib.h>
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #ifdef HAVE_CoreServices
  16. // For the old LocaleStringToLangAndRegionCodes() function, to convert
  17. // to the old Script Manager RegionCode values needed for the 'LPic' data
  18. // structure used for generating multi-lingual SLAs.
  19. #include <CoreServices/CoreServices.h>
  20. #endif
  21. static const char* SLAHeader =
  22. "data 'LPic' (5000) {\n"
  23. " $\"0002 0011 0003 0001 0000 0000 0002 0000\"\n"
  24. " $\"0008 0003 0000 0001 0004 0000 0004 0005\"\n"
  25. " $\"0000 000E 0006 0001 0005 0007 0000 0007\"\n"
  26. " $\"0008 0000 0047 0009 0000 0034 000A 0001\"\n"
  27. " $\"0035 000B 0001 0020 000C 0000 0011 000D\"\n"
  28. " $\"0000 005B 0004 0000 0033 000F 0001 000C\"\n"
  29. " $\"0010 0000 000B 000E 0000\"\n"
  30. "};\n"
  31. "\n";
  32. static const char* SLASTREnglish =
  33. "resource 'STR#' (5002, \"English\") {\n"
  34. " {\n"
  35. " \"English\",\n"
  36. " \"Agree\",\n"
  37. " \"Disagree\",\n"
  38. " \"Print\",\n"
  39. " \"Save...\",\n"
  40. " \"You agree to the License Agreement terms when you click \"\n"
  41. " \"the \\\"Agree\\\" button.\",\n"
  42. " \"Software License Agreement\",\n"
  43. " \"This text cannot be saved. This disk may be full or locked, "
  44. "or the \"\n"
  45. " \"file may be locked.\",\n"
  46. " \"Unable to print. Make sure you have selected a printer.\"\n"
  47. " }\n"
  48. "};\n"
  49. "\n";
  50. cmCPackDragNDropGenerator::cmCPackDragNDropGenerator()
  51. : singleLicense(false)
  52. {
  53. // default to one package file for components
  54. this->componentPackageMethod = ONE_PACKAGE;
  55. }
  56. cmCPackDragNDropGenerator::~cmCPackDragNDropGenerator()
  57. {
  58. }
  59. int cmCPackDragNDropGenerator::InitializeInternal()
  60. {
  61. // Starting with Xcode 4.3, look in "/Applications/Xcode.app" first:
  62. //
  63. std::vector<std::string> paths;
  64. paths.push_back("/Applications/Xcode.app/Contents/Developer/Tools");
  65. paths.push_back("/Developer/Tools");
  66. const std::string hdiutil_path =
  67. cmSystemTools::FindProgram("hdiutil", std::vector<std::string>(), false);
  68. if (hdiutil_path.empty()) {
  69. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate hdiutil command"
  70. << std::endl);
  71. return 0;
  72. }
  73. this->SetOptionIfNotSet("CPACK_COMMAND_HDIUTIL", hdiutil_path.c_str());
  74. const std::string setfile_path =
  75. cmSystemTools::FindProgram("SetFile", paths, false);
  76. if (setfile_path.empty()) {
  77. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate SetFile command"
  78. << std::endl);
  79. return 0;
  80. }
  81. this->SetOptionIfNotSet("CPACK_COMMAND_SETFILE", setfile_path.c_str());
  82. const std::string rez_path = cmSystemTools::FindProgram("Rez", paths, false);
  83. if (rez_path.empty()) {
  84. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate Rez command"
  85. << std::endl);
  86. return 0;
  87. }
  88. this->SetOptionIfNotSet("CPACK_COMMAND_REZ", rez_path.c_str());
  89. if (this->IsSet("CPACK_DMG_SLA_DIR")) {
  90. slaDirectory = this->GetOption("CPACK_DMG_SLA_DIR");
  91. if (!slaDirectory.empty() && this->IsSet("CPACK_RESOURCE_FILE_LICENSE")) {
  92. std::string license_file =
  93. this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  94. if (!license_file.empty() &&
  95. (license_file.find("CPack.GenericLicense.txt") ==
  96. std::string::npos)) {
  97. cmCPackLogger(
  98. cmCPackLog::LOG_OUTPUT,
  99. "Both CPACK_DMG_SLA_DIR and CPACK_RESOURCE_FILE_LICENSE specified, "
  100. "using CPACK_RESOURCE_FILE_LICENSE as a license for all languages."
  101. << std::endl);
  102. singleLicense = true;
  103. }
  104. }
  105. if (!this->IsSet("CPACK_DMG_SLA_LANGUAGES")) {
  106. cmCPackLogger(cmCPackLog::LOG_ERROR,
  107. "CPACK_DMG_SLA_DIR set but no languages defined "
  108. "(set CPACK_DMG_SLA_LANGUAGES)"
  109. << std::endl);
  110. return 0;
  111. }
  112. if (!cmSystemTools::FileExists(slaDirectory, false)) {
  113. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_DMG_SLA_DIR does not exist"
  114. << std::endl);
  115. return 0;
  116. }
  117. std::vector<std::string> languages;
  118. cmSystemTools::ExpandListArgument(
  119. this->GetOption("CPACK_DMG_SLA_LANGUAGES"), languages);
  120. if (languages.empty()) {
  121. cmCPackLogger(cmCPackLog::LOG_ERROR,
  122. "CPACK_DMG_SLA_LANGUAGES set but empty" << std::endl);
  123. return 0;
  124. }
  125. for (size_t i = 0; i < languages.size(); ++i) {
  126. std::string license = slaDirectory + "/" + languages[i] + ".license.txt";
  127. if (!singleLicense && !cmSystemTools::FileExists(license)) {
  128. cmCPackLogger(cmCPackLog::LOG_ERROR, "Missing license file "
  129. << languages[i] << ".license.txt" << std::endl);
  130. return 0;
  131. }
  132. std::string menu = slaDirectory + "/" + languages[i] + ".menu.txt";
  133. if (!cmSystemTools::FileExists(menu)) {
  134. cmCPackLogger(cmCPackLog::LOG_ERROR, "Missing menu file "
  135. << languages[i] << ".menu.txt" << std::endl);
  136. return 0;
  137. }
  138. }
  139. }
  140. return this->Superclass::InitializeInternal();
  141. }
  142. const char* cmCPackDragNDropGenerator::GetOutputExtension()
  143. {
  144. return ".dmg";
  145. }
  146. int cmCPackDragNDropGenerator::PackageFiles()
  147. {
  148. // gather which directories to make dmg files for
  149. // multiple directories occur if packaging components or groups separately
  150. // monolith
  151. if (this->Components.empty()) {
  152. return this->CreateDMG(toplevel, packageFileNames[0]);
  153. }
  154. // component install
  155. std::vector<std::string> package_files;
  156. std::map<std::string, cmCPackComponent>::iterator compIt;
  157. for (compIt = this->Components.begin(); compIt != this->Components.end();
  158. ++compIt) {
  159. std::string name = GetComponentInstallDirNameSuffix(compIt->first);
  160. package_files.push_back(name);
  161. }
  162. std::sort(package_files.begin(), package_files.end());
  163. package_files.erase(std::unique(package_files.begin(), package_files.end()),
  164. package_files.end());
  165. // loop to create dmg files
  166. packageFileNames.clear();
  167. for (size_t i = 0; i < package_files.size(); i++) {
  168. std::string full_package_name = std::string(toplevel) + std::string("/");
  169. if (package_files[i] == "ALL_IN_ONE") {
  170. full_package_name += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  171. } else {
  172. full_package_name += package_files[i];
  173. }
  174. full_package_name += std::string(GetOutputExtension());
  175. packageFileNames.push_back(full_package_name);
  176. std::string src_dir = toplevel;
  177. src_dir += "/";
  178. src_dir += package_files[i];
  179. if (0 == this->CreateDMG(src_dir, full_package_name)) {
  180. return 0;
  181. }
  182. }
  183. return 1;
  184. }
  185. bool cmCPackDragNDropGenerator::CopyFile(std::ostringstream& source,
  186. std::ostringstream& target)
  187. {
  188. if (!cmSystemTools::CopyFileIfDifferent(source.str().c_str(),
  189. target.str().c_str())) {
  190. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error copying "
  191. << source.str() << " to " << target.str() << std::endl);
  192. return false;
  193. }
  194. return true;
  195. }
  196. bool cmCPackDragNDropGenerator::CreateEmptyFile(std::ostringstream& target,
  197. size_t size)
  198. {
  199. cmsys::ofstream fout(target.str().c_str(), std::ios::out | std::ios::binary);
  200. if (!fout) {
  201. return false;
  202. } else {
  203. // Seek to desired size - 1 byte
  204. fout.seekp(size - 1, std::ios::beg);
  205. char byte = 0;
  206. // Write one byte to ensure file grows
  207. fout.write(&byte, 1);
  208. }
  209. return true;
  210. }
  211. bool cmCPackDragNDropGenerator::RunCommand(std::ostringstream& command,
  212. std::string* output)
  213. {
  214. int exit_code = 1;
  215. bool result = cmSystemTools::RunSingleCommand(command.str().c_str(), output,
  216. output, &exit_code, nullptr,
  217. this->GeneratorVerbose, 0);
  218. if (!result || exit_code) {
  219. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error executing: " << command.str()
  220. << std::endl);
  221. return false;
  222. }
  223. return true;
  224. }
  225. int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir,
  226. const std::string& output_file)
  227. {
  228. // Get optional arguments ...
  229. const std::string cpack_package_icon = this->GetOption("CPACK_PACKAGE_ICON")
  230. ? this->GetOption("CPACK_PACKAGE_ICON")
  231. : "";
  232. const std::string cpack_dmg_volume_name =
  233. this->GetOption("CPACK_DMG_VOLUME_NAME")
  234. ? this->GetOption("CPACK_DMG_VOLUME_NAME")
  235. : this->GetOption("CPACK_PACKAGE_FILE_NAME");
  236. const std::string cpack_dmg_format = this->GetOption("CPACK_DMG_FORMAT")
  237. ? this->GetOption("CPACK_DMG_FORMAT")
  238. : "UDZO";
  239. // Get optional arguments ...
  240. std::string cpack_license_file =
  241. this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  242. ? this->GetOption("CPACK_RESOURCE_FILE_LICENSE")
  243. : "";
  244. const std::string cpack_dmg_background_image =
  245. this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  246. ? this->GetOption("CPACK_DMG_BACKGROUND_IMAGE")
  247. : "";
  248. const std::string cpack_dmg_ds_store = this->GetOption("CPACK_DMG_DS_STORE")
  249. ? this->GetOption("CPACK_DMG_DS_STORE")
  250. : "";
  251. const std::string cpack_dmg_languages =
  252. this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  253. ? this->GetOption("CPACK_DMG_SLA_LANGUAGES")
  254. : "";
  255. const std::string cpack_dmg_ds_store_setup_script =
  256. this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  257. ? this->GetOption("CPACK_DMG_DS_STORE_SETUP_SCRIPT")
  258. : "";
  259. const bool cpack_dmg_disable_applications_symlink =
  260. this->IsOn("CPACK_DMG_DISABLE_APPLICATIONS_SYMLINK");
  261. // only put license on dmg if is user provided
  262. if (!cpack_license_file.empty() &&
  263. cpack_license_file.find("CPack.GenericLicense.txt") !=
  264. std::string::npos) {
  265. cpack_license_file = "";
  266. }
  267. // use sla_dir if both sla_dir and license_file are set
  268. if (!cpack_license_file.empty() && !slaDirectory.empty() && !singleLicense) {
  269. cpack_license_file = "";
  270. }
  271. // The staging directory contains everything that will end-up inside the
  272. // final disk image ...
  273. std::ostringstream staging;
  274. staging << src_dir;
  275. // Add a symlink to /Applications so users can drag-and-drop the bundle
  276. // into it unless this behaviour was disabled
  277. if (!cpack_dmg_disable_applications_symlink) {
  278. std::ostringstream application_link;
  279. application_link << staging.str() << "/Applications";
  280. cmSystemTools::CreateSymlink("/Applications", application_link.str());
  281. }
  282. // Optionally add a custom volume icon ...
  283. if (!cpack_package_icon.empty()) {
  284. std::ostringstream package_icon_source;
  285. package_icon_source << cpack_package_icon;
  286. std::ostringstream package_icon_destination;
  287. package_icon_destination << staging.str() << "/.VolumeIcon.icns";
  288. if (!this->CopyFile(package_icon_source, package_icon_destination)) {
  289. cmCPackLogger(cmCPackLog::LOG_ERROR,
  290. "Error copying disk volume icon. "
  291. "Check the value of CPACK_PACKAGE_ICON."
  292. << std::endl);
  293. return 0;
  294. }
  295. }
  296. // Optionally add a custom .DS_Store file
  297. // (e.g. for setting background/layout) ...
  298. if (!cpack_dmg_ds_store.empty()) {
  299. std::ostringstream package_settings_source;
  300. package_settings_source << cpack_dmg_ds_store;
  301. std::ostringstream package_settings_destination;
  302. package_settings_destination << staging.str() << "/.DS_Store";
  303. if (!this->CopyFile(package_settings_source,
  304. package_settings_destination)) {
  305. cmCPackLogger(cmCPackLog::LOG_ERROR,
  306. "Error copying disk volume settings file. "
  307. "Check the value of CPACK_DMG_DS_STORE."
  308. << std::endl);
  309. return 0;
  310. }
  311. }
  312. // Optionally add a custom background image ...
  313. // Make sure the background file type is the same as the custom image
  314. // and that the file is hidden so it doesn't show up.
  315. if (!cpack_dmg_background_image.empty()) {
  316. const std::string extension =
  317. cmSystemTools::GetFilenameLastExtension(cpack_dmg_background_image);
  318. std::ostringstream package_background_source;
  319. package_background_source << cpack_dmg_background_image;
  320. std::ostringstream package_background_destination;
  321. package_background_destination << staging.str()
  322. << "/.background/background" << extension;
  323. if (!this->CopyFile(package_background_source,
  324. package_background_destination)) {
  325. cmCPackLogger(cmCPackLog::LOG_ERROR,
  326. "Error copying disk volume background image. "
  327. "Check the value of CPACK_DMG_BACKGROUND_IMAGE."
  328. << std::endl);
  329. return 0;
  330. }
  331. }
  332. bool remount_image =
  333. !cpack_package_icon.empty() || !cpack_dmg_ds_store_setup_script.empty();
  334. std::string temp_image_format = "UDZO";
  335. // Create 1 MB dummy padding file in staging area when we need to remount
  336. // image, so we have enough space for storing changes ...
  337. if (remount_image) {
  338. std::ostringstream dummy_padding;
  339. dummy_padding << staging.str() << "/.dummy-padding-file";
  340. if (!this->CreateEmptyFile(dummy_padding, 1048576)) {
  341. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error creating dummy padding file."
  342. << std::endl);
  343. return 0;
  344. }
  345. temp_image_format = "UDRW";
  346. }
  347. // Create a temporary read-write disk image ...
  348. std::string temp_image = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  349. temp_image += "/temp.dmg";
  350. std::ostringstream temp_image_command;
  351. temp_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  352. temp_image_command << " create";
  353. temp_image_command << " -ov";
  354. temp_image_command << " -srcfolder \"" << staging.str() << "\"";
  355. temp_image_command << " -volname \"" << cpack_dmg_volume_name << "\"";
  356. temp_image_command << " -format " << temp_image_format;
  357. temp_image_command << " \"" << temp_image << "\"";
  358. if (!this->RunCommand(temp_image_command)) {
  359. cmCPackLogger(cmCPackLog::LOG_ERROR,
  360. "Error generating temporary disk image." << std::endl);
  361. return 0;
  362. }
  363. if (remount_image) {
  364. // Store that we have a failure so that we always unmount the image
  365. // before we exit.
  366. bool had_error = false;
  367. std::ostringstream attach_command;
  368. attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  369. attach_command << " attach";
  370. attach_command << " \"" << temp_image << "\"";
  371. std::string attach_output;
  372. if (!this->RunCommand(attach_command, &attach_output)) {
  373. cmCPackLogger(cmCPackLog::LOG_ERROR,
  374. "Error attaching temporary disk image." << std::endl);
  375. return 0;
  376. }
  377. cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*");
  378. mountpoint_regex.find(attach_output.c_str());
  379. std::string const temp_mount = mountpoint_regex.match(1);
  380. std::string const temp_mount_name =
  381. temp_mount.substr(sizeof("/Volumes/") - 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. << " \"" << temp_mount_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. nullptr, languages[i].c_str(), kCFStringEncodingUTF8);
  471. CFStringRef iso_language =
  472. CFLocaleCreateCanonicalLanguageIdentifierFromString(
  473. nullptr, 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 = static_cast<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. if (temp_image_format != "UDZO") {
  548. temp_image_format = "UDZO";
  549. // convert to UDZO to enable unflatten/flatten
  550. std::string temp_udzo = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  551. temp_udzo += "/temp-udzo.dmg";
  552. std::ostringstream udco_image_command;
  553. udco_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  554. udco_image_command << " convert \"" << temp_image << "\"";
  555. udco_image_command << " -format UDZO";
  556. udco_image_command << " -ov -o \"" << temp_udzo << "\"";
  557. if (!this->RunCommand(udco_image_command, &error)) {
  558. cmCPackLogger(cmCPackLog::LOG_ERROR,
  559. "Error converting to UDCO dmg for adding SLA."
  560. << std::endl
  561. << error << std::endl);
  562. return 0;
  563. }
  564. temp_image = temp_udzo;
  565. }
  566. // unflatten dmg
  567. std::ostringstream unflatten_command;
  568. unflatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  569. unflatten_command << " unflatten ";
  570. unflatten_command << "\"" << temp_image << "\"";
  571. if (!this->RunCommand(unflatten_command, &error)) {
  572. cmCPackLogger(cmCPackLog::LOG_ERROR,
  573. "Error unflattening dmg for adding SLA." << std::endl
  574. << error
  575. << std::endl);
  576. return 0;
  577. }
  578. // Rez the SLA
  579. std::ostringstream embed_sla_command;
  580. embed_sla_command << this->GetOption("CPACK_COMMAND_REZ");
  581. const char* sysroot = this->GetOption("CPACK_OSX_SYSROOT");
  582. if (sysroot && sysroot[0] != '\0') {
  583. embed_sla_command << " -isysroot \"" << sysroot << "\"";
  584. }
  585. embed_sla_command << " \"" << sla_r << "\"";
  586. embed_sla_command << " -a -o ";
  587. embed_sla_command << "\"" << temp_image << "\"";
  588. if (!this->RunCommand(embed_sla_command, &error)) {
  589. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding SLA." << std::endl
  590. << error
  591. << std::endl);
  592. return 0;
  593. }
  594. // flatten dmg
  595. std::ostringstream flatten_command;
  596. flatten_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  597. flatten_command << " flatten ";
  598. flatten_command << "\"" << temp_image << "\"";
  599. if (!this->RunCommand(flatten_command, &error)) {
  600. cmCPackLogger(cmCPackLog::LOG_ERROR,
  601. "Error flattening dmg for adding SLA." << std::endl
  602. << error
  603. << std::endl);
  604. return 0;
  605. }
  606. }
  607. // Create the final compressed read-only disk image ...
  608. std::ostringstream final_image_command;
  609. final_image_command << this->GetOption("CPACK_COMMAND_HDIUTIL");
  610. final_image_command << " convert \"" << temp_image << "\"";
  611. final_image_command << " -format ";
  612. final_image_command << cpack_dmg_format;
  613. final_image_command << " -imagekey";
  614. final_image_command << " zlib-level=9";
  615. final_image_command << " -o \"" << output_file << "\"";
  616. if (!this->RunCommand(final_image_command)) {
  617. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error compressing disk image."
  618. << std::endl);
  619. return 0;
  620. }
  621. return 1;
  622. }
  623. bool cmCPackDragNDropGenerator::SupportsComponentInstallation() const
  624. {
  625. return true;
  626. }
  627. std::string cmCPackDragNDropGenerator::GetComponentInstallDirNameSuffix(
  628. const std::string& componentName)
  629. {
  630. // we want to group components together that go in the same dmg package
  631. std::string package_file_name = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  632. // we have 3 mutually exclusive modes to work in
  633. // 1. all components in one package
  634. // 2. each group goes in its own package with left over
  635. // components in their own package
  636. // 3. ignore groups - if grouping is defined, it is ignored
  637. // and each component goes in its own package
  638. if (this->componentPackageMethod == ONE_PACKAGE) {
  639. return "ALL_IN_ONE";
  640. }
  641. if (this->componentPackageMethod == ONE_PACKAGE_PER_GROUP) {
  642. // We have to find the name of the COMPONENT GROUP
  643. // the current COMPONENT belongs to.
  644. std::string groupVar =
  645. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  646. const char* _groupName = GetOption(groupVar);
  647. if (_groupName) {
  648. std::string groupName = _groupName;
  649. groupName =
  650. GetComponentPackageFileName(package_file_name, groupName, true);
  651. return groupName;
  652. }
  653. }
  654. return GetComponentPackageFileName(package_file_name, componentName, false);
  655. }
  656. bool cmCPackDragNDropGenerator::WriteLicense(
  657. cmGeneratedFileStream& outputStream, int licenseNumber,
  658. std::string licenseLanguage, const std::string& licenseFile,
  659. std::string* error)
  660. {
  661. if (!licenseFile.empty() && !singleLicense) {
  662. licenseNumber = 5002;
  663. licenseLanguage = "English";
  664. }
  665. // License header
  666. outputStream << "data 'TEXT' (" << licenseNumber << ", \"" << licenseLanguage
  667. << "\") {\n";
  668. // License body
  669. std::string actual_license = !licenseFile.empty()
  670. ? licenseFile
  671. : (slaDirectory + "/" + licenseLanguage + ".license.txt");
  672. cmsys::ifstream license_ifs;
  673. license_ifs.open(actual_license.c_str());
  674. if (license_ifs.is_open()) {
  675. while (license_ifs.good()) {
  676. std::string line;
  677. std::getline(license_ifs, line);
  678. if (!line.empty()) {
  679. EscapeQuotesAndBackslashes(line);
  680. std::vector<std::string> lines;
  681. if (!this->BreakLongLine(line, lines, error)) {
  682. return false;
  683. }
  684. for (size_t i = 0; i < lines.size(); ++i) {
  685. outputStream << " \"" << lines[i] << "\"\n";
  686. }
  687. }
  688. outputStream << " \"\\n\"\n";
  689. }
  690. license_ifs.close();
  691. }
  692. // End of License
  693. outputStream << "};\n\n";
  694. if (!licenseFile.empty() && !singleLicense) {
  695. outputStream << SLASTREnglish;
  696. } else {
  697. // Menu header
  698. outputStream << "resource 'STR#' (" << licenseNumber << ", \""
  699. << licenseLanguage << "\") {\n";
  700. outputStream << " {\n";
  701. // Menu body
  702. cmsys::ifstream menu_ifs;
  703. menu_ifs.open(
  704. (slaDirectory + "/" + licenseLanguage + ".menu.txt").c_str());
  705. if (menu_ifs.is_open()) {
  706. size_t lines_written = 0;
  707. while (menu_ifs.good()) {
  708. // Lines written from original file, not from broken up lines
  709. std::string line;
  710. std::getline(menu_ifs, line);
  711. if (!line.empty()) {
  712. EscapeQuotesAndBackslashes(line);
  713. std::vector<std::string> lines;
  714. if (!this->BreakLongLine(line, lines, error)) {
  715. return false;
  716. }
  717. for (size_t i = 0; i < lines.size(); ++i) {
  718. std::string comma;
  719. // We need a comma after every complete string,
  720. // but not on the very last line
  721. if (lines_written != 8 && i == lines.size() - 1) {
  722. comma = ",";
  723. } else {
  724. comma = "";
  725. }
  726. outputStream << " \"" << lines[i] << "\"" << comma << "\n";
  727. }
  728. ++lines_written;
  729. }
  730. }
  731. menu_ifs.close();
  732. }
  733. // End of menu
  734. outputStream << " }\n";
  735. outputStream << "};\n";
  736. outputStream << "\n";
  737. }
  738. return true;
  739. }
  740. bool cmCPackDragNDropGenerator::BreakLongLine(const std::string& line,
  741. std::vector<std::string>& lines,
  742. std::string* error)
  743. {
  744. const size_t max_line_length = 512;
  745. for (size_t i = 0; i < line.size(); i += max_line_length) {
  746. size_t line_length = max_line_length;
  747. if (i + line_length > line.size()) {
  748. line_length = line.size() - i;
  749. } else
  750. while (line_length > 0 && line[i + line_length - 1] != ' ') {
  751. line_length = line_length - 1;
  752. }
  753. if (line_length == 0) {
  754. *error = "Please make sure there are no words "
  755. "(or character sequences not broken up by spaces or newlines) "
  756. "in your license file which are more than 512 characters long.";
  757. return false;
  758. }
  759. lines.push_back(line.substr(i, line_length));
  760. }
  761. return true;
  762. }
  763. void cmCPackDragNDropGenerator::EscapeQuotesAndBackslashes(std::string& line)
  764. {
  765. std::string::size_type backslash_pos = line.find('\\');
  766. while (backslash_pos != std::string::npos) {
  767. line.replace(backslash_pos, 1, "\\\\");
  768. backslash_pos = line.find('\\', backslash_pos + 2);
  769. }
  770. std::string::size_type quote_pos = line.find('\"');
  771. while (quote_pos != std::string::npos) {
  772. line.replace(quote_pos, 1, "\\\"");
  773. quote_pos = line.find('\"', quote_pos + 2);
  774. }
  775. }