cmCPackPKGGenerator.cxx 13 KB

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