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