cmCPackExternalGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "cmCPackExternalGenerator.h"
  4. #include <map>
  5. #include <utility>
  6. #include <vector>
  7. #include <cm/memory>
  8. #include <cm3p/json/value.h>
  9. #include <cm3p/json/writer.h>
  10. #include "cmsys/FStream.hxx"
  11. #include "cmCPackComponentGroup.h"
  12. #include "cmCPackLog.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmList.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmValue.h"
  18. int cmCPackExternalGenerator::InitializeInternal()
  19. {
  20. this->SetOption("CPACK_EXTERNAL_KNOWN_VERSIONS", "1.0");
  21. if (!this->ReadListFile("Internal/CPack/CPackExternal.cmake")) {
  22. cmCPackLogger(cmCPackLog::LOG_ERROR,
  23. "Error while executing CPackExternal.cmake" << std::endl);
  24. return 0;
  25. }
  26. std::string major = this->GetOption("CPACK_EXTERNAL_SELECTED_MAJOR");
  27. if (major == "1") {
  28. this->Generator = cm::make_unique<cmCPackExternalVersion1Generator>(this);
  29. }
  30. return this->Superclass::InitializeInternal();
  31. }
  32. int cmCPackExternalGenerator::PackageFiles()
  33. {
  34. Json::StreamWriterBuilder builder;
  35. builder["indentation"] = " ";
  36. std::string filename = "package.json";
  37. if (!this->packageFileNames.empty()) {
  38. filename = this->packageFileNames[0];
  39. }
  40. {
  41. cmGeneratedFileStream fout(filename);
  42. std::unique_ptr<Json::StreamWriter> jout(builder.newStreamWriter());
  43. Json::Value root(Json::objectValue);
  44. if (!this->Generator->WriteToJSON(root)) {
  45. return 0;
  46. }
  47. if (jout->write(root, &fout)) {
  48. return 0;
  49. }
  50. }
  51. cmValue packageScript = this->GetOption("CPACK_EXTERNAL_PACKAGE_SCRIPT");
  52. if (cmNonempty(packageScript)) {
  53. if (!cmSystemTools::FileIsFullPath(*packageScript)) {
  54. cmCPackLogger(
  55. cmCPackLog::LOG_ERROR,
  56. "CPACK_EXTERNAL_PACKAGE_SCRIPT does not contain a full file path"
  57. << std::endl);
  58. return 0;
  59. }
  60. bool res = this->MakefileMap->ReadListFile(*packageScript);
  61. if (cmSystemTools::GetErrorOccurredFlag() || !res) {
  62. return 0;
  63. }
  64. cmValue builtPackages = this->GetOption("CPACK_EXTERNAL_BUILT_PACKAGES");
  65. if (builtPackages) {
  66. cmExpandList(builtPackages, this->packageFileNames);
  67. }
  68. }
  69. return 1;
  70. }
  71. bool cmCPackExternalGenerator::SupportsComponentInstallation() const
  72. {
  73. return true;
  74. }
  75. int cmCPackExternalGenerator::InstallProjectViaInstallCommands(
  76. bool setDestDir, std::string const& tempInstallDirectory)
  77. {
  78. if (this->StagingEnabled()) {
  79. return this->cmCPackGenerator::InstallProjectViaInstallCommands(
  80. setDestDir, tempInstallDirectory);
  81. }
  82. return 1;
  83. }
  84. int cmCPackExternalGenerator::InstallProjectViaInstallScript(
  85. bool setDestDir, std::string const& tempInstallDirectory)
  86. {
  87. if (this->StagingEnabled()) {
  88. return this->cmCPackGenerator::InstallProjectViaInstallScript(
  89. setDestDir, tempInstallDirectory);
  90. }
  91. return 1;
  92. }
  93. int cmCPackExternalGenerator::InstallProjectViaInstalledDirectories(
  94. bool setDestDir, std::string const& tempInstallDirectory,
  95. mode_t const* default_dir_mode)
  96. {
  97. if (this->StagingEnabled()) {
  98. return this->cmCPackGenerator::InstallProjectViaInstalledDirectories(
  99. setDestDir, tempInstallDirectory, default_dir_mode);
  100. }
  101. return 1;
  102. }
  103. int cmCPackExternalGenerator::RunPreinstallTarget(
  104. std::string const& installProjectName, std::string const& installDirectory,
  105. cmGlobalGenerator* globalGenerator, std::string const& buildConfig)
  106. {
  107. if (this->StagingEnabled()) {
  108. return this->cmCPackGenerator::RunPreinstallTarget(
  109. installProjectName, installDirectory, globalGenerator, buildConfig);
  110. }
  111. return 1;
  112. }
  113. int cmCPackExternalGenerator::InstallCMakeProject(
  114. bool setDestDir, std::string const& installDirectory,
  115. std::string const& baseTempInstallDirectory, mode_t const* default_dir_mode,
  116. std::string const& component, bool componentInstall,
  117. std::string const& installSubDirectory, std::string const& buildConfig,
  118. std::string& absoluteDestFiles)
  119. {
  120. if (this->StagingEnabled()) {
  121. return this->cmCPackGenerator::InstallCMakeProject(
  122. setDestDir, installDirectory, baseTempInstallDirectory, default_dir_mode,
  123. component, componentInstall, installSubDirectory, buildConfig,
  124. absoluteDestFiles);
  125. }
  126. return 1;
  127. }
  128. bool cmCPackExternalGenerator::StagingEnabled() const
  129. {
  130. return !this->GetOption("CPACK_EXTERNAL_ENABLE_STAGING").IsOff();
  131. }
  132. cmCPackExternalGenerator::cmCPackExternalVersionGenerator::
  133. cmCPackExternalVersionGenerator(cmCPackExternalGenerator* parent)
  134. : Parent(parent)
  135. {
  136. }
  137. int cmCPackExternalGenerator::cmCPackExternalVersionGenerator::WriteVersion(
  138. Json::Value& root)
  139. {
  140. root["formatVersionMajor"] = this->GetVersionMajor();
  141. root["formatVersionMinor"] = this->GetVersionMinor();
  142. return 1;
  143. }
  144. int cmCPackExternalGenerator::cmCPackExternalVersionGenerator::WriteToJSON(
  145. Json::Value& root)
  146. {
  147. if (!this->WriteVersion(root)) {
  148. return 0;
  149. }
  150. cmValue packageName = this->Parent->GetOption("CPACK_PACKAGE_NAME");
  151. if (packageName) {
  152. root["packageName"] = *packageName;
  153. }
  154. cmValue packageVersion = this->Parent->GetOption("CPACK_PACKAGE_VERSION");
  155. if (packageVersion) {
  156. root["packageVersion"] = *packageVersion;
  157. }
  158. cmValue packageDescriptionFile =
  159. this->Parent->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  160. if (packageDescriptionFile) {
  161. root["packageDescriptionFile"] = *packageDescriptionFile;
  162. }
  163. cmValue packageDescriptionSummary =
  164. this->Parent->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY");
  165. if (packageDescriptionSummary) {
  166. root["packageDescriptionSummary"] = *packageDescriptionSummary;
  167. }
  168. cmValue buildConfigCstr = this->Parent->GetOption("CPACK_BUILD_CONFIG");
  169. if (buildConfigCstr) {
  170. root["buildConfig"] = *buildConfigCstr;
  171. }
  172. cmValue defaultDirectoryPermissions =
  173. this->Parent->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  174. if (cmNonempty(defaultDirectoryPermissions)) {
  175. root["defaultDirectoryPermissions"] = *defaultDirectoryPermissions;
  176. }
  177. if (cmIsInternallyOn(this->Parent->GetOption("CPACK_SET_DESTDIR"))) {
  178. root["setDestdir"] = true;
  179. root["packagingInstallPrefix"] =
  180. *this->Parent->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  181. } else {
  182. root["setDestdir"] = false;
  183. }
  184. root["stripFiles"] = !this->Parent->GetOption("CPACK_STRIP_FILES").IsOff();
  185. root["warnOnAbsoluteInstallDestination"] =
  186. this->Parent->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION");
  187. root["errorOnAbsoluteInstallDestination"] =
  188. this->Parent->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION");
  189. Json::Value& projects = root["projects"] = Json::Value(Json::arrayValue);
  190. for (auto& project : this->Parent->CMakeProjects) {
  191. Json::Value jsonProject(Json::objectValue);
  192. jsonProject["projectName"] = project.ProjectName;
  193. jsonProject["component"] = project.Component;
  194. jsonProject["directory"] = project.Directory;
  195. jsonProject["subDirectory"] = project.SubDirectory;
  196. Json::Value& installationTypes = jsonProject["installationTypes"] =
  197. Json::Value(Json::arrayValue);
  198. for (auto& installationType : project.InstallationTypes) {
  199. installationTypes.append(installationType->Name);
  200. }
  201. Json::Value& components = jsonProject["components"] =
  202. Json::Value(Json::arrayValue);
  203. for (auto& component : project.Components) {
  204. components.append(component->Name);
  205. }
  206. projects.append(jsonProject);
  207. }
  208. Json::Value& installationTypes = root["installationTypes"] =
  209. Json::Value(Json::objectValue);
  210. for (auto& installationType : this->Parent->InstallationTypes) {
  211. Json::Value& jsonInstallationType =
  212. installationTypes[installationType.first] =
  213. Json::Value(Json::objectValue);
  214. jsonInstallationType["name"] = installationType.second.Name;
  215. jsonInstallationType["displayName"] = installationType.second.DisplayName;
  216. jsonInstallationType["index"] = installationType.second.Index;
  217. }
  218. Json::Value& components = root["components"] =
  219. Json::Value(Json::objectValue);
  220. for (auto& component : this->Parent->Components) {
  221. Json::Value& jsonComponent = components[component.first] =
  222. Json::Value(Json::objectValue);
  223. jsonComponent["name"] = component.second.Name;
  224. jsonComponent["displayName"] = component.second.DisplayName;
  225. if (component.second.Group) {
  226. jsonComponent["group"] = component.second.Group->Name;
  227. }
  228. jsonComponent["isRequired"] = component.second.IsRequired;
  229. jsonComponent["isHidden"] = component.second.IsHidden;
  230. jsonComponent["isDisabledByDefault"] =
  231. component.second.IsDisabledByDefault;
  232. jsonComponent["isDownloaded"] = component.second.IsDownloaded;
  233. jsonComponent["description"] = component.second.Description;
  234. jsonComponent["archiveFile"] = component.second.ArchiveFile;
  235. Json::Value& cmpInstallationTypes = jsonComponent["installationTypes"] =
  236. Json::Value(Json::arrayValue);
  237. for (auto& installationType : component.second.InstallationTypes) {
  238. cmpInstallationTypes.append(installationType->Name);
  239. }
  240. Json::Value& dependencies = jsonComponent["dependencies"] =
  241. Json::Value(Json::arrayValue);
  242. for (auto& dep : component.second.Dependencies) {
  243. dependencies.append(dep->Name);
  244. }
  245. }
  246. Json::Value& groups = root["componentGroups"] =
  247. Json::Value(Json::objectValue);
  248. for (auto& group : this->Parent->ComponentGroups) {
  249. Json::Value& jsonGroup = groups[group.first] =
  250. Json::Value(Json::objectValue);
  251. jsonGroup["name"] = group.second.Name;
  252. jsonGroup["displayName"] = group.second.DisplayName;
  253. jsonGroup["description"] = group.second.Description;
  254. jsonGroup["isBold"] = group.second.IsBold;
  255. jsonGroup["isExpandedByDefault"] = group.second.IsExpandedByDefault;
  256. if (group.second.ParentGroup) {
  257. jsonGroup["parentGroup"] = group.second.ParentGroup->Name;
  258. }
  259. Json::Value& subgroups = jsonGroup["subgroups"] =
  260. Json::Value(Json::arrayValue);
  261. for (auto& subgroup : group.second.Subgroups) {
  262. subgroups.append(subgroup->Name);
  263. }
  264. Json::Value& groupComponents = jsonGroup["components"] =
  265. Json::Value(Json::arrayValue);
  266. for (auto& component : group.second.Components) {
  267. groupComponents.append(component->Name);
  268. }
  269. }
  270. return 1;
  271. }