cmCPackPackageMakerGenerator.cxx 32 KB

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