cmInstallGenerator.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "cmInstallGenerator.h"
  4. #include <ostream>
  5. #include <utility>
  6. #include "cmMakefile.h"
  7. #include "cmSystemTools.h"
  8. cmInstallGenerator::cmInstallGenerator(
  9. std::string destination, std::vector<std::string> const& configurations,
  10. std::string component, MessageLevel message, bool exclude_from_all)
  11. : cmScriptGenerator("CMAKE_INSTALL_CONFIG_NAME", configurations)
  12. , Destination(std::move(destination))
  13. , Component(std::move(component))
  14. , Message(message)
  15. , ExcludeFromAll(exclude_from_all)
  16. {
  17. }
  18. cmInstallGenerator::~cmInstallGenerator() = default;
  19. bool cmInstallGenerator::HaveInstall()
  20. {
  21. return true;
  22. }
  23. void cmInstallGenerator::CheckCMP0082(bool& haveSubdirectoryInstall,
  24. bool& haveInstallAfterSubdirectory)
  25. {
  26. if (haveSubdirectoryInstall) {
  27. haveInstallAfterSubdirectory = true;
  28. }
  29. }
  30. void cmInstallGenerator::AddInstallRule(
  31. std::ostream& os, std::string const& dest, cmInstallType type,
  32. std::vector<std::string> const& files, bool optional /* = false */,
  33. const char* permissions_file /* = 0 */,
  34. const char* permissions_dir /* = 0 */, const char* rename /* = 0 */,
  35. const char* literal_args /* = 0 */, Indent indent)
  36. {
  37. // Use the FILE command to install the file.
  38. std::string stype;
  39. switch (type) {
  40. case cmInstallType_DIRECTORY:
  41. stype = "DIRECTORY";
  42. break;
  43. case cmInstallType_PROGRAMS:
  44. stype = "PROGRAM";
  45. break;
  46. case cmInstallType_EXECUTABLE:
  47. stype = "EXECUTABLE";
  48. break;
  49. case cmInstallType_STATIC_LIBRARY:
  50. stype = "STATIC_LIBRARY";
  51. break;
  52. case cmInstallType_SHARED_LIBRARY:
  53. stype = "SHARED_LIBRARY";
  54. break;
  55. case cmInstallType_MODULE_LIBRARY:
  56. stype = "MODULE";
  57. break;
  58. case cmInstallType_FILES:
  59. stype = "FILE";
  60. break;
  61. }
  62. os << indent;
  63. if (cmSystemTools::FileIsFullPath(dest)) {
  64. os << "list(APPEND CMAKE_ABSOLUTE_DESTINATION_FILES\n";
  65. os << indent << " \"";
  66. bool firstIteration = true;
  67. for (std::string const& file : files) {
  68. if (!firstIteration) {
  69. os << ";";
  70. }
  71. os << dest << "/";
  72. if (rename && *rename) {
  73. os << rename;
  74. } else {
  75. os << cmSystemTools::GetFilenameName(file);
  76. }
  77. firstIteration = false;
  78. }
  79. os << "\")\n";
  80. os << indent << "if(CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
  81. os << indent << indent << "message(WARNING \"ABSOLUTE path INSTALL "
  82. << "DESTINATION : ${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
  83. os << indent << "endif()\n";
  84. os << indent << "if(CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
  85. os << indent << indent << "message(FATAL_ERROR \"ABSOLUTE path INSTALL "
  86. << "DESTINATION forbidden (by caller): "
  87. << "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
  88. os << indent << "endif()\n";
  89. }
  90. std::string absDest = this->ConvertToAbsoluteDestination(dest);
  91. os << "file(INSTALL DESTINATION \"" << absDest << "\" TYPE " << stype;
  92. if (optional) {
  93. os << " OPTIONAL";
  94. }
  95. switch (this->Message) {
  96. case MessageDefault:
  97. break;
  98. case MessageAlways:
  99. os << " MESSAGE_ALWAYS";
  100. break;
  101. case MessageLazy:
  102. os << " MESSAGE_LAZY";
  103. break;
  104. case MessageNever:
  105. os << " MESSAGE_NEVER";
  106. break;
  107. }
  108. if (permissions_file && *permissions_file) {
  109. os << " PERMISSIONS" << permissions_file;
  110. }
  111. if (permissions_dir && *permissions_dir) {
  112. os << " DIR_PERMISSIONS" << permissions_dir;
  113. }
  114. if (rename && *rename) {
  115. os << " RENAME \"" << rename << "\"";
  116. }
  117. os << " FILES";
  118. if (files.size() == 1) {
  119. os << " \"" << files[0] << "\"";
  120. } else {
  121. for (std::string const& f : files) {
  122. os << "\n" << indent << " \"" << f << "\"";
  123. }
  124. os << "\n" << indent << " ";
  125. if (!(literal_args && *literal_args)) {
  126. os << " ";
  127. }
  128. }
  129. if (literal_args && *literal_args) {
  130. os << literal_args;
  131. }
  132. os << ")\n";
  133. }
  134. std::string cmInstallGenerator::CreateComponentTest(
  135. const std::string& component, bool exclude_from_all)
  136. {
  137. std::string result = R"("x${CMAKE_INSTALL_COMPONENT}x" STREQUAL "x)";
  138. result += component;
  139. result += "x\"";
  140. if (!exclude_from_all) {
  141. result += " OR NOT CMAKE_INSTALL_COMPONENT";
  142. }
  143. return result;
  144. }
  145. void cmInstallGenerator::GenerateScript(std::ostream& os)
  146. {
  147. // Track indentation.
  148. Indent indent;
  149. // Begin this block of installation.
  150. std::string component_test =
  151. this->CreateComponentTest(this->Component, this->ExcludeFromAll);
  152. os << indent << "if(" << component_test << ")\n";
  153. // Generate the script possibly with per-configuration code.
  154. this->GenerateScriptConfigs(os, indent.Next());
  155. // End this block of installation.
  156. os << indent << "endif()\n\n";
  157. }
  158. bool cmInstallGenerator::InstallsForConfig(const std::string& config)
  159. {
  160. return this->GeneratesForConfig(config);
  161. }
  162. std::string cmInstallGenerator::ConvertToAbsoluteDestination(
  163. std::string const& dest) const
  164. {
  165. std::string result;
  166. if (!dest.empty() && !cmSystemTools::FileIsFullPath(dest)) {
  167. result = "${CMAKE_INSTALL_PREFIX}/";
  168. }
  169. result += dest;
  170. return result;
  171. }
  172. cmInstallGenerator::MessageLevel cmInstallGenerator::SelectMessageLevel(
  173. cmMakefile* mf, bool never)
  174. {
  175. if (never) {
  176. return MessageNever;
  177. }
  178. std::string m = mf->GetSafeDefinition("CMAKE_INSTALL_MESSAGE");
  179. if (m == "ALWAYS") {
  180. return MessageAlways;
  181. }
  182. if (m == "LAZY") {
  183. return MessageLazy;
  184. }
  185. if (m == "NEVER") {
  186. return MessageNever;
  187. }
  188. return MessageDefault;
  189. }