cmCPackPackageMakerGenerator.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 "cmCPackPackageMakerGenerator.h"
  11. #include "cmake.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmSystemTools.h"
  15. #include "cmMakefile.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmCPackComponentGroup.h"
  18. #include "cmCPackLog.h"
  19. #include <cmsys/SystemTools.hxx>
  20. #include <cmsys/Glob.hxx>
  21. #include <cmsys/FStream.hxx>
  22. //----------------------------------------------------------------------
  23. cmCPackPackageMakerGenerator::cmCPackPackageMakerGenerator()
  24. {
  25. this->PackageMakerVersion = 0.0;
  26. this->PackageCompatibilityVersion = 10.4;
  27. }
  28. //----------------------------------------------------------------------
  29. cmCPackPackageMakerGenerator::~cmCPackPackageMakerGenerator()
  30. {
  31. }
  32. //----------------------------------------------------------------------
  33. bool cmCPackPackageMakerGenerator::SupportsComponentInstallation() const
  34. {
  35. return this->PackageCompatibilityVersion >= 10.4;
  36. }
  37. //----------------------------------------------------------------------
  38. int cmCPackPackageMakerGenerator::CopyInstallScript(const char* resdir,
  39. const char* script,
  40. const char* name)
  41. {
  42. std::string dst = resdir;
  43. dst += "/";
  44. dst += name;
  45. cmSystemTools::CopyFileAlways(script, dst.c_str());
  46. cmSystemTools::SetPermissions(dst.c_str(),0777);
  47. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  48. "copy script : " << script << "\ninto " << dst.c_str() <<
  49. std::endl);
  50. return 1;
  51. }
  52. //----------------------------------------------------------------------
  53. int cmCPackPackageMakerGenerator::PackageFiles()
  54. {
  55. // TODO: Use toplevel
  56. // It is used! Is this an obsolete comment?
  57. std::string resDir; // Where this package's resources will go.
  58. std::string packageDirFileName
  59. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  60. if (this->Components.empty())
  61. {
  62. packageDirFileName += ".pkg";
  63. resDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  64. resDir += "/Resources";
  65. }
  66. else
  67. {
  68. packageDirFileName += ".mpkg";
  69. if ( !cmsys::SystemTools::MakeDirectory(packageDirFileName.c_str()))
  70. {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR,
  72. "unable to create package directory "
  73. << packageDirFileName << std::endl);
  74. return 0;
  75. }
  76. resDir = packageDirFileName;
  77. resDir += "/Contents";
  78. if ( !cmsys::SystemTools::MakeDirectory(resDir.c_str()))
  79. {
  80. cmCPackLogger(cmCPackLog::LOG_ERROR,
  81. "unable to create package subdirectory " << resDir
  82. << std::endl);
  83. return 0;
  84. }
  85. resDir += "/Resources";
  86. if ( !cmsys::SystemTools::MakeDirectory(resDir.c_str()))
  87. {
  88. cmCPackLogger(cmCPackLog::LOG_ERROR,
  89. "unable to create package subdirectory " << resDir
  90. << std::endl);
  91. return 0;
  92. }
  93. resDir += "/en.lproj";
  94. }
  95. const char* preflight = this->GetOption("CPACK_PREFLIGHT_SCRIPT");
  96. const char* postflight = this->GetOption("CPACK_POSTFLIGHT_SCRIPT");
  97. const char* postupgrade = this->GetOption("CPACK_POSTUPGRADE_SCRIPT");
  98. if(this->Components.empty())
  99. {
  100. // Create directory structure
  101. std::string preflightDirName = resDir + "/PreFlight";
  102. std::string postflightDirName = resDir + "/PostFlight";
  103. // if preflight or postflight scripts not there create directories
  104. // of the same name, I think this makes it work
  105. if(!preflight)
  106. {
  107. if ( !cmsys::SystemTools::MakeDirectory(preflightDirName.c_str()))
  108. {
  109. cmCPackLogger(cmCPackLog::LOG_ERROR,
  110. "Problem creating installer directory: "
  111. << preflightDirName.c_str() << std::endl);
  112. return 0;
  113. }
  114. }
  115. if(!postflight)
  116. {
  117. if ( !cmsys::SystemTools::MakeDirectory(postflightDirName.c_str()))
  118. {
  119. cmCPackLogger(cmCPackLog::LOG_ERROR,
  120. "Problem creating installer directory: "
  121. << postflightDirName.c_str() << std::endl);
  122. return 0;
  123. }
  124. }
  125. // if preflight, postflight, or postupgrade are set
  126. // then copy them into the resource directory and make
  127. // them executable
  128. if(preflight)
  129. {
  130. this->CopyInstallScript(resDir.c_str(),
  131. preflight,
  132. "preflight");
  133. }
  134. if(postflight)
  135. {
  136. this->CopyInstallScript(resDir.c_str(),
  137. postflight,
  138. "postflight");
  139. }
  140. if(postupgrade)
  141. {
  142. this->CopyInstallScript(resDir.c_str(),
  143. postupgrade,
  144. "postupgrade");
  145. }
  146. }
  147. else if(postflight)
  148. {
  149. // create a postflight component to house the script
  150. this->PostFlightComponent.Name = "PostFlight";
  151. this->PostFlightComponent.DisplayName = "PostFlight";
  152. this->PostFlightComponent.Description = "PostFlight";
  153. this->PostFlightComponent.IsHidden = true;
  154. // empty directory for pkg contents
  155. std::string packageDir = toplevel + "/" + PostFlightComponent.Name;
  156. if (!cmsys::SystemTools::MakeDirectory(packageDir.c_str()))
  157. {
  158. cmCPackLogger(cmCPackLog::LOG_ERROR,
  159. "Problem creating component packages directory: "
  160. << packageDir.c_str() << std::endl);
  161. return 0;
  162. }
  163. // create package
  164. std::string packageFileDir = packageDirFileName + "/Contents/Packages/";
  165. if (!cmsys::SystemTools::MakeDirectory(packageFileDir.c_str()))
  166. {
  167. cmCPackLogger(cmCPackLog::LOG_ERROR,
  168. "Problem creating component PostFlight Packages directory: "
  169. << packageFileDir.c_str() << std::endl);
  170. return 0;
  171. }
  172. std::string packageFile = packageFileDir +
  173. this->GetPackageName(PostFlightComponent);
  174. if (!this->GenerateComponentPackage(packageFile.c_str(),
  175. packageDir.c_str(),
  176. PostFlightComponent))
  177. {
  178. return 0;
  179. }
  180. // copy postflight script into resource directory of .pkg
  181. std::string resourceDir = packageFile + "/Contents/Resources";
  182. this->CopyInstallScript(resourceDir.c_str(),
  183. postflight,
  184. "postflight");
  185. }
  186. if (!this->Components.empty())
  187. {
  188. // Create the directory where component packages will be built.
  189. std::string basePackageDir = packageDirFileName;
  190. basePackageDir += "/Contents/Packages";
  191. if (!cmsys::SystemTools::MakeDirectory(basePackageDir.c_str()))
  192. {
  193. cmCPackLogger(cmCPackLog::LOG_ERROR,
  194. "Problem creating component packages directory: "
  195. << basePackageDir.c_str() << std::endl);
  196. return 0;
  197. }
  198. // Create the directory where downloaded component packages will
  199. // be placed.
  200. const char* userUploadDirectory =
  201. this->GetOption("CPACK_UPLOAD_DIRECTORY");
  202. std::string uploadDirectory;
  203. if (userUploadDirectory && *userUploadDirectory)
  204. {
  205. uploadDirectory = userUploadDirectory;
  206. }
  207. else
  208. {
  209. uploadDirectory= this->GetOption("CPACK_PACKAGE_DIRECTORY");
  210. uploadDirectory += "/CPackUploads";
  211. }
  212. // Create packages for each component
  213. bool warnedAboutDownloadCompatibility = false;
  214. std::map<std::string, cmCPackComponent>::iterator compIt;
  215. for (compIt = this->Components.begin(); compIt != this->Components.end();
  216. ++compIt)
  217. {
  218. std::string packageFile;
  219. if (compIt->second.IsDownloaded)
  220. {
  221. if (this->PackageCompatibilityVersion >= 10.5 &&
  222. this->PackageMakerVersion >= 3.0)
  223. {
  224. // Build this package within the upload directory.
  225. packageFile = uploadDirectory;
  226. if(!cmSystemTools::FileExists(uploadDirectory.c_str()))
  227. {
  228. if (!cmSystemTools::MakeDirectory(uploadDirectory.c_str()))
  229. {
  230. cmCPackLogger(cmCPackLog::LOG_ERROR,
  231. "Unable to create package upload directory "
  232. << uploadDirectory << std::endl);
  233. return 0;
  234. }
  235. }
  236. }
  237. else if (!warnedAboutDownloadCompatibility)
  238. {
  239. if (this->PackageCompatibilityVersion < 10.5)
  240. {
  241. cmCPackLogger(
  242. cmCPackLog::LOG_WARNING,
  243. "CPack warning: please set CPACK_OSX_PACKAGE_VERSION to 10.5 "
  244. "or greater enable downloaded packages. CPack will build a "
  245. "non-downloaded package."
  246. << std::endl);
  247. }
  248. if (this->PackageMakerVersion < 3)
  249. {
  250. cmCPackLogger(cmCPackLog::LOG_WARNING,
  251. "CPack warning: unable to build downloaded "
  252. "packages with PackageMaker versions prior "
  253. "to 3.0. CPack will build a non-downloaded package."
  254. << std::endl);
  255. }
  256. warnedAboutDownloadCompatibility = true;
  257. }
  258. }
  259. if (packageFile.empty())
  260. {
  261. // Build this package within the overall distribution
  262. // metapackage.
  263. packageFile = basePackageDir;
  264. // We're not downloading this component, even if the user
  265. // requested it.
  266. compIt->second.IsDownloaded = false;
  267. }
  268. packageFile += '/';
  269. packageFile += GetPackageName(compIt->second);
  270. std::string packageDir = toplevel;
  271. packageDir += '/';
  272. packageDir += compIt->first;
  273. if (!this->GenerateComponentPackage(packageFile.c_str(),
  274. packageDir.c_str(),
  275. compIt->second))
  276. {
  277. return 0;
  278. }
  279. }
  280. }
  281. this->SetOption("CPACK_MODULE_VERSION_SUFFIX", "");
  282. // Copy or create all of the resource files we need.
  283. if ( !this->CopyCreateResourceFile("License", resDir.c_str())
  284. || !this->CopyCreateResourceFile("ReadMe", resDir.c_str())
  285. || !this->CopyCreateResourceFile("Welcome", resDir.c_str())
  286. || !this->CopyResourcePlistFile("Info.plist")
  287. || !this->CopyResourcePlistFile("Description.plist") )
  288. {
  289. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
  290. << std::endl);
  291. return 0;
  292. }
  293. if (this->Components.empty())
  294. {
  295. // Use PackageMaker to build the package.
  296. cmOStringStream pkgCmd;
  297. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  298. << "\" -build -p \"" << packageDirFileName << "\"";
  299. if (this->Components.empty())
  300. {
  301. pkgCmd << " -f \"" << this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  302. }
  303. else
  304. {
  305. pkgCmd << " -mi \"" << this->GetOption("CPACK_TEMPORARY_DIRECTORY")
  306. << "/packages/";
  307. }
  308. pkgCmd << "\" -r \"" << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  309. << "/Resources\" -i \""
  310. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  311. << "/Info.plist\" -d \""
  312. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  313. << "/Description.plist\"";
  314. if ( this->PackageMakerVersion > 2.0 )
  315. {
  316. pkgCmd << " -v";
  317. }
  318. if (!RunPackageMaker(pkgCmd.str().c_str(), packageDirFileName.c_str()))
  319. return 0;
  320. }
  321. else
  322. {
  323. // We have built the package in place. Generate the
  324. // distribution.dist file to describe it for the installer.
  325. WriteDistributionFile(packageDirFileName.c_str());
  326. }
  327. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  328. tmpFile += "/hdiutilOutput.log";
  329. cmOStringStream dmgCmd;
  330. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
  331. << "\" create -ov -format UDZO -srcfolder \"" << packageDirFileName
  332. << "\" \"" << packageFileNames[0] << "\"";
  333. std::string output;
  334. int retVal = 1;
  335. int numTries = 10;
  336. bool res = false;
  337. while(numTries > 0)
  338. {
  339. res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  340. &retVal, 0, this->GeneratorVerbose,
  341. 0);
  342. if ( res && !retVal )
  343. {
  344. numTries = -1;
  345. break;
  346. }
  347. cmSystemTools::Delay(500);
  348. numTries--;
  349. }
  350. if ( !res || retVal )
  351. {
  352. cmGeneratedFileStream ofs(tmpFile.c_str());
  353. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  354. << "# Output:" << std::endl
  355. << output.c_str() << std::endl;
  356. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  357. << dmgCmd.str().c_str() << std::endl
  358. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  359. return 0;
  360. }
  361. return 1;
  362. }
  363. //----------------------------------------------------------------------
  364. int cmCPackPackageMakerGenerator::InitializeInternal()
  365. {
  366. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  367. "cmCPackPackageMakerGenerator::Initialize()" << std::endl);
  368. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  369. // Starting with Xcode 4.3, PackageMaker is a separate app, and you
  370. // can put it anywhere you want. So... use a variable for its location.
  371. // People who put it in unexpected places can use the variable to tell
  372. // us where it is.
  373. //
  374. // Use the following locations, in "most recent installation" order,
  375. // to search for the PackageMaker app. Assume people who copy it into
  376. // the new Xcode 4.3 app in "/Applications" will copy it into the nested
  377. // Applications folder inside the Xcode bundle itself. Or directly in
  378. // the "/Applications" directory.
  379. //
  380. // If found, save result in the CPACK_INSTALLER_PROGRAM variable.
  381. std::vector<std::string> paths;
  382. paths.push_back(
  383. "/Applications/Xcode.app/Contents/Applications"
  384. "/PackageMaker.app/Contents/MacOS");
  385. paths.push_back(
  386. "/Applications/Utilities"
  387. "/PackageMaker.app/Contents/MacOS");
  388. paths.push_back(
  389. "/Applications"
  390. "/PackageMaker.app/Contents/MacOS");
  391. paths.push_back(
  392. "/Developer/Applications/Utilities"
  393. "/PackageMaker.app/Contents/MacOS");
  394. paths.push_back(
  395. "/Developer/Applications"
  396. "/PackageMaker.app/Contents/MacOS");
  397. std::string pkgPath;
  398. const char *inst_program = this->GetOption("CPACK_INSTALLER_PROGRAM");
  399. if (inst_program && *inst_program)
  400. {
  401. pkgPath = inst_program;
  402. }
  403. else
  404. {
  405. pkgPath = cmSystemTools::FindProgram("PackageMaker", paths, false);
  406. if ( pkgPath.empty() )
  407. {
  408. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find PackageMaker compiler"
  409. << std::endl);
  410. return 0;
  411. }
  412. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  413. }
  414. // Get path to the real PackageMaker, not a symlink:
  415. pkgPath = cmSystemTools::GetRealPath(pkgPath.c_str());
  416. // Up from there to find the version.plist file in the "Contents" dir:
  417. std::string contents_dir;
  418. contents_dir = cmSystemTools::GetFilenamePath(pkgPath);
  419. contents_dir = cmSystemTools::GetFilenamePath(contents_dir);
  420. std::string versionFile = contents_dir + "/version.plist";
  421. if ( !cmSystemTools::FileExists(versionFile.c_str()) )
  422. {
  423. cmCPackLogger(cmCPackLog::LOG_ERROR,
  424. "Cannot find PackageMaker compiler version file: "
  425. << versionFile.c_str()
  426. << std::endl);
  427. return 0;
  428. }
  429. cmsys::ifstream ifs(versionFile.c_str());
  430. if ( !ifs )
  431. {
  432. cmCPackLogger(cmCPackLog::LOG_ERROR,
  433. "Cannot open PackageMaker compiler version file" << std::endl);
  434. return 0;
  435. }
  436. // Check the PackageMaker version
  437. cmsys::RegularExpression rexKey("<key>CFBundleShortVersionString</key>");
  438. cmsys::RegularExpression rexVersion("<string>([0-9]+.[0-9.]+)</string>");
  439. std::string line;
  440. bool foundKey = false;
  441. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  442. {
  443. if ( rexKey.find(line) )
  444. {
  445. foundKey = true;
  446. break;
  447. }
  448. }
  449. if ( !foundKey )
  450. {
  451. cmCPackLogger(cmCPackLog::LOG_ERROR,
  452. "Cannot find CFBundleShortVersionString in the PackageMaker compiler "
  453. "version file" << std::endl);
  454. return 0;
  455. }
  456. if ( !cmSystemTools::GetLineFromStream(ifs, line) ||
  457. !rexVersion.find(line) )
  458. {
  459. cmCPackLogger(cmCPackLog::LOG_ERROR,
  460. "Problem reading the PackageMaker compiler version file: "
  461. << versionFile.c_str() << std::endl);
  462. return 0;
  463. }
  464. this->PackageMakerVersion = atof(rexVersion.match(1).c_str());
  465. if ( this->PackageMakerVersion < 1.0 )
  466. {
  467. cmCPackLogger(cmCPackLog::LOG_ERROR, "Require PackageMaker 1.0 or higher"
  468. << std::endl);
  469. return 0;
  470. }
  471. cmCPackLogger(cmCPackLog::LOG_DEBUG, "PackageMaker version is: "
  472. << this->PackageMakerVersion << std::endl);
  473. // Determine the package compatibility version. If it wasn't
  474. // specified by the user, we define it based on which features the
  475. // user requested.
  476. const char *packageCompat = this->GetOption("CPACK_OSX_PACKAGE_VERSION");
  477. if (packageCompat && *packageCompat)
  478. {
  479. this->PackageCompatibilityVersion = atof(packageCompat);
  480. }
  481. else if (this->GetOption("CPACK_DOWNLOAD_SITE"))
  482. {
  483. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.5");
  484. this->PackageCompatibilityVersion = 10.5;
  485. }
  486. else if (this->GetOption("CPACK_COMPONENTS_ALL"))
  487. {
  488. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.4");
  489. this->PackageCompatibilityVersion = 10.4;
  490. }
  491. else
  492. {
  493. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.3");
  494. this->PackageCompatibilityVersion = 10.3;
  495. }
  496. std::vector<std::string> no_paths;
  497. pkgPath = cmSystemTools::FindProgram("hdiutil", no_paths, false);
  498. if ( pkgPath.empty() )
  499. {
  500. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  501. << std::endl);
  502. return 0;
  503. }
  504. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  505. pkgPath.c_str());
  506. return this->Superclass::InitializeInternal();
  507. }
  508. //----------------------------------------------------------------------
  509. bool cmCPackPackageMakerGenerator::CopyCreateResourceFile(
  510. const std::string& name,
  511. const std::string& dirName)
  512. {
  513. std::string uname = cmSystemTools::UpperCase(name);
  514. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  515. const char* inFileName = this->GetOption(cpackVar.c_str());
  516. if ( !inFileName )
  517. {
  518. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  519. << " not specified. It should point to "
  520. << (!name.empty() ? name : "<empty>")
  521. << ".rtf, " << name
  522. << ".html, or " << name << ".txt file" << std::endl);
  523. return false;
  524. }
  525. if ( !cmSystemTools::FileExists(inFileName) )
  526. {
  527. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  528. << (!name.empty() ? name : "<empty>")
  529. << " resource file: " << inFileName << std::endl);
  530. return false;
  531. }
  532. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  533. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  534. {
  535. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  536. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  537. << std::endl);
  538. return false;
  539. }
  540. std::string destFileName = dirName;
  541. destFileName += '/';
  542. destFileName += name + ext;
  543. // Set this so that distribution.dist gets the right name (without
  544. // the path).
  545. this->SetOption(("CPACK_RESOURCE_FILE_" + uname + "_NOPATH").c_str(),
  546. (name + ext).c_str());
  547. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  548. << (inFileName ? inFileName : "(NULL)")
  549. << " to " << destFileName.c_str() << std::endl);
  550. this->ConfigureFile(inFileName, destFileName.c_str());
  551. return true;
  552. }
  553. bool cmCPackPackageMakerGenerator::CopyResourcePlistFile(const char* name,
  554. const char* outName)
  555. {
  556. if (!outName)
  557. {
  558. outName = name;
  559. }
  560. std::string inFName = "CPack.";
  561. inFName += name;
  562. inFName += ".in";
  563. std::string inFileName = this->FindTemplate(inFName.c_str());
  564. if ( inFileName.empty() )
  565. {
  566. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  567. << inFName << std::endl);
  568. return false;
  569. }
  570. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  571. destFileName += "/";
  572. destFileName += outName;
  573. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  574. << inFileName.c_str() << " to " << destFileName.c_str() << std::endl);
  575. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  576. return true;
  577. }
  578. //----------------------------------------------------------------------
  579. bool cmCPackPackageMakerGenerator::RunPackageMaker(const char *command,
  580. const char *packageFile)
  581. {
  582. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  583. tmpFile += "/PackageMakerOutput.log";
  584. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  585. std::string output;
  586. int retVal = 1;
  587. bool res = cmSystemTools::RunSingleCommand(command, &output, &retVal, 0,
  588. this->GeneratorVerbose, 0);
  589. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running package maker"
  590. << std::endl);
  591. if ( !res || retVal )
  592. {
  593. cmGeneratedFileStream ofs(tmpFile.c_str());
  594. ofs << "# Run command: " << command << std::endl
  595. << "# Output:" << std::endl
  596. << output.c_str() << std::endl;
  597. cmCPackLogger(cmCPackLog::LOG_ERROR,
  598. "Problem running PackageMaker command: " << command
  599. << std::endl << "Please check " << tmpFile.c_str() << " for errors"
  600. << std::endl);
  601. return false;
  602. }
  603. // sometimes the command finishes but the directory is not yet
  604. // created, so try 10 times to see if it shows up
  605. int tries = 10;
  606. while(tries > 0 &&
  607. !cmSystemTools::FileExists(packageFile))
  608. {
  609. cmSystemTools::Delay(500);
  610. tries--;
  611. }
  612. if(!cmSystemTools::FileExists(packageFile))
  613. {
  614. cmCPackLogger(
  615. cmCPackLog::LOG_ERROR,
  616. "Problem running PackageMaker command: " << command
  617. << std::endl << "Package not created: " << packageFile
  618. << std::endl);
  619. return false;
  620. }
  621. return true;
  622. }
  623. //----------------------------------------------------------------------
  624. std::string
  625. cmCPackPackageMakerGenerator::GetPackageName(const cmCPackComponent& component)
  626. {
  627. if (component.ArchiveFile.empty())
  628. {
  629. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  630. packagesDir += ".dummy";
  631. cmOStringStream out;
  632. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir)
  633. << "-" << component.Name << ".pkg";
  634. return out.str();
  635. }
  636. else
  637. {
  638. return component.ArchiveFile + ".pkg";
  639. }
  640. }
  641. //----------------------------------------------------------------------
  642. bool
  643. cmCPackPackageMakerGenerator::
  644. GenerateComponentPackage(const char *packageFile,
  645. const char *packageDir,
  646. const cmCPackComponent& component)
  647. {
  648. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  649. "- Building component package: " <<
  650. packageFile << std::endl);
  651. // The command that will be used to run PackageMaker
  652. cmOStringStream pkgCmd;
  653. if (this->PackageCompatibilityVersion < 10.5 ||
  654. this->PackageMakerVersion < 3.0)
  655. {
  656. // Create Description.plist and Info.plist files for normal Mac OS
  657. // X packages, which work on Mac OS X 10.3 and newer.
  658. std::string descriptionFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  659. descriptionFile += '/' + component.Name + "-Description.plist";
  660. cmsys::ofstream out(descriptionFile.c_str());
  661. out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl
  662. << "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
  663. << "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" << std::endl
  664. << "<plist version=\"1.4\">" << std::endl
  665. << "<dict>" << std::endl
  666. << " <key>IFPkgDescriptionTitle</key>" << std::endl
  667. << " <string>" << component.DisplayName << "</string>" << std::endl
  668. << " <key>IFPkgDescriptionVersion</key>" << std::endl
  669. << " <string>" << this->GetOption("CPACK_PACKAGE_VERSION")
  670. << "</string>" << std::endl
  671. << " <key>IFPkgDescriptionDescription</key>" << std::endl
  672. << " <string>" + this->EscapeForXML(component.Description)
  673. << "</string>" << std::endl
  674. << "</dict>" << std::endl
  675. << "</plist>" << std::endl;
  676. out.close();
  677. // Create the Info.plist file for this component
  678. std::string moduleVersionSuffix = ".";
  679. moduleVersionSuffix += component.Name;
  680. this->SetOption("CPACK_MODULE_VERSION_SUFFIX",
  681. moduleVersionSuffix.c_str());
  682. std::string infoFileName = component.Name;
  683. infoFileName += "-Info.plist";
  684. if (!this->CopyResourcePlistFile("Info.plist", infoFileName.c_str()))
  685. {
  686. return false;
  687. }
  688. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  689. << "\" -build -p \"" << packageFile << "\""
  690. << " -f \"" << packageDir << "\""
  691. << " -i \"" << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  692. << "/" << infoFileName << "\""
  693. << " -d \"" << descriptionFile << "\"";
  694. }
  695. else
  696. {
  697. // Create a "flat" package on Mac OS X 10.5 and newer. Flat
  698. // packages are stored in a single file, rather than a directory
  699. // like normal packages, and can be downloaded by the installer
  700. // on-the-fly in Mac OS X 10.5 or newer. Thus, we need to create
  701. // flat packages when the packages will be downloaded on the fly.
  702. std::string pkgId = "com.";
  703. pkgId += this->GetOption("CPACK_PACKAGE_VENDOR");
  704. pkgId += '.';
  705. pkgId += this->GetOption("CPACK_PACKAGE_NAME");
  706. pkgId += '.';
  707. pkgId += component.Name;
  708. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  709. << "\" --root \"" << packageDir << "\""
  710. << " --id " << pkgId
  711. << " --target " << this->GetOption("CPACK_OSX_PACKAGE_VERSION")
  712. << " --out \"" << packageFile << "\"";
  713. }
  714. // Run PackageMaker
  715. return RunPackageMaker(pkgCmd.str().c_str(), packageFile);
  716. }
  717. //----------------------------------------------------------------------
  718. void
  719. cmCPackPackageMakerGenerator::
  720. WriteDistributionFile(const char* metapackageFile)
  721. {
  722. std::string distributionTemplate
  723. = this->FindTemplate("CPack.distribution.dist.in");
  724. if ( distributionTemplate.empty() )
  725. {
  726. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  727. << distributionTemplate << std::endl);
  728. return;
  729. }
  730. std::string distributionFile = metapackageFile;
  731. distributionFile += "/Contents/distribution.dist";
  732. // Create the choice outline, which provides a tree-based view of
  733. // the components in their groups.
  734. cmOStringStream choiceOut;
  735. choiceOut << "<choices-outline>" << std::endl;
  736. // Emit the outline for the groups
  737. std::map<std::string, cmCPackComponentGroup>::iterator groupIt;
  738. for (groupIt = this->ComponentGroups.begin();
  739. groupIt != this->ComponentGroups.end();
  740. ++groupIt)
  741. {
  742. if (groupIt->second.ParentGroup == 0)
  743. {
  744. CreateChoiceOutline(groupIt->second, choiceOut);
  745. }
  746. }
  747. // Emit the outline for the non-grouped components
  748. std::map<std::string, cmCPackComponent>::iterator compIt;
  749. for (compIt = this->Components.begin(); compIt != this->Components.end();
  750. ++compIt)
  751. {
  752. if (!compIt->second.Group)
  753. {
  754. choiceOut << "<line choice=\"" << compIt->first << "Choice\"></line>"
  755. << std::endl;
  756. }
  757. }
  758. if(!this->PostFlightComponent.Name.empty())
  759. {
  760. choiceOut << "<line choice=\"" << PostFlightComponent.Name
  761. << "Choice\"></line>" << std::endl;
  762. }
  763. choiceOut << "</choices-outline>" << std::endl;
  764. // Create the actual choices
  765. for (groupIt = this->ComponentGroups.begin();
  766. groupIt != this->ComponentGroups.end();
  767. ++groupIt)
  768. {
  769. CreateChoice(groupIt->second, choiceOut);
  770. }
  771. for (compIt = this->Components.begin(); compIt != this->Components.end();
  772. ++compIt)
  773. {
  774. CreateChoice(compIt->second, choiceOut);
  775. }
  776. if(!this->PostFlightComponent.Name.empty())
  777. {
  778. CreateChoice(PostFlightComponent, choiceOut);
  779. }
  780. this->SetOption("CPACK_PACKAGEMAKER_CHOICES", choiceOut.str().c_str());
  781. // Create the distribution.dist file in the metapackage to turn it
  782. // into a distribution package.
  783. this->ConfigureFile(distributionTemplate.c_str(),
  784. distributionFile.c_str());
  785. }
  786. //----------------------------------------------------------------------
  787. void
  788. cmCPackPackageMakerGenerator::
  789. CreateChoiceOutline(const cmCPackComponentGroup& group, cmOStringStream& out)
  790. {
  791. out << "<line choice=\"" << group.Name << "Choice\">" << std::endl;
  792. std::vector<cmCPackComponentGroup*>::const_iterator groupIt;
  793. for (groupIt = group.Subgroups.begin(); groupIt != group.Subgroups.end();
  794. ++groupIt)
  795. {
  796. CreateChoiceOutline(**groupIt, out);
  797. }
  798. std::vector<cmCPackComponent*>::const_iterator compIt;
  799. for (compIt = group.Components.begin(); compIt != group.Components.end();
  800. ++compIt)
  801. {
  802. out << " <line choice=\"" << (*compIt)->Name << "Choice\"></line>"
  803. << std::endl;
  804. }
  805. out << "</line>" << std::endl;
  806. }
  807. //----------------------------------------------------------------------
  808. void
  809. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponentGroup& group,
  810. cmOStringStream& out)
  811. {
  812. out << "<choice id=\"" << group.Name << "Choice\" "
  813. << "title=\"" << group.DisplayName << "\" "
  814. << "start_selected=\"true\" "
  815. << "start_enabled=\"true\" "
  816. << "start_visible=\"true\" ";
  817. if (!group.Description.empty())
  818. {
  819. out << "description=\"" << EscapeForXML(group.Description)
  820. << "\"";
  821. }
  822. out << "></choice>" << std::endl;
  823. }
  824. //----------------------------------------------------------------------
  825. void
  826. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponent& component,
  827. cmOStringStream& out)
  828. {
  829. std::string packageId = "com.";
  830. packageId += this->GetOption("CPACK_PACKAGE_VENDOR");
  831. packageId += '.';
  832. packageId += this->GetOption("CPACK_PACKAGE_NAME");
  833. packageId += '.';
  834. packageId += component.Name;
  835. out << "<choice id=\"" << component.Name << "Choice\" "
  836. << "title=\"" << component.DisplayName << "\" "
  837. << "start_selected=\""
  838. << (component.IsDisabledByDefault &&
  839. !component.IsRequired? "false" : "true")
  840. << "\" "
  841. << "start_enabled=\""
  842. << (component.IsRequired? "false" : "true")
  843. << "\" "
  844. << "start_visible=\"" << (component.IsHidden? "false" : "true") << "\" ";
  845. if (!component.Description.empty())
  846. {
  847. out << "description=\"" << EscapeForXML(component.Description)
  848. << "\" ";
  849. }
  850. if (!component.Dependencies.empty() ||
  851. !component.ReverseDependencies.empty())
  852. {
  853. // The "selected" expression is evaluated each time any choice is
  854. // selected, for all choices *except* the one that the user
  855. // selected. A component is marked selected if it has been
  856. // selected (my.choice.selected in Javascript) and all of the
  857. // components it depends on have been selected (transitively) or
  858. // if any of the components that depend on it have been selected
  859. // (transitively). Assume that we have components A, B, C, D, and
  860. // E, where each component depends on the previous component (B
  861. // depends on A, C depends on B, D depends on C, and E depends on
  862. // D). The expression we build for the component C will be
  863. // my.choice.selected && B && A || D || E
  864. // This way, selecting C will automatically select everything it depends
  865. // on (B and A), while selecting something that depends on C--either D
  866. // or E--will automatically cause C to get selected.
  867. out << "selected=\"my.choice.selected";
  868. std::set<const cmCPackComponent *> visited;
  869. AddDependencyAttributes(component, visited, out);
  870. visited.clear();
  871. AddReverseDependencyAttributes(component, visited, out);
  872. out << "\"";
  873. }
  874. out << ">" << std::endl;
  875. out << " <pkg-ref id=\"" << packageId << "\"></pkg-ref>" << std::endl;
  876. out << "</choice>" << std::endl;
  877. // Create a description of the package associated with this
  878. // component.
  879. std::string relativePackageLocation = "Contents/Packages/";
  880. relativePackageLocation += this->GetPackageName(component);
  881. // Determine the installed size of the package.
  882. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  883. dirName += '/';
  884. dirName += component.Name;
  885. unsigned long installedSize
  886. = component.GetInstalledSizeInKbytes(dirName.c_str());
  887. out << "<pkg-ref id=\"" << packageId << "\" "
  888. << "version=\"" << this->GetOption("CPACK_PACKAGE_VERSION") << "\" "
  889. << "installKBytes=\"" << installedSize << "\" "
  890. << "auth=\"Admin\" onConclusion=\"None\">";
  891. if (component.IsDownloaded)
  892. {
  893. out << this->GetOption("CPACK_DOWNLOAD_SITE")
  894. << this->GetPackageName(component);
  895. }
  896. else
  897. {
  898. out << "file:./" << relativePackageLocation;
  899. }
  900. out << "</pkg-ref>" << std::endl;
  901. }
  902. //----------------------------------------------------------------------
  903. void
  904. cmCPackPackageMakerGenerator::
  905. AddDependencyAttributes(const cmCPackComponent& component,
  906. std::set<const cmCPackComponent *>& visited,
  907. cmOStringStream& out)
  908. {
  909. if (visited.find(&component) != visited.end())
  910. {
  911. return;
  912. }
  913. visited.insert(&component);
  914. std::vector<cmCPackComponent *>::const_iterator dependIt;
  915. for (dependIt = component.Dependencies.begin();
  916. dependIt != component.Dependencies.end();
  917. ++dependIt)
  918. {
  919. out << " &amp;&amp; choices['" <<
  920. (*dependIt)->Name << "Choice'].selected";
  921. AddDependencyAttributes(**dependIt, visited, out);
  922. }
  923. }
  924. //----------------------------------------------------------------------
  925. void
  926. cmCPackPackageMakerGenerator::
  927. AddReverseDependencyAttributes(const cmCPackComponent& component,
  928. std::set<const cmCPackComponent *>& visited,
  929. cmOStringStream& out)
  930. {
  931. if (visited.find(&component) != visited.end())
  932. {
  933. return;
  934. }
  935. visited.insert(&component);
  936. std::vector<cmCPackComponent *>::const_iterator dependIt;
  937. for (dependIt = component.ReverseDependencies.begin();
  938. dependIt != component.ReverseDependencies.end();
  939. ++dependIt)
  940. {
  941. out << " || choices['" << (*dependIt)->Name << "Choice'].selected";
  942. AddReverseDependencyAttributes(**dependIt, visited, out);
  943. }
  944. }
  945. //----------------------------------------------------------------------
  946. std::string cmCPackPackageMakerGenerator::EscapeForXML(std::string str)
  947. {
  948. cmSystemTools::ReplaceString(str, "&", "&amp;");
  949. cmSystemTools::ReplaceString(str, "<", "&lt;");
  950. cmSystemTools::ReplaceString(str, ">", "&gt;");
  951. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  952. return str;
  953. }