cmCPackExternalGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. 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 (cmNonempty(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. const char* builtPackagesStr =
  61. this->GetOption("CPACK_EXTERNAL_BUILT_PACKAGES");
  62. if (builtPackagesStr) {
  63. cmExpandList(builtPackagesStr, 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 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 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 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 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 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. const char* packageName = this->Parent->GetOption("CPACK_PACKAGE_NAME");
  148. if (packageName) {
  149. root["packageName"] = packageName;
  150. }
  151. const char* packageVersion =
  152. this->Parent->GetOption("CPACK_PACKAGE_VERSION");
  153. if (packageVersion) {
  154. root["packageVersion"] = packageVersion;
  155. }
  156. const char* packageDescriptionFile =
  157. this->Parent->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  158. if (packageDescriptionFile) {
  159. root["packageDescriptionFile"] = packageDescriptionFile;
  160. }
  161. const char* packageDescriptionSummary =
  162. this->Parent->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY");
  163. if (packageDescriptionSummary) {
  164. root["packageDescriptionSummary"] = packageDescriptionSummary;
  165. }
  166. const char* buildConfigCstr = this->Parent->GetOption("CPACK_BUILD_CONFIG");
  167. if (buildConfigCstr) {
  168. root["buildConfig"] = buildConfigCstr;
  169. }
  170. const char* defaultDirectoryPermissions =
  171. this->Parent->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  172. if (cmNonempty(defaultDirectoryPermissions)) {
  173. root["defaultDirectoryPermissions"] = defaultDirectoryPermissions;
  174. }
  175. if (cmIsInternallyOn(this->Parent->GetOption("CPACK_SET_DESTDIR"))) {
  176. root["setDestdir"] = true;
  177. root["packagingInstallPrefix"] =
  178. this->Parent->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  179. } else {
  180. root["setDestdir"] = false;
  181. }
  182. root["stripFiles"] = !cmIsOff(this->Parent->GetOption("CPACK_STRIP_FILES"));
  183. root["warnOnAbsoluteInstallDestination"] =
  184. this->Parent->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION");
  185. root["errorOnAbsoluteInstallDestination"] =
  186. this->Parent->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION");
  187. Json::Value& projects = root["projects"] = Json::Value(Json::arrayValue);
  188. for (auto& project : this->Parent->CMakeProjects) {
  189. Json::Value jsonProject(Json::objectValue);
  190. jsonProject["projectName"] = project.ProjectName;
  191. jsonProject["component"] = project.Component;
  192. jsonProject["directory"] = project.Directory;
  193. jsonProject["subDirectory"] = project.SubDirectory;
  194. Json::Value& installationTypes = jsonProject["installationTypes"] =
  195. Json::Value(Json::arrayValue);
  196. for (auto& installationType : project.InstallationTypes) {
  197. installationTypes.append(installationType->Name);
  198. }
  199. Json::Value& components = jsonProject["components"] =
  200. Json::Value(Json::arrayValue);
  201. for (auto& component : project.Components) {
  202. components.append(component->Name);
  203. }
  204. projects.append(jsonProject);
  205. }
  206. Json::Value& installationTypes = root["installationTypes"] =
  207. Json::Value(Json::objectValue);
  208. for (auto& installationType : this->Parent->InstallationTypes) {
  209. Json::Value& jsonInstallationType =
  210. installationTypes[installationType.first] =
  211. Json::Value(Json::objectValue);
  212. jsonInstallationType["name"] = installationType.second.Name;
  213. jsonInstallationType["displayName"] = installationType.second.DisplayName;
  214. jsonInstallationType["index"] = installationType.second.Index;
  215. }
  216. Json::Value& components = root["components"] =
  217. Json::Value(Json::objectValue);
  218. for (auto& component : this->Parent->Components) {
  219. Json::Value& jsonComponent = components[component.first] =
  220. Json::Value(Json::objectValue);
  221. jsonComponent["name"] = component.second.Name;
  222. jsonComponent["displayName"] = component.second.DisplayName;
  223. if (component.second.Group) {
  224. jsonComponent["group"] = component.second.Group->Name;
  225. }
  226. jsonComponent["isRequired"] = component.second.IsRequired;
  227. jsonComponent["isHidden"] = component.second.IsHidden;
  228. jsonComponent["isDisabledByDefault"] =
  229. component.second.IsDisabledByDefault;
  230. jsonComponent["isDownloaded"] = component.second.IsDownloaded;
  231. jsonComponent["description"] = component.second.Description;
  232. jsonComponent["archiveFile"] = component.second.ArchiveFile;
  233. Json::Value& cmpInstallationTypes = jsonComponent["installationTypes"] =
  234. Json::Value(Json::arrayValue);
  235. for (auto& installationType : component.second.InstallationTypes) {
  236. cmpInstallationTypes.append(installationType->Name);
  237. }
  238. Json::Value& dependencies = jsonComponent["dependencies"] =
  239. Json::Value(Json::arrayValue);
  240. for (auto& dep : component.second.Dependencies) {
  241. dependencies.append(dep->Name);
  242. }
  243. }
  244. Json::Value& groups = root["componentGroups"] =
  245. Json::Value(Json::objectValue);
  246. for (auto& group : this->Parent->ComponentGroups) {
  247. Json::Value& jsonGroup = groups[group.first] =
  248. Json::Value(Json::objectValue);
  249. jsonGroup["name"] = group.second.Name;
  250. jsonGroup["displayName"] = group.second.DisplayName;
  251. jsonGroup["description"] = group.second.Description;
  252. jsonGroup["isBold"] = group.second.IsBold;
  253. jsonGroup["isExpandedByDefault"] = group.second.IsExpandedByDefault;
  254. if (group.second.ParentGroup) {
  255. jsonGroup["parentGroup"] = group.second.ParentGroup->Name;
  256. }
  257. Json::Value& subgroups = jsonGroup["subgroups"] =
  258. Json::Value(Json::arrayValue);
  259. for (auto& subgroup : group.second.Subgroups) {
  260. subgroups.append(subgroup->Name);
  261. }
  262. Json::Value& groupComponents = jsonGroup["components"] =
  263. Json::Value(Json::arrayValue);
  264. for (auto& component : group.second.Components) {
  265. groupComponents.append(component->Name);
  266. }
  267. }
  268. return 1;
  269. }