cmCPackExternalGenerator.cxx 10 KB

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