cmCPackPackageMakerGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 = 4;
  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. }
  303. numTries--;
  304. }
  305. if ( !res || retVal )
  306. {
  307. cmGeneratedFileStream ofs(tmpFile.c_str());
  308. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  309. << "# Output:" << std::endl
  310. << output.c_str() << std::endl;
  311. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  312. << dmgCmd.str().c_str() << std::endl
  313. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  314. return 0;
  315. }
  316. return 1;
  317. }
  318. //----------------------------------------------------------------------
  319. int cmCPackPackageMakerGenerator::InitializeInternal()
  320. {
  321. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  322. "cmCPackPackageMakerGenerator::Initialize()" << std::endl);
  323. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  324. std::vector<std::string> path;
  325. std::string pkgPath
  326. = "/Developer/Applications/Utilities/PackageMaker.app/Contents";
  327. std::string versionFile = pkgPath + "/version.plist";
  328. if ( !cmSystemTools::FileExists(versionFile.c_str()) )
  329. {
  330. pkgPath = "/Developer/Applications/PackageMaker.app/Contents";
  331. std::string newVersionFile = pkgPath + "/version.plist";
  332. if ( !cmSystemTools::FileExists(newVersionFile.c_str()) )
  333. {
  334. cmCPackLogger(cmCPackLog::LOG_ERROR,
  335. "Cannot find PackageMaker compiler version file: "
  336. << versionFile.c_str() << " or " << newVersionFile.c_str()
  337. << std::endl);
  338. return 0;
  339. }
  340. versionFile = newVersionFile;
  341. }
  342. std::ifstream ifs(versionFile.c_str());
  343. if ( !ifs )
  344. {
  345. cmCPackLogger(cmCPackLog::LOG_ERROR,
  346. "Cannot open PackageMaker compiler version file" << std::endl);
  347. return 0;
  348. }
  349. // Check the PackageMaker version
  350. cmsys::RegularExpression rexKey("<key>CFBundleShortVersionString</key>");
  351. cmsys::RegularExpression rexVersion("<string>([0-9]+.[0-9.]+)</string>");
  352. std::string line;
  353. bool foundKey = false;
  354. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  355. {
  356. if ( rexKey.find(line) )
  357. {
  358. foundKey = true;
  359. break;
  360. }
  361. }
  362. if ( !foundKey )
  363. {
  364. cmCPackLogger(cmCPackLog::LOG_ERROR,
  365. "Cannot find CFBundleShortVersionString in the PackageMaker compiler "
  366. "version file" << std::endl);
  367. return 0;
  368. }
  369. if ( !cmSystemTools::GetLineFromStream(ifs, line) ||
  370. !rexVersion.find(line) )
  371. {
  372. cmCPackLogger(cmCPackLog::LOG_ERROR,
  373. "Problem reading the PackageMaker compiler version file: "
  374. << versionFile.c_str() << std::endl);
  375. return 0;
  376. }
  377. this->PackageMakerVersion = atof(rexVersion.match(1).c_str());
  378. if ( this->PackageMakerVersion < 1.0 )
  379. {
  380. cmCPackLogger(cmCPackLog::LOG_ERROR, "Require PackageMaker 1.0 or higher"
  381. << std::endl);
  382. return 0;
  383. }
  384. cmCPackLogger(cmCPackLog::LOG_DEBUG, "PackageMaker version is: "
  385. << this->PackageMakerVersion << std::endl);
  386. // Determine the package compatibility version. If it wasn't
  387. // specified by the user, we define it based on which features the
  388. // user requested.
  389. const char *packageCompat = this->GetOption("CPACK_OSX_PACKAGE_VERSION");
  390. if (packageCompat && *packageCompat)
  391. {
  392. this->PackageCompatibilityVersion = atof(packageCompat);
  393. }
  394. else if (this->GetOption("CPACK_DOWNLOAD_SITE"))
  395. {
  396. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.5");
  397. this->PackageCompatibilityVersion = 10.5;
  398. }
  399. else if (this->GetOption("CPACK_COMPONENTS_ALL"))
  400. {
  401. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.4");
  402. this->PackageCompatibilityVersion = 10.4;
  403. }
  404. else
  405. {
  406. this->SetOption("CPACK_OSX_PACKAGE_VERSION", "10.3");
  407. this->PackageCompatibilityVersion = 10.3;
  408. }
  409. pkgPath += "/MacOS";
  410. path.push_back(pkgPath);
  411. pkgPath = cmSystemTools::FindProgram("PackageMaker", path, false);
  412. if ( pkgPath.empty() )
  413. {
  414. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find PackageMaker compiler"
  415. << std::endl);
  416. return 0;
  417. }
  418. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  419. pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  420. if ( pkgPath.empty() )
  421. {
  422. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  423. << std::endl);
  424. return 0;
  425. }
  426. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  427. pkgPath.c_str());
  428. return this->Superclass::InitializeInternal();
  429. }
  430. //----------------------------------------------------------------------
  431. bool cmCPackPackageMakerGenerator::CopyCreateResourceFile(const char* name,
  432. const char* dirName)
  433. {
  434. std::string uname = cmSystemTools::UpperCase(name);
  435. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  436. const char* inFileName = this->GetOption(cpackVar.c_str());
  437. if ( !inFileName )
  438. {
  439. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  440. << " not specified. It should point to "
  441. << (name ? name : "(NULL)")
  442. << ".rtf, " << name
  443. << ".html, or " << name << ".txt file" << std::endl);
  444. return false;
  445. }
  446. if ( !cmSystemTools::FileExists(inFileName) )
  447. {
  448. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  449. << (name ? name : "(NULL)")
  450. << " resource file: " << inFileName << std::endl);
  451. return false;
  452. }
  453. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  454. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  455. {
  456. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  457. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  458. << std::endl);
  459. return false;
  460. }
  461. std::string destFileName = dirName;
  462. destFileName += '/';
  463. destFileName += name + ext;
  464. // Set this so that distribution.dist gets the right name (without
  465. // the path).
  466. this->SetOption(("CPACK_RESOURCE_FILE_" + uname + "_NOPATH").c_str(),
  467. (name + ext).c_str());
  468. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  469. << (inFileName ? inFileName : "(NULL)")
  470. << " to " << destFileName.c_str() << std::endl);
  471. this->ConfigureFile(inFileName, destFileName.c_str());
  472. return true;
  473. }
  474. bool cmCPackPackageMakerGenerator::CopyResourcePlistFile(const char* name,
  475. const char* outName)
  476. {
  477. if (!outName)
  478. {
  479. outName = name;
  480. }
  481. std::string inFName = "CPack.";
  482. inFName += name;
  483. inFName += ".in";
  484. std::string inFileName = this->FindTemplate(inFName.c_str());
  485. if ( inFileName.empty() )
  486. {
  487. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  488. << inFName << std::endl);
  489. return false;
  490. }
  491. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  492. destFileName += "/";
  493. destFileName += outName;
  494. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  495. << inFileName.c_str() << " to " << destFileName.c_str() << std::endl);
  496. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  497. return true;
  498. }
  499. //----------------------------------------------------------------------
  500. bool cmCPackPackageMakerGenerator::RunPackageMaker(const char *command,
  501. const char *packageFile)
  502. {
  503. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  504. tmpFile += "/PackageMakerOutput.log";
  505. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  506. std::string output;
  507. int retVal = 1;
  508. bool res = cmSystemTools::RunSingleCommand(command, &output, &retVal, 0,
  509. this->GeneratorVerbose, 0);
  510. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running package maker"
  511. << std::endl);
  512. if ( !res || retVal )
  513. {
  514. cmGeneratedFileStream ofs(tmpFile.c_str());
  515. ofs << "# Run command: " << command << std::endl
  516. << "# Output:" << std::endl
  517. << output.c_str() << std::endl;
  518. cmCPackLogger(cmCPackLog::LOG_ERROR,
  519. "Problem running PackageMaker command: " << command
  520. << std::endl << "Please check " << tmpFile.c_str() << " for errors"
  521. << std::endl);
  522. return false;
  523. }
  524. // sometimes the command finishes but the directory is not yet
  525. // created, so try 10 times to see if it shows up
  526. int tries = 10;
  527. while(tries > 0 &&
  528. !cmSystemTools::FileExists(packageFile))
  529. {
  530. cmSystemTools::Delay(500);
  531. tries--;
  532. }
  533. if(!cmSystemTools::FileExists(packageFile))
  534. {
  535. cmCPackLogger(
  536. cmCPackLog::LOG_ERROR,
  537. "Problem running PackageMaker command: " << command
  538. << std::endl << "Package not created: " << packageFile
  539. << std::endl);
  540. return false;
  541. }
  542. return true;
  543. }
  544. //----------------------------------------------------------------------
  545. std::string
  546. cmCPackPackageMakerGenerator::GetPackageName(const cmCPackComponent& component)
  547. {
  548. if (component.ArchiveFile.empty())
  549. {
  550. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  551. packagesDir += ".dummy";
  552. cmOStringStream out;
  553. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir)
  554. << "-" << component.Name << ".pkg";
  555. return out.str();
  556. }
  557. else
  558. {
  559. return component.ArchiveFile + ".pkg";
  560. }
  561. }
  562. //----------------------------------------------------------------------
  563. bool
  564. cmCPackPackageMakerGenerator::
  565. GenerateComponentPackage(const char *packageFile,
  566. const char *packageDir,
  567. const cmCPackComponent& component)
  568. {
  569. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  570. "- Building component package: " <<
  571. packageFile << std::endl);
  572. // The command that will be used to run PackageMaker
  573. cmOStringStream pkgCmd;
  574. if (this->PackageCompatibilityVersion < 10.5 ||
  575. this->PackageMakerVersion < 3.0)
  576. {
  577. // Create Description.plist and Info.plist files for normal Mac OS
  578. // X packages, which work on Mac OS X 10.3 and newer.
  579. std::string descriptionFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  580. descriptionFile += '/' + component.Name + "-Description.plist";
  581. std::ofstream out(descriptionFile.c_str());
  582. out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl
  583. << "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
  584. << "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" << std::endl
  585. << "<plist version=\"1.4\">" << std::endl
  586. << "<dict>" << std::endl
  587. << " <key>IFPkgDescriptionTitle</key>" << std::endl
  588. << " <string>" << component.DisplayName << "</string>" << std::endl
  589. << " <key>IFPkgDescriptionVersion</key>" << std::endl
  590. << " <string>" << this->GetOption("CPACK_PACKAGE_VERSION")
  591. << "</string>" << std::endl
  592. << " <key>IFPkgDescriptionDescription</key>" << std::endl
  593. << " <string>" + this->EscapeForXML(component.Description)
  594. << "</string>" << std::endl
  595. << "</dict>" << std::endl
  596. << "</plist>" << std::endl;
  597. out.close();
  598. // Create the Info.plist file for this component
  599. std::string moduleVersionSuffix = ".";
  600. moduleVersionSuffix += component.Name;
  601. this->SetOption("CPACK_MODULE_VERSION_SUFFIX",
  602. moduleVersionSuffix.c_str());
  603. std::string infoFileName = component.Name;
  604. infoFileName += "-Info.plist";
  605. if (!this->CopyResourcePlistFile("Info.plist", infoFileName.c_str()))
  606. {
  607. return false;
  608. }
  609. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  610. << "\" -build -p \"" << packageFile << "\""
  611. << " -f \"" << packageDir << "\""
  612. << " -i \"" << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  613. << "/" << infoFileName << "\""
  614. << " -d \"" << descriptionFile << "\"";
  615. }
  616. else
  617. {
  618. // Create a "flat" package on Mac OS X 10.5 and newer. Flat
  619. // packages are stored in a single file, rather than a directory
  620. // like normal packages, and can be downloaded by the installer
  621. // on-the-fly in Mac OS X 10.5 or newer. Thus, we need to create
  622. // flat packages when the packages will be downloaded on the fly.
  623. std::string pkgId = "com.";
  624. pkgId += this->GetOption("CPACK_PACKAGE_VENDOR");
  625. pkgId += '.';
  626. pkgId += this->GetOption("CPACK_PACKAGE_NAME");
  627. pkgId += '.';
  628. pkgId += component.Name;
  629. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  630. << "\" --root \"" << packageDir << "\""
  631. << " --id " << pkgId
  632. << " --target " << this->GetOption("CPACK_OSX_PACKAGE_VERSION")
  633. << " --out \"" << packageFile << "\"";
  634. }
  635. // Run PackageMaker
  636. return RunPackageMaker(pkgCmd.str().c_str(), packageFile);
  637. }
  638. //----------------------------------------------------------------------
  639. void
  640. cmCPackPackageMakerGenerator::
  641. WriteDistributionFile(const char* metapackageFile)
  642. {
  643. std::string distributionTemplate
  644. = this->FindTemplate("CPack.distribution.dist.in");
  645. if ( distributionTemplate.empty() )
  646. {
  647. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  648. << distributionTemplate << std::endl);
  649. return;
  650. }
  651. std::string distributionFile = metapackageFile;
  652. distributionFile += "/Contents/distribution.dist";
  653. // Create the choice outline, which provides a tree-based view of
  654. // the components in their groups.
  655. cmOStringStream choiceOut;
  656. choiceOut << "<choices-outline>" << std::endl;
  657. // Emit the outline for the groups
  658. std::map<std::string, cmCPackComponentGroup>::iterator groupIt;
  659. for (groupIt = this->ComponentGroups.begin();
  660. groupIt != this->ComponentGroups.end();
  661. ++groupIt)
  662. {
  663. if (groupIt->second.ParentGroup == 0)
  664. {
  665. CreateChoiceOutline(groupIt->second, choiceOut);
  666. }
  667. }
  668. // Emit the outline for the non-grouped components
  669. std::map<std::string, cmCPackComponent>::iterator compIt;
  670. for (compIt = this->Components.begin(); compIt != this->Components.end();
  671. ++compIt)
  672. {
  673. if (!compIt->second.Group)
  674. {
  675. choiceOut << "<line choice=\"" << compIt->first << "Choice\"></line>"
  676. << std::endl;
  677. }
  678. }
  679. choiceOut << "</choices-outline>" << std::endl;
  680. // Create the actual choices
  681. for (groupIt = this->ComponentGroups.begin();
  682. groupIt != this->ComponentGroups.end();
  683. ++groupIt)
  684. {
  685. CreateChoice(groupIt->second, choiceOut);
  686. }
  687. for (compIt = this->Components.begin(); compIt != this->Components.end();
  688. ++compIt)
  689. {
  690. CreateChoice(compIt->second, choiceOut);
  691. }
  692. this->SetOption("CPACK_PACKAGEMAKER_CHOICES", choiceOut.str().c_str());
  693. // Create the distribution.dist file in the metapackage to turn it
  694. // into a distribution package.
  695. this->ConfigureFile(distributionTemplate.c_str(),
  696. distributionFile.c_str());
  697. }
  698. //----------------------------------------------------------------------
  699. void
  700. cmCPackPackageMakerGenerator::
  701. CreateChoiceOutline(const cmCPackComponentGroup& group, cmOStringStream& out)
  702. {
  703. out << "<line choice=\"" << group.Name << "Choice\">" << std::endl;
  704. std::vector<cmCPackComponentGroup*>::const_iterator groupIt;
  705. for (groupIt = group.Subgroups.begin(); groupIt != group.Subgroups.end();
  706. ++groupIt)
  707. {
  708. CreateChoiceOutline(**groupIt, out);
  709. }
  710. std::vector<cmCPackComponent*>::const_iterator compIt;
  711. for (compIt = group.Components.begin(); compIt != group.Components.end();
  712. ++compIt)
  713. {
  714. out << " <line choice=\"" << (*compIt)->Name << "Choice\"></line>"
  715. << std::endl;
  716. }
  717. out << "</line>" << std::endl;
  718. }
  719. //----------------------------------------------------------------------
  720. void
  721. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponentGroup& group,
  722. cmOStringStream& out)
  723. {
  724. out << "<choice id=\"" << group.Name << "Choice\" "
  725. << "title=\"" << group.DisplayName << "\" "
  726. << "start_selected=\"true\" "
  727. << "start_enabled=\"true\" "
  728. << "start_visible=\"true\" ";
  729. if (!group.Description.empty())
  730. {
  731. out << "description=\"" << EscapeForXML(group.Description)
  732. << "\"";
  733. }
  734. out << "></choice>" << std::endl;
  735. }
  736. //----------------------------------------------------------------------
  737. void
  738. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponent& component,
  739. cmOStringStream& out)
  740. {
  741. std::string packageId = "com.";
  742. packageId += this->GetOption("CPACK_PACKAGE_VENDOR");
  743. packageId += '.';
  744. packageId += this->GetOption("CPACK_PACKAGE_NAME");
  745. packageId += '.';
  746. packageId += component.Name;
  747. out << "<choice id=\"" << component.Name << "Choice\" "
  748. << "title=\"" << component.DisplayName << "\" "
  749. << "start_selected=\""
  750. << (component.IsDisabledByDefault &&
  751. !component.IsRequired? "false" : "true")
  752. << "\" "
  753. << "start_enabled=\""
  754. << (component.IsRequired? "false" : "true")
  755. << "\" "
  756. << "start_visible=\"" << (component.IsHidden? "false" : "true") << "\" ";
  757. if (!component.Description.empty())
  758. {
  759. out << "description=\"" << EscapeForXML(component.Description)
  760. << "\" ";
  761. }
  762. if (!component.Dependencies.empty() ||
  763. !component.ReverseDependencies.empty())
  764. {
  765. // The "selected" expression is evaluated each time any choice is
  766. // selected, for all choices *except* the one that the user
  767. // selected. A component is marked selected if it has been
  768. // selected (my.choice.selected in Javascript) and all of the
  769. // components it depends on have been selected (transitively) or
  770. // if any of the components that depend on it have been selected
  771. // (transitively). Assume that we have components A, B, C, D, and
  772. // E, where each component depends on the previous component (B
  773. // depends on A, C depends on B, D depends on C, and E depends on
  774. // D). The expression we build for the component C will be
  775. // my.choice.selected && B && A || D || E
  776. // This way, selecting C will automatically select everything it depends
  777. // on (B and A), while selecting something that depends on C--either D
  778. // or E--will automatically cause C to get selected.
  779. out << "selected=\"my.choice.selected";
  780. std::set<const cmCPackComponent *> visited;
  781. AddDependencyAttributes(component, visited, out);
  782. visited.clear();
  783. AddReverseDependencyAttributes(component, visited, out);
  784. out << "\"";
  785. }
  786. out << ">" << std::endl;
  787. out << " <pkg-ref id=\"" << packageId << "\"></pkg-ref>" << std::endl;
  788. out << "</choice>" << std::endl;
  789. // Create a description of the package associated with this
  790. // component.
  791. std::string relativePackageLocation = "Contents/Packages/";
  792. relativePackageLocation += this->GetPackageName(component);
  793. // Determine the installed size of the package.
  794. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  795. dirName += '/';
  796. dirName += component.Name;
  797. unsigned long installedSize
  798. = component.GetInstalledSizeInKbytes(dirName.c_str());
  799. out << "<pkg-ref id=\"" << packageId << "\" "
  800. << "version=\"" << this->GetOption("CPACK_PACKAGE_VERSION") << "\" "
  801. << "installKBytes=\"" << installedSize << "\" "
  802. << "auth=\"Admin\" onConclusion=\"None\">";
  803. if (component.IsDownloaded)
  804. {
  805. out << this->GetOption("CPACK_DOWNLOAD_SITE")
  806. << this->GetPackageName(component);
  807. }
  808. else
  809. {
  810. out << "file:./" << relativePackageLocation;
  811. }
  812. out << "</pkg-ref>" << std::endl;
  813. }
  814. //----------------------------------------------------------------------
  815. void
  816. cmCPackPackageMakerGenerator::
  817. AddDependencyAttributes(const cmCPackComponent& component,
  818. std::set<const cmCPackComponent *>& visited,
  819. cmOStringStream& out)
  820. {
  821. if (visited.find(&component) != visited.end())
  822. {
  823. return;
  824. }
  825. visited.insert(&component);
  826. std::vector<cmCPackComponent *>::const_iterator dependIt;
  827. for (dependIt = component.Dependencies.begin();
  828. dependIt != component.Dependencies.end();
  829. ++dependIt)
  830. {
  831. out << " &amp;&amp; choices['" <<
  832. (*dependIt)->Name << "Choice'].selected";
  833. AddDependencyAttributes(**dependIt, visited, out);
  834. }
  835. }
  836. //----------------------------------------------------------------------
  837. void
  838. cmCPackPackageMakerGenerator::
  839. AddReverseDependencyAttributes(const cmCPackComponent& component,
  840. std::set<const cmCPackComponent *>& visited,
  841. cmOStringStream& out)
  842. {
  843. if (visited.find(&component) != visited.end())
  844. {
  845. return;
  846. }
  847. visited.insert(&component);
  848. std::vector<cmCPackComponent *>::const_iterator dependIt;
  849. for (dependIt = component.ReverseDependencies.begin();
  850. dependIt != component.ReverseDependencies.end();
  851. ++dependIt)
  852. {
  853. out << " || choices['" << (*dependIt)->Name << "Choice'].selected";
  854. AddReverseDependencyAttributes(**dependIt, visited, out);
  855. }
  856. }
  857. //----------------------------------------------------------------------
  858. std::string cmCPackPackageMakerGenerator::EscapeForXML(std::string str)
  859. {
  860. cmSystemTools::ReplaceString(str, "&", "&amp;");
  861. cmSystemTools::ReplaceString(str, "<", "&lt;");
  862. cmSystemTools::ReplaceString(str, ">", "&gt;");
  863. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  864. return str;
  865. }