cmCPackPKGGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "cmCPackPKGGenerator.h"
  11. #include "cmCPackComponentGroup.h"
  12. #include "cmCPackLog.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. #include <cmsys/Glob.hxx>
  20. #include <cmsys/SystemTools.hxx>
  21. cmCPackPKGGenerator::cmCPackPKGGenerator()
  22. {
  23. this->componentPackageMethod = ONE_PACKAGE;
  24. }
  25. cmCPackPKGGenerator::~cmCPackPKGGenerator()
  26. {
  27. }
  28. bool cmCPackPKGGenerator::SupportsComponentInstallation() const
  29. {
  30. return true;
  31. }
  32. int cmCPackPKGGenerator::InitializeInternal()
  33. {
  34. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackPKGGenerator::Initialize()"
  35. << std::endl);
  36. return this->Superclass::InitializeInternal();
  37. }
  38. std::string cmCPackPKGGenerator::GetPackageName(
  39. const cmCPackComponent& component)
  40. {
  41. if (component.ArchiveFile.empty()) {
  42. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  43. packagesDir += ".dummy";
  44. std::ostringstream out;
  45. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir) << "-"
  46. << component.Name << ".pkg";
  47. return out.str();
  48. } else {
  49. return component.ArchiveFile + ".pkg";
  50. }
  51. }
  52. void cmCPackPKGGenerator::WriteDistributionFile(const char* metapackageFile)
  53. {
  54. std::string distributionTemplate =
  55. this->FindTemplate("CPack.distribution.dist.in");
  56. if (distributionTemplate.empty()) {
  57. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  58. << distributionTemplate << std::endl);
  59. return;
  60. }
  61. std::string distributionFile = metapackageFile;
  62. distributionFile += "/Contents/distribution.dist";
  63. // Create the choice outline, which provides a tree-based view of
  64. // the components in their groups.
  65. std::ostringstream choiceOut;
  66. choiceOut << "<choices-outline>" << std::endl;
  67. // Emit the outline for the groups
  68. std::map<std::string, cmCPackComponentGroup>::iterator groupIt;
  69. for (groupIt = this->ComponentGroups.begin();
  70. groupIt != this->ComponentGroups.end(); ++groupIt) {
  71. if (groupIt->second.ParentGroup == 0) {
  72. CreateChoiceOutline(groupIt->second, choiceOut);
  73. }
  74. }
  75. // Emit the outline for the non-grouped components
  76. std::map<std::string, cmCPackComponent>::iterator compIt;
  77. for (compIt = this->Components.begin(); compIt != this->Components.end();
  78. ++compIt) {
  79. if (!compIt->second.Group) {
  80. choiceOut << "<line choice=\"" << compIt->first << "Choice\"></line>"
  81. << std::endl;
  82. }
  83. }
  84. if (!this->PostFlightComponent.Name.empty()) {
  85. choiceOut << "<line choice=\"" << PostFlightComponent.Name
  86. << "Choice\"></line>" << std::endl;
  87. }
  88. choiceOut << "</choices-outline>" << std::endl;
  89. // Create the actual choices
  90. for (groupIt = this->ComponentGroups.begin();
  91. groupIt != this->ComponentGroups.end(); ++groupIt) {
  92. CreateChoice(groupIt->second, choiceOut);
  93. }
  94. for (compIt = this->Components.begin(); compIt != this->Components.end();
  95. ++compIt) {
  96. CreateChoice(compIt->second, choiceOut);
  97. }
  98. if (!this->PostFlightComponent.Name.empty()) {
  99. CreateChoice(PostFlightComponent, choiceOut);
  100. }
  101. this->SetOption("CPACK_PACKAGEMAKER_CHOICES", choiceOut.str().c_str());
  102. // Create the distribution.dist file in the metapackage to turn it
  103. // into a distribution package.
  104. this->ConfigureFile(distributionTemplate.c_str(), distributionFile.c_str());
  105. }
  106. void cmCPackPKGGenerator::CreateChoiceOutline(
  107. const cmCPackComponentGroup& group, std::ostringstream& out)
  108. {
  109. out << "<line choice=\"" << group.Name << "Choice\">" << std::endl;
  110. std::vector<cmCPackComponentGroup*>::const_iterator groupIt;
  111. for (groupIt = group.Subgroups.begin(); groupIt != group.Subgroups.end();
  112. ++groupIt) {
  113. CreateChoiceOutline(**groupIt, out);
  114. }
  115. std::vector<cmCPackComponent*>::const_iterator compIt;
  116. for (compIt = group.Components.begin(); compIt != group.Components.end();
  117. ++compIt) {
  118. out << " <line choice=\"" << (*compIt)->Name << "Choice\"></line>"
  119. << std::endl;
  120. }
  121. out << "</line>" << std::endl;
  122. }
  123. void cmCPackPKGGenerator::CreateChoice(const cmCPackComponentGroup& group,
  124. std::ostringstream& out)
  125. {
  126. out << "<choice id=\"" << group.Name << "Choice\" "
  127. << "title=\"" << group.DisplayName << "\" "
  128. << "start_selected=\"true\" "
  129. << "start_enabled=\"true\" "
  130. << "start_visible=\"true\" ";
  131. if (!group.Description.empty()) {
  132. out << "description=\"" << EscapeForXML(group.Description) << "\"";
  133. }
  134. out << "></choice>" << std::endl;
  135. }
  136. void cmCPackPKGGenerator::CreateChoice(const cmCPackComponent& component,
  137. std::ostringstream& out)
  138. {
  139. std::string packageId = "com.";
  140. packageId += this->GetOption("CPACK_PACKAGE_VENDOR");
  141. packageId += '.';
  142. packageId += this->GetOption("CPACK_PACKAGE_NAME");
  143. packageId += '.';
  144. packageId += component.Name;
  145. out << "<choice id=\"" << component.Name << "Choice\" "
  146. << "title=\"" << component.DisplayName << "\" "
  147. << "start_selected=\""
  148. << (component.IsDisabledByDefault && !component.IsRequired ? "false"
  149. : "true")
  150. << "\" "
  151. << "start_enabled=\"" << (component.IsRequired ? "false" : "true")
  152. << "\" "
  153. << "start_visible=\"" << (component.IsHidden ? "false" : "true")
  154. << "\" ";
  155. if (!component.Description.empty()) {
  156. out << "description=\"" << EscapeForXML(component.Description) << "\" ";
  157. }
  158. if (!component.Dependencies.empty() ||
  159. !component.ReverseDependencies.empty()) {
  160. // The "selected" expression is evaluated each time any choice is
  161. // selected, for all choices *except* the one that the user
  162. // selected. A component is marked selected if it has been
  163. // selected (my.choice.selected in Javascript) and all of the
  164. // components it depends on have been selected (transitively) or
  165. // if any of the components that depend on it have been selected
  166. // (transitively). Assume that we have components A, B, C, D, and
  167. // E, where each component depends on the previous component (B
  168. // depends on A, C depends on B, D depends on C, and E depends on
  169. // D). The expression we build for the component C will be
  170. // my.choice.selected && B && A || D || E
  171. // This way, selecting C will automatically select everything it depends
  172. // on (B and A), while selecting something that depends on C--either D
  173. // or E--will automatically cause C to get selected.
  174. out << "selected=\"my.choice.selected";
  175. std::set<const cmCPackComponent*> visited;
  176. AddDependencyAttributes(component, visited, out);
  177. visited.clear();
  178. AddReverseDependencyAttributes(component, visited, out);
  179. out << "\"";
  180. }
  181. out << ">" << std::endl;
  182. out << " <pkg-ref id=\"" << packageId << "\"></pkg-ref>" << std::endl;
  183. out << "</choice>" << std::endl;
  184. // Create a description of the package associated with this
  185. // component.
  186. std::string relativePackageLocation = "Contents/Packages/";
  187. relativePackageLocation += this->GetPackageName(component);
  188. // Determine the installed size of the package.
  189. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  190. dirName += '/';
  191. dirName += component.Name;
  192. dirName += this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  193. unsigned long installedSize =
  194. component.GetInstalledSizeInKbytes(dirName.c_str());
  195. out << "<pkg-ref id=\"" << packageId << "\" "
  196. << "version=\"" << this->GetOption("CPACK_PACKAGE_VERSION") << "\" "
  197. << "installKBytes=\"" << installedSize << "\" "
  198. << ">";
  199. if (component.IsDownloaded) {
  200. out << this->GetOption("CPACK_DOWNLOAD_SITE")
  201. << this->GetPackageName(component);
  202. } else {
  203. out << "file:./" << relativePackageLocation;
  204. }
  205. out << "</pkg-ref>" << std::endl;
  206. }
  207. void cmCPackPKGGenerator::AddDependencyAttributes(
  208. const cmCPackComponent& component,
  209. std::set<const cmCPackComponent*>& visited, std::ostringstream& out)
  210. {
  211. if (visited.find(&component) != visited.end()) {
  212. return;
  213. }
  214. visited.insert(&component);
  215. std::vector<cmCPackComponent*>::const_iterator dependIt;
  216. for (dependIt = component.Dependencies.begin();
  217. dependIt != component.Dependencies.end(); ++dependIt) {
  218. out << " &amp;&amp; choices['" << (*dependIt)->Name << "Choice'].selected";
  219. AddDependencyAttributes(**dependIt, visited, out);
  220. }
  221. }
  222. void cmCPackPKGGenerator::AddReverseDependencyAttributes(
  223. const cmCPackComponent& component,
  224. std::set<const cmCPackComponent*>& visited, std::ostringstream& out)
  225. {
  226. if (visited.find(&component) != visited.end()) {
  227. return;
  228. }
  229. visited.insert(&component);
  230. std::vector<cmCPackComponent*>::const_iterator dependIt;
  231. for (dependIt = component.ReverseDependencies.begin();
  232. dependIt != component.ReverseDependencies.end(); ++dependIt) {
  233. out << " || choices['" << (*dependIt)->Name << "Choice'].selected";
  234. AddReverseDependencyAttributes(**dependIt, visited, out);
  235. }
  236. }
  237. std::string cmCPackPKGGenerator::EscapeForXML(std::string str)
  238. {
  239. cmSystemTools::ReplaceString(str, "&", "&amp;");
  240. cmSystemTools::ReplaceString(str, "<", "&lt;");
  241. cmSystemTools::ReplaceString(str, ">", "&gt;");
  242. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  243. return str;
  244. }
  245. bool cmCPackPKGGenerator::CopyCreateResourceFile(const std::string& name,
  246. const std::string& dirName)
  247. {
  248. std::string uname = cmSystemTools::UpperCase(name);
  249. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  250. const char* inFileName = this->GetOption(cpackVar.c_str());
  251. if (!inFileName) {
  252. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: "
  253. << cpackVar.c_str()
  254. << " not specified. It should point to "
  255. << (!name.empty() ? name : "<empty>") << ".rtf, " << name
  256. << ".html, or " << name << ".txt file" << std::endl);
  257. return false;
  258. }
  259. if (!cmSystemTools::FileExists(inFileName)) {
  260. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  261. << (!name.empty() ? name : "<empty>")
  262. << " resource file: " << inFileName << std::endl);
  263. return false;
  264. }
  265. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  266. if (ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt") {
  267. cmCPackLogger(
  268. cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  269. << ext
  270. << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  271. << std::endl);
  272. return false;
  273. }
  274. std::string destFileName = dirName;
  275. destFileName += '/';
  276. destFileName += name + ext;
  277. // Set this so that distribution.dist gets the right name (without
  278. // the path).
  279. this->SetOption(("CPACK_RESOURCE_FILE_" + uname + "_NOPATH").c_str(),
  280. (name + ext).c_str());
  281. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  282. "Configure file: " << (inFileName ? inFileName : "(NULL)")
  283. << " to " << destFileName << std::endl);
  284. this->ConfigureFile(inFileName, destFileName.c_str());
  285. return true;
  286. }
  287. bool cmCPackPKGGenerator::CopyResourcePlistFile(const std::string& name,
  288. const char* outName)
  289. {
  290. if (!outName) {
  291. outName = name.c_str();
  292. }
  293. std::string inFName = "CPack.";
  294. inFName += name;
  295. inFName += ".in";
  296. std::string inFileName = this->FindTemplate(inFName.c_str());
  297. if (inFileName.empty()) {
  298. cmCPackLogger(cmCPackLog::LOG_ERROR,
  299. "Cannot find input file: " << inFName << std::endl);
  300. return false;
  301. }
  302. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  303. destFileName += "/";
  304. destFileName += outName;
  305. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  306. << inFileName << " to " << destFileName << std::endl);
  307. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  308. return true;
  309. }
  310. int cmCPackPKGGenerator::CopyInstallScript(const std::string& resdir,
  311. const std::string& script,
  312. const std::string& name)
  313. {
  314. std::string dst = resdir;
  315. dst += "/";
  316. dst += name;
  317. cmSystemTools::CopyFileAlways(script.c_str(), dst.c_str());
  318. cmSystemTools::SetPermissions(dst.c_str(), 0777);
  319. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  320. "copy script : " << script << "\ninto " << dst << std::endl);
  321. return 1;
  322. }