cmCPackExternalGenerator.cxx 9.9 KB

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