cmCPackPKGGenerator.cxx 12 KB

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