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