cmCPackPKGGenerator.cxx 12 KB

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