cmCPackPKGGenerator.cxx 12 KB

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