cmInstallGenerator.cxx 5.8 KB

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