cmCPackPackageMakerGenerator.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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(const char* name,
  510. const char* dirName)
  511. {
  512. std::string uname = cmSystemTools::UpperCase(name);
  513. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  514. const char* inFileName = this->GetOption(cpackVar.c_str());
  515. if ( !inFileName )
  516. {
  517. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  518. << " not specified. It should point to "
  519. << (name ? name : "(NULL)")
  520. << ".rtf, " << name
  521. << ".html, or " << name << ".txt file" << std::endl);
  522. return false;
  523. }
  524. if ( !cmSystemTools::FileExists(inFileName) )
  525. {
  526. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  527. << (name ? name : "(NULL)")
  528. << " resource file: " << inFileName << std::endl);
  529. return false;
  530. }
  531. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  532. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  533. {
  534. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  535. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  536. << std::endl);
  537. return false;
  538. }
  539. std::string destFileName = dirName;
  540. destFileName += '/';
  541. destFileName += name + ext;
  542. // Set this so that distribution.dist gets the right name (without
  543. // the path).
  544. this->SetOption(("CPACK_RESOURCE_FILE_" + uname + "_NOPATH").c_str(),
  545. (name + ext).c_str());
  546. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  547. << (inFileName ? inFileName : "(NULL)")
  548. << " to " << destFileName.c_str() << std::endl);
  549. this->ConfigureFile(inFileName, destFileName.c_str());
  550. return true;
  551. }
  552. bool cmCPackPackageMakerGenerator::CopyResourcePlistFile(const char* name,
  553. const char* outName)
  554. {
  555. if (!outName)
  556. {
  557. outName = name;
  558. }
  559. std::string inFName = "CPack.";
  560. inFName += name;
  561. inFName += ".in";
  562. std::string inFileName = this->FindTemplate(inFName.c_str());
  563. if ( inFileName.empty() )
  564. {
  565. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  566. << inFName << std::endl);
  567. return false;
  568. }
  569. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  570. destFileName += "/";
  571. destFileName += outName;
  572. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  573. << inFileName.c_str() << " to " << destFileName.c_str() << std::endl);
  574. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  575. return true;
  576. }
  577. //----------------------------------------------------------------------
  578. bool cmCPackPackageMakerGenerator::RunPackageMaker(const char *command,
  579. const char *packageFile)
  580. {
  581. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  582. tmpFile += "/PackageMakerOutput.log";
  583. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  584. std::string output;
  585. int retVal = 1;
  586. bool res = cmSystemTools::RunSingleCommand(command, &output, &retVal, 0,
  587. this->GeneratorVerbose, 0);
  588. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running package maker"
  589. << std::endl);
  590. if ( !res || retVal )
  591. {
  592. cmGeneratedFileStream ofs(tmpFile.c_str());
  593. ofs << "# Run command: " << command << std::endl
  594. << "# Output:" << std::endl
  595. << output.c_str() << std::endl;
  596. cmCPackLogger(cmCPackLog::LOG_ERROR,
  597. "Problem running PackageMaker command: " << command
  598. << std::endl << "Please check " << tmpFile.c_str() << " for errors"
  599. << std::endl);
  600. return false;
  601. }
  602. // sometimes the command finishes but the directory is not yet
  603. // created, so try 10 times to see if it shows up
  604. int tries = 10;
  605. while(tries > 0 &&
  606. !cmSystemTools::FileExists(packageFile))
  607. {
  608. cmSystemTools::Delay(500);
  609. tries--;
  610. }
  611. if(!cmSystemTools::FileExists(packageFile))
  612. {
  613. cmCPackLogger(
  614. cmCPackLog::LOG_ERROR,
  615. "Problem running PackageMaker command: " << command
  616. << std::endl << "Package not created: " << packageFile
  617. << std::endl);
  618. return false;
  619. }
  620. return true;
  621. }
  622. //----------------------------------------------------------------------
  623. std::string
  624. cmCPackPackageMakerGenerator::GetPackageName(const cmCPackComponent& component)
  625. {
  626. if (component.ArchiveFile.empty())
  627. {
  628. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  629. packagesDir += ".dummy";
  630. cmOStringStream out;
  631. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir)
  632. << "-" << component.Name << ".pkg";
  633. return out.str();
  634. }
  635. else
  636. {
  637. return component.ArchiveFile + ".pkg";
  638. }
  639. }
  640. //----------------------------------------------------------------------
  641. bool
  642. cmCPackPackageMakerGenerator::
  643. GenerateComponentPackage(const char *packageFile,
  644. const char *packageDir,
  645. const cmCPackComponent& component)
  646. {
  647. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  648. "- Building component package: " <<
  649. packageFile << std::endl);
  650. // The command that will be used to run PackageMaker
  651. cmOStringStream pkgCmd;
  652. if (this->PackageCompatibilityVersion < 10.5 ||
  653. this->PackageMakerVersion < 3.0)
  654. {
  655. // Create Description.plist and Info.plist files for normal Mac OS
  656. // X packages, which work on Mac OS X 10.3 and newer.
  657. std::string descriptionFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  658. descriptionFile += '/' + component.Name + "-Description.plist";
  659. cmsys::ofstream out(descriptionFile.c_str());
  660. out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl
  661. << "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
  662. << "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" << std::endl
  663. << "<plist version=\"1.4\">" << std::endl
  664. << "<dict>" << std::endl
  665. << " <key>IFPkgDescriptionTitle</key>" << std::endl
  666. << " <string>" << component.DisplayName << "</string>" << std::endl
  667. << " <key>IFPkgDescriptionVersion</key>" << std::endl
  668. << " <string>" << this->GetOption("CPACK_PACKAGE_VERSION")
  669. << "</string>" << std::endl
  670. << " <key>IFPkgDescriptionDescription</key>" << std::endl
  671. << " <string>" + this->EscapeForXML(component.Description)
  672. << "</string>" << std::endl
  673. << "</dict>" << std::endl
  674. << "</plist>" << std::endl;
  675. out.close();
  676. // Create the Info.plist file for this component
  677. std::string moduleVersionSuffix = ".";
  678. moduleVersionSuffix += component.Name;
  679. this->SetOption("CPACK_MODULE_VERSION_SUFFIX",
  680. moduleVersionSuffix.c_str());
  681. std::string infoFileName = component.Name;
  682. infoFileName += "-Info.plist";
  683. if (!this->CopyResourcePlistFile("Info.plist", infoFileName.c_str()))
  684. {
  685. return false;
  686. }
  687. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  688. << "\" -build -p \"" << packageFile << "\""
  689. << " -f \"" << packageDir << "\""
  690. << " -i \"" << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  691. << "/" << infoFileName << "\""
  692. << " -d \"" << descriptionFile << "\"";
  693. }
  694. else
  695. {
  696. // Create a "flat" package on Mac OS X 10.5 and newer. Flat
  697. // packages are stored in a single file, rather than a directory
  698. // like normal packages, and can be downloaded by the installer
  699. // on-the-fly in Mac OS X 10.5 or newer. Thus, we need to create
  700. // flat packages when the packages will be downloaded on the fly.
  701. std::string pkgId = "com.";
  702. pkgId += this->GetOption("CPACK_PACKAGE_VENDOR");
  703. pkgId += '.';
  704. pkgId += this->GetOption("CPACK_PACKAGE_NAME");
  705. pkgId += '.';
  706. pkgId += component.Name;
  707. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  708. << "\" --root \"" << packageDir << "\""
  709. << " --id " << pkgId
  710. << " --target " << this->GetOption("CPACK_OSX_PACKAGE_VERSION")
  711. << " --out \"" << packageFile << "\"";
  712. }
  713. // Run PackageMaker
  714. return RunPackageMaker(pkgCmd.str().c_str(), packageFile);
  715. }
  716. //----------------------------------------------------------------------
  717. void
  718. cmCPackPackageMakerGenerator::
  719. WriteDistributionFile(const char* metapackageFile)
  720. {
  721. std::string distributionTemplate
  722. = this->FindTemplate("CPack.distribution.dist.in");
  723. if ( distributionTemplate.empty() )
  724. {
  725. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  726. << distributionTemplate << std::endl);
  727. return;
  728. }
  729. std::string distributionFile = metapackageFile;
  730. distributionFile += "/Contents/distribution.dist";
  731. // Create the choice outline, which provides a tree-based view of
  732. // the components in their groups.
  733. cmOStringStream choiceOut;
  734. choiceOut << "<choices-outline>" << std::endl;
  735. // Emit the outline for the groups
  736. std::map<std::string, cmCPackComponentGroup>::iterator groupIt;
  737. for (groupIt = this->ComponentGroups.begin();
  738. groupIt != this->ComponentGroups.end();
  739. ++groupIt)
  740. {
  741. if (groupIt->second.ParentGroup == 0)
  742. {
  743. CreateChoiceOutline(groupIt->second, choiceOut);
  744. }
  745. }
  746. // Emit the outline for the non-grouped components
  747. std::map<std::string, cmCPackComponent>::iterator compIt;
  748. for (compIt = this->Components.begin(); compIt != this->Components.end();
  749. ++compIt)
  750. {
  751. if (!compIt->second.Group)
  752. {
  753. choiceOut << "<line choice=\"" << compIt->first << "Choice\"></line>"
  754. << std::endl;
  755. }
  756. }
  757. if(!this->PostFlightComponent.Name.empty())
  758. {
  759. choiceOut << "<line choice=\"" << PostFlightComponent.Name
  760. << "Choice\"></line>" << std::endl;
  761. }
  762. choiceOut << "</choices-outline>" << std::endl;
  763. // Create the actual choices
  764. for (groupIt = this->ComponentGroups.begin();
  765. groupIt != this->ComponentGroups.end();
  766. ++groupIt)
  767. {
  768. CreateChoice(groupIt->second, choiceOut);
  769. }
  770. for (compIt = this->Components.begin(); compIt != this->Components.end();
  771. ++compIt)
  772. {
  773. CreateChoice(compIt->second, choiceOut);
  774. }
  775. if(!this->PostFlightComponent.Name.empty())
  776. {
  777. CreateChoice(PostFlightComponent, choiceOut);
  778. }
  779. this->SetOption("CPACK_PACKAGEMAKER_CHOICES", choiceOut.str().c_str());
  780. // Create the distribution.dist file in the metapackage to turn it
  781. // into a distribution package.
  782. this->ConfigureFile(distributionTemplate.c_str(),
  783. distributionFile.c_str());
  784. }
  785. //----------------------------------------------------------------------
  786. void
  787. cmCPackPackageMakerGenerator::
  788. CreateChoiceOutline(const cmCPackComponentGroup& group, cmOStringStream& out)
  789. {
  790. out << "<line choice=\"" << group.Name << "Choice\">" << std::endl;
  791. std::vector<cmCPackComponentGroup*>::const_iterator groupIt;
  792. for (groupIt = group.Subgroups.begin(); groupIt != group.Subgroups.end();
  793. ++groupIt)
  794. {
  795. CreateChoiceOutline(**groupIt, out);
  796. }
  797. std::vector<cmCPackComponent*>::const_iterator compIt;
  798. for (compIt = group.Components.begin(); compIt != group.Components.end();
  799. ++compIt)
  800. {
  801. out << " <line choice=\"" << (*compIt)->Name << "Choice\"></line>"
  802. << std::endl;
  803. }
  804. out << "</line>" << std::endl;
  805. }
  806. //----------------------------------------------------------------------
  807. void
  808. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponentGroup& group,
  809. cmOStringStream& out)
  810. {
  811. out << "<choice id=\"" << group.Name << "Choice\" "
  812. << "title=\"" << group.DisplayName << "\" "
  813. << "start_selected=\"true\" "
  814. << "start_enabled=\"true\" "
  815. << "start_visible=\"true\" ";
  816. if (!group.Description.empty())
  817. {
  818. out << "description=\"" << EscapeForXML(group.Description)
  819. << "\"";
  820. }
  821. out << "></choice>" << std::endl;
  822. }
  823. //----------------------------------------------------------------------
  824. void
  825. cmCPackPackageMakerGenerator::CreateChoice(const cmCPackComponent& component,
  826. cmOStringStream& out)
  827. {
  828. std::string packageId = "com.";
  829. packageId += this->GetOption("CPACK_PACKAGE_VENDOR");
  830. packageId += '.';
  831. packageId += this->GetOption("CPACK_PACKAGE_NAME");
  832. packageId += '.';
  833. packageId += component.Name;
  834. out << "<choice id=\"" << component.Name << "Choice\" "
  835. << "title=\"" << component.DisplayName << "\" "
  836. << "start_selected=\""
  837. << (component.IsDisabledByDefault &&
  838. !component.IsRequired? "false" : "true")
  839. << "\" "
  840. << "start_enabled=\""
  841. << (component.IsRequired? "false" : "true")
  842. << "\" "
  843. << "start_visible=\"" << (component.IsHidden? "false" : "true") << "\" ";
  844. if (!component.Description.empty())
  845. {
  846. out << "description=\"" << EscapeForXML(component.Description)
  847. << "\" ";
  848. }
  849. if (!component.Dependencies.empty() ||
  850. !component.ReverseDependencies.empty())
  851. {
  852. // The "selected" expression is evaluated each time any choice is
  853. // selected, for all choices *except* the one that the user
  854. // selected. A component is marked selected if it has been
  855. // selected (my.choice.selected in Javascript) and all of the
  856. // components it depends on have been selected (transitively) or
  857. // if any of the components that depend on it have been selected
  858. // (transitively). Assume that we have components A, B, C, D, and
  859. // E, where each component depends on the previous component (B
  860. // depends on A, C depends on B, D depends on C, and E depends on
  861. // D). The expression we build for the component C will be
  862. // my.choice.selected && B && A || D || E
  863. // This way, selecting C will automatically select everything it depends
  864. // on (B and A), while selecting something that depends on C--either D
  865. // or E--will automatically cause C to get selected.
  866. out << "selected=\"my.choice.selected";
  867. std::set<const cmCPackComponent *> visited;
  868. AddDependencyAttributes(component, visited, out);
  869. visited.clear();
  870. AddReverseDependencyAttributes(component, visited, out);
  871. out << "\"";
  872. }
  873. out << ">" << std::endl;
  874. out << " <pkg-ref id=\"" << packageId << "\"></pkg-ref>" << std::endl;
  875. out << "</choice>" << std::endl;
  876. // Create a description of the package associated with this
  877. // component.
  878. std::string relativePackageLocation = "Contents/Packages/";
  879. relativePackageLocation += this->GetPackageName(component);
  880. // Determine the installed size of the package.
  881. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  882. dirName += '/';
  883. dirName += component.Name;
  884. unsigned long installedSize
  885. = component.GetInstalledSizeInKbytes(dirName.c_str());
  886. out << "<pkg-ref id=\"" << packageId << "\" "
  887. << "version=\"" << this->GetOption("CPACK_PACKAGE_VERSION") << "\" "
  888. << "installKBytes=\"" << installedSize << "\" "
  889. << "auth=\"Admin\" onConclusion=\"None\">";
  890. if (component.IsDownloaded)
  891. {
  892. out << this->GetOption("CPACK_DOWNLOAD_SITE")
  893. << this->GetPackageName(component);
  894. }
  895. else
  896. {
  897. out << "file:./" << relativePackageLocation;
  898. }
  899. out << "</pkg-ref>" << std::endl;
  900. }
  901. //----------------------------------------------------------------------
  902. void
  903. cmCPackPackageMakerGenerator::
  904. AddDependencyAttributes(const cmCPackComponent& component,
  905. std::set<const cmCPackComponent *>& visited,
  906. cmOStringStream& out)
  907. {
  908. if (visited.find(&component) != visited.end())
  909. {
  910. return;
  911. }
  912. visited.insert(&component);
  913. std::vector<cmCPackComponent *>::const_iterator dependIt;
  914. for (dependIt = component.Dependencies.begin();
  915. dependIt != component.Dependencies.end();
  916. ++dependIt)
  917. {
  918. out << " &amp;&amp; choices['" <<
  919. (*dependIt)->Name << "Choice'].selected";
  920. AddDependencyAttributes(**dependIt, visited, out);
  921. }
  922. }
  923. //----------------------------------------------------------------------
  924. void
  925. cmCPackPackageMakerGenerator::
  926. AddReverseDependencyAttributes(const cmCPackComponent& component,
  927. std::set<const cmCPackComponent *>& visited,
  928. cmOStringStream& out)
  929. {
  930. if (visited.find(&component) != visited.end())
  931. {
  932. return;
  933. }
  934. visited.insert(&component);
  935. std::vector<cmCPackComponent *>::const_iterator dependIt;
  936. for (dependIt = component.ReverseDependencies.begin();
  937. dependIt != component.ReverseDependencies.end();
  938. ++dependIt)
  939. {
  940. out << " || choices['" << (*dependIt)->Name << "Choice'].selected";
  941. AddReverseDependencyAttributes(**dependIt, visited, out);
  942. }
  943. }
  944. //----------------------------------------------------------------------
  945. std::string cmCPackPackageMakerGenerator::EscapeForXML(std::string str)
  946. {
  947. cmSystemTools::ReplaceString(str, "&", "&amp;");
  948. cmSystemTools::ReplaceString(str, "<", "&lt;");
  949. cmSystemTools::ReplaceString(str, ">", "&gt;");
  950. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  951. return str;
  952. }