cmCPackPKGGenerator.cxx 12 KB

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