cmCPackPKGGenerator.cxx 13 KB

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