cmInstallGenerator.cxx 5.6 KB

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