cmCPackExternalGenerator.cxx 9.9 KB

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